X Tutup
Skip to content

Commit 56d1faf

Browse files
authored
build: Wrap bundles using webpack (electron#25557)
1 parent f7945ad commit 56d1faf

File tree

9 files changed

+168
-177
lines changed

9 files changed

+168
-177
lines changed

build/webpack/get-outputs.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

build/webpack/run-compiler.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

build/webpack/webpack.config.base.js

Lines changed: 143 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ const fs = require('fs');
22
const path = require('path');
33
const webpack = require('webpack');
44
const TerserPlugin = require('terser-webpack-plugin');
5+
const WrapperPlugin = require('wrapper-webpack-plugin');
56

67
const electronRoot = path.resolve(__dirname, '../..');
78

8-
const onlyPrintingGraph = !!process.env.PRINT_WEBPACK_GRAPH;
9-
109
class AccessDependenciesPlugin {
1110
apply (compiler) {
12-
// Only hook into webpack when we are printing the dependency graph
13-
if (!onlyPrintingGraph) return;
14-
1511
compiler.hooks.compilation.tap('AccessDependenciesPlugin', compilation => {
1612
compilation.hooks.finishModules.tap('AccessDependenciesPlugin', modules => {
1713
const filePaths = modules.map(m => m.resource).filter(p => p).map(p => path.relative(electronRoot, p));
@@ -21,49 +17,6 @@ class AccessDependenciesPlugin {
2117
}
2218
}
2319

24-
const defines = {
25-
BUILDFLAG: onlyPrintingGraph ? '(a => a)' : ''
26-
};
27-
28-
const buildFlagsPrefix = '--buildflags=';
29-
const buildFlagArg = process.argv.find(arg => arg.startsWith(buildFlagsPrefix));
30-
31-
if (buildFlagArg) {
32-
const buildFlagPath = buildFlagArg.substr(buildFlagsPrefix.length);
33-
34-
const flagFile = fs.readFileSync(buildFlagPath, 'utf8');
35-
for (const line of flagFile.split(/(\r\n|\r|\n)/g)) {
36-
const flagMatch = line.match(/#define BUILDFLAG_INTERNAL_(.+?)\(\) \(([01])\)/);
37-
if (flagMatch) {
38-
const [, flagName, flagValue] = flagMatch;
39-
defines[flagName] = JSON.stringify(Boolean(parseInt(flagValue, 10)));
40-
}
41-
}
42-
}
43-
44-
const ignoredModules = [];
45-
46-
if (defines.ENABLE_DESKTOP_CAPTURER === 'false') {
47-
ignoredModules.push(
48-
'@electron/internal/browser/desktop-capturer',
49-
'@electron/internal/browser/api/desktop-capturer',
50-
'@electron/internal/renderer/api/desktop-capturer'
51-
);
52-
}
53-
54-
if (defines.ENABLE_REMOTE_MODULE === 'false') {
55-
ignoredModules.push(
56-
'@electron/internal/browser/remote/server',
57-
'@electron/internal/renderer/api/remote'
58-
);
59-
}
60-
61-
if (defines.ENABLE_VIEWS_API === 'false') {
62-
ignoredModules.push(
63-
'@electron/internal/browser/api/views/image-view.js'
64-
);
65-
}
66-
6720
module.exports = ({
6821
alwaysHasNode,
6922
loadElectronFromAlternateTarget,
@@ -79,76 +32,148 @@ module.exports = ({
7932

8033
const electronAPIFile = path.resolve(electronRoot, 'lib', loadElectronFromAlternateTarget || target, 'api', 'exports', 'electron.ts');
8134

82-
return ({
83-
mode: 'development',
84-
devtool: false,
85-
entry,
86-
target: alwaysHasNode ? 'node' : 'web',
87-
output: {
88-
filename: `${target}.bundle.js`
89-
},
90-
wrapInitWithProfilingTimeout,
91-
wrapInitWithTryCatch,
92-
resolve: {
93-
alias: {
94-
'@electron/internal': path.resolve(electronRoot, 'lib'),
95-
electron$: electronAPIFile,
96-
'electron/main$': electronAPIFile,
97-
'electron/renderer$': electronAPIFile,
98-
'electron/common$': electronAPIFile,
99-
// Force timers to resolve to our dependency that doesn't use window.postMessage
100-
timers: path.resolve(electronRoot, 'node_modules', 'timers-browserify', 'main.js')
101-
},
102-
extensions: ['.ts', '.js']
103-
},
104-
module: {
105-
rules: [{
106-
test: (moduleName) => !onlyPrintingGraph && ignoredModules.includes(moduleName),
107-
loader: 'null-loader'
108-
}, {
109-
test: /\.ts$/,
110-
loader: 'ts-loader',
111-
options: {
112-
configFile: path.resolve(electronRoot, 'tsconfig.electron.json'),
113-
transpileOnly: onlyPrintingGraph,
114-
ignoreDiagnostics: [
115-
// File '{0}' is not under 'rootDir' '{1}'.
116-
6059
117-
]
35+
return (env = {}, argv = {}) => {
36+
const onlyPrintingGraph = !!env.PRINT_WEBPACK_GRAPH;
37+
const outputFilename = argv['output-filename'] || `${target}.bundle.js`;
38+
39+
const defines = {
40+
BUILDFLAG: onlyPrintingGraph ? '(a => a)' : ''
41+
};
42+
43+
if (env.buildflags) {
44+
const flagFile = fs.readFileSync(env.buildflags, 'utf8');
45+
for (const line of flagFile.split(/(\r\n|\r|\n)/g)) {
46+
const flagMatch = line.match(/#define BUILDFLAG_INTERNAL_(.+?)\(\) \(([01])\)/);
47+
if (flagMatch) {
48+
const [, flagName, flagValue] = flagMatch;
49+
defines[flagName] = JSON.stringify(Boolean(parseInt(flagValue, 10)));
11850
}
119-
}]
120-
},
121-
node: {
122-
__dirname: false,
123-
__filename: false,
124-
// We provide our own "timers" import above, any usage of setImmediate inside
125-
// one of our renderer bundles should import it from the 'timers' package
126-
setImmediate: false
127-
},
128-
optimization: {
129-
minimize: true,
130-
minimizer: [
131-
new TerserPlugin({
132-
terserOptions: {
133-
keep_classnames: true,
134-
keep_fnames: true
51+
}
52+
}
53+
54+
const ignoredModules = [];
55+
56+
if (defines.ENABLE_DESKTOP_CAPTURER === 'false') {
57+
ignoredModules.push(
58+
'@electron/internal/browser/desktop-capturer',
59+
'@electron/internal/browser/api/desktop-capturer',
60+
'@electron/internal/renderer/api/desktop-capturer'
61+
);
62+
}
63+
64+
if (defines.ENABLE_REMOTE_MODULE === 'false') {
65+
ignoredModules.push(
66+
'@electron/internal/browser/remote/server',
67+
'@electron/internal/renderer/api/remote'
68+
);
69+
}
70+
71+
if (defines.ENABLE_VIEWS_API === 'false') {
72+
ignoredModules.push(
73+
'@electron/internal/browser/api/views/image-view.js'
74+
);
75+
}
76+
77+
const plugins = [];
78+
79+
if (onlyPrintingGraph) {
80+
plugins.push(new AccessDependenciesPlugin());
81+
}
82+
83+
if (targetDeletesNodeGlobals) {
84+
plugins.push(new webpack.ProvidePlugin({
85+
process: ['@electron/internal/common/webpack-provider', 'process'],
86+
global: ['@electron/internal/common/webpack-provider', '_global'],
87+
Buffer: ['@electron/internal/common/webpack-provider', 'Buffer']
88+
}));
89+
}
90+
91+
plugins.push(new webpack.ProvidePlugin({
92+
Promise: ['@electron/internal/common/webpack-globals-provider', 'Promise']
93+
}));
94+
95+
plugins.push(new webpack.DefinePlugin(defines));
96+
97+
if (wrapInitWithProfilingTimeout) {
98+
plugins.push(new WrapperPlugin({
99+
header: 'function ___electron_webpack_init__() {',
100+
footer: `
101+
};
102+
if ((globalThis.process || binding.process).argv.includes("--profile-electron-init")) {
103+
setTimeout(___electron_webpack_init__, 0);
104+
} else {
105+
___electron_webpack_init__();
106+
}`
107+
}));
108+
}
109+
110+
if (wrapInitWithTryCatch) {
111+
plugins.push(new WrapperPlugin({
112+
header: 'try {',
113+
footer: `
114+
} catch (err) {
115+
console.error('Electron ${outputFilename} script failed to run');
116+
console.error(err);
117+
}`
118+
}));
119+
}
120+
121+
return {
122+
mode: 'development',
123+
devtool: false,
124+
entry,
125+
target: alwaysHasNode ? 'node' : 'web',
126+
output: {
127+
filename: outputFilename
128+
},
129+
resolve: {
130+
alias: {
131+
'@electron/internal': path.resolve(electronRoot, 'lib'),
132+
electron$: electronAPIFile,
133+
'electron/main$': electronAPIFile,
134+
'electron/renderer$': electronAPIFile,
135+
'electron/common$': electronAPIFile,
136+
// Force timers to resolve to our dependency that doesn't use window.postMessage
137+
timers: path.resolve(electronRoot, 'node_modules', 'timers-browserify', 'main.js')
138+
},
139+
extensions: ['.ts', '.js']
140+
},
141+
module: {
142+
rules: [{
143+
test: (moduleName) => !onlyPrintingGraph && ignoredModules.includes(moduleName),
144+
loader: 'null-loader'
145+
}, {
146+
test: /\.ts$/,
147+
loader: 'ts-loader',
148+
options: {
149+
configFile: path.resolve(electronRoot, 'tsconfig.electron.json'),
150+
transpileOnly: onlyPrintingGraph,
151+
ignoreDiagnostics: [
152+
// File '{0}' is not under 'rootDir' '{1}'.
153+
6059
154+
]
135155
}
136-
})
137-
]
138-
},
139-
plugins: [
140-
new AccessDependenciesPlugin(),
141-
...(targetDeletesNodeGlobals ? [
142-
new webpack.ProvidePlugin({
143-
process: ['@electron/internal/common/webpack-provider', 'process'],
144-
global: ['@electron/internal/common/webpack-provider', '_global'],
145-
Buffer: ['@electron/internal/common/webpack-provider', 'Buffer']
146-
})
147-
] : []),
148-
new webpack.ProvidePlugin({
149-
Promise: ['@electron/internal/common/webpack-globals-provider', 'Promise']
150-
}),
151-
new webpack.DefinePlugin(defines)
152-
]
153-
});
156+
}]
157+
},
158+
node: {
159+
__dirname: false,
160+
__filename: false,
161+
// We provide our own "timers" import above, any usage of setImmediate inside
162+
// one of our renderer bundles should import it from the 'timers' package
163+
setImmediate: false
164+
},
165+
optimization: {
166+
minimize: true,
167+
minimizer: [
168+
new TerserPlugin({
169+
terserOptions: {
170+
keep_classnames: true,
171+
keep_fnames: true
172+
}
173+
})
174+
]
175+
},
176+
plugins
177+
};
178+
};
154179
};

build/webpack/webpack.gni

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ template("webpack_build") {
1616
inputs = [
1717
invoker.config_file,
1818
"//electron/build/webpack/webpack.config.base.js",
19-
"//electron/build/webpack/run-compiler.js",
2019
"//electron/tsconfig.json",
2120
"//electron/yarn.lock",
2221
"//electron/typings/internal-ambient.d.ts",
2322
"//electron/typings/internal-electron.d.ts",
2423
] + invoker.inputs
2524

2625
args = [
26+
"--config",
2727
rebase_path(invoker.config_file),
28-
rebase_path(invoker.out_file),
29-
"--buildflags=" + rebase_path("$target_gen_dir/buildflags/buildflags.h"),
28+
"--output-filename=" + get_path_info(invoker.out_file, "file"),
29+
"--output-path=" + rebase_path(get_path_info(invoker.out_file, "dir")),
30+
"--env.buildflags=" +
31+
rebase_path("$target_gen_dir/buildflags/buildflags.h"),
3032
]
3133
deps += [ "buildflags" ]
3234

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
"ts-node": "6.2.0",
6464
"typescript": "^4.0.2",
6565
"webpack": "^4.43.0",
66-
"webpack-cli": "^3.3.12"
66+
"webpack-cli": "^3.3.12",
67+
"wrapper-webpack-plugin": "^2.1.0"
6768
},
6869
"private": true,
6970
"scripts": {
@@ -91,7 +92,7 @@
9192
"start": "node ./script/start.js",
9293
"test": "node ./script/spec-runner.js",
9394
"tsc": "tsc",
94-
"webpack": "node build/webpack/run-compiler"
95+
"webpack": "webpack"
9596
},
9697
"license": "MIT",
9798
"author": "Electron Community",
@@ -141,4 +142,4 @@
141142
"@types/temp": "^0.8.34",
142143
"aws-sdk": "^2.727.1"
143144
}
144-
}
145+
}

0 commit comments

Comments
 (0)
X Tutup