Skip to content

Commit 05b066c

Browse files
test: more
1 parent a199bc3 commit 05b066c

8 files changed

Lines changed: 204 additions & 1 deletion

File tree

packages/create-webpack-app/src/generators/init/default.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
190190
),
191191
fileType: file.fileType,
192192
data: answers,
193+
force: answers.force,
193194
});
194195
}
195196

packages/create-webpack-app/src/generators/init/react.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
210210
),
211211
fileType: file.fileType,
212212
data: answers,
213+
force: answers.force,
213214
});
214215
}
215216

packages/create-webpack-app/src/generators/init/svelte.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ export default async function svelteInitGenerator(plop: NodePlopAPI) {
197197
),
198198
fileType: file.fileType,
199199
data: answers,
200+
force: answers.force,
200201
});
201202
}
202203

packages/create-webpack-app/src/generators/init/vue.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ export default async function vueInitGenerator(plop: NodePlopAPI) {
219219
),
220220
fileType: file.fileType,
221221
data: answers,
222+
force: answers.force,
222223
});
223224
}
224225

packages/create-webpack-app/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ program
105105
{
106106
...initValues[templateOption],
107107
projectPath,
108+
force,
108109
},
109110
{
110111
onSuccess: onSuccessHandler,

packages/create-webpack-app/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type ActionType as ActionTypeBase, type CustomActionConfig } from "node-plop";
22

3-
export type Answers = { projectPath?: string } & Record<string, unknown>;
3+
export type Answers = { force?: boolean; projectPath?: string } & Record<string, unknown>;
44
export type LoaderAnswers = { name: string; projectPath: string } & Record<string, unknown>;
55
export type PluginAnswers = { name: string; projectPath: string } & Record<string, unknown>;
66

test/create-webpack-app/init/__snapshots__/init.test.js.snap.webpack5

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,172 @@ const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
363363
const isProduction = process.env.NODE_ENV === 'production';
364364

365365

366+
/** @type {import("webpack").Configuration} */
367+
const config = {
368+
entry: './src/index.js',
369+
output: {
370+
path: path.resolve(__dirname, 'dist'),
371+
},
372+
devServer: {
373+
open: true,
374+
host: 'localhost',
375+
},
376+
plugins: [
377+
new HtmlWebpackPlugin({
378+
template: 'index.html',
379+
}),
380+
381+
// Add your plugins here
382+
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
383+
],
384+
module: {
385+
rules: [
386+
{
387+
test: /\\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
388+
type: 'asset',
389+
},
390+
391+
{
392+
test: /\\.html$/i,
393+
use: ['html-loader'],
394+
},
395+
396+
// Add your rules for custom modules here
397+
// Learn more about loaders from https://webpack.js.org/loaders/
398+
],
399+
},
400+
};
401+
402+
module.exports = () => {
403+
if (isProduction) {
404+
config.mode = 'production';
405+
406+
407+
config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());
408+
409+
} else {
410+
config.mode = 'development';
411+
}
412+
return config;
413+
};
414+
"
415+
`;
416+
417+
exports[`create-webpack-app cli should generate default project when nothing is passed and override when content exist 1`] = `
418+
{
419+
"description": "My webpack project",
420+
"devDependencies": {
421+
"html-loader": "x.x.x",
422+
"html-webpack-plugin": "x.x.x",
423+
"webpack": "x.x.x",
424+
"webpack-cli": "x.x.x",
425+
"webpack-dev-server": "x.x.x",
426+
"workbox-webpack-plugin": "x.x.x",
427+
},
428+
"name": "webpack-project",
429+
"scripts": {
430+
"build": "webpack --mode=production --config-node-env=production",
431+
"build:dev": "webpack --mode=development",
432+
"serve": "webpack serve",
433+
"watch": "webpack --watch",
434+
},
435+
"version": "1.0.0",
436+
}
437+
`;
438+
439+
exports[`create-webpack-app cli should generate default project when nothing is passed and override when content exist 2`] = `
440+
"// Generated using webpack-cli https://github.com/webpack/webpack-cli
441+
442+
const path = require('path');
443+
const HtmlWebpackPlugin = require('html-webpack-plugin');
444+
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
445+
446+
const isProduction = process.env.NODE_ENV === 'production';
447+
448+
449+
/** @type {import("webpack").Configuration} */
450+
const config = {
451+
entry: './src/index.js',
452+
output: {
453+
path: path.resolve(__dirname, 'dist'),
454+
},
455+
devServer: {
456+
open: true,
457+
host: 'localhost',
458+
},
459+
plugins: [
460+
new HtmlWebpackPlugin({
461+
template: 'index.html',
462+
}),
463+
464+
// Add your plugins here
465+
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
466+
],
467+
module: {
468+
rules: [
469+
{
470+
test: /\\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
471+
type: 'asset',
472+
},
473+
474+
{
475+
test: /\\.html$/i,
476+
use: ['html-loader'],
477+
},
478+
479+
// Add your rules for custom modules here
480+
// Learn more about loaders from https://webpack.js.org/loaders/
481+
],
482+
},
483+
};
484+
485+
module.exports = () => {
486+
if (isProduction) {
487+
config.mode = 'production';
488+
489+
490+
config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());
491+
492+
} else {
493+
config.mode = 'development';
494+
}
495+
return config;
496+
};
497+
"
498+
`;
499+
500+
exports[`create-webpack-app cli should generate default project when nothing is passed and override when content exist 3`] = `
501+
{
502+
"description": "My webpack project",
503+
"devDependencies": {
504+
"html-loader": "x.x.x",
505+
"html-webpack-plugin": "x.x.x",
506+
"webpack": "x.x.x",
507+
"webpack-cli": "x.x.x",
508+
"webpack-dev-server": "x.x.x",
509+
"workbox-webpack-plugin": "x.x.x",
510+
},
511+
"name": "webpack-project",
512+
"scripts": {
513+
"build": "webpack --mode=production --config-node-env=production",
514+
"build:dev": "webpack --mode=development",
515+
"serve": "webpack serve",
516+
"watch": "webpack --watch",
517+
},
518+
"version": "1.0.0",
519+
}
520+
`;
521+
522+
exports[`create-webpack-app cli should generate default project when nothing is passed and override when content exist 4`] = `
523+
"// Generated using webpack-cli https://github.com/webpack/webpack-cli
524+
525+
const path = require('path');
526+
const HtmlWebpackPlugin = require('html-webpack-plugin');
527+
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
528+
529+
const isProduction = process.env.NODE_ENV === 'production';
530+
531+
366532
/** @type {import("webpack").Configuration} */
367533
const config = {
368534
entry: './src/index.js',

test/create-webpack-app/init/init.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,38 @@ describe("create-webpack-app cli", () => {
8585
expect(readFromWebpackConfig(dir)).toMatchSnapshot();
8686
});
8787

88+
it("should generate default project when nothing is passed and override when content exist", async () => {
89+
const { stdout } = await run(dir, ["init", "--force"]);
90+
91+
expect(stdout).toContain("Project has been initialised with webpack!");
92+
expect(stdout).toContain("webpack.config.js");
93+
// Test files
94+
for (const file of defaultTemplateFiles) {
95+
expect(existsSync(resolve(dir, file))).toBeTruthy();
96+
}
97+
98+
// Check if the generated package.json file content matches the snapshot
99+
expect(readFromPkgJSON(dir)).toMatchSnapshot();
100+
101+
// Check if the generated webpack configuration matches the snapshot
102+
expect(readFromWebpackConfig(dir)).toMatchSnapshot();
103+
104+
const { stdout: nextStdout } = await run(dir, ["init", "--force"]);
105+
106+
expect(nextStdout).toContain("Project has been initialised with webpack!");
107+
expect(nextStdout).toContain("webpack.config.js");
108+
// Test files
109+
for (const file of defaultTemplateFiles) {
110+
expect(existsSync(resolve(dir, file))).toBeTruthy();
111+
}
112+
113+
// Check if the generated package.json file content matches the snapshot
114+
expect(readFromPkgJSON(dir)).toMatchSnapshot();
115+
116+
// Check if the generated webpack configuration matches the snapshot
117+
expect(readFromWebpackConfig(dir)).toMatchSnapshot();
118+
});
119+
88120
it("should generate project when generationPath is supplied", async () => {
89121
const { stdout } = await run(__dirname, ["init", dir, "--force"]);
90122

0 commit comments

Comments
 (0)