Skip to content
Open
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
11 changes: 8 additions & 3 deletions packages/create-webpack-app/src/generators/init/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
devDependencies.push("babel-loader", "@babel/core", "@babel/preset-env");
break;
case "Typescript":
devDependencies.push("typescript", "ts-loader");
devDependencies.push("typescript", "ts-loader", "ts-node");
break;
}

Expand Down Expand Up @@ -149,7 +149,10 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {

const files: FileRecord[] = [
{ filePath: "./index.html", fileType: "text" },
{ filePath: "webpack.config.js", fileType: "text" },
{
filePath: answers.langType === "Typescript" ? "webpack.config.ts" : "webpack.config.js",
fileType: "text",
},
{ filePath: "package.json", fileType: "text" },
{ filePath: "README.md", fileType: "text" },
];
Expand Down Expand Up @@ -186,7 +189,9 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
templateFile: join(
plop.getPlopfilePath(),
"../templates/init/default",
`${file.filePath}.tpl`,
file.filePath.startsWith("webpack.config")
? "webpack.config.tpl"
: `${file.filePath}.tpl`,
),
fileType: file.fileType,
data: answers,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
<% const nodeOptions = langType === "Typescript" ? "NODE_OPTIONS='--loader ts-node/esm --no-warnings' " : ""; %>{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support loading from typescript out of box using Node.js API (with fallback), so I don't think we need it here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And never use --no-warnings, it is a very bad practice

"version": "1.0.0",
"description": "My webpack project",
"name": "webpack-project",
"type": "module",
"scripts": {
"build": "webpack --mode=production --config-node-env=production",
"build:dev": "webpack --mode=development",
"build": "<%- nodeOptions %>webpack --mode=production --config-node-env=production <%- langType === 'Typescript' ? ' -c ./webpack.config.ts' : '' %>",
"build:dev": "<%- nodeOptions %>webpack --mode=development <%- langType === 'Typescript' ? ' -c ./webpack.config.ts' : '' %>",
<% if (devServer) { %>
"serve": "webpack serve",
"serve": "<%- nodeOptions %>webpack serve <%- langType === 'Typescript' ? ' -c ./webpack.config.ts' : '' %>",
<% } %>
"watch": "webpack --watch"
"watch": "<%- nodeOptions %>webpack --watch <%- langType === 'Typescript' ? ' -c ./webpack.config.ts' : '' %>"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"allowJs": true
"module": "esnext",
"moduleResolution": "bundler",
"target": "esnext",
"allowJs": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"isolatedModules": true,
"rewriteRelativeImportExtensions": true
},
"files": ["src/index.ts"]
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Generated using webpack-cli https://github.com/webpack/webpack-cli

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

Expand All @@ -15,12 +17,10 @@ const stylesHandler = MiniCssExtractPlugin.loader;
<% } else if (extractPlugin === "Only for Production") { %>
const stylesHandler = isProduction ? MiniCssExtractPlugin.loader : "style-loader";
<% } else { %>
const stylesHandler = "style-loader";
<% } %>
<% } %>
const stylesHandler = "style-loader";<% } %><% } %>

/** @type {import("webpack").Configuration} */
const config = {
const config <% if (langType === "Typescript") { %>: Configuration<% } %> = {
entry: "<%= entryPoint %>",
output: {
path: path.resolve(__dirname, "dist"),
Expand Down Expand Up @@ -91,12 +91,8 @@ const config = {
export default () => {
if (isProduction) {
config.mode = "production";
<% if (extractPlugin === "Only for Production") { %>
config.plugins.push(new MiniCssExtractPlugin());
<% } %>
<% if (workboxWebpackPlugin) { %>
config.plugins.push(new WorkboxWebpackPlugin.GenerateSW());
<% } %>
<% if (extractPlugin === "Only for Production") { %>config.plugins!.push(new MiniCssExtractPlugin());<% } %>
<% if (workboxWebpackPlugin) { %>config.plugins!.push(new WorkboxWebpackPlugin.GenerateSW());<% } %>
} else {
config.mode = "development";
}
Expand Down
Loading
Loading