axios-miniprogram/rollup.config.ts

78 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,
2023-04-18 19:50:40 +08:00
exports: 'named',
2023-03-23 20:09:00 +08:00
indent: false,
sourcemap: isDts ? false : sourceMap,
};
2023-04-18 10:16:35 +08:00
const plugins: Plugin[] = [];
if (isDts) {
plugins.push(
dtsPlugin({
tsconfig: resolve('tsconfig.json'),
}),
patchTypePlugin([resolve('global.d.ts')]),
);
} else {
plugins.push(
esbuildPlugin({
tsconfig: resolve('tsconfig.json'),
sourceMap: output.sourcemap as boolean,
target: 'es2015',
minify: true,
}),
);
}
2023-03-23 20:09:00 +08:00
return {
input: inputPath,
output,
2023-04-18 10:16:35 +08:00
plugins,
2023-03-23 20:09:00 +08:00
};
}
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-18 10:16:35 +08:00
function patchTypePlugin(files: string[]): Plugin {
2023-03-24 19:41:17 +08:00
return {
2023-04-18 10:16:35 +08:00
name: 'patch-type',
2023-03-24 19:41:17 +08:00
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
};
}