
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const path = require('path');
const {build} = require('esbuild');
const fs = require('fs');

const outFilePath = path.resolve(__dirname, '../dist/echarts.js');

const umdMark = '// ------------- WRAPPED UMD --------------- //';
const umdWrapperHead = `
${umdMark}
(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['exports'], factory);
    } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
        // CommonJS
        factory(exports);
    } else {
        // Browser globals
        factory((root.echarts = {}));
    }
}(typeof self !== 'undefined' ? self : this, function (exports, b) {
`;

const umdWrapperTail = `
}));`;

async function wrapUMDCode() {
    const code = fs.readFileSync(outFilePath, 'utf-8');
    if (code.indexOf(umdMark) >= 0) {
        return;
    }

    fs.writeFileSync(
        outFilePath,
        // Replaced __DEV__ with a same length string to avoid breaking source map
        umdWrapperHead + code.replace(/__DEV__/g, "\'_DEV_\'") + umdWrapperTail,
        'utf-8'
    );

    const sourceMap = JSON.parse(fs.readFileSync(outFilePath + '.map', 'utf-8'));
    // Fast trick for fixing source map
    sourceMap.mappings = umdWrapperHead.split('\n').map(() => '').join(';') + sourceMap.mappings;

    fs.writeFileSync(outFilePath + '.map', JSON.stringify(sourceMap), 'utf-8');
}

build({
    // stdio: 'inherit',
    entryPoints: [path.resolve(__dirname, '../src/echarts.all.ts')],
    outfile: outFilePath,
    format: 'cjs',
    sourcemap: true,
    bundle: true,
    watch: {
        async onRebuild(error, result) {
            if (error) {
                console.error('watch build failed:', error)
            } else {
                console.time('Wrap UMD');
                await wrapUMDCode();
                console.timeEnd('Wrap UMD');
            }
        },
    },
}).then(async (result) => {
    console.time('Wrap UMD');
    await wrapUMDCode();
    console.timeEnd('Wrap UMD');
}).catch(e => console.error(e.toString()))

@@ -855,34 +855,8 @@
 d');
-%0Aconst fs = require('fs');
 %0A%0Aco
@@ -1048,24 +1048,51 @@
  factory) %7B%0A
+    window.__DEV__ = true;%0A
     if (type
@@ -1546,723 +1546,15 @@
 %60;%0A%0A
-async function wrapUMDCode() %7B%0A    const code = fs.readFileSync(outFilePath, 'utf-8');%0A    if (code.indexOf(umdMark) %3E= 0) %7B%0A        return;%0A    %7D%0A%0A    fs.writeFileSync(%0A        outFilePath,%0A        // Replaced __DEV__ with a same length string to avoid breaking source map%0A        umdWrapperHead + code.replace(/__DEV__/g, %22%5C'_DEV_%5C'%22) + umdWrapperTail,%0A        'utf-8'%0A    );%0A%0A    const sourceMap = JSON.parse(fs.readFileSync(outFilePath + '.map', 'utf-8'));%0A    // Fast trick for fixing source map%0A    sourceMap.mappings = umdWrapperHead.split('%5Cn').map(() =%3E '').join(';') + sourceMap.mappings;%0A%0A    fs.writeFileSync(outFilePath + '.map', JSON.stringify(sourceMap), 'utf-8');%0A%7D%0A%0Abuild(%7B%0A    // stdio: 'inherit',
+build(%7B
 %0A   
@@ -1699,24 +1699,80 @@
 ndle: true,%0A
+    banner: umdWrapperHead,%0A    footer: umdWrapperTail,%0A
     watch: %7B
@@ -1805,16 +1805,8 @@
 rror
-, result
 ) %7B%0A
@@ -1939,107 +1939,25 @@
 ole.
-time('Wrap UMD');%0A                await wrapUMDCode();%0A                console.timeEnd('Wrap UMD
+log('build done
 ')
-;
 %0A   
@@ -2004,14 +2004,8 @@
 nc (
-result
 ) =%3E
@@ -2023,83 +2023,25 @@
 ole.
-time('Wrap UMD');%0A    await wrapUMDCode();%0A    console.timeEnd('Wrap UMD
+log('build done
 ')
-;
 %0A%7D).

use banner and footer to wrap code