@@ -2,16 +2,12 @@ const fs = require('fs');
22const path = require ( 'path' ) ;
33const webpack = require ( 'webpack' ) ;
44const TerserPlugin = require ( 'terser-webpack-plugin' ) ;
5+ const WrapperPlugin = require ( 'wrapper-webpack-plugin' ) ;
56
67const electronRoot = path . resolve ( __dirname , '../..' ) ;
78
8- const onlyPrintingGraph = ! ! process . env . PRINT_WEBPACK_GRAPH ;
9-
109class 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 ( / # d e f i n e B U I L D F L A G _ I N T E R N A L _ ( .+ ?) \( \) \( ( [ 0 1 ] ) \) / ) ;
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-
6720module . 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 : / \. t s $ / ,
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 ( / # d e f i n e B U I L D F L A G _ I N T E R N A L _ ( .+ ?) \( \) \( ( [ 0 1 ] ) \) / ) ;
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 : / \. t s $ / ,
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} ;
0 commit comments