forked from mjackson/unpkg
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rollup.config.mjs
97 lines (88 loc) · 2.71 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
import { builtinModules } from 'module';
import { execSync } from 'child_process';
import { readFileSync } from 'fs';
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import compiler from '@ampproject/rollup-plugin-closure-compiler';
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';
import resolve from '@rollup/plugin-node-resolve';
import url from '@rollup/plugin-url';
import entryManifest from './plugins/entryManifest.mjs';
const pkg = JSON.parse(readFileSync('./package.json'));
const buildId = process.env.BUILD_ID || execSync('git rev-parse --short HEAD').toString().trim();
const manifest = entryManifest();
const client = ['browse', 'main'].map(entryName => {
return {
external: ['@emotion/react', 'react', 'react-dom'],
input: `modules/client/${entryName}.js`,
output: {
format: 'iife',
dir: 'public/_client',
entryFileNames: '[name]-[hash].js',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'@emotion/react': 'emotionReact',
},
},
moduleContext: {
'node_modules/react-icons/lib/esm/iconBase.js': 'window',
},
plugins: [
manifest.record({ publicPath: '/_client/' }),
babel({ exclude: /node_modules/ }),
json(),
resolve(),
commonjs({
namedExports: {
'node_modules/react/index.js': ['createContext', 'createElement', 'forwardRef', 'Component', 'Fragment'],
},
}),
replace({
preventAssignment: true,
values: {
'process.env.BUILD_ID': JSON.stringify(buildId),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
},
}),
url({
limit: 5 * 1024,
publicPath: '/_client/',
}),
compiler(),
],
};
});
const dependencies = (
process.env.NODE_ENV === 'development'
? Object.keys(pkg.dependencies).concat(Object.keys(pkg.devDependencies || {}))
: Object.keys(pkg.dependencies)
).concat('react-dom/server');
const server = {
external: builtinModules.concat(dependencies).filter(d => !['mime'].includes(d)),
input: 'modules/server.js',
output: { file: 'server.js', format: 'cjs' },
moduleContext: {
'node_modules/react-icons/lib/esm/iconBase.js': 'global',
},
plugins: [
manifest.inject({ virtualId: 'entry-manifest' }),
babel({ exclude: /node_modules/ }),
json(),
resolve(),
commonjs(),
url({
limit: 5 * 1024,
publicPath: '/_client/',
emitFiles: false,
}),
replace({
preventAssignment: true,
values: {
'process.env.BUILD_ID': JSON.stringify(buildId),
},
}),
],
};
export default client.concat(server);