shoelace/scripts/build.js

108 wiersze
3.6 KiB
JavaScript
Czysty Zwykły widok Historia

2021-06-17 21:38:48 +00:00
import chalk from 'chalk';
import { execSync } from 'child_process';
2021-06-17 21:38:48 +00:00
import commandLineArgs from 'command-line-args';
2022-07-27 20:17:23 +00:00
import { deleteSync } from 'del';
2021-06-17 21:38:48 +00:00
import esbuild from 'esbuild';
import fs from 'fs';
import { globby } from 'globby';
import copy from 'recursive-copy';
2021-02-26 14:09:13 +00:00
2021-10-22 14:51:17 +00:00
const { bundle, copydir, dir, serve, types } = commandLineArgs([
{ name: 'bundle', type: Boolean },
{ name: 'copydir', type: String },
{ name: 'dir', type: String, defaultValue: 'dist' },
{ name: 'serve', type: Boolean },
{ name: 'types', type: Boolean }
]);
2021-05-11 12:35:31 +00:00
2021-10-16 14:35:42 +00:00
const outdir = dir;
2022-07-27 20:17:23 +00:00
deleteSync(outdir);
fs.mkdirSync(outdir, { recursive: true });
2021-02-26 14:09:13 +00:00
(async () => {
try {
execSync(`node scripts/make-metadata.js --outdir "${outdir}"`, { stdio: 'inherit' });
execSync(`node scripts/make-search.js --outdir "${outdir}"`, { stdio: 'inherit' });
2022-01-26 13:46:20 +00:00
execSync(`node scripts/make-react.js --outdir "${outdir}"`, { stdio: 'inherit' });
2022-02-16 21:02:21 +00:00
execSync(`node scripts/make-web-types.js --outdir "${outdir}"`, { stdio: 'inherit' });
2022-01-25 22:09:53 +00:00
execSync(`node scripts/make-themes.js --outdir "${outdir}"`, { stdio: 'inherit' });
execSync(`node scripts/make-icons.js --outdir "${outdir}"`, { stdio: 'inherit' });
2022-02-10 21:41:20 +00:00
if (types) {
console.log('Running the TypeScript compiler...');
2022-03-24 12:01:09 +00:00
execSync(`tsc --project ./tsconfig.prod.json --outdir "${outdir}"`, { stdio: 'inherit' });
2022-02-10 21:41:20 +00:00
}
} catch (err) {
console.error(chalk.red(err));
process.exit(1);
}
2021-11-04 11:27:18 +00:00
const alwaysExternal = ['@lit-labs/react', 'react'];
2021-02-26 14:09:13 +00:00
const buildResult = await esbuild
.build({
format: 'esm',
target: 'es2017',
2021-10-08 14:11:12 +00:00
entryPoints: [
2022-11-22 16:00:36 +00:00
//
// NOTE: Entry points must be mapped in package.json > exports, otherwise users won't be able to import them!
//
// The whole shebang
2021-10-08 14:11:12 +00:00
'./src/shoelace.ts',
2023-02-22 19:18:04 +00:00
// The auto-loader
'./src/shoelace-autoloader.ts',
2021-10-08 14:11:12 +00:00
// Components
...(await globby('./src/components/**/!(*.(style|test)).ts')),
2021-12-06 15:57:54 +00:00
// Translations
...(await globby('./src/translations/**/*.ts')),
2021-10-08 14:11:12 +00:00
// Public utilities
...(await globby('./src/utilities/**/!(*.(style|test)).ts')),
2021-10-08 14:11:12 +00:00
// Theme stylesheets
...(await globby('./src/themes/**/!(*.test).ts')),
2021-11-04 11:27:18 +00:00
// React wrappers
...(await globby('./src/react/**/*.ts'))
2021-10-08 14:11:12 +00:00
],
outdir,
2021-03-02 22:23:49 +00:00
chunkNames: 'chunks/[name].[hash]',
incremental: serve,
2021-02-26 14:09:13 +00:00
define: {
2022-03-03 20:48:20 +00:00
// Floating UI requires this to be set
2021-02-26 14:09:13 +00:00
'process.env.NODE_ENV': '"production"'
},
bundle: true,
//
2021-10-22 14:51:17 +00:00
// We don't bundle certain dependencies in the unbundled build. This ensures we ship bare module specifiers,
// allowing end users to better optimize when using a bundler. (Only packages that ship ESM can be external.)
//
2021-11-04 11:27:18 +00:00
// We never bundle React or @lit-labs/react though!
//
external: bundle
? alwaysExternal
2022-03-03 20:48:20 +00:00
: [...alwaysExternal, '@floating-ui/dom', '@shoelace-style/animations', 'lit', 'qr-creator'],
2021-02-26 14:09:13 +00:00
splitting: true,
plugins: []
2021-02-26 14:09:13 +00:00
})
.catch(err => {
console.error(chalk.red(err));
process.exit(1);
});
2021-10-22 14:51:17 +00:00
// Copy the build output to an additional directory
if (copydir) {
2022-07-27 20:17:23 +00:00
deleteSync(copydir);
2021-10-22 14:51:17 +00:00
copy(outdir, copydir);
}
console.log(chalk.green(`The build has been generated at ${outdir} 📦\n`));
2021-02-26 14:09:13 +00:00
if (serve) {
2023-06-06 12:22:18 +00:00
// Dev
execSync('npx @11ty/eleventy --serve --incremental', { stdio: 'inherit', cwd: 'docs' });
} else {
// Build
execSync('npx @11ty/eleventy', { stdio: 'inherit', cwd: 'docs' });
2021-02-26 14:09:13 +00:00
}
2021-10-08 14:11:12 +00:00
// Cleanup on exit
process.on('SIGTERM', () => buildResult.rebuild.dispose());
2021-02-26 14:09:13 +00:00
})();