2022-07-27 16:42:03 +02:00
|
|
|
#!/usr/bin/env ../../node_modules/.bin/ts-node
|
2022-07-21 15:42:01 +02:00
|
|
|
|
2022-04-28 18:44:12 +02:00
|
|
|
import fs from 'fs-extra';
|
2022-06-23 16:43:18 +02:00
|
|
|
import path, { join } from 'path';
|
2022-04-28 18:44:12 +02:00
|
|
|
import { build } from 'tsup';
|
2022-06-23 16:43:18 +02:00
|
|
|
import aliasPlugin from 'esbuild-plugin-alias';
|
2022-08-10 11:20:47 +02:00
|
|
|
import dedent from 'ts-dedent';
|
|
|
|
import { exec } from '../utils/exec';
|
2022-04-28 18:44:12 +02:00
|
|
|
|
2022-05-26 11:16:11 +02:00
|
|
|
const hasFlag = (flags: string[], name: string) => !!flags.find((s) => s.startsWith(`--${name}`));
|
|
|
|
|
2022-04-28 18:44:12 +02:00
|
|
|
const run = async ({ cwd, flags }: { cwd: string; flags: string[] }) => {
|
2022-06-27 14:13:19 +02:00
|
|
|
const {
|
|
|
|
name,
|
|
|
|
dependencies,
|
|
|
|
peerDependencies,
|
2022-08-10 11:20:47 +02:00
|
|
|
bundler: { entries, platform, pre, post },
|
2022-06-27 14:13:19 +02:00
|
|
|
} = await fs.readJson(join(cwd, 'package.json'));
|
2022-04-28 18:44:12 +02:00
|
|
|
|
2022-08-10 11:20:47 +02:00
|
|
|
const tsnodePath = join(__dirname, '..', 'node_modules', '.bin', 'ts-node');
|
2022-07-04 12:25:43 +02:00
|
|
|
|
2022-07-06 13:35:13 +02:00
|
|
|
if (pre) {
|
2022-08-10 11:20:47 +02:00
|
|
|
await exec(`${tsnodePath} ${pre}`, { cwd });
|
2022-07-06 13:35:13 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 11:16:11 +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-05-26 11:16:11 +02:00
|
|
|
if (!optimized) {
|
2022-06-24 15:51:17 +02:00
|
|
|
await Promise.all(
|
2022-06-27 14:13:19 +02:00
|
|
|
entries.map(async (file: string) => {
|
2022-06-24 15:51:17 +02:00
|
|
|
console.log(`skipping generating types for ${file}`);
|
2022-07-23 17:15:13 +02:00
|
|
|
const { name: entryName } = path.parse(file);
|
2022-06-24 15:51:17 +02:00
|
|
|
|
2022-07-23 17:15:13 +02:00
|
|
|
const pathName = join(process.cwd(), 'dist', `${entryName}.d.ts`);
|
2022-06-24 15:51:17 +02:00
|
|
|
await fs.ensureFile(pathName);
|
2022-08-10 11:20:47 +02:00
|
|
|
await fs.writeFile(
|
|
|
|
pathName,
|
|
|
|
dedent`
|
|
|
|
// devmode
|
|
|
|
export * from '../src/${entryName}'
|
|
|
|
`
|
|
|
|
);
|
2022-06-24 15:51:17 +02:00
|
|
|
})
|
|
|
|
);
|
2022-05-25 16:00:47 +02:00
|
|
|
}
|
2022-05-25 13:10:48 +02:00
|
|
|
|
2022-07-21 15:42:01 +02:00
|
|
|
const tsConfigPath = join(cwd, 'tsconfig.json');
|
|
|
|
const tsConfigExists = await fs.pathExists(tsConfigPath);
|
2022-06-24 12:28:48 +02:00
|
|
|
await Promise.all([
|
|
|
|
build({
|
2022-07-21 15:42:01 +02:00
|
|
|
entry: entries.map((e: string) => join(cwd, e)),
|
2022-06-24 12:28:48 +02:00
|
|
|
watch,
|
2022-07-23 17:15:13 +02:00
|
|
|
...(tsConfigExists ? { tsconfig: tsConfigPath } : {}),
|
2022-07-21 15:42:01 +02:00
|
|
|
outDir: join(process.cwd(), 'dist'),
|
2022-06-24 12:28:48 +02:00
|
|
|
// sourcemap: optimized,
|
|
|
|
format: ['esm'],
|
|
|
|
target: 'chrome100',
|
2022-07-15 13:02:37 +10:00
|
|
|
clean: !watch,
|
2022-06-28 11:57:00 +02:00
|
|
|
platform: platform || 'browser',
|
2022-06-24 12:28:48 +02:00
|
|
|
esbuildPlugins: [
|
|
|
|
aliasPlugin({
|
2022-08-21 22:17:23 +03:00
|
|
|
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
|
|
|
|
2022-07-23 17:15:13 +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 */
|
2022-08-26 14:30:07 +02:00
|
|
|
c.conditions = ['module'];
|
2022-06-24 12:28:48 +02:00
|
|
|
c.define = optimized
|
2022-07-23 17:15:13 +02:00
|
|
|
? {
|
|
|
|
'process.env.NODE_ENV': "'production'",
|
|
|
|
'process.env': '{}',
|
|
|
|
global: 'window',
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
'process.env.NODE_ENV': "'development'",
|
|
|
|
'process.env': '{}',
|
|
|
|
global: 'window',
|
|
|
|
};
|
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;
|
|
|
|
c.minifyIdentifiers = optimized;
|
|
|
|
c.minifySyntax = optimized;
|
|
|
|
/* eslint-enable no-param-reassign */
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
build({
|
2022-07-21 15:42:01 +02:00
|
|
|
entry: entries.map((e: string) => 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'),
|
2022-07-23 17:15:13 +02:00
|
|
|
...(tsConfigExists ? { tsconfig: tsConfigPath } : {}),
|
2022-06-24 12:28:48 +02:00
|
|
|
format: ['cjs'],
|
|
|
|
target: 'node14',
|
2022-06-28 11:57:00 +02:00
|
|
|
platform: 'node',
|
2022-07-15 13:02:37 +10:00
|
|
|
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 */
|
|
|
|
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) {
|
|
|
|
await exec(`${tsnodePath} ${post}`, { cwd }, { debug: true });
|
|
|
|
}
|
2022-04-28 18:44:12 +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) {
|
|
|
|
console.error(err.message);
|
|
|
|
}
|
2022-04-28 18:44:12 +02:00
|
|
|
process.exit(1);
|
|
|
|
});
|