|
1 | 1 | import type { |
2 | 2 | Alias, |
| 3 | + BuildOptions, |
3 | 4 | Plugin, |
4 | 5 | UserConfig, |
5 | 6 | } from 'vite' |
6 | | -import type { ExternalOption, RollupOptions } from 'rollup' |
| 7 | +import type { RollupOptions } from 'rollup' |
7 | 8 | import { electronBuiltins } from './utils' |
8 | 9 |
|
9 | 10 | export default function buildConfig(nodeIntegration?: boolean): Plugin[] { |
@@ -54,45 +55,49 @@ export default function buildConfig(nodeIntegration?: boolean): Plugin[] { |
54 | 55 |
|
55 | 56 | if (nodeIntegration) { |
56 | 57 | config.build.rollupOptions ??= {} |
57 | | - config.build.rollupOptions.external = withExternal(config.build.rollupOptions.external) |
58 | | - setOutputFormat(config.build.rollupOptions) |
| 58 | + config.build.rollupOptions.output ??= {} |
| 59 | + |
| 60 | + // `fs-extra` will extend the `fs` module |
| 61 | + setOutputFreeze(config.build.rollupOptions) |
| 62 | + |
| 63 | + // Some third-party modules, such as `fs-extra`, extend the native module |
| 64 | + // `__esModule` to bypass Rollup's `getAugmentedNamespace` |
| 65 | + // see - https://github.com/rollup/plugins/blob/commonjs-v24.0.0/packages/commonjs/src/helpers.js#L38 |
| 66 | + withIgnore(config.build) |
59 | 67 | } |
60 | 68 | }, |
61 | 69 | }, |
62 | 70 | ] |
63 | 71 | } |
64 | 72 |
|
65 | | -function withExternal(external?: ExternalOption) { |
66 | | - if ( |
67 | | - Array.isArray(external) || |
68 | | - typeof external === 'string' || |
69 | | - external instanceof RegExp |
70 | | - ) { |
71 | | - // @ts-ignore |
72 | | - external = electronBuiltins.concat(external) |
73 | | - } else if (typeof external === 'function') { |
74 | | - const original = external |
75 | | - external = function externalFn(source, importer, isResolved) { |
76 | | - if (electronBuiltins.includes(source)) { |
77 | | - return true |
78 | | - } |
79 | | - return original(source, importer, isResolved) |
| 73 | +function setOutputFreeze(rollupOptions: RollupOptions) { |
| 74 | + rollupOptions.output ??= {} |
| 75 | + if (Array.isArray(rollupOptions.output)) { |
| 76 | + for (const o of rollupOptions.output) { |
| 77 | + o.freeze ??= false |
80 | 78 | } |
81 | 79 | } else { |
82 | | - external = electronBuiltins |
| 80 | + rollupOptions.output.freeze ??= false |
83 | 81 | } |
84 | | - return external |
85 | 82 | } |
86 | 83 |
|
87 | | -// At present, Electron can only support CommonJs |
88 | | -function setOutputFormat(rollupOptions: RollupOptions) { |
89 | | - rollupOptions.output ??= {} |
90 | | - if (Array.isArray(rollupOptions.output)) { |
91 | | - for (const o of rollupOptions.output) { |
92 | | - o.format ??= 'cjs' |
| 84 | +function withIgnore(configBuild: BuildOptions) { |
| 85 | + configBuild.commonjsOptions ??= {} |
| 86 | + if (configBuild.commonjsOptions.ignore) { |
| 87 | + if (typeof configBuild.commonjsOptions.ignore === 'function') { |
| 88 | + const userIgnore = configBuild.commonjsOptions.ignore |
| 89 | + configBuild.commonjsOptions.ignore = id => { |
| 90 | + if (userIgnore?.(id) === true) { |
| 91 | + return true |
| 92 | + } |
| 93 | + return electronBuiltins.includes(id) |
| 94 | + } |
| 95 | + } else { |
| 96 | + // @ts-ignore |
| 97 | + configBuild.commonjsOptions.ignore.push(...electronBuiltins) |
93 | 98 | } |
94 | 99 | } else { |
95 | | - rollupOptions.output.format ??= 'cjs' |
| 100 | + configBuild.commonjsOptions.ignore = electronBuiltins |
96 | 101 | } |
97 | 102 | } |
98 | 103 |
|
|
0 commit comments