提交 285be458 编写于 作者: P pissang

chore: use trick to fix sourcemap instead of parsing full to add UMD wrapper.

上级 17407266
......@@ -3,7 +3,7 @@ const path = require('path');
const {build} = require('esbuild');
const fs = require('fs');
const debounce = require('lodash.debounce');
const sourceMap = require('source-map');
// const sourceMap = require('source-map');
const outFilePath = path.resolve(__dirname, '../dist/echarts.js');
......@@ -26,16 +26,25 @@ const umdWrapperTail = `
}));`;
async function wrapUMDCode() {
const consumer = await new sourceMap.SourceMapConsumer(fs.readFileSync(outFilePath + '.map', 'utf8'));
const node = sourceMap.SourceNode.fromStringWithSourceMap(fs.readFileSync(outFilePath, 'utf-8'), consumer);
// add six empty lines
node.prepend(umdWrapperHead);
node.add(umdWrapperTail);
const res = node.toStringWithSourceMap({
file: outFilePath
});
fs.writeFileSync(outFilePath, res.code, 'utf-8');
fs.writeFileSync(outFilePath + '.map', res.map.toString(), 'utf-8');
// const consumer = await new sourceMap.SourceMapConsumer(fs.readFileSync(outFilePath + '.map', 'utf8'));
// const node = sourceMap.SourceNode.fromStringWithSourceMap(fs.readFileSync(outFilePath, 'utf-8'), consumer);
// // add six empty lines
// node.prepend(umdWrapperHead);
// node.add(umdWrapperTail);
// const res = node.toStringWithSourceMap({
// file: outFilePath
// });
// fs.writeFileSync(outFilePath, res.code, 'utf-8');
// fs.writeFileSync(outFilePath + '.map', res.map.toString(), 'utf-8');
const code = fs.readFileSync(outFilePath, 'utf-8');
fs.writeFileSync(outFilePath, umdWrapperHead + code + 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() {
......@@ -48,14 +57,14 @@ function rebuild() {
bundle: true,
}).catch(e => {
console.error(e.toString());
}).then(() => {
}).then(async () => {
console.time('Wrap UMD');
wrapUMDCode();
await wrapUMDCode();
console.timeEnd('Wrap UMD');
})
}
const debouncedRebuild = debounce(rebuild, 200);
const debouncedRebuild = debounce(rebuild, 100);
chokidar.watch([
path.resolve(__dirname, '../src/**/*.ts'),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册