139 lines
4.0 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, { join } 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';
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-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-08-10 11:20:47 +02:00
const tsnodePath = join(__dirname, '..', 'node_modules', '.bin', 'ts-node');
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
}
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'));
}
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}`);
const { name: entryName } = path.parse(file);
2022-06-24 15:51:17 +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-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,
...(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',
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(
2022-07-21 15:42:01 +02:00
'../node_modules/rollup-plugin-node-polyfills/polyfills/process-es6.js'
2022-06-24 12:28:48 +02:00
),
2022-07-21 15:42:01 +02:00
util: path.resolve('../node_modules/rollup-plugin-node-polyfills/polyfills/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.define = optimized
? {
'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'),
...(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',
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 });
}
};
const flags = process.argv.slice(2);
const cwd = process.cwd();
run({ cwd, flags }).catch((err) => {
console.error(err.stack);
process.exit(1);
});