Skip to content

Commit

Permalink
Merge pull request #14177 from susiwen8/esbuild-watch
Browse files Browse the repository at this point in the history
Feat: dev-fast use esbuild watch mode
  • Loading branch information
pissang authored Feb 1, 2021
2 parents 893255e + 51aab9e commit cf89ecc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 100 deletions.
69 changes: 21 additions & 48 deletions build/dev-fast.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@
* under the License.
*/

const chokidar = require('chokidar');
const path = require('path');
const {build} = require('esbuild');
const fs = require('fs');
const debounce = require('lodash.debounce');

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

const umdMark = '// ------------- WRAPPED UMD --------------- //';
const umdWrapperHead = `
${umdMark}
(function (root, factory) {
window.__DEV__ = true;
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports'], factory);
Expand All @@ -46,48 +44,23 @@ ${umdMark}
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');
}

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

const debouncedRebuild = debounce(rebuild, 100);

chokidar.watch([
path.resolve(__dirname, '../src/**/*.ts'),
path.resolve(__dirname, '../node_modules/zrender/src/**/*.ts'),
], {
persistent: true
}).on('all', debouncedRebuild);
build({
entryPoints: [path.resolve(__dirname, '../src/echarts.all.ts')],
outfile: outFilePath,
format: 'cjs',
sourcemap: true,
bundle: true,
banner: umdWrapperHead,
footer: umdWrapperTail,
watch: {
async onRebuild(error) {
if (error) {
console.error('watch build failed:', error)
} else {
console.log('build done')
}
},
},
}).then(async () => {
console.log('build done')
}).catch(e => console.error(e.toString()))
52 changes: 3 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@
"@typescript-eslint/parser": "^4.9.1",
"canvas": "^2.6.0",
"chalk": "^3.0.0",
"chokidar": "^3.4.0",
"commander": "2.11.0",
"dtslint": "^4.0.5",
"esbuild": "^0.8.23",
"esbuild": "^0.8.39",
"eslint": "^7.15.0",
"fs-extra": "0.26.7",
"glob": "7.0.0",
Expand All @@ -94,7 +93,6 @@
"jest": "^26.6.1",
"jest-canvas-mock": "^2.2.0",
"jshint": "2.10.2",
"lodash.debounce": "^4.0.8",
"magic-string": "^0.25.7",
"open": "6.4.0",
"pixelmatch": "5.0.2",
Expand Down

0 comments on commit cf89ecc

Please sign in to comment.