-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
161 lines (148 loc) · 4.75 KB
/
rollup.config.ts
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
// import nodePolyfills from 'rollup-plugin-node-polyfills';
import typescript from '@rollup/plugin-typescript'
import json from '@rollup/plugin-json'
// import MagicString from 'magic-string'
import type { RollupOptions } from 'rollup'
import { defineConfig } from 'rollup'
const pkg = JSON.parse(
readFileSync(new URL('./package.json', import.meta.url)).toString(),
)
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const sharedNodeOptions = defineConfig({
treeshake: {
moduleSideEffects: 'no-external',
propertyReadSideEffects: false,
tryCatchDeoptimization: false,
},
output: {
dir: './dist',
entryFileNames: `node/[name].js`,
chunkFileNames: 'node/chunks/dep-[hash].js',
exports: 'named',
format: 'esm',
externalLiveBindings: false,
freeze: false,
},
onwarn(warning, warn) {
if (warning.message.includes('Circular dependency')) {
return
}
warn(warning)
},
})
function createNodePlugins(
isProduction: boolean,
sourceMap: boolean,
declarationDir: string | false,
): (any)[] {
return [
// nodePolyfills(),
nodeResolve({ preferBuiltins: true }),
typescript({
tsconfig: path.resolve(__dirname, 'src/node/tsconfig.json'),
sourceMap,
declaration: declarationDir !== false,
declarationDir: declarationDir !== false ? declarationDir : undefined,
}),
commonjs({
extensions: ['.js'],
// Optional peer deps of ws. Native deps that are mostly for performance.
// Since ws is not that perf critical for us, just ignore these deps.
ignore: ['bufferutil', 'utf-8-validate'],
}),
json(),
// cjsPatchPlugin(),
]
}
function createNodeConfig(isProduction: boolean) {
return defineConfig({
...sharedNodeOptions,
input: {
cli: path.resolve(__dirname, 'src/node/cli.ts'),
constants: path.resolve(__dirname, 'src/node/constants.ts'),
},
output: {
...sharedNodeOptions.output,
sourcemap: !isProduction,
},
external: [
'vite',
'svg-sprite', // move these to 'dependencies'? or move all to dependencies? whats the difference in this setup?
...Object.keys(pkg.dependencies),
...(isProduction ? [] : Object.keys(pkg.devDependencies)),
],
plugins: createNodePlugins(
isProduction,
!isProduction,
// in production we use api-extractor for dts generation
// in development we need to rely on the rollup ts plugin
isProduction ? false : './dist/node',
),
})
}
// function createCjsConfig(isProduction: boolean) {
// return defineConfig({
// ...sharedNodeOptions,
// input: {
// publicUtils: path.resolve(__dirname, 'src/node/publicUtils.ts'),
// },
// output: {
// dir: './dist',
// entryFileNames: `node-cjs/[name].cjs`,
// chunkFileNames: 'node-cjs/chunks/dep-[hash].js',
// exports: 'named',
// format: 'cjs',
// externalLiveBindings: false,
// freeze: false,
// sourcemap: false,
// },
// external: [
// ...Object.keys(pkg.dependencies),
// ...(isProduction ? [] : Object.keys(pkg.devDependencies)),
// ],
// plugins: [...createNodePlugins(false, false, false)],
// })
// }
/**
* Inject CJS Context for each deps chunk
*/
// function cjsPatchPlugin(): any {
// const cjsPatch = `
// import { fileURLToPath as __cjs_fileURLToPath } from 'node:url';
// import { dirname as __cjs_dirname } from 'node:path';
// import { createRequire as __cjs_createRequire } from 'node:module';
// const __filename = __cjs_fileURLToPath(import.meta.url);
// const __dirname = __cjs_dirname(__filename);
// const require = __cjs_createRequire(import.meta.url);
// const __require = require;
// `.trimStart()
// return {
// name: 'cjs-chunk-patch',
// renderChunk(code: any, chunk: any) {
// if (!chunk.fileName.includes('chunks/dep-')) return
// const match = code.match(/^(?:import[\s\S]*?;\s*)+/)
// const index = match ? match.index! + match[0].length : 0
// const s = new MagicString(code)
// // inject after the last `import`
// s.appendRight(index, cjsPatch)
// console.log('patched cjs context: ' + chunk.fileName)
// return {
// code: s.toString(),
// map: s.generateMap({ hires: true }),
// }
// },
// }
// }
export default (commandLineArgs: any): RollupOptions[] => {
const isDev = commandLineArgs.watch // ToDo: check if this is really utilized
const isProduction = !isDev
return defineConfig([
createNodeConfig(isProduction),
// createCjsConfig(isProduction)
])
}