add legacy.js

This commit is contained in:
Harvey committed 2022-10-11 17:54:25 +08:00
1 parent 1d5197ab07
commit 5e330a59cc
22 files changed
+110 -23

No files matched your search

+41 -14
View File
@@ -8,24 +8,40 @@ import { formatDate, getProjects } from './utils.js';
const projects = getProjects();
const compiledPath = path.resolve(`docs/compiled`);
async function build(name) {
async function build(name, targetName) {
const { version } = JSON.parse(fs.readFileSync(`${projects[name]}/package.json`, 'utf-8'));
process.chdir(projects[name]);
const targets = {
main: {
distDir: `${projects[name]}/dist`,
sourceMap: false,
outputFormat: 'global',
engines: {
browsers: 'last 1 Chrome version',
},
},
legacy: {
distDir: `${projects[name]}/dist`,
sourceMap: false,
outputFormat: 'global',
engines: {
browsers: '> 0.5%, last 2 versions, not dead',
},
},
};
const names = {
main: name,
legacy: `${name}.legacy.js`,
};
const bundler = new Parcel({
entries: `${projects[name]}/src/index.js`,
defaultConfig: '@parcel/config-default',
mode: 'production',
targets: {
main: {
distDir: `${projects[name]}/dist`,
sourceMap: false,
outputFormat: 'global',
engines: {
browsers: ['last 1 Chrome version'],
// "browsers": "> 0.5%, last 2 versions, not dead"
},
},
[targetName]: targets[targetName],
},
env: {
NODE_ENV: 'production',
@@ -44,14 +60,20 @@ async function build(name) {
const { bundleGraph, buildTime } = await bundler.run();
const bundles = bundleGraph.getBundles();
const filePath = `${projects[name]}/dist/index.js`;
const newFilePath = `${projects[name]}/dist/${name}.js`;
const newFilePath = `${projects[name]}/dist/${names[targetName]}.js`;
const code = banner + fs.readFileSync(filePath);
fs.writeFileSync(filePath, code.replace(/\\n*\s*</g, '<').replace(/>\\n*\s*/g, '>'));
fs.renameSync(filePath, newFilePath);
await cpy(newFilePath, compiledPath);
const size = fs.statSync(newFilePath).size / 1024;
console.log(`✨ Built ${name}@${version}`, `Bundles@${bundles.length}`, `Time@${buildTime}ms`, `Size@${size}kb`);
console.log(
`✨ Built@${targetName} ${name}@${version}`,
`Bundles@${bundles.length}`,
`Time@${buildTime}ms`,
`Size@${size}kb`,
);
}
function runPromisesInSeries(ps) {
@@ -59,7 +81,11 @@ function runPromisesInSeries(ps) {
}
if (process.argv.pop() === 'all') {
const bundles = Object.keys(projects).map((name) => () => build(name));
const bundles = Object.keys(projects).map((name) => async () => {
await build(name, 'main');
await build(name, 'legacy');
});
runPromisesInSeries(bundles).then(() => {
console.log(`✨ Finished building all packages!`);
});
@@ -80,7 +106,8 @@ if (process.argv.pop() === 'all') {
});
if (response.value) {
build(response.value);
await build(response.value, 'main');
await build(response.value, 'legacy');
}
})();
}