-
Notifications
You must be signed in to change notification settings - Fork 5
/
rollup.config.js
69 lines (61 loc) · 1.96 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
const typescript = require('@rollup/plugin-typescript');
const terser = require('@rollup/plugin-terser');
const commonjs = require('@rollup/plugin-commonjs');
const json = require('@rollup/plugin-json');
const path = require('path');
const fs = require('fs');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const { optimizeLodashImports } = require('@optimize-lodash/rollup-plugin');
const BASE_PATH = process.cwd();
const getConfigFile = name => {
const filePath = path.resolve(BASE_PATH, name);
if (fs.existsSync(path.resolve(BASE_PATH, name))) {
return filePath;
}
return require.resolve(`@stoplight/scripts/${name}`);
};
const plugins = () =>
[
typescript({
tsconfig: getConfigFile('tsconfig.build.json'),
}),
nodeResolve(),
commonjs(),
optimizeLodashImports(),
process.env.MINIFY ? terser() : null,
].filter(Boolean);
const packageJson = JSON.parse(fs.readFileSync(path.resolve(BASE_PATH, 'package.json'), { encoding: 'utf-8' }));
const dependencies = [
...Object.keys(packageJson.dependencies || {}),
...Object.keys(packageJson.peerDependencies || {}),
];
const rollupConfig = packageJson.rollup || {};
const alwaysInclude = rollupConfig.bundleDeps || [];
const external = module => {
const isDefaultExternal = dependencies.some(dep => module === dep || module.startsWith(`${dep}/`));
const forceBundle = alwaysInclude.some(dep => module === dep || module.startsWith(`${dep}/`));
return isDefaultExternal && !forceBundle;
};
module.exports = [
{
input: path.resolve(BASE_PATH, 'src/index.ts'),
output: {
entryFileNames: '[name].js',
dir: path.resolve(BASE_PATH, 'dist'),
format: 'cjs',
},
plugins: [json(), ...plugins()],
external,
},
{
input: path.resolve(BASE_PATH, 'src/index.ts'),
output: {
entryFileNames: '[name].mjs',
dir: path.resolve(BASE_PATH, 'dist'),
format: 'esm',
},
plugins: plugins(),
external,
},
];