axios-miniprogram/rollup.config.js

70 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-05-21 14:26:22 +08:00
import path from 'path';
import rimraf from 'rimraf';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { babel } from '@rollup/plugin-babel';
import typescript from 'rollup-plugin-typescript2';
import commonjs from '@rollup/plugin-commonjs';
import { DEFAULT_EXTENSIONS } from '@babel/core';
2021-05-11 10:22:44 +08:00
2021-05-21 14:26:22 +08:00
import filterEmptyLines from './scripts/@rollup/plugin-filter-empty-lines';
import pkg from './package.json';
2021-05-11 10:22:44 +08:00
const entryFilePath = path.resolve(__dirname, 'src/index.ts');
2021-05-21 14:26:22 +08:00
const distPath = path.resolve(__dirname, 'dist');
2021-05-30 18:26:43 +08:00
const pkgName = pkg.name;
2021-05-11 10:22:44 +08:00
const extensions = [].concat(DEFAULT_EXTENSIONS, '.ts');
2021-05-21 14:26:22 +08:00
const plugins = [
2021-05-11 10:22:44 +08:00
nodeResolve({
extensions,
}),
2021-05-21 14:26:22 +08:00
typescript({
useTsconfigDeclarationDir: true,
}),
2021-05-11 10:22:44 +08:00
babel({
extensions,
2021-05-21 14:26:22 +08:00
babelHelpers: 'bundled',
comments: false,
}),
commonjs({
include: /node_modules/,
2021-05-11 10:22:44 +08:00
}),
2021-05-21 14:26:22 +08:00
filterEmptyLines(),
2021-05-11 10:22:44 +08:00
];
2021-05-21 14:26:22 +08:00
function resolveOutputFilePath(format) {
2021-05-30 18:26:43 +08:00
return path.resolve(distPath, format, `${pkgName}.js`);
2021-05-21 14:26:22 +08:00
}
2021-05-11 10:22:44 +08:00
2021-05-21 14:26:22 +08:00
function generateConfig(format) {
2021-05-11 10:22:44 +08:00
return {
input: entryFilePath,
output: {
file: resolveOutputFilePath(format),
format,
2021-05-30 18:26:43 +08:00
name: pkgName,
2021-05-11 10:22:44 +08:00
exports: 'default',
2021-05-21 14:26:22 +08:00
indent: false,
2021-05-11 10:22:44 +08:00
},
plugins,
};
}
let run;
2021-05-21 14:26:22 +08:00
const promise = new Promise((resolve, reject) => {
run = (err) => {
if (err) {
reject(err);
return;
2021-05-11 10:22:44 +08:00
}
2021-05-21 14:26:22 +08:00
resolve([generateConfig('esm'), generateConfig('cjs')]);
2021-05-11 10:22:44 +08:00
};
});
2021-05-21 14:26:22 +08:00
export default () => {
rimraf(distPath, run);
2021-05-11 10:22:44 +08:00
return promise;
};