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
5 changes: 5 additions & 0 deletions .changeset/native-css-templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-webpack-app": minor
---

feat: use webpack's native CSS and HTML support in the `init` templates
22 changes: 1 addition & 21 deletions packages/create-webpack-app/src/generators/init/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
},
{
type: "confirm",
name: "htmlWebpackPlugin",
name: "html",
message: "Do you want to simplify the creation of HTML files for your bundle?",
default: true,
},
Expand All @@ -55,7 +55,6 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
if (input === "none") {
answers.isCSS = false;
answers.isPostCSS = false;
answers.extractPlugin = "No";
} else if (input === "CSS only") {
answers.isCSS = true;
}
Expand All @@ -76,13 +75,6 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
message: "Do you want to use PostCSS in your project?",
default: (answers: Answers) => answers.cssType === "CSS only",
},
{
type: "list",
name: "extractPlugin",
message: "Do you want to extract CSS into separate files?",
choices: ["No", "Only for Production", "Yes"],
default: "Only for Production",
},
{
type: "list",
name: "packageManager",
Expand Down Expand Up @@ -113,10 +105,6 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
devDependencies.push("webpack-dev-server");
}

if (answers.htmlWebpackPlugin) {
devDependencies.push("html-webpack-plugin", "html-loader");
}

if (answers.workboxWebpackPlugin) {
devDependencies.push("workbox-webpack-plugin");
}
Expand All @@ -125,12 +113,7 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
devDependencies.push("postcss-loader", "postcss", "autoprefixer");
}

if (answers.extractPlugin !== "No") {
devDependencies.push("mini-css-extract-plugin");
}

if (answers.cssType !== "none") {
devDependencies.push("style-loader", "css-loader");
switch (answers.cssType) {
case "SASS":
devDependencies.push("sass-loader", "sass");
Expand All @@ -143,9 +126,6 @@ export default async function defaultInitGenerator(plop: NodePlopAPI) {
break;
}
}
if (answers.extractPlugin !== "No") {
devDependencies.push("mini-css-extract-plugin");
}

const files: FileRecord[] = [
{ filePath: "./index.html", fileType: "text" },
Expand Down
16 changes: 1 addition & 15 deletions packages/create-webpack-app/src/generators/init/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
"react@18",
"react-dom@18",
"webpack-dev-server",
"html-webpack-plugin",
"react-router-dom",
"@types/react-router-dom",
];
Expand Down Expand Up @@ -58,7 +57,6 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
if (input === "none") {
answers.isCSS = false;
answers.isPostCSS = false;
answers.extractPlugin = "No";
} else if (input === "CSS only") {
answers.isCSS = true;
}
Expand All @@ -79,13 +77,6 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
message: "Do you want to use PostCSS in your project?",
default: (answers: Answers) => answers.cssType === "CSS only",
},
{
type: "list",
name: "extractPlugin",
message: "Do you want to extract CSS into separate files?",
choices: ["No", "Only for Production", "Yes"],
default: "Only for Production",
},
{
type: "list",
name: "packageManager",
Expand All @@ -103,7 +94,7 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
actions: function actions(answers: Answers) {
// setting some default values based on the answers
const actions: ActionType[] = [];
answers.htmlWebpackPlugin = true;
answers.html = true;
answers.devServer = true;
switch (answers.langType) {
case "ES6":
Expand All @@ -124,9 +115,7 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
if (answers.cssType === "none") {
answers.isCSS = false;
answers.isPostCSS = false;
answers.extractPlugin = "No";
} else {
devDependencies.push("style-loader", "css-loader");
switch (answers.cssType) {
case "CSS only":
answers.isCSS = true;
Expand All @@ -142,9 +131,6 @@ export default async function reactInitGenerator(plop: NodePlopAPI) {
break;
}
}
if (answers.extractPlugin !== "No") {
devDependencies.push("mini-css-extract-plugin");
}
if (answers.workboxWebpackPlugin) {
devDependencies.push("workbox-webpack-plugin");
}
Expand Down
17 changes: 1 addition & 16 deletions packages/create-webpack-app/src/generators/init/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default async function svelteInitGenerator(plop: NodePlopAPI) {
"svelte",
"svelte-loader",
"webpack-dev-server",
"html-webpack-plugin",
];

await plop.load("../../utils/install-dependencies.js", {}, true);
Expand Down Expand Up @@ -49,7 +48,6 @@ export default async function svelteInitGenerator(plop: NodePlopAPI) {
if (input === "none") {
answers.isCSS = false;
answers.isPostCSS = false;
answers.extractPlugin = "No";
} else if (input === "CSS only") {
answers.isCSS = true;
}
Expand All @@ -70,13 +68,6 @@ export default async function svelteInitGenerator(plop: NodePlopAPI) {
message: "Do you want to use PostCSS in your project?",
default: (answers: Answers) => answers.cssType === "CSS only",
},
{
type: "list",
name: "extractPlugin",
message: "Do you want to extract CSS into separate files?",
choices: ["No", "Only for Production", "Yes"],
default: "Only for Production",
},
{
type: "list",
name: "packageManager",
Expand All @@ -94,7 +85,7 @@ export default async function svelteInitGenerator(plop: NodePlopAPI) {
actions: function actions(answers: Answers) {
// setting some default values based on the answers
const actions: ActionType[] = [];
answers.htmlWebpackPlugin = true;
answers.html = true;
answers.devServer = true;

switch (answers.langType) {
Expand All @@ -117,9 +108,7 @@ export default async function svelteInitGenerator(plop: NodePlopAPI) {
if (answers.cssType === "none") {
answers.isCSS = false;
answers.isPostCSS = false;
answers.extractPlugin = "No";
} else {
devDependencies.push("style-loader", "css-loader");
switch (answers.cssType) {
case "CSS only":
answers.isCSS = true;
Expand All @@ -136,10 +125,6 @@ export default async function svelteInitGenerator(plop: NodePlopAPI) {
}
}

if (answers.extractPlugin !== "No") {
devDependencies.push("mini-css-extract-plugin");
}

const files: FileRecord[] = [
{ filePath: "./index.html", fileType: "text" },
{ filePath: "./src/assets/webpack.png", fileType: "binary" },
Expand Down
17 changes: 1 addition & 16 deletions packages/create-webpack-app/src/generators/init/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default async function vueInitGenerator(plop: NodePlopAPI) {
"webpack-cli",
"vue@3",
"webpack-dev-server",
"html-webpack-plugin",
"vue-loader@next",
"@vue/compiler-sfc",
"vue-router@4",
Expand Down Expand Up @@ -57,7 +56,6 @@ export default async function vueInitGenerator(plop: NodePlopAPI) {
if (input === "none") {
answers.isCSS = false;
answers.isPostCSS = false;
answers.extractPlugin = "No";
} else if (input === "CSS only") {
answers.isCSS = true;
}
Expand All @@ -78,13 +76,6 @@ export default async function vueInitGenerator(plop: NodePlopAPI) {
message: "Do you want to use PostCSS in your project?",
default: (answers: Answers) => answers.cssType === "CSS only",
},
{
type: "list",
name: "extractPlugin",
message: "Do you want to extract CSS into separate files?",
choices: ["No", "Only for Production", "Yes"],
default: "Only for Production",
},
{
type: "list",
name: "packageManager",
Expand All @@ -102,7 +93,7 @@ export default async function vueInitGenerator(plop: NodePlopAPI) {
actions: function actions(answers: Answers) {
// setting some default values based on the answers
const actions: ActionType[] = [];
answers.htmlWebpackPlugin = true;
answers.html = true;
answers.devServer = true;

switch (answers.langType) {
Expand All @@ -129,9 +120,7 @@ export default async function vueInitGenerator(plop: NodePlopAPI) {
if (answers.cssType === "none") {
answers.isCSS = false;
answers.isPostCSS = false;
answers.extractPlugin = "No";
} else {
devDependencies.push("vue-style-loader", "style-loader", "css-loader");
switch (answers.cssType) {
case "CSS only":
answers.isCSS = true;
Expand All @@ -148,10 +137,6 @@ export default async function vueInitGenerator(plop: NodePlopAPI) {
}
}

if (answers.extractPlugin !== "No") {
devDependencies.push("mini-css-extract-plugin");
}

const files: FileRecord[] = [
{ filePath: "./index.html", fileType: "text" },
{ filePath: "./src/assets/webpack.png", fileType: "binary" },
Expand Down
3 changes: 1 addition & 2 deletions packages/create-webpack-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ const baseAnswers: Answers = {
projectPath: process.cwd(),
langType: "none",
devServer: true,
htmlWebpackPlugin: true,
html: true,
workboxWebpackPlugin: true,
cssType: "none",
isCSS: false,
isPostCSS: false,
extractPlugin: "No",
packageManager: "npm",
};
const initValues: Record<string, Answers> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
</head>
<body>
<h1>Hello world!</h1>
<h2>Tip: Check your console</h2>
<h2>Tip: Check your console</h2><% if (html) { %>
<script src="<%= entryPoint %>"></script><% } %>
</body>
<% if (workboxWebpackPlugin) { %>
<script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,26 @@
import path from "node:path";
import { fileURLToPath } from "node:url";<% if (langType === "Typescript") { %>
import { type 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 "webpack-dev-server";<% } %><% } %><% if (workboxWebpackPlugin) { %>
import WorkboxWebpackPlugin from "workbox-webpack-plugin";<% } %>

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const isProduction = process.env.NODE_ENV === "production";<% if (cssType !== "none") { %><% if (extractPlugin === "Yes") { %>
const stylesHandler = MiniCssExtractPlugin.loader;<% } else if (extractPlugin === "Only for Production") { %>
const stylesHandler = isProduction ? MiniCssExtractPlugin.loader : "style-loader";<% } else { %>
const stylesHandler = "style-loader";<% } %><% } %>
const isProduction = process.env.NODE_ENV === "production";

/** @type {import("webpack").Configuration} */
const config <% if (langType === "Typescript") { %>: Configuration <% } %>= {
entry: "<%= entryPoint %>",
entry: <% if (html) { %>"./index.html"<% } else { %>"<%= entryPoint %>"<% } %>,
output: {
path: path.resolve(__dirname, "dist"),
},<% if (devServer) { %>
devServer: {
open: true,
},<% } %><% if (isCSS && isPostCSS) { %>
experiments: {
css: true,
},<% } %>
plugins: [<% if (htmlWebpackPlugin) { %>
new HtmlWebpackPlugin({
template: "index.html",
}),<% } %><% if (extractPlugin === "Yes") { %>
new MiniCssExtractPlugin(),<% } %>
plugins: [
// Add your plugins here
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
],
Expand All @@ -42,36 +36,31 @@ const config <% if (langType === "Typescript") { %>: Configuration <% } %>= {
test: /\.(ts|tsx)$/i,
loader: "ts-loader",
exclude: ["/node_modules/"],
},<% } %><% if (isCSS && !isPostCSS) { %>
},<% } %><% if (isCSS && isPostCSS) { %>
{
test: /\.css$/i,
use: [stylesHandler,"css-loader"],
type: "css/auto",
use: ["postcss-loader"],
},<% } %><% if (cssType == "SASS") { %>
{
test: /\.s[ac]ss$/i,
use: [stylesHandler, "css-loader", <% if (isPostCSS) { %>"postcss-loader", <% } %>"sass-loader"],
type: "css/auto",
use: [<% if (isPostCSS) { %>"postcss-loader", <% } %>"sass-loader"],
},<% } %><% if (cssType == "LESS") { %>
{
test: /\.less$/i,
use: [stylesHandler, "css-loader", <% if (isPostCSS) { %>"postcss-loader", <% } %>"less-loader"],
type: "css/auto",
use: [<% if (isPostCSS) { %>"postcss-loader", <% } %>"less-loader"],
},<% } %><% if (cssType == "Stylus") { %>
{
test: /\.styl$/i,
use: [stylesHandler, "css-loader", <% if (isPostCSS) { %>"postcss-loader", <% } %>"stylus-loader"],
},<% } %><% if (isPostCSS && isCSS) { %>
{
test: /\.css$/i,
use: [stylesHandler, "css-loader", "postcss-loader"],
type: "css/auto",
use: [<% if (isPostCSS) { %>"postcss-loader", <% } %>"stylus-loader"],
},<% } %>
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
type: "asset",
},
%><% if (htmlWebpackPlugin) { %>
{
test: /\.html$/i,
use: ["html-loader"],
},<% } %>

// Add your rules for custom modules here
// Learn more about loaders from https://webpack.js.org/loaders/
Expand All @@ -84,8 +73,7 @@ const config <% if (langType === "Typescript") { %>: Configuration <% } %>= {

export default () => {
if (isProduction) {
config.mode = "production";<% if (extractPlugin === "Only for Production") { %>
config.plugins?.push(new MiniCssExtractPlugin());<% } %><% if (workboxWebpackPlugin) { %>
config.mode = "production";<% if (workboxWebpackPlugin) { %>
config.plugins?.push(new WorkboxWebpackPlugin.GenerateSW());<% } %>
} else {
config.mode = "development";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
</head>
<body>
<div id="root"></div>
<script src="<%= entry %>"></script>
</body>
</html>
Loading
Loading