shoelace/scripts/build.js

133 wiersze
3.7 KiB
JavaScript
Czysty Zwykły widok Historia

2021-02-26 14:09:13 +00:00
//
// Builds the project. To spin up a dev server, pass the --serve flag.
//
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 path from 'path';
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-03-07 14:02:57 +00:00
const { dev } = commandLineArgs({ name: 'dev', type: Boolean });
2021-02-26 14:09:13 +00:00
2021-05-11 12:35:31 +00:00
del.sync('./dist');
2021-08-09 12:33:30 +00:00
try {
if (!dev) execSync('tsc', { stdio: 'inherit' }); // for type declarations
execSync('node scripts/make-metadata.js', { stdio: 'inherit' });
execSync('node scripts/make-vscode-data.js', { stdio: 'inherit' });
execSync('node scripts/make-css.js', { stdio: 'inherit' });
2021-08-09 13:21:55 +00:00
execSync('node scripts/make-icons.js', { stdio: 'inherit' });
2021-08-09 12:33:30 +00:00
} catch (err) {
console.error(chalk.red(err));
process.exit(1);
}
2021-02-26 14:09:13 +00:00
(async () => {
const entryPoints = [
2021-03-18 13:04:23 +00:00
// The whole shebang dist
2021-02-26 14:09:13 +00:00
'./src/shoelace.ts',
// Components
2021-07-10 00:45:44 +00:00
...(await glob('./src/components/**/!(*.(style|test)).ts')),
2021-02-26 14:09:13 +00:00
// Public utilities
2021-07-10 00:45:44 +00:00
...(await glob('./src/utilities/**/!(*.(style|test)).ts')),
2021-02-26 14:09:13 +00:00
// Theme stylesheets
2021-05-27 20:50:58 +00:00
...(await glob('./src/themes/**/!(*.test).ts'))
2021-02-26 14:09:13 +00:00
];
const buildResult = await esbuild
.build({
format: 'esm',
target: 'es2017',
entryPoints,
outdir: './dist',
2021-03-02 22:23:49 +00:00
chunkNames: 'chunks/[name].[hash]',
2021-03-07 14:02:57 +00:00
incremental: dev,
2021-02-26 14:09:13 +00:00
define: {
// Popper.js expects this to be set
'process.env.NODE_ENV': '"production"'
},
bundle: true,
splitting: true,
plugins: []
2021-02-26 14:09:13 +00:00
})
.catch(err => {
console.error(chalk.red(err));
process.exit(1);
});
// Create the docs distribution by copying dist into the docs folder. This is what powers the website. It doesn't need
// to exist in dev because Browser Sync routes it virtually.
2021-02-26 14:09:13 +00:00
await del('./docs/dist');
2021-03-07 14:02:57 +00:00
if (!dev) {
await Promise.all([copy('./dist', './docs/dist')]);
2021-02-26 14:09:13 +00:00
}
console.log(chalk.green('The build has finished! 📦\n'));
2021-02-26 14:09:13 +00:00
2021-03-07 14:02:57 +00:00
if (dev) {
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-02-26 14:09:13 +00:00
server: {
baseDir: 'docs',
routes: {
'/dist': './dist'
}
}
});
// 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', { 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', { stdio: 'inherit' });
})
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
bs.watch(['docs/**/*']).on('change', filename => {
console.log(`Docs file changed - ${filename}`);
bs.reload();
});
// Cleanup on exit
process.on('SIGTERM', () => buildResult.rebuild.dispose());
}
})();