diff --git a/packages/api/core/test/slow/api_spec_slow.ts b/packages/api/core/test/slow/api_spec_slow.ts index f7d30cfa0a..79dc16e594 100644 --- a/packages/api/core/test/slow/api_spec_slow.ts +++ b/packages/api/core/test/slow/api_spec_slow.ts @@ -40,8 +40,12 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => { }); }; - const expectProjectPathExists = async (subPath: string, pathType: string) => { - expect(await fs.pathExists(path.resolve(dir, subPath)), `the ${subPath} ${pathType} should exist`).to.equal(true); + const expectProjectPathExists = async (subPath: string, pathType: string, exists = true) => { + expect(await fs.pathExists(path.resolve(dir, subPath)), `the ${subPath} ${pathType} should exist`).to.equal(exists); + }; + + const expectProjectPathNotExists = async (subPath: string, pathType: string) => { + expectProjectPathExists(subPath, pathType, false); }; describe('init', () => { @@ -125,6 +129,28 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => { expect(Object.keys(require(path.resolve(dir, 'package.json')).devDependencies)).to.contain('@electron-forge/plugin-webpack'); }); + it('should copy the appropriate template files', async () => { + const expectedFiles = [ + 'webpack.main.config.js', + 'webpack.renderer.config.js', + 'webpack.rules.js', + path.join('src', 'renderer.js'), + ]; + for (const filename of expectedFiles) { + await expectProjectPathExists(filename, 'file'); + } + }); + + it('should move and rewrite the main process file', async () => { + await expectProjectPathNotExists(path.join('src', 'index.js'), 'file'); + await expectProjectPathExists(path.join('src', 'main.js'), 'file'); + expect((await fs.readFile(path.join(dir, 'src', 'main.js'))).toString()).to.match(/MAIN_WINDOW_WEBPACK_ENTRY/); + }); + + it('should remove the stylesheet link from the HTML file', async () => { + expect((await fs.readFile(path.join(dir, 'src', 'index.html'))).toString()).to.not.match(/link rel="stylesheet"/); + }); + after(async () => { await fs.remove(dir); }); diff --git a/packages/template/webpack/src/WebpackTemplate.ts b/packages/template/webpack/src/WebpackTemplate.ts index 99de31763f..d71398ab07 100644 --- a/packages/template/webpack/src/WebpackTemplate.ts +++ b/packages/template/webpack/src/WebpackTemplate.ts @@ -6,6 +6,23 @@ import path from 'path'; const currentVersion = require('../package').version; +const copyTemplateFile = async (destDir: string, basename: string) => { + const templateDir = path.resolve(__dirname, '..', 'tmpl'); + await fs.copy(path.join(templateDir, basename), path.resolve(destDir, basename)); +}; + +const updateFileByLine = async ( + inputPath: string, + lineHandler: (line: string) => string, + outputPath: string | undefined = undefined, +) => { + const fileContents = (await fs.readFile(inputPath, 'utf8')).split('\n').map(lineHandler).join('\n'); + await fs.writeFile(outputPath || inputPath, fileContents); + if (outputPath !== undefined) { + await fs.remove(inputPath); + } +}; + class WebpackTemplate implements ForgeTemplate { public devDependencies = [ `@electron-forge/plugin-webpack@${currentVersion}`, @@ -41,18 +58,20 @@ class WebpackTemplate implements ForgeTemplate { }); }); await asyncOra('Setting up webpack configuration', async () => { - await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'webpack.main.config.js'), path.resolve(directory, 'webpack.main.config.js')); - await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'webpack.renderer.config.js'), path.resolve(directory, 'webpack.renderer.config.js')); - await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'webpack.rules.js'), path.resolve(directory, 'webpack.rules.js')); - await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'renderer.js'), path.resolve(directory, 'src', 'renderer.js')); - let indexContents = await fs.readFile(path.resolve(directory, 'src', 'index.js'), 'utf8'); - indexContents = indexContents.split('\n').map((line) => { + await copyTemplateFile(directory, 'webpack.main.config.js'); + await copyTemplateFile(directory, 'webpack.renderer.config.js'); + await copyTemplateFile(directory, 'webpack.rules.js'); + await copyTemplateFile(path.join(directory, 'src'), 'renderer.js'); + + await updateFileByLine(path.resolve(directory, 'src', 'index.js'), (line) => { if (line.includes('mainWindow.loadURL')) return ' mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);'; + return line; + }, path.resolve(directory, 'src', 'main.js')); + + await updateFileByLine(path.resolve(directory, 'src', 'index.html'), (line) => { if (line.includes('link rel="stylesheet"')) return ''; return line; - }).join('\n'); - await fs.writeFile(path.resolve(directory, 'src', 'main.js'), indexContents); - await fs.remove(path.resolve(directory, 'src', 'index.js')); + }); }); } }