-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathrollup.config.mjs
110 lines (102 loc) · 3.24 KB
/
rollup.config.mjs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import path from 'node:path';
import esbuild from 'rollup-plugin-esbuild';
import injectProcessEnv from 'rollup-plugin-inject-process-env';
import peerDepsExternalPlugin from 'rollup-plugin-peer-deps-external';
import sourcemaps from 'rollup-plugin-sourcemaps';
import repo from './package.json' with { type: 'json' };
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
const moduleTarget = 'es2020';
const paths = {
distributable: path.join(process.cwd(), 'dist'),
library: path.join(process.cwd(), 'lib'),
source: path.join(process.cwd(), 'src'),
};
const {
dependencies = {},
} = repo;
const plugins = ({ env, esmExternals = false } = {}) => [
json(),
esbuild({ target: moduleTarget, minify: env === 'production' }),
sourcemaps(),
commonjs({ esmExternals }),
...(env ? [injectProcessEnv({
NODE_ENV: env
})] : []),
resolve({
browser: true,
preferBuiltins: false,
}),
peerDepsExternalPlugin(),
];
/**
* Escapes the `RegExp` special characters.
* @param {string} str
*/
function escapeRegExp(str)
{
return str.replace(/[$()*+.?[\\\]^{|}]/g, '\\$&');
}
/**
* Convert the name of a package to a `RegExp` that matches the package's export names.
* @param {string} packageName
*/
function convertPackageNameToRegExp(packageName)
{
return new RegExp(`^${escapeRegExp(packageName)}(/.+)?$`);
}
const external = ({ bundleDeps = true } = {}) => (bundleDeps ? [] : Object.keys(dependencies).map(convertPackageNameToRegExp));
const targets = {
lib: {
path: paths.library,
entryFileNames: '[name]',
env: undefined,
external: external(),
preserveModules: true,
esmExternals: false,
},
'dist-dev': {
path: paths.distributable,
entryFileNames: 'pixi-react',
env: 'development',
external: external({ bundleDeps: true }),
preserveModules: false,
esmExternals: true,
},
'dist-prod': {
path: paths.distributable,
entryFileNames: 'pixi-react.min',
env: 'production',
external: external({ bundleDeps: true }),
preserveModules: false,
esmExternals: true,
},
};
export default ['lib', 'dist-dev', 'dist-prod'].map((target) =>
({
input: 'src/index.ts',
output: [
{
dir: targets[target].path,
entryFileNames: `${targets[target].entryFileNames}.js`,
exports: 'named',
format: 'cjs',
preserveModules: targets[target].preserveModules,
preserveModulesRoot: paths.source,
sourcemap: true,
},
{
dir: targets[target].path,
entryFileNames: `${targets[target].entryFileNames}.mjs`,
exports: 'named',
format: 'esm',
preserveModules: targets[target].preserveModules,
preserveModulesRoot: paths.source,
sourcemap: true,
},
],
plugins: plugins({ env: targets[target].env, esmExternals: targets[target].esmExternals }),
external: targets[target].external,
treeshake: false
}));