Skip to content

Commit

Permalink
fix(plugin-webpack): fix incorrect PRELOAD_WEBPACK_ENTRY. (#635)
Browse files Browse the repository at this point in the history
* 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.

* Use String.raw, keep isProd private
  • Loading branch information
tgds authored and MarshallOfSound committed Dec 13, 2018
1 parent 91c3586 commit 6eae1b5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/plugin/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
"license": "MIT",
"main": "dist/WebpackPlugin.js",
"typings": "dist/WebpackPlugin.d.ts",
"devDependencies": {},
"scripts": {
"test": "mocha --require ts-node/register test/**/*_spec.ts test/**/**/*_spec.ts --opts ../../../mocha.opts"
},
"devDependencies": {
"chai": "4.2.0",
"mocha": "^5.0.0"
},
"engines": {
"node": ">= 6.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/webpack/src/WebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 55 additions & 0 deletions packages/plugin/webpack/test/WebpackPlugin_spec.ts
Original file line number Diff line number Diff line change
@@ -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');
const defines = p.getDefines();

if (process.platform === 'win32') {
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: {},
renderer: {
config: {},
entryPoints: [
{
js: 'window.js',
name: 'window',
preload: {
js: 'preload.js',
},
},
],
},
});
p.init(process.platform === 'win32' ? 'C:\\baseDir' : '/baseDir');
(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')");
});
});
});

0 comments on commit 6eae1b5

Please sign in to comment.