-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
122 lines (117 loc) · 3.46 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
111
112
113
114
115
116
117
118
119
120
121
122
import { nodeResolve } from '@rollup/plugin-node-resolve';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import { babel } from '@rollup/plugin-babel';
import json from '@rollup/plugin-json';
import commonjs from '@rollup/plugin-commonjs';
import replace from '@rollup/plugin-replace';
import globby from 'fast-glob';
import path from 'path';
import terser from '@rollup/plugin-terser';
const extensions = ['.js', '.ts', '.jsx', '.tsx'];
const babelIncludes = ['./src/**/*'];
const bundleNpmWorkspacePackages = [];
const bundlePackages = [];
const neverBundlePackages = [];
const shouldBundleLocalFilesTogether = false;
const shouldBundleNodeModules = false;
const isDevelopment = process.env.ROLLUP_WATCH;
const decorators = false;
const isProduction = process.env.NODE_ENV === 'production';
const isPackageDependency = (pkg, path, importer = '') => {
return path.includes('/' + pkg + '/') || (importer.includes('/' + pkg + '/') && path.startsWith('.')) || path === pkg;
};
const getRollupConfig =
({ isBrowser = false, format = 'esm' } = { isBrowser: false, format: 'esm' }) =>
(localInput) => {
const input = localInput;
return {
input,
output: {
file: path.join(
'./dist',
format,
// isBrowser ? '' : 'server',
localInput.replace('/src', '').replace(/\.(tsx|ts)/, format === 'cjs' ? '.js' : '.js')
),
format,
},
external(id, second = '') {
const sanitizedId = id.split('?')[0];
const isNodeModule = id.includes('node_modules');
if (id.endsWith('.json')) return false;
if (sanitizedId.endsWith(input.replace('./', '/'))) {
return false;
}
// No need to pass second because the entry will be stopped
if (neverBundlePackages.find((pkg) => isPackageDependency(pkg, id))) {
return true;
}
if (bundlePackages.find((pkg) => isPackageDependency(pkg, id, second))) {
return false;
}
if (
!id.includes('node_modules') &&
!second.includes('node_modules') &&
bundleNpmWorkspacePackages.find((pkg) => id.includes('/' + pkg + '/') || second.includes('/' + pkg + '/'))
) {
return false;
}
if (isNodeModule) {
return !shouldBundleNodeModules;
}
return !shouldBundleLocalFilesTogether;
},
plugins: [
replace({
preventAssignment: true,
'process.env.NODE_ENV': `'${process.env.NODE_ENV}'`,
}),
json(),
nodeResolve({
extensions,
preferBuiltins: true,
browser: isBrowser ? true : false,
}),
commonjs(),
peerDepsExternal(),
babel({
extensions,
babelHelpers: 'runtime',
include: babelIncludes,
}),
isDevelopment ? undefined : terser({ keep_fnames: decorators }),
],
};
};
const inputs = [
{ include: ['./src/**', '!./src/client/**'], name: 'server' },
{ include: ['./src/client/**'], name: 'server2', browser: true },
];
/**[
{
include: ['./src/**', '!./src/client/**'],
entry: `./src/index.ts`,
name: 'server',
},
{
include: ['./src/client/**'],
entry: `./src/client/index.ts`,
name: 'server',
browser: true,
},
];
*/
const wow = inputs.reduce((acc, input) => {
const files = globby.sync([...input.include, '!*.json'], {
// cwd: process.env.FOLDER_PATH,
});
// const tempp = files.map((file) => path.join(process.env.FOLDER_PATH, file));
const formats = ['cjs', 'esm'];
return [
...acc,
...formats.reduce((acc, format) => {
return [...acc, ...files.map(getRollupConfig({ isBrowser: input.browser, format }))];
}, []),
];
}, []);
export default wow;