axios-miniprogram/rollup.config.ts

73 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-03-25 14:16:10 +08:00
import { readFileSync } from 'node:fs';
2023-04-17 00:00:45 +08:00
import { RollupOptions, OutputOptions, Plugin, ModuleFormat } from 'rollup';
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-04-17 00:00:45 +08:00
import { __dirname, distPath, getPkgJSON, resolve } from './scripts/utils';
2023-03-23 20:09:00 +08:00
const pkg = getPkgJSON();
2023-04-10 15:12:28 +08:00
const inputPath = resolve('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;
}
2023-04-17 00:00:45 +08:00
function buildConfig(format: ModuleFormat | 'dts'): RollupOptions {
2023-03-23 20:09:00 +08:00
const isDts = format === 'dts';
2023-04-17 00:00:45 +08:00
const output: OutputOptions = {
2023-04-03 21:03:33 +08:00
file: resolveOutput(format, isDts),
2023-03-23 20:09:00 +08:00
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({
2023-04-10 15:12:28 +08:00
tsconfig: resolve('tsconfig.json'),
2023-03-24 19:41:17 +08:00
}),
2023-04-10 15:12:28 +08:00
compleTypePlugin([resolve('global.d.ts')]),
2023-03-24 19:41:17 +08:00
]
: esbuildPlugin({
2023-04-10 15:12:28 +08:00
tsconfig: resolve('tsconfig.json'),
2023-04-17 00:00:45 +08:00
sourceMap: output.sourcemap as boolean,
2023-04-05 08:40:00 +08:00
target: 'es2015',
2023-03-23 20:09:00 +08:00
minify: true,
}),
],
};
}
2023-04-17 00:00:45 +08:00
function resolveOutput(format: string, isDts?: boolean) {
2023-04-10 15:12:28 +08:00
return resolve(distPath, `${pkg.name}${isDts ? '.d.ts' : `.${format}.js`}`);
2023-03-23 20:09:00 +08:00
}
2023-03-24 19:41:17 +08:00
2023-04-17 00:00:45 +08:00
function compleTypePlugin(files: string[]): Plugin {
2023-03-24 19:41:17 +08:00
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
};
}