2023-03-23 20:09:00 +08:00
|
|
|
import path from 'node:path';
|
2023-03-25 14:16:10 +08:00
|
|
|
import { readFileSync } from 'node:fs';
|
2023-03-24 19:41:17 +08:00
|
|
|
import esbuildPlugin from 'rollup-plugin-esbuild';
|
2023-03-23 20:09:00 +08:00
|
|
|
import dtsPlugin from 'rollup-plugin-dts';
|
2023-03-24 19:41:17 +08:00
|
|
|
import { __dirname, distPath, getPkgJSON } from './scripts/utils.js';
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
|
|
const pkg = getPkgJSON();
|
|
|
|
const inputPath = path.resolve(__dirname, 'src/index.ts');
|
2023-03-24 19:41:17 +08:00
|
|
|
|
2023-03-23 20:09:00 +08:00
|
|
|
const sourceMap = process.env.SOURCE_MAP === 'true';
|
|
|
|
const dts = process.env.DTS === 'true';
|
|
|
|
|
|
|
|
export default main();
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
const configs = [buildConfig('esm'), buildConfig('cjs')];
|
|
|
|
if (dts) {
|
|
|
|
configs.push(buildConfig('dts'));
|
|
|
|
}
|
|
|
|
return configs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildConfig(format) {
|
|
|
|
const isDts = format === 'dts';
|
|
|
|
const output = {
|
|
|
|
file: resolvePath(format, isDts),
|
|
|
|
format: isDts ? 'es' : format,
|
|
|
|
name: pkg.name,
|
|
|
|
exports: 'default',
|
|
|
|
indent: false,
|
|
|
|
sourcemap: isDts ? false : sourceMap,
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
input: inputPath,
|
|
|
|
output,
|
|
|
|
plugins: [
|
|
|
|
isDts
|
2023-03-24 19:41:17 +08:00
|
|
|
? [
|
|
|
|
dtsPlugin({
|
|
|
|
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
|
|
|
|
}),
|
|
|
|
compleTypePlugin([path.resolve(__dirname, 'global.d.ts')]),
|
|
|
|
]
|
|
|
|
: esbuildPlugin({
|
|
|
|
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
|
2023-03-23 20:09:00 +08:00
|
|
|
sourceMap: output.sourcemap,
|
|
|
|
minify: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolvePath(format, isDts) {
|
|
|
|
return path.resolve(
|
2023-03-24 19:41:17 +08:00
|
|
|
distPath,
|
2023-03-23 20:09:00 +08:00
|
|
|
`${pkg.name}${isDts ? '.d.ts' : `.${format}.js`}`,
|
|
|
|
);
|
|
|
|
}
|
2023-03-24 19:41:17 +08:00
|
|
|
|
|
|
|
function compleTypePlugin(files) {
|
|
|
|
return {
|
|
|
|
name: 'comple-type',
|
|
|
|
renderChunk: (code) =>
|
|
|
|
`${files
|
|
|
|
.map(
|
|
|
|
(file) =>
|
|
|
|
`// ${file.replace(__dirname, '')}\n${readFileSync(
|
|
|
|
file,
|
2023-03-25 14:16:10 +08:00
|
|
|
).toString()}\n// ${file.replace(__dirname, '')} end`,
|
2023-03-24 19:41:17 +08:00
|
|
|
)
|
2023-03-25 14:16:10 +08:00
|
|
|
.join('\n')}\n${code}`,
|
2023-03-24 19:41:17 +08:00
|
|
|
};
|
|
|
|
}
|