shoelace/scripts/build.js

147 wiersze
4.8 KiB
JavaScript
Czysty Zwykły widok Historia

2021-06-17 21:38:48 +00:00
import browserSync from 'browser-sync';
import chalk from 'chalk';
import commandLineArgs from 'command-line-args';
import copy from 'recursive-copy';
import del from 'del';
import esbuild from 'esbuild';
import fs from 'fs';
2021-06-17 21:38:48 +00:00
import getPort from 'get-port';
import glob from 'globby';
import mkdirp from 'mkdirp';
2021-06-17 21:38:48 +00:00
import path from 'path';
import { URL } from 'url';
import { execSync } from 'child_process';
2021-02-26 14:09:13 +00:00
2021-06-17 21:38:48 +00:00
const build = esbuild.build;
const bs = browserSync.create();
2021-02-26 14:09:13 +00:00
const { bundle, dir, serve, types } = commandLineArgs([
{ name: 'dir', type: String, defaultValue: 'dist' },
{ name: 'serve', type: Boolean },
{ name: 'bundle', type: Boolean },
{ name: 'types', type: Boolean }
]);
2021-05-11 12:35:31 +00:00
const __dirname = new URL('.', import.meta.url).pathname;
const rootDir = path.dirname(__dirname);
const outdir = path.relative(rootDir, dir);
del.sync(outdir);
mkdirp.sync(outdir);
2021-02-26 14:09:13 +00:00
(async () => {
try {
if (types) execSync(`tsc --project . --outdir "${outdir}"`, { stdio: 'inherit' });
execSync(`node scripts/make-metadata.js --outdir "${outdir}"`, { stdio: 'inherit' });
execSync(`node scripts/make-search.js --outdir "${outdir}"`, { stdio: 'inherit' });
execSync(`node scripts/make-vscode-data.js --outdir "${outdir}"`, { stdio: 'inherit' });
execSync(`node scripts/make-css.js --outdir "${outdir}"`, { stdio: 'inherit' });
execSync(`node scripts/make-icons.js --outdir "${outdir}"`, { stdio: 'inherit' });
} catch (err) {
console.error(chalk.red(err));
process.exit(1);
}
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: [
// The whole shebang
2021-10-08 14:11:12 +00:00
'./src/shoelace.ts',
// Components
...(await glob('./src/components/**/!(*.(style|test)).ts')),
// Public utilities
...(await glob('./src/utilities/**/!(*.(style|test)).ts')),
// Theme stylesheets
...(await glob('./src/themes/**/!(*.test).ts'))
],
outdir,
2021-03-02 22:23:49 +00:00
chunkNames: 'chunks/[name].[hash]',
incremental: serve,
2021-02-26 14:09:13 +00:00
define: {
// Popper.js expects this to be set
'process.env.NODE_ENV': '"production"'
},
bundle: true,
//
// We don't bundle certain dependencies in the production build. This ensures the dist ships with bare module
// specifiers, allowing end users to optimize better. jsDelivr understands this if you add /+esm to the URL. Note
// that we can't bundle packages that don't ship ESM. https://github.com/jsdelivr/jsdelivr/issues/18263
//
// We still bundle for the dev environment and the docs build since we don't use a CDN for those. Once import maps
// are better supported, we can adjust for that and use the same build again. https://caniuse.com/import-maps
//
external: bundle ? undefined : ['@popperjs/core', '@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);
});
console.log(chalk.green(`The build has been generated at ${outdir} 📦\n`));
2021-02-26 14:09:13 +00:00
2021-10-08 14:11:12 +00:00
// Dev server
if (serve) {
2021-02-26 14:09:13 +00:00
const port = await getPort({
port: getPort.makeRange(4000, 4999)
});
console.log(chalk.cyan(`Launching the Shoelace dev server at http://localhost:${port}! 🥾\n`));
2021-02-26 14:09:13 +00:00
// Launch browser sync
bs.init({
startPath: '/',
port,
logLevel: 'silent',
logPrefix: '[shoelace]',
logFileChanges: true,
notify: false,
2021-08-24 12:49:27 +00:00
single: true,
2021-08-24 13:03:01 +00:00
ghostMode: false,
2021-02-26 14:09:13 +00:00
server: {
2021-10-14 12:39:17 +00:00
baseDir: 'docs',
routes: {
'/dist': './dist'
}
2021-02-26 14:09:13 +00:00
}
});
// Rebuild and reload when source files change
2021-05-27 20:50:58 +00:00
bs.watch(['src/**/!(*.test).*']).on('change', async filename => {
2021-02-26 14:09:13 +00:00
console.log(`Source file changed - ${filename}`);
buildResult
// Rebuild and reload
2021-02-26 14:09:13 +00:00
.rebuild()
2021-08-04 22:03:24 +00:00
.then(async () => {
// Rebuild stylesheets when a theme file changes
if (/^src\/themes/.test(filename)) {
execSync(`node scripts/make-css.js --outdir "${outdir}"`, { stdio: 'inherit' });
2021-08-04 22:03:24 +00:00
}
})
.then(() => {
// Skip metadata when styles are changed
if (/(\.css|\.styles\.ts)$/.test(filename)) {
return;
}
execSync(`node scripts/make-metadata.js --outdir "${outdir}"`, { stdio: 'inherit' });
2021-08-04 22:03:24 +00:00
})
2021-02-26 14:09:13 +00:00
.then(() => bs.reload())
.catch(err => console.error(chalk.red(err)));
});
// Reload without rebuilding when the docs change
2021-09-07 17:13:18 +00:00
bs.watch(['docs/**/*.md']).on('change', filename => {
2021-02-26 14:09:13 +00:00
console.log(`Docs file changed - ${filename}`);
execSync(`node scripts/make-search.js --outdir "${outdir}"`, { stdio: 'inherit' });
2021-02-26 14:09:13 +00:00
bs.reload();
});
}
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
})();