build: update build script

This commit is contained in:
chenshenhai 2021-11-13 16:21:53 +08:00
parent be7f4aec95
commit be94b52814
10 changed files with 66 additions and 15 deletions

View file

@ -7,7 +7,7 @@
"dev:core": "node ./scripts/dev.js core",
"build": "npm run build:src && npm run build:min",
"build:src": "NODE_ENV=production BUILD_MODE=reset node ./scripts/build.js",
"build:min": "NODE_ENV=production BUILD_MODE=mini node ./scripts/build.js",
"build:min": "node ./scripts/minify.js",
"snapshot": "node ./scripts/build.js && node ./scripts/snapshot.js",
"e2e": "mocha --exit ./__tests__/e2e.test.js",
"init": "lerna bootstrap",
@ -63,6 +63,7 @@
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.30.0",
"serve-handler": "^6.1.3",
"terser": "^5.9.0",
"ts-node": "^9.1.1",
"tslib": "^2.2.0",
"typescript": "^4.3.2"

View file

@ -30,5 +30,6 @@
},
"publishConfig": {
"access": "public"
}
},
"gitHead": "be7f4aec953e25aed3fc0d1f61ef9f99407b77f8"
}

View file

@ -31,5 +31,6 @@
},
"publishConfig": {
"access": "public"
}
},
"gitHead": "be7f4aec953e25aed3fc0d1f61ef9f99407b77f8"
}

View file

@ -31,5 +31,6 @@
},
"publishConfig": {
"access": "public"
}
},
"gitHead": "be7f4aec953e25aed3fc0d1f61ef9f99407b77f8"
}

View file

@ -11,7 +11,8 @@
<canvas id="canvas"></canvas>
</div>
<script src="./../../dist/index.global.js"></script>
<!-- <script src="./../../dist/index.global.js"></script> -->
<script src="./../../dist/index.global.min.js"></script>
<script type="module" src="./lib/main.js"></script>
</body>
</html>

View file

@ -30,5 +30,6 @@
},
"publishConfig": {
"access": "public"
}
},
"gitHead": "be7f4aec953e25aed3fc0d1f61ef9f99407b77f8"
}

View file

@ -21,5 +21,6 @@
"homepage": "https://github.com/idrawjs/idraw#readme",
"publishConfig": {
"access": "public"
}
},
"gitHead": "be7f4aec953e25aed3fc0d1f61ef9f99407b77f8"
}

View file

@ -27,5 +27,6 @@
},
"publishConfig": {
"access": "public"
}
},
"gitHead": "be7f4aec953e25aed3fc0d1f61ef9f99407b77f8"
}

36
scripts/minify.js Normal file
View file

@ -0,0 +1,36 @@
const path = require('path');
const fs = require('fs');
const compose = require('koa-compose')
const { minify } = require('terser');
const { packages } = require('./config');
async function main() {
const tasks = [];
packages.forEach((p) => {
tasks.push(async (ctx, next) => {
const baseFileName = 'index.global.js';
const targetFileName = 'index.global.min.js';
const moduleBasePath = path.join(__dirname, '..', 'packages', p.dirName, 'dist');
console.log(`minify file: ${path.join(p.dirName, baseFileName)} > ${path.join(p.dirName, targetFileName)}`)
const filePath = path.join(moduleBasePath, baseFileName);
const targetPath = path.join(moduleBasePath, targetFileName);
const code = fs.readFileSync(filePath, { encoding: 'utf8' });
const options = {
output: {
beautify: false,
comments: false,
indent_level: 2,
quote_style: 3,
},
};
const result = await minify(code, options);
fs.writeFileSync(targetPath, result.code);
await next();
})
});
await compose(tasks)();
}
main();

View file

@ -22,13 +22,13 @@ const external = [ '@idraw/types', '@idraw/util', '@idraw/board', '@idraw/core'
for(let i = 0; i < packages.length; i++) {
const pkg = packages[i];
if (process.env.BUILD_MODE === 'mini') {
modules.push({
input: resolveFile([pkg.dirName, 'src', 'index.ts']),
output: resolveFile([pkg.dirName, 'dist', 'index.global.min.js']),
name: pkg.globalName,
format: 'iife',
plugins: []
});
// modules.push({
// input: resolveFile([pkg.dirName, 'src', 'index.ts']),
// output: resolveFile([pkg.dirName, 'dist', 'index.global.min.js']),
// name: pkg.globalName,
// format: 'iife',
// plugins: [],
// });
} else {
modules.push({
input: resolveFile([pkg.dirName, 'src', 'index.ts']),
@ -37,6 +37,13 @@ for(let i = 0; i < packages.length; i++) {
format: 'iife',
plugins: []
});
modules.push({
input: resolveFile([pkg.dirName, 'src', 'index.ts']),
output: resolveFile([pkg.dirName, 'dist', 'index.global.min.js']),
name: pkg.globalName,
format: 'iife',
plugins: [],
});
modules.push({
input: resolveFile([pkg.dirName, 'src', 'index.ts']),
output: resolveFile([pkg.dirName, 'dist', 'index.cjs.js']),