146 lines
4.3 KiB
TypeScript
Raw Normal View History

2022-07-27 16:42:03 +02:00
#!/usr/bin/env ../../node_modules/.bin/ts-node
2022-07-21 15:42:01 +02:00
import fs from 'fs-extra';
import path, { dirname, join, relative } from 'path';
import { build } from 'tsup';
import aliasPlugin from 'esbuild-plugin-alias';
2022-08-10 11:20:47 +02:00
import dedent from 'ts-dedent';
2022-09-27 23:23:57 +01:00
import slash from 'slash';
2022-08-10 11:20:47 +02:00
import { exec } from '../utils/exec';
const hasFlag = (flags: string[], name: string) => !!flags.find((s) => s.startsWith(`--${name}`));
const run = async ({ cwd, flags }: { cwd: string; flags: string[] }) => {
2022-06-27 14:13:19 +02:00
const {
name,
dependencies,
peerDependencies,
2022-11-02 13:50:45 +01:00
bundler: { entries = [], untypedEntries = [], platform, pre, post },
2022-06-27 14:13:19 +02:00
} = await fs.readJson(join(cwd, 'package.json'));
2022-07-06 13:35:13 +02:00
if (pre) {
2022-11-28 23:31:32 +01:00
await exec(`node -r ${__dirname}/../node_modules/esbuild-register/register.js ${pre}`, { cwd });
2022-07-06 13:35:13 +02:00
}
const reset = hasFlag(flags, 'reset');
const watch = hasFlag(flags, 'watch');
const optimized = hasFlag(flags, 'optimized');
2022-05-26 13:20:58 +02:00
if (reset) {
await fs.emptyDir(join(process.cwd(), 'dist'));
}
2022-07-21 15:42:01 +02:00
const tsConfigPath = join(cwd, 'tsconfig.json');
const tsConfigExists = await fs.pathExists(tsConfigPath);
2022-11-11 16:49:03 +01:00
2022-06-24 12:28:48 +02:00
await Promise.all([
2022-11-11 16:49:03 +01:00
// SHIM DTS FILES (only for development)
...(optimized
? []
: entries.map(async (file: string) => {
const { name: entryName, dir } = path.parse(file);
const pathName = join(process.cwd(), dir.replace('./src', 'dist'), `${entryName}.d.ts`);
const srcName = join(process.cwd(), file);
const rel = relative(dirname(pathName), dirname(srcName))
.split(path.sep)
.join(path.posix.sep);
await fs.ensureFile(pathName);
await fs.writeFile(
pathName,
dedent`
// dev-mode
export * from '${rel}/${entryName}';
`
);
})),
// BROWSER EMS
2022-06-24 12:28:48 +02:00
build({
2022-11-11 16:49:03 +01:00
silent: true,
2022-11-02 13:50:45 +01:00
entry: [...entries, ...untypedEntries].map((e: string) => slash(join(cwd, e))),
2022-06-24 12:28:48 +02:00
watch,
...(tsConfigExists ? { tsconfig: tsConfigPath } : {}),
2022-07-21 15:42:01 +02:00
outDir: join(process.cwd(), 'dist'),
2022-06-24 12:28:48 +02:00
format: ['esm'],
target: 'chrome100',
clean: !watch,
2022-06-28 11:57:00 +02:00
platform: platform || 'browser',
2022-06-24 12:28:48 +02:00
esbuildPlugins: [
aliasPlugin({
process: path.resolve('../node_modules/process/browser.js'),
util: path.resolve('../node_modules/util/util.js'),
2022-06-24 12:28:48 +02:00
}),
],
2022-06-27 14:13:19 +02:00
external: [name, ...Object.keys(dependencies || {}), ...Object.keys(peerDependencies || {})],
2022-04-29 10:06:04 +02:00
dts:
optimized && tsConfigExists
? {
entry: entries,
resolve: true,
}
: false,
2022-06-24 12:28:48 +02:00
esbuildOptions: (c) => {
/* eslint-disable no-param-reassign */
c.conditions = ['module'];
2022-11-02 13:50:45 +01:00
c.logLevel = 'error';
2022-06-27 14:13:19 +02:00
c.platform = platform || 'browser';
2022-06-24 12:28:48 +02:00
c.legalComments = 'none';
c.minifyWhitespace = optimized;
2022-11-23 16:00:33 +01:00
c.minifyIdentifiers = false;
2022-06-24 12:28:48 +02:00
c.minifySyntax = optimized;
/* eslint-enable no-param-reassign */
},
}),
2022-11-11 16:49:03 +01:00
// NODE CJS
2022-06-24 12:28:48 +02:00
build({
2022-11-11 16:49:03 +01:00
silent: true,
2022-11-02 13:50:45 +01:00
entry: [...entries, ...untypedEntries].map((e: string) => slash(join(cwd, e))),
2022-06-24 12:28:48 +02:00
watch,
2022-07-21 15:42:01 +02:00
outDir: join(process.cwd(), 'dist'),
...(tsConfigExists ? { tsconfig: tsConfigPath } : {}),
2022-06-24 12:28:48 +02:00
format: ['cjs'],
2022-10-18 11:30:29 +02:00
target: 'node16',
2022-06-28 11:57:00 +02:00
platform: 'node',
clean: !watch,
2022-06-27 14:13:19 +02:00
external: [name, ...Object.keys(dependencies || {}), ...Object.keys(peerDependencies || {})],
2022-06-24 12:28:48 +02:00
esbuildOptions: (c) => {
/* eslint-disable no-param-reassign */
2022-11-02 13:50:45 +01:00
c.logLevel = 'error';
2022-06-24 12:28:48 +02:00
c.platform = 'node';
c.legalComments = 'none';
c.minifyWhitespace = optimized;
c.minifyIdentifiers = optimized;
c.minifySyntax = optimized;
/* eslint-enable no-param-reassign */
},
}),
]);
2022-08-10 11:20:47 +02:00
if (post) {
2022-11-28 23:31:32 +01:00
await exec(
`node -r ${__dirname}/../node_modules/esbuild-register/register.js ${post}`,
{ cwd },
{ debug: true }
);
2022-08-10 11:20:47 +02:00
}
};
const flags = process.argv.slice(2);
const cwd = process.cwd();
2022-08-24 17:48:56 -04:00
run({ cwd, flags }).catch((err: unknown) => {
// We can't let the stack try to print, it crashes in a way that sets the exit code to 0.
// Seems to have something to do with running JSON.parse() on binary / base64 encoded sourcemaps
// in @cspotcode/source-map-support
if (err instanceof Error) {
2022-11-11 16:49:03 +01:00
console.error(err.stack);
2022-08-24 17:48:56 -04:00
}
process.exit(1);
});