forked from webpack/webpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.ts
More file actions
211 lines (193 loc) · 6.57 KB
/
default.ts
File metadata and controls
211 lines (193 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { type DynamicActionsFunction, type NodePlopAPI } from "node-plop";
import { type ActionType, type Answers, type FileRecord } from "../../types.js";
export default async function defaultInitGenerator(plop: NodePlopAPI) {
const __dirname = dirname(fileURLToPath(import.meta.url));
// dependencies to be installed
const devDependencies: string[] = ["webpack", "webpack-cli"];
await plop.load("../../utils/install-dependencies.js", {}, true);
await plop.load("../../utils/generate-files.js", {}, true);
plop.setDefaultInclude({ generators: true, actionTypes: true });
plop.setPlopfilePath(resolve(__dirname, "../../plopfile.js"));
// Define a custom action for installing packages
// Define a base generator for the project structure
plop.setGenerator("init-default", {
description: "Create a basic webpack project",
prompts: [
{
type: "list",
name: "langType",
message: "Which of the following JS solutions do you want to use?",
choices: ["none", "ES6", "Typescript"],
default: "none",
},
{
type: "confirm",
name: "devServer",
message: "Would you like to use Webpack Dev server?",
default: true,
},
{
type: "confirm",
name: "htmlWebpackPlugin",
message: "Do you want to simplify the creation of HTML files for your bundle?",
default: true,
},
{
type: "confirm",
name: "workboxWebpackPlugin",
message: "Do you want to add PWA support?",
default: true,
},
{
type: "list",
name: "cssType",
message: "Which of the following CSS solution do you want to use?",
choices: ["none", "CSS only", "SASS", "LESS", "Stylus"],
default: "CSS only",
filter: (input: string, answers: Answers) => {
if (input === "none") {
answers.isCSS = false;
answers.isPostCSS = false;
answers.extractPlugin = "No";
} else if (input === "CSS only") {
answers.isCSS = true;
}
return input;
},
},
{
type: "confirm",
name: "isCSS",
message: (answers: Answers) =>
`Will you be using CSS styles along with ${answers.cssType} in your project?`,
when: (answers: Answers) => answers.cssType !== "CSS only",
default: true,
},
{
type: "confirm",
name: "isPostCSS",
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",
message: "Which package manager do you want to use?",
choices: ["npm", "yarn", "pnpm"],
default: "npm",
validate(input: string) {
if (!input.trim()) {
return "Package manager cannot be empty";
}
return true;
},
},
],
actions: function actions(answers: Answers) {
const actions: ActionType[] = [];
switch (answers.langType) {
case "ES6":
devDependencies.push("babel-loader", "@babel/core", "@babel/preset-env");
break;
case "Typescript":
devDependencies.push("typescript", "ts-loader");
break;
}
if (answers.devServer) {
devDependencies.push("webpack-dev-server");
}
if (answers.htmlWebpackPlugin) {
devDependencies.push("html-webpack-plugin", "html-loader");
}
if (answers.workboxWebpackPlugin) {
devDependencies.push("workbox-webpack-plugin");
}
if (answers.isPostCSS) {
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");
break;
case "LESS":
devDependencies.push("less-loader", "less");
break;
case "Stylus":
devDependencies.push("stylus-loader", "stylus");
break;
}
}
if (answers.extractPlugin !== "No") {
devDependencies.push("mini-css-extract-plugin");
}
const files: FileRecord[] = [
{ filePath: "./index.html", fileType: "text" },
{
filePath: answers.langType === "Typescript" ? "webpack.config.ts" : "webpack.config.js",
fileType: "text",
},
{ filePath: "package.json", fileType: "text" },
{ filePath: "README.md", fileType: "text" },
];
switch (answers.langType) {
case "Typescript":
answers.entryPoint = "./src/index.ts";
files.push(
{ filePath: "tsconfig.json", fileType: "text" },
{ filePath: answers.entryPoint as string, fileType: "text" },
);
break;
case "ES6":
answers.entryPoint = "./src/index.js";
files.push(
{ filePath: "babel.config.json", fileType: "text" },
{ filePath: answers.entryPoint as string, fileType: "text" },
);
break;
default:
answers.entryPoint = "./src/index.js";
files.push({ filePath: answers.entryPoint as string, fileType: "text" });
break;
}
if (answers.isPostCSS) {
files.push({ filePath: "postcss.config.js", fileType: "text" });
}
for (const file of files) {
actions.push({
type: "generate-files",
path: join(answers.projectPath as string, file.filePath),
templateFile: join(
plop.getPlopfilePath(),
"../templates/init/default",
file.filePath.startsWith("webpack.config")
? "webpack.config.tpl"
: `${file.filePath}.tpl`,
),
fileType: file.fileType,
data: answers,
force: answers.force,
});
}
actions.push({
type: "install-dependencies",
path: answers.projectPath,
packages: devDependencies,
});
return actions;
} as DynamicActionsFunction,
});
}