forked from strapi/strapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.utils.mjs
More file actions
134 lines (120 loc) · 3.01 KB
/
Copy pathrollup.utils.mjs
File metadata and controls
134 lines (120 loc) · 3.01 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
import path from 'node:path';
import { defineConfig } from 'rollup';
import swc from '@rollup/plugin-swc';
import json from '@rollup/plugin-json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
import commonjs from '@rollup/plugin-commonjs';
import image from '@rollup/plugin-image';
import html from 'rollup-plugin-html';
import postcss from 'rollup-plugin-postcss';
const isExernal = (id) => !path.isAbsolute(id) && !id.startsWith('.');
const basePlugins = () => [
image(),
html(),
json(),
postcss({
extensions: ['.css'],
inject: true,
minimize: true,
extract: true,
}),
nodeResolve({
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json', '.css'],
}),
commonjs({
ignoreDynamicRequires: true,
}),
swc({
swc: {
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
},
target: 'es2020',
transform: {
react: {
runtime: 'automatic',
},
},
},
sourceMaps: true,
},
}),
dynamicImportVars({}),
];
const isInput = (id, input) => {
if (typeof input === 'string') {
return id.includes(path.resolve(input));
}
return Object.values(input).some((i) => id.includes(path.resolve(i)));
};
const baseConfig = (opts = {}) => {
const { rootDir, outDir = './dist', input = './src/index.ts', ...rest } = opts;
return defineConfig({
input,
external: isExernal,
output: baseOutput({ outDir, rootDir }),
plugins: basePlugins(),
onwarn(warning, warn) {
if (warning.code === 'MIXED_EXPORTS') {
// json files are always mixed exports
if (warning?.id?.endsWith('.json')) {
return;
}
// we only care about mixed exports in our input files
if (warning.id && !isInput(warning.id, input)) {
return;
}
}
if (warning.code === 'UNUSED_EXTERNAL_IMPORT' && warning.exporter === 'react') {
return;
}
warn(warning);
},
...rest,
});
};
const baseOutput = ({ outDir, rootDir }) => {
return [
{
dir: outDir,
entryFileNames: '[name].js',
chunkFileNames: '[name]-[hash].js',
exports: 'auto',
format: 'cjs',
sourcemap: true,
preserveModules: true,
...(rootDir ? { preserveModulesRoot: rootDir } : {}),
},
{
dir: outDir,
entryFileNames: '[name].mjs',
chunkFileNames: '[name]-[hash].mjs',
format: 'esm',
sourcemap: true,
preserveModules: true,
...(rootDir ? { preserveModulesRoot: rootDir } : {}),
},
];
};
const basePluginConfig = () => {
return defineConfig([
baseConfig({
input: {
index: './server/src/index.ts',
},
rootDir: './server/src',
outDir: './dist/server',
}),
baseConfig({
input: {
index: './admin/src/index.ts',
},
rootDir: './admin/src',
outDir: './dist/admin',
}),
]);
};
export { baseConfig, basePluginConfig };