forked from single-spa/single-spa-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.ts
More file actions
92 lines (88 loc) · 2.86 KB
/
Copy pathrollup.config.ts
File metadata and controls
92 lines (88 loc) · 2.86 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
import { stripCode } from '@codecb/rollup-plugin-strip-code';
import { babel } from '@rollup/plugin-babel';
import replace from '@rollup/plugin-replace';
import { resolve } from 'node:path';
import { defineConfig, ModuleFormat } from 'rollup';
import { terser } from 'rollup-plugin-terser';
import ts from 'rollup-plugin-ts';
import pkg from './package.json';
const rootDir = process.cwd();
const isDev =
process.env['ROLLUP_WATCH'] === 'true' ||
process.env['NODE_ENV'] === 'development';
export type BundleTarget = 'browser' | 'server';
const createConfig = ({
format,
target,
}: {
format: ModuleFormat;
target: BundleTarget;
}) => {
const inputFile =
target === 'server'
? resolve(rootDir, 'src/server/index.ts')
: resolve(rootDir, 'src/browser/index.ts');
const extension =
format === 'esm' || format === 'es' || format === 'module'
? 'mjs'
: format === 'cjs' || format === 'commonjs'
? 'cjs'
: 'js';
const outputFile = resolve(rootDir, `dist/${format}/${target}.${extension}`);
return defineConfig({
input: inputFile,
output: {
banner: `/*! ${pkg.name}/${target}@${pkg.version} - ${format} format */`,
file: outputFile,
format,
sourcemap: isDev,
},
external: [/^node:*/, /^parse5.*/, 'single-spa'],
plugins: [
!isDev &&
stripCode({
comments: [{ end: '#devOnlyEnd', start: '#devOnlyStart' }],
}),
ts({
hook: {
// There may be a bug in `rollup-plugin-ts` that make it unable to load `.d.mts` files.
// So let's make it generate `.d.ts` files instead for now.
// We could remove this after this is resolved by `rollup-plugin-ts`.
outputPath: (path, kind) =>
kind === 'declaration' || kind === 'declarationMap'
? path.replace(/\.d\.[mc]ts/, '.d.ts')
: path,
},
tsconfig: resolve(
rootDir,
isDev ? 'tsconfig.dev.json' : 'tsconfig.json',
),
}),
replace({
preventAssignment: true,
values: {
'process.env.BABEL_ENV': JSON.stringify('production'),
'process.env.NODE_ENV': JSON.stringify(
isDev ? 'development' : 'production',
),
},
}),
babel({
babelHelpers: 'bundled',
presets: [['@codecb/babel', { target }]],
}),
!isDev && terser({ compress: { passes: 2 } }),
],
onwarn(warning, defaultHandler) {
// Circular dependency between recursive render functions is intended
if (!['CIRCULAR_DEPENDENCY'].includes(warning.code!))
defaultHandler(warning);
},
});
};
export default [
createConfig({ format: 'esm', target: 'browser' }),
createConfig({ format: 'system', target: 'browser' }),
createConfig({ format: 'cjs', target: 'server' }),
createConfig({ format: 'esm', target: 'server' }),
];