Skip to content

Commit c13517d

Browse files
fix(init): generate webpack.config.ts when TypeScript is selected
- Replace `webpack.config.js.tpl` with a unified `webpack.config.tpl` based on `langType` - Add conditional `import "webpack-dev-server"` when devServer is selected - Add `ts-node` to devDependencies when TypeScript is selected - Update tests to assert `webpack.config.ts` is generated for TypeScript projects
1 parent 1a0f0b4 commit c13517d

File tree

6 files changed

+138
-129
lines changed

6 files changed

+138
-129
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
189189
templateFile: join(
190190
plop.getPlopfilePath(),
191191
"../templates/init/default",
192-
`${file.filePath}.tpl`,
192+
file.filePath.startsWith("webpack.config")
193+
? "webpack.config.tpl"
194+
: `${file.filePath}.tpl`,
193195
),
194196
fileType: file.fileType,
195197
data: answers,
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
{
1+
<% const nodeOptions = langType === "Typescript" ? "NODE_OPTIONS='--loader ts-node/esm --no-warnings' " : ""; %>{
22
"version": "1.0.0",
33
"description": "My webpack project",
44
"name": "webpack-project",
55
"type": "module",
66
"scripts": {
7-
"build": "webpack --mode=production --config-node-env=production",
8-
"build:dev": "webpack --mode=development",
7+
"build": "<%- nodeOptions %>webpack --mode=production --config-node-env=production <%- langType === 'Typescript' ? ' -c ./webpack.config.ts' : '' %>",
8+
"build:dev": "<%- nodeOptions %>webpack --mode=development <%- langType === 'Typescript' ? ' -c ./webpack.config.ts' : '' %>",
99
<% if (devServer) { %>
10-
"serve": "webpack serve",
10+
"serve": "<%- nodeOptions %>webpack serve <%- langType === 'Typescript' ? ' -c ./webpack.config.ts' : '' %>",
1111
<% } %>
12-
"watch": "webpack --watch"
12+
"watch": "<%- nodeOptions %>webpack --watch <%- langType === 'Typescript' ? ' -c ./webpack.config.ts' : '' %>"
1313
}
1414
}

packages/create-webpack-app/templates/init/default/tsconfig.json.tpl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
"compilerOptions": {
33
"allowSyntheticDefaultImports": true,
44
"noImplicitAny": true,
5-
"module": "es6",
6-
"target": "es5",
7-
"allowJs": true
5+
"module": "esnext",
6+
"moduleResolution": "bundler",
7+
"target": "esnext",
8+
"allowJs": true,
9+
"esModuleInterop": true,
10+
"resolveJsonModule": true,
11+
"isolatedModules": true,
12+
"rewriteRelativeImportExtensions": true
813
},
9-
"files": ["src/index.ts"]
14+
"include": ["src/**/*"],
15+
"exclude": ["node_modules"]
1016
}

packages/create-webpack-app/templates/init/default/webpack.config.js.tpl renamed to packages/create-webpack-app/templates/init/default/webpack.config.tpl

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Generated using webpack-cli https://github.com/webpack/webpack-cli
22

33
import path from "node:path";
4-
import { fileURLToPath } from "node:url";<% if (htmlWebpackPlugin) { %>
5-
import HtmlWebpackPlugin from "html-webpack-plugin";<% } %><% if (extractPlugin !== "No") { %>
4+
import { fileURLToPath } from "node:url";
5+
<% if (langType === "Typescript") { %>import { Configuration } from "webpack";
6+
<% if (devServer) { %>import "webpack-dev-server";<% } %><% } %>
7+
<% if (htmlWebpackPlugin) { %>import HtmlWebpackPlugin from "html-webpack-plugin";<% } %><% if (extractPlugin !== "No") { %>
68
import MiniCssExtractPlugin from "mini-css-extract-plugin";<% } %><% if (workboxWebpackPlugin) { %>
79
import WorkboxWebpackPlugin from "workbox-webpack-plugin";<% } %>
810

@@ -15,12 +17,10 @@ const stylesHandler = MiniCssExtractPlugin.loader;
1517
<% } else if (extractPlugin === "Only for Production") { %>
1618
const stylesHandler = isProduction ? MiniCssExtractPlugin.loader : "style-loader";
1719
<% } else { %>
18-
const stylesHandler = "style-loader";
19-
<% } %>
20-
<% } %>
20+
const stylesHandler = "style-loader";<% } %><% } %>
2121

2222
/** @type {import("webpack").Configuration} */
23-
const config = {
23+
const config <% if (langType === "Typescript") { %>: Configuration<% } %> = {
2424
entry: "<%= entryPoint %>",
2525
output: {
2626
path: path.resolve(__dirname, "dist"),
@@ -91,12 +91,8 @@ const config = {
9191
export default () => {
9292
if (isProduction) {
9393
config.mode = "production";
94-
<% if (extractPlugin === "Only for Production") { %>
95-
config.plugins.push(new MiniCssExtractPlugin());
96-
<% } %>
97-
<% if (workboxWebpackPlugin) { %>
98-
config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());
99-
<% } %>
94+
<% if (extractPlugin === "Only for Production") { %>config.plugins!.push(new MiniCssExtractPlugin());<% } %>
95+
<% if (workboxWebpackPlugin) { %>config.plugins!.push(new WorkboxWebpackPlugin.GenerateSW());<% } %>
10096
} else {
10197
config.mode = "development";
10298
}

0 commit comments

Comments
 (0)