Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack template fixes #1031

Merged
merged 4 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/api/core/test/slow/api_spec_slow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
Expand Down
37 changes: 28 additions & 9 deletions packages/template/webpack/src/WebpackTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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'));
});
});
}
}
Expand Down