From e87059e2459f6469652c8169b4015eb2599957a1 Mon Sep 17 00:00:00 2001 From: Tomas Gajdos Date: Fri, 23 Nov 2018 15:43:12 +0100 Subject: [PATCH 1/2] fix(plugin-webpack): fix incorrect PRELOAD_WEBPACK_ENTRY. BrowserWindow preload parameter should be an absolute path. Remove prepending 'file://' from prod value of PRELOAD_WEBPACK_ENTRY. Properly escape backslashes in the dev value of PRELOAD_WEBPACK_ENTRY to prevent failure on Windows. --- packages/plugin/webpack/package.json | 9 ++- packages/plugin/webpack/src/WebpackPlugin.ts | 6 +- .../plugin/webpack/test/WebpackPlugin_spec.ts | 55 +++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 packages/plugin/webpack/test/WebpackPlugin_spec.ts diff --git a/packages/plugin/webpack/package.json b/packages/plugin/webpack/package.json index 353c4283da..e1126df2ca 100644 --- a/packages/plugin/webpack/package.json +++ b/packages/plugin/webpack/package.json @@ -8,9 +8,12 @@ "main": "dist/WebpackPlugin.js", "typings": "dist/WebpackPlugin.d.ts", "scripts": { - "test": "exit 0" + "test": "mocha --require ts-node/register test/**/*_spec.ts test/**/**/*_spec.ts --opts ../../../mocha.opts" + }, + "devDependencies": { + "chai": "4.2.0", + "mocha": "^5.0.0" }, - "devDependencies": {}, "engines": { "node": ">= 6.0" }, @@ -30,4 +33,4 @@ "webpack-hot-middleware": "^2.21.0", "webpack-merge": "^4.1.1" } -} \ No newline at end of file +} diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index b3ff92688d..8fd63455ee 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -24,7 +24,7 @@ const BASE_PORT = 3000; export default class WebpackPlugin extends PluginBase { name = 'webpack'; - private isProd = false; + isProd = false; private projectDir!: string; private baseDir!: string; private watchers: webpack.Compiler.Watching[] = []; @@ -164,8 +164,8 @@ Your packaged app may be larger than expected if you dont ignore everything othe if (entryPoint.preload) { defines[`${entryPoint.name.toUpperCase().replace(/ /g, '_')}_PRELOAD_WEBPACK_ENTRY`] = this.isProd - ? `\`file://\$\{require('path').resolve(__dirname, '../renderer', '${entryPoint.name}', 'preload.js')\}\`` - : `'${path.resolve(this.baseDir, 'renderer', entryPoint.name, 'preload.js')}'`; + ? `require('path').resolve(__dirname, '../renderer', '${entryPoint.name}', 'preload.js')` + : `'${path.resolve(this.baseDir, 'renderer', entryPoint.name, 'preload.js').replace(/\\/g, '\\\\')}'`; } } return defines; diff --git a/packages/plugin/webpack/test/WebpackPlugin_spec.ts b/packages/plugin/webpack/test/WebpackPlugin_spec.ts new file mode 100644 index 0000000000..2b27dd01a7 --- /dev/null +++ b/packages/plugin/webpack/test/WebpackPlugin_spec.ts @@ -0,0 +1,55 @@ +import { expect } from 'chai'; + +import WebpackPlugin from '../src/WebpackPlugin'; + +describe('WebpackPlugin', () => { + describe('PRELOAD_WEBPACK_ENTRY', () => { + it('should assign absolute preload script path in development', () => { + const p = new WebpackPlugin({ + mainConfig: {}, + renderer: { + config: {}, + entryPoints: [ + { + js: 'window.js', + name: 'window', + preload: { + js: 'preload.js', + }, + }, + ], + }, + }); + p.init(process.platform === 'win32' ? 'C:\\baseDir' : '/baseDir'); + p.isProd = false; + const defines = p.getDefines(); + + if (process.platform === 'win32') { + expect(defines.WINDOW_PRELOAD_WEBPACK_ENTRY).to.be.eq("'C:\\\\baseDir\\\\.webpack\\\\renderer\\\\window\\\\preload.js'"); + } else { + expect(defines.WINDOW_PRELOAD_WEBPACK_ENTRY).to.be.eq("'/baseDir/.webpack/renderer/window/preload.js'"); + } + }); + it('should assign an expression to resolve the preload script in production', () => { + const p = new WebpackPlugin({ + mainConfig: {}, + renderer: { + config: {}, + entryPoints: [ + { + js: 'window.js', + name: 'window', + preload: { + js: 'preload.js', + }, + }, + ], + }, + }); + p.init(process.platform === 'win32' ? 'C:\\baseDir' : '/baseDir'); + p.isProd = true; + const defines = p.getDefines(); + expect(defines.WINDOW_PRELOAD_WEBPACK_ENTRY).to.be.eq("require('path').resolve(__dirname, '../renderer', 'window', 'preload.js')"); + }); + }); +}); From b0d2e860b38a3c699d3fd6eec46fedbd55c5a518 Mon Sep 17 00:00:00 2001 From: tgds Date: Mon, 26 Nov 2018 17:59:03 +0100 Subject: [PATCH 2/2] Use String.raw, keep isProd private --- packages/plugin/webpack/src/WebpackPlugin.ts | 2 +- packages/plugin/webpack/test/WebpackPlugin_spec.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index 8fd63455ee..fe429d9ba0 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -24,7 +24,7 @@ const BASE_PORT = 3000; export default class WebpackPlugin extends PluginBase { name = 'webpack'; - isProd = false; + private isProd = false; private projectDir!: string; private baseDir!: string; private watchers: webpack.Compiler.Watching[] = []; diff --git a/packages/plugin/webpack/test/WebpackPlugin_spec.ts b/packages/plugin/webpack/test/WebpackPlugin_spec.ts index 2b27dd01a7..43921be291 100644 --- a/packages/plugin/webpack/test/WebpackPlugin_spec.ts +++ b/packages/plugin/webpack/test/WebpackPlugin_spec.ts @@ -21,15 +21,15 @@ describe('WebpackPlugin', () => { }, }); p.init(process.platform === 'win32' ? 'C:\\baseDir' : '/baseDir'); - p.isProd = false; const defines = p.getDefines(); if (process.platform === 'win32') { - expect(defines.WINDOW_PRELOAD_WEBPACK_ENTRY).to.be.eq("'C:\\\\baseDir\\\\.webpack\\\\renderer\\\\window\\\\preload.js'"); + expect(defines.WINDOW_PRELOAD_WEBPACK_ENTRY).to.be.eq(String.raw`'C:\\baseDir\\.webpack\\renderer\\window\\preload.js'`); } else { expect(defines.WINDOW_PRELOAD_WEBPACK_ENTRY).to.be.eq("'/baseDir/.webpack/renderer/window/preload.js'"); } }); + it('should assign an expression to resolve the preload script in production', () => { const p = new WebpackPlugin({ mainConfig: {}, @@ -47,7 +47,7 @@ describe('WebpackPlugin', () => { }, }); p.init(process.platform === 'win32' ? 'C:\\baseDir' : '/baseDir'); - p.isProd = true; + (p as any).isProd = true; const defines = p.getDefines(); expect(defines.WINDOW_PRELOAD_WEBPACK_ENTRY).to.be.eq("require('path').resolve(__dirname, '../renderer', 'window', 'preload.js')"); });