Skip to content

Commit 11be634

Browse files
feat(create-webpack-app): stop asking about HTML and CSS (#4836)
* feat(create-webpack-app): stop asking about HTML and CSS webpack handles both out of the box, so the templates no longer ask whether to simplify HTML creation, which CSS solution to use, whether PostCSS is wanted or whether CSS should be extracted. `index.html` is always the entry point, a stylesheet is always scaffolded, and one question remains for the preprocessors webpack does not cover, wired through a `type: "css/auto"` rule that adds only sass-loader/less-loader/stylus-loader. Also fixes what the generated projects needed to build and run: `baseUrl` for the Vue TypeScript `paths`, a fully specified `./router/index.js` import under `"type": "module"`, Svelte 5's `mount()` instead of `new App()`, and style-module declarations for the TypeScript templates. * feat(create-webpack-app): keep PostCSS, on its own or over a preprocessor The single CSS question now lists every tool webpack does not cover itself: PostCSS, Sass, Less, Stylus, and each preprocessor combined with PostCSS. A preprocessor rule then reads `use: ["postcss-loader", "sass-loader"]`, which webpack applies right to left, so the stylesheet is compiled first and PostCSS runs over the result. PostCSS also scaffolds `postcss.config.js` — in every template now, not just the default one — and asks for `experiments.css`, since its `.css` rule turns the automatic detection off. Covers the answer handling with a test that builds each generator's action list without running it, so all four templates and all eight answers are exercised in a second instead of an install apiece. * feat(create-webpack-app): scaffold TypeScript on webpack's own support The default template no longer installs ts-loader or registers a rule for it: webpack strips the types itself. `typescript` stays for the editor and for the `check:types` script (`tsc --noEmit`, since the build type checks nothing), `tsconfig.json` gains `noEmit`, and `package.json` records the Node.js floor type stripping needs. This also gets the template off ts-loader 9, which crashes on TypeScript 7. The React, Vue and Svelte templates keep ts-loader: stripping handles neither `.tsx` nor the single-file components those loaders produce. * feat(create-webpack-app): ask how TypeScript should be compiled Type stripping is erasable syntax only and checks nothing, so a project that wants type checking, decorators, enums or `.tsx` still needs a loader. The default template asks which to use and defaults to webpack's built-in support; `ts-loader` restores the rule and the dependency. Every template that installs ts-loader now pins `typescript@5` with it: its latest release declares `typescript: "*"` but throws `Cannot read properties of undefined (reading 'fileExists')` on TypeScript 7. `tsconfig.json` also loses `noEmit`, which stopped ts-loader from emitting anything; `check:types` passes the flag itself. * feat(create-webpack-app): say what each TypeScript compiler choice means "built-in" and "ts-loader" named the mechanism but not the trade-off, so the prompt now spells it out: webpack strips types with no type checking and needs Node.js >= 22.6, while ts-loader type checks as it builds and handles `.tsx`, enums and decorators. The stored answers are unchanged. * feat(create-webpack-app): pin typescript@6 with ts-loader, not typescript@5 6.0.3 is the last release of the JavaScript line, and ts-loader 9 builds and type checks against it; only TypeScript 7, the native port, breaks it. Pinning the newest version that works keeps the scaffold current. * fix(create-webpack-app): drop the Vue baseUrl that TypeScript 6 rejects `baseUrl` is deprecated in TypeScript 6, so every module in the Vue TypeScript project failed with TS5101 once the pin moved to 6. A `paths` mapping needs no `baseUrl` when it is relative, which is also what fixed the TS5090 the option was added for.
1 parent a2418aa commit 11be634

39 files changed

Lines changed: 830 additions & 496 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-webpack-app": minor
3+
---
4+
5+
feat: stop asking about HTML and CSS in `init`, scaffold TypeScript on webpack's own support, and keep one question for the CSS tools webpack does not cover (PostCSS, Sass, Less, Stylus, and each preprocessor with PostCSS)

‎.cspell.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"splitted",
8888
"Statsv",
8989
"styl",
90+
"stylesheet",
9091
"Tanjiro",
9192
"tapable",
9293
"taskkill",

‎packages/create-webpack-app/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ Available templates:
6565
- [`vue`](https://vuejs.org/)
6666
- [`svelte`](https://svelte.dev/)
6767

68+
Every template scaffolds `index.html` as the entry point of the project and relies on
69+
webpack's built-in HTML and CSS support (webpack >= 5.109), so the generated projects
70+
carry no `html-webpack-plugin`, `css-loader`, `style-loader` or `mini-css-extract-plugin`.
71+
Picking Sass, Less, Stylus or PostCSS adds only that tool's loader on top.
72+
6873
Available templates for `loader` and `plugin` generators:
6974

7075
- `default` (used by default when nothing specified) - generate bootstrap code

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

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { fileURLToPath } from "node:url";
33
import { type DynamicActionsFunction, type NodePlopAPI } from "node-plop";
44
import { type ActionType, type Answers, type FileRecord } from "../../types.js";
55

6+
const STYLE_EXTENSIONS: Record<string, string> = {
7+
SASS: "scss",
8+
LESS: "less",
9+
Stylus: "styl",
10+
};
11+
612
export default async function defaultInitGenerator(plop: NodePlopAPI) {
713
const __dirname = dirname(fileURLToPath(import.meta.url));
814

@@ -28,15 +34,28 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
2834
default: "none",
2935
},
3036
{
31-
type: "confirm",
32-
name: "devServer",
33-
message: "Would you like to use Webpack Dev server?",
34-
default: true,
37+
type: "list",
38+
name: "tsCompiler",
39+
message: "How should TypeScript be compiled?",
40+
// The labels spell out the trade-off; a label must not end with `)`,
41+
// which is what the prompt tests watch for to send the next answer
42+
choices: [
43+
{
44+
name: "webpack itself: strips types, no type checking, needs Node.js >= 22.6",
45+
value: "built-in",
46+
},
47+
{
48+
name: "ts-loader: type checks while it builds, and handles .tsx, enums and decorators",
49+
value: "ts-loader",
50+
},
51+
],
52+
default: "built-in",
53+
when: (answers: Answers) => answers.langType === "Typescript",
3554
},
3655
{
3756
type: "confirm",
38-
name: "html",
39-
message: "Do you want to simplify the creation of HTML files for your bundle?",
57+
name: "devServer",
58+
message: "Would you like to use Webpack Dev server?",
4059
default: true,
4160
},
4261
{
@@ -47,33 +66,19 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
4766
},
4867
{
4968
type: "list",
50-
name: "cssType",
51-
message: "Which of the following CSS solution do you want to use?",
52-
choices: ["none", "CSS only", "SASS", "LESS", "Stylus"],
53-
default: "CSS only",
54-
filter: (input: string, answers: Answers) => {
55-
if (input === "none") {
56-
answers.isCSS = false;
57-
answers.isPostCSS = false;
58-
} else if (input === "CSS only") {
59-
answers.isCSS = true;
60-
}
61-
return input;
62-
},
63-
},
64-
{
65-
type: "confirm",
66-
name: "isCSS",
67-
message: (answers: Answers) =>
68-
`Will you be using CSS styles along with ${answers.cssType} in your project?`,
69-
when: (answers: Answers) => answers.cssType !== "CSS only",
70-
default: true,
71-
},
72-
{
73-
type: "confirm",
74-
name: "isPostCSS",
75-
message: "Do you want to use PostCSS in your project?",
76-
default: (answers: Answers) => answers.cssType === "CSS only",
69+
name: "cssTool",
70+
message: "Which CSS tool do you want to use?",
71+
choices: [
72+
"none",
73+
"PostCSS",
74+
"SASS",
75+
"SASS with PostCSS",
76+
"LESS",
77+
"LESS with PostCSS",
78+
"Stylus",
79+
"Stylus with PostCSS",
80+
],
81+
default: "none",
7782
},
7883
{
7984
type: "list",
@@ -92,12 +97,25 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
9297
actions: function actions(answers: Answers) {
9398
const actions: ActionType[] = [];
9499

100+
// the config template reads this, so it has to be set whatever the language is
101+
answers.useTsLoader = false;
102+
95103
switch (answers.langType) {
96104
case "ES6":
97105
devDependencies.push("babel-loader", "@babel/core", "@babel/preset-env");
98106
break;
99107
case "Typescript":
100-
devDependencies.push("typescript", "ts-loader");
108+
// Type stripping covers erasable syntax only, so ts-loader stays on
109+
// offer; either way `typescript` backs the editor and `check:types`
110+
answers.useTsLoader = answers.tsCompiler === "ts-loader";
111+
112+
if (answers.useTsLoader) {
113+
// ts-loader 9 (its latest) throws on TypeScript 7, the native port,
114+
// so pin the last JavaScript-line release it still works with
115+
devDependencies.push("typescript@6", "ts-loader");
116+
} else {
117+
devDependencies.push("typescript");
118+
}
101119
break;
102120
}
103121

@@ -109,22 +127,31 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
109127
devDependencies.push("workbox-webpack-plugin");
110128
}
111129

112-
if (answers.isPostCSS) {
130+
const cssTool = (answers.cssTool as string | undefined) ?? "none";
131+
// PostCSS runs on its own or over a preprocessor, so both are read off the answer
132+
const preprocessor = Object.keys(STYLE_EXTENSIONS).find((name) => cssTool.startsWith(name));
133+
134+
answers.usePostCSS = cssTool.includes("PostCSS");
135+
answers.useSASS = preprocessor === "SASS";
136+
answers.useLESS = preprocessor === "LESS";
137+
answers.useStylus = preprocessor === "Stylus";
138+
// The starter stylesheet is written in the language that was picked
139+
answers.styleExtension = preprocessor ? STYLE_EXTENSIONS[preprocessor] : "css";
140+
141+
if (answers.usePostCSS) {
113142
devDependencies.push("postcss-loader", "postcss", "autoprefixer");
114143
}
115144

116-
if (answers.cssType !== "none") {
117-
switch (answers.cssType) {
118-
case "SASS":
119-
devDependencies.push("sass-loader", "sass");
120-
break;
121-
case "LESS":
122-
devDependencies.push("less-loader", "less");
123-
break;
124-
case "Stylus":
125-
devDependencies.push("stylus-loader", "stylus");
126-
break;
127-
}
145+
if (answers.useSASS) {
146+
devDependencies.push("sass-loader", "sass");
147+
}
148+
149+
if (answers.useLESS) {
150+
devDependencies.push("less-loader", "less");
151+
}
152+
153+
if (answers.useStylus) {
154+
devDependencies.push("stylus-loader", "stylus");
128155
}
129156

130157
const files: FileRecord[] = [
@@ -158,10 +185,15 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
158185
break;
159186
}
160187

161-
if (answers.isPostCSS) {
188+
if (answers.usePostCSS) {
162189
files.push({ filePath: "postcss.config.js", fileType: "text" });
163190
}
164191

192+
files.push({
193+
filePath: `./src/styles.${answers.styleExtension}`,
194+
fileType: "text",
195+
});
196+
165197
for (const file of files) {
166198
actions.push({
167199
type: "generate-files",

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

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { fileURLToPath } from "node:url";
33
import { type DynamicActionsFunction, type NodePlopAPI } from "node-plop";
44
import { type ActionType, type Answers, type FileRecord } from "../../types.js";
55

6+
const STYLE_EXTENSIONS: Record<string, string> = {
7+
SASS: "scss",
8+
LESS: "less",
9+
Stylus: "styl",
10+
};
11+
612
export default async function reactInitGenerator(plop: NodePlopAPI) {
713
const __dirname = dirname(fileURLToPath(import.meta.url));
814

@@ -49,33 +55,19 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
4955
},
5056
{
5157
type: "list",
52-
name: "cssType",
53-
message: "Which of the following CSS solution do you want to use?",
54-
choices: ["none", "CSS only", "SASS", "LESS", "Stylus"],
55-
default: "CSS only",
56-
filter: (input: string, answers: Answers) => {
57-
if (input === "none") {
58-
answers.isCSS = false;
59-
answers.isPostCSS = false;
60-
} else if (input === "CSS only") {
61-
answers.isCSS = true;
62-
}
63-
return input;
64-
},
65-
},
66-
{
67-
type: "confirm",
68-
name: "isCSS",
69-
message: (answers: Answers) =>
70-
`Will you be using CSS styles along with ${answers.cssType} in your project?`,
71-
when: (answers: Answers) => answers.cssType !== "CSS only",
72-
default: true,
73-
},
74-
{
75-
type: "confirm",
76-
name: "isPostCSS",
77-
message: "Do you want to use PostCSS in your project?",
78-
default: (answers: Answers) => answers.cssType === "CSS only",
58+
name: "cssTool",
59+
message: "Which CSS tool do you want to use?",
60+
choices: [
61+
"none",
62+
"PostCSS",
63+
"SASS",
64+
"SASS with PostCSS",
65+
"LESS",
66+
"LESS with PostCSS",
67+
"Stylus",
68+
"Stylus with PostCSS",
69+
],
70+
default: "none",
7971
},
8072
{
8173
type: "list",
@@ -94,7 +86,6 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
9486
actions: function actions(answers: Answers) {
9587
// setting some default values based on the answers
9688
const actions: ActionType[] = [];
97-
answers.html = true;
9889
answers.devServer = true;
9990
switch (answers.langType) {
10091
case "ES6":
@@ -106,33 +97,40 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
10697
);
10798
break;
10899
case "Typescript":
109-
devDependencies.push("typescript", "ts-loader", "@types/react", "@types/react-dom");
100+
// ts-loader 9 (its latest) throws on TypeScript 7, the native port, so pin
101+
// the last release of the JavaScript line it still works with
102+
devDependencies.push("typescript@6", "ts-loader", "@types/react", "@types/react-dom");
110103
break;
111104
}
112-
if (answers.isPostCSS) {
105+
if (answers.workboxWebpackPlugin) {
106+
devDependencies.push("workbox-webpack-plugin");
107+
}
108+
109+
const cssTool = (answers.cssTool as string | undefined) ?? "none";
110+
// PostCSS runs on its own or over a preprocessor, so both are read off the answer
111+
const preprocessor = Object.keys(STYLE_EXTENSIONS).find((name) => cssTool.startsWith(name));
112+
113+
answers.usePostCSS = cssTool.includes("PostCSS");
114+
answers.useSASS = preprocessor === "SASS";
115+
answers.useLESS = preprocessor === "LESS";
116+
answers.useStylus = preprocessor === "Stylus";
117+
// The starter stylesheet is written in the language that was picked
118+
answers.styleExtension = preprocessor ? STYLE_EXTENSIONS[preprocessor] : "css";
119+
120+
if (answers.usePostCSS) {
113121
devDependencies.push("postcss-loader", "postcss", "autoprefixer");
114122
}
115-
if (answers.cssType === "none") {
116-
answers.isCSS = false;
117-
answers.isPostCSS = false;
118-
} else {
119-
switch (answers.cssType) {
120-
case "CSS only":
121-
answers.isCSS = true;
122-
break;
123-
case "SASS":
124-
devDependencies.push("sass-loader", "sass");
125-
break;
126-
case "LESS":
127-
devDependencies.push("less-loader", "less");
128-
break;
129-
case "Stylus":
130-
devDependencies.push("stylus-loader", "stylus");
131-
break;
132-
}
123+
124+
if (answers.useSASS) {
125+
devDependencies.push("sass-loader", "sass");
133126
}
134-
if (answers.workboxWebpackPlugin) {
135-
devDependencies.push("workbox-webpack-plugin");
127+
128+
if (answers.useLESS) {
129+
devDependencies.push("less-loader", "less");
130+
}
131+
132+
if (answers.useStylus) {
133+
devDependencies.push("stylus-loader", "stylus");
136134
}
137135

138136
const files: FileRecord[] = [
@@ -173,21 +171,15 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
173171
break;
174172
}
175173

176-
switch (answers.cssType) {
177-
case "CSS only":
178-
files.push({ filePath: "./src/styles/global.css", fileType: "text" });
179-
break;
180-
case "SASS":
181-
files.push({ filePath: "./src/styles/global.scss", fileType: "text" });
182-
break;
183-
case "LESS":
184-
files.push({ filePath: "./src/styles/global.less", fileType: "text" });
185-
break;
186-
case "Stylus":
187-
files.push({ filePath: "./src/styles/global.styl", fileType: "text" });
188-
break;
174+
if (answers.usePostCSS) {
175+
files.push({ filePath: "postcss.config.js", fileType: "text" });
189176
}
190177

178+
files.push({
179+
filePath: `./src/styles/global.${answers.styleExtension}`,
180+
fileType: "text",
181+
});
182+
191183
for (const file of files) {
192184
actions.push({
193185
type: "generate-files",

0 commit comments

Comments
 (0)