Files
webawesome/scripts/build.js

195 lines
6.0 KiB
JavaScript
Raw Normal View History

2022-07-27 16:17:23 -04:00
import { deleteSync } from 'del';
2023-06-06 17:02:15 -04:00
import { globby } from 'globby';
import { execSync, spawn } from 'child_process';
import browserSync from 'browser-sync';
import chalk from 'chalk';
2023-06-06 17:02:15 -04:00
import chokidar from 'chokidar';
import commandLineArgs from 'command-line-args';
2023-06-06 17:02:15 -04:00
import copy from 'recursive-copy';
2021-06-17 17:38:48 -04:00
import esbuild from 'esbuild';
import fs from 'fs';
2023-06-06 17:02:15 -04:00
import getPort, { portNumbers } from 'get-port';
2021-02-26 09:09:13 -05:00
2023-06-06 15:46:50 -04:00
const abortController = new AbortController();
const abortSignal = abortController.signal;
2023-06-06 17:02:15 -04:00
function buildTheDocs(watch = false) {
deleteSync('./_site');
2023-06-06 15:46:50 -04:00
if (!watch) {
2023-06-06 17:02:15 -04:00
return execSync('npx @11ty/eleventy --quiet', { stdio: 'inherit', cwd: 'docs' });
2023-06-06 15:46:50 -04:00
}
2023-06-06 17:02:15 -04:00
return spawn('npx', ['@11ty/eleventy', '--watch', '--incremental', '--quiet'], {
stdio: 'inherit',
cwd: 'docs',
signal: abortSignal
});
}
2021-10-22 10:51:17 -04: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 08:35:31 -04:00
2021-10-16 10:35:42 -04:00
const outdir = dir;
2022-07-27 16:17:23 -04:00
deleteSync(outdir);
fs.mkdirSync(outdir, { recursive: true });
2021-02-26 09:09:13 -05:00
2023-06-06 17:02:15 -04:00
(async () => {
try {
execSync(`node scripts/make-metadata.js --outdir "${outdir}"`, { stdio: 'inherit' });
2022-01-26 08:46:20 -05:00
execSync(`node scripts/make-react.js --outdir "${outdir}"`, { stdio: 'inherit' });
2022-02-16 16:02:21 -05:00
execSync(`node scripts/make-web-types.js --outdir "${outdir}"`, { stdio: 'inherit' });
2022-01-25 17:09:53 -05:00
execSync(`node scripts/make-themes.js --outdir "${outdir}"`, { stdio: 'inherit' });
execSync(`node scripts/make-icons.js --outdir "${outdir}"`, { stdio: 'inherit' });
2022-02-10 16:41:20 -05:00
if (types) {
console.log('Running the TypeScript compiler...');
2022-03-24 08:01:09 -04:00
execSync(`tsc --project ./tsconfig.prod.json --outdir "${outdir}"`, { stdio: 'inherit' });
2022-02-10 16:41:20 -05:00
}
} catch (err) {
console.error(chalk.red(err));
process.exit(1);
}
2021-11-04 07:27:18 -04:00
const alwaysExternal = ['@lit-labs/react', 'react'];
2021-02-26 09:09:13 -05:00
const buildResult = await esbuild
.build({
format: 'esm',
target: 'es2017',
2021-10-08 10:11:12 -04:00
entryPoints: [
2022-11-22 11:00:36 -05: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 10:11:12 -04:00
'./src/shoelace.ts',
2023-02-22 14:18:04 -05:00
// The auto-loader
'./src/shoelace-autoloader.ts',
2021-10-08 10:11:12 -04:00
// Components
...(await globby('./src/components/**/!(*.(style|test)).ts')),
2021-12-06 10:57:54 -05:00
// Translations
...(await globby('./src/translations/**/*.ts')),
2021-10-08 10:11:12 -04:00
// Public utilities
...(await globby('./src/utilities/**/!(*.(style|test)).ts')),
2021-10-08 10:11:12 -04:00
// Theme stylesheets
...(await globby('./src/themes/**/!(*.test).ts')),
2021-11-04 07:27:18 -04:00
// React wrappers
...(await globby('./src/react/**/*.ts'))
2021-10-08 10:11:12 -04:00
],
outdir,
2021-03-02 17:23:49 -05:00
chunkNames: 'chunks/[name].[hash]',
incremental: serve,
2021-02-26 09:09:13 -05:00
define: {
2022-03-03 15:48:20 -05:00
// Floating UI requires this to be set
2021-02-26 09:09:13 -05:00
'process.env.NODE_ENV': '"production"'
},
bundle: true,
//
2021-10-22 10:51:17 -04: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 07:27:18 -04:00
// We never bundle React or @lit-labs/react though!
//
external: bundle
? alwaysExternal
2022-03-03 15:48:20 -05:00
: [...alwaysExternal, '@floating-ui/dom', '@shoelace-style/animations', 'lit', 'qr-creator'],
2021-02-26 09:09:13 -05:00
splitting: true,
plugins: []
2021-02-26 09:09:13 -05:00
})
.catch(err => {
console.error(chalk.red(err));
process.exit(1);
});
2021-10-22 10:51:17 -04:00
// Copy the build output to an additional directory
if (copydir) {
2022-07-27 16:17:23 -04:00
deleteSync(copydir);
2021-10-22 10:51:17 -04:00
copy(outdir, copydir);
}
if (serve) {
2023-06-06 17:02:15 -04:00
// Build it with --watch and --incremental
buildTheDocs(true);
// Wait for the search index to appear before launching the browser. This file is generated during eleventy.after,
// so it's usually the last one to appear.
const watcher = chokidar.watch('./_site', { persistent: true });
watcher.on('add', async filename => {
if (filename.endsWith('search.json')) {
watcher.close();
const bs = browserSync.create();
const port = await getPort({
port: portNumbers(4000, 4999)
});
const browserSyncConfig = {
startPath: '/',
port,
logLevel: 'silent',
logPrefix: '[shoelace]',
logFileChanges: true,
notify: false,
2023-06-07 07:46:41 -04:00
single: false,
2023-06-06 17:02:15 -04:00
ghostMode: false,
server: {
baseDir: '_site',
routes: {
'/dist': './dist'
}
}
2023-06-06 17:02:15 -04:00
};
// Launch browser sync
bs.init(browserSyncConfig, () => {
const url = `http://localhost:${port}`;
console.log(chalk.cyan(`Launched the Shoelace dev server at ${url} 🥾\n`));
});
// Rebuild and reload when source files change
bs.watch(['src/**/!(*.test).*']).on('change', async filename => {
buildResult
// Rebuild and reload
.rebuild()
.then(() => {
// Rebuild stylesheets when a theme file changes
if (/^src\/themes/.test(filename)) {
execSync(`node scripts/make-themes.js --outdir "${outdir}"`, { stdio: 'inherit' });
}
})
.then(() => {
// Skip metadata when styles are changed
if (/(\.css|\.styles\.ts)$/.test(filename)) {
return;
}
execSync(`node scripts/make-metadata.js --outdir "${outdir}"`, { stdio: 'inherit' });
})
.then(() => bs.reload())
.catch(err => console.error(chalk.red(err)));
});
// Reload without rebuilding when the docs change
bs.watch(['_site/**/*.*']).on('change', () => {
bs.reload();
});
}
});
2023-06-06 17:02:15 -04:00
}
2023-06-06 17:02:15 -04:00
// Prod build
if (!serve) {
buildTheDocs();
2021-02-26 09:09:13 -05:00
}
2021-10-08 10:11:12 -04:00
// Cleanup on exit
2023-06-06 15:46:50 -04:00
process.on('SIGTERM', () => {
2023-06-06 17:02:15 -04:00
buildResult.rebuild.dispose();
2023-06-06 15:46:50 -04:00
abortController.abort(); // Stops the child process
});
2021-02-26 09:09:13 -05:00
})();