@omnific/react-scripts 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build-CoW3Nti_.js +195 -0
- package/dist/dev-D0bbZds7.js +298 -0
- package/dist/index.js +330 -22
- package/dist/main.d.ts +3 -3
- package/dist/main.js +1 -4
- package/dist/paths-DdPGCuT5.js +35 -0
- package/dist/scripts/build.js +2 -195
- package/dist/scripts/dev.js +2 -299
- package/package.json +13 -13
- package/dist/env-DxzdvROc.js +0 -22
- package/dist/main.cjs +0 -73
- package/dist/main.d.cts +0 -30
- package/dist/rspack.config-8gFQGJU3.js +0 -285
package/dist/index.js
CHANGED
|
@@ -1,29 +1,337 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { t as startBuild } from "./build-CoW3Nti_.js";
|
|
3
|
+
import { a as isDevelopment, i as getEnv, n as DEVELOPMENT, o as isProduction, r as PRODUCTION, t as paths_default } from "./paths-DdPGCuT5.js";
|
|
4
|
+
import { t as startDev } from "./dev-D0bbZds7.js";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import { parseArgs, styleText } from "node:util";
|
|
7
|
+
import { merge } from "webpack-merge";
|
|
8
|
+
import { rspack } from "@rspack/core";
|
|
9
|
+
import pth from "node:path";
|
|
10
|
+
import { existsSync } from "node:fs";
|
|
11
|
+
import { TsCheckerRspackPlugin } from "ts-checker-rspack-plugin";
|
|
12
|
+
import { ReactRefreshRspackPlugin } from "@rspack/plugin-react-refresh";
|
|
13
|
+
//#region utils/find-entry-file.ts
|
|
14
|
+
const fileExtensions = [
|
|
15
|
+
"ts",
|
|
16
|
+
"mts",
|
|
17
|
+
"mjs",
|
|
18
|
+
"cjs",
|
|
19
|
+
"js"
|
|
20
|
+
];
|
|
21
|
+
/**
|
|
22
|
+
* Find a file with any of the supported JavaScript extensions
|
|
23
|
+
* @param basePath - The base path without extension
|
|
24
|
+
* @returns The file path with extension if found, or a default path
|
|
25
|
+
*/
|
|
26
|
+
function findEntryFile(basePath) {
|
|
27
|
+
for (const ext of fileExtensions) {
|
|
28
|
+
const filePath = `${basePath}.${ext}`;
|
|
29
|
+
if (existsSync(filePath)) return filePath;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region utils/detect-package.ts
|
|
34
|
+
const require$1 = createRequire(import.meta.url);
|
|
35
|
+
function detectPackage(packageName) {
|
|
36
|
+
try {
|
|
37
|
+
require$1.resolve(packageName);
|
|
38
|
+
return true;
|
|
39
|
+
} catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region alias.ts
|
|
45
|
+
const moduleFileExtensions = [
|
|
46
|
+
"ts",
|
|
47
|
+
"tsx",
|
|
48
|
+
"js",
|
|
49
|
+
"json",
|
|
50
|
+
"mjs",
|
|
51
|
+
"jsx"
|
|
52
|
+
].map((ext) => `.${ext}`);
|
|
53
|
+
const aliasSymbol = {
|
|
54
|
+
src: "@",
|
|
55
|
+
root: "@app"
|
|
56
|
+
};
|
|
57
|
+
const alias = {
|
|
58
|
+
[aliasSymbol.src]: paths_default.appSrc,
|
|
59
|
+
[aliasSymbol.root]: paths_default.appPath
|
|
60
|
+
};
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region rspack.config.ts
|
|
63
|
+
const hasJsxRuntime = detectPackage("react/jsx-runtime");
|
|
64
|
+
const hasTailwind = detectPackage("tailwindcss");
|
|
65
|
+
const hasSwcHelper = detectPackage("@swc/helpers");
|
|
66
|
+
const lessRegex = /\.less$/;
|
|
67
|
+
const sassRegex = /\.(scss|sass)$/;
|
|
68
|
+
const cssRegex = /\.css$/;
|
|
69
|
+
const imageInlineSizeLimit = 1e4;
|
|
70
|
+
const require = createRequire(import.meta.url);
|
|
71
|
+
function resolvePackage(packageName) {
|
|
72
|
+
return require.resolve(packageName);
|
|
73
|
+
}
|
|
74
|
+
function createRspackConfig() {
|
|
75
|
+
const isEnvironmentDevelopment = isDevelopment();
|
|
76
|
+
const isEnvironmentProduction = isProduction();
|
|
77
|
+
function getStyleLoaders(preProcessor) {
|
|
78
|
+
const loaders = [
|
|
79
|
+
isEnvironmentProduction && {
|
|
80
|
+
loader: rspack.CssExtractRspackPlugin.loader,
|
|
81
|
+
options: paths_default.publicUrlOrPath.startsWith(".") ? { publicPath: "../../" } : {}
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
loader: "builtin:lightningcss-loader",
|
|
85
|
+
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
|
|
86
|
+
options: { minify: isEnvironmentProduction }
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
loader: resolvePackage("postcss-loader"),
|
|
90
|
+
options: hasTailwind ? { postcssOptions: {
|
|
91
|
+
ident: "postcss",
|
|
92
|
+
config: false,
|
|
93
|
+
plugins: [require("@tailwindcss/postcss")]
|
|
94
|
+
} } : void 0
|
|
95
|
+
}
|
|
96
|
+
].filter(Boolean);
|
|
97
|
+
if (preProcessor) loaders.push(preProcessor);
|
|
98
|
+
return loaders;
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
target: ["browserslist"],
|
|
102
|
+
stats: "errors-warnings",
|
|
103
|
+
mode: getEnv(),
|
|
104
|
+
bail: isEnvironmentProduction,
|
|
105
|
+
devtool: isEnvironmentDevelopment && "cheap-module-source-map",
|
|
106
|
+
entry: paths_default.appIndexJs,
|
|
107
|
+
output: {
|
|
108
|
+
path: paths_default.appBuild,
|
|
109
|
+
pathinfo: isEnvironmentDevelopment,
|
|
110
|
+
filename: isEnvironmentProduction ? "static/js/[name].[contenthash:8].js" : "static/js/[name].js",
|
|
111
|
+
chunkFilename: isEnvironmentProduction ? "static/js/[name].[contenthash:8].chunk.js" : "static/js/[name].chunk.js",
|
|
112
|
+
assetModuleFilename: "static/media/[name].[hash][ext]",
|
|
113
|
+
publicPath: paths_default.publicUrlOrPath,
|
|
114
|
+
devtoolModuleFilenameTemplate: isEnvironmentProduction ? (info) => pth.relative(paths_default.appSrc, info.absoluteResourcePath).replaceAll("\\", "/") : (info) => pth.resolve(info.absoluteResourcePath).replaceAll("\\", "/")
|
|
115
|
+
},
|
|
116
|
+
infrastructureLogging: { level: "none" },
|
|
117
|
+
optimization: {
|
|
118
|
+
runtimeChunk: "single",
|
|
119
|
+
splitChunks: {
|
|
120
|
+
chunks: "async",
|
|
121
|
+
minChunks: 1,
|
|
122
|
+
minSize: 2e4,
|
|
123
|
+
maxAsyncRequests: 30,
|
|
124
|
+
maxInitialRequests: 30,
|
|
125
|
+
cacheGroups: {
|
|
126
|
+
vendors: {
|
|
127
|
+
test: /[/\\]node_modules[/\\]/,
|
|
128
|
+
name: "chunk-vendors",
|
|
129
|
+
priority: -10,
|
|
130
|
+
reuseExistingChunk: true
|
|
131
|
+
},
|
|
132
|
+
default: {
|
|
133
|
+
minChunks: 2,
|
|
134
|
+
priority: -20,
|
|
135
|
+
reuseExistingChunk: true
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
minimizer: [new rspack.LightningCssMinimizerRspackPlugin(), new rspack.SwcJsMinimizerRspackPlugin({
|
|
140
|
+
extractComments: false,
|
|
141
|
+
minimizerOptions: {
|
|
142
|
+
minify: true,
|
|
143
|
+
mangle: true,
|
|
144
|
+
ecma: 5,
|
|
145
|
+
compress: { passes: 2 },
|
|
146
|
+
format: { comments: false }
|
|
147
|
+
}
|
|
148
|
+
})]
|
|
149
|
+
},
|
|
150
|
+
resolve: {
|
|
151
|
+
extensions: moduleFileExtensions,
|
|
152
|
+
alias
|
|
153
|
+
},
|
|
154
|
+
module: {
|
|
155
|
+
rules: [{ oneOf: [
|
|
156
|
+
{
|
|
157
|
+
test: [/\.avif$/],
|
|
158
|
+
type: "asset",
|
|
159
|
+
mimetype: "image/avif",
|
|
160
|
+
parser: { dataUrlCondition: { maxSize: imageInlineSizeLimit } }
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
test: [
|
|
164
|
+
/\.bmp$/,
|
|
165
|
+
/\.gif$/,
|
|
166
|
+
/\.jpe?g$/,
|
|
167
|
+
/\.png$/
|
|
168
|
+
],
|
|
169
|
+
type: "asset",
|
|
170
|
+
parser: { dataUrlCondition: { maxSize: imageInlineSizeLimit } }
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
test: /\.[jt]sx?$/,
|
|
174
|
+
exclude: [/node_modules/],
|
|
175
|
+
use: {
|
|
176
|
+
loader: "builtin:swc-loader",
|
|
177
|
+
options: {
|
|
178
|
+
jsc: {
|
|
179
|
+
externalHelpers: hasSwcHelper,
|
|
180
|
+
parser: {
|
|
181
|
+
syntax: "typescript",
|
|
182
|
+
tsx: true
|
|
183
|
+
},
|
|
184
|
+
transform: { react: {
|
|
185
|
+
throwIfNamespace: true,
|
|
186
|
+
development: isEnvironmentDevelopment,
|
|
187
|
+
useBuiltins: true,
|
|
188
|
+
runtime: hasJsxRuntime ? "automatic" : "classic",
|
|
189
|
+
refresh: isEnvironmentDevelopment
|
|
190
|
+
} }
|
|
191
|
+
},
|
|
192
|
+
module: { type: "es6" }
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
test: lessRegex,
|
|
198
|
+
use: getStyleLoaders({ loader: resolvePackage("less-loader") }),
|
|
199
|
+
sideEffects: true,
|
|
200
|
+
type: "css/auto"
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
test: sassRegex,
|
|
204
|
+
use: getStyleLoaders({
|
|
205
|
+
loader: resolvePackage("sass-loader"),
|
|
206
|
+
options: {
|
|
207
|
+
api: "modern-compiler",
|
|
208
|
+
implementation: resolvePackage("sass-embedded")
|
|
209
|
+
}
|
|
210
|
+
}),
|
|
211
|
+
sideEffects: true,
|
|
212
|
+
type: "css/auto"
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
test: cssRegex,
|
|
216
|
+
use: getStyleLoaders(),
|
|
217
|
+
sideEffects: true,
|
|
218
|
+
type: "css/auto"
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
exclude: [
|
|
222
|
+
/^$/,
|
|
223
|
+
/\.(js|mjs|jsx|ts|tsx)$/,
|
|
224
|
+
/\.html$/,
|
|
225
|
+
/\.json$/
|
|
226
|
+
],
|
|
227
|
+
type: "asset/resource"
|
|
228
|
+
}
|
|
229
|
+
] }],
|
|
230
|
+
parser: { "css/auto": { namedExports: false } }
|
|
231
|
+
},
|
|
232
|
+
plugins: [
|
|
233
|
+
new rspack.HtmlRspackPlugin({
|
|
234
|
+
inject: true,
|
|
235
|
+
template: paths_default.appHtml,
|
|
236
|
+
minify: isEnvironmentProduction
|
|
237
|
+
}),
|
|
238
|
+
isEnvironmentDevelopment && new rspack.CaseSensitivePlugin(),
|
|
239
|
+
isEnvironmentDevelopment && new ReactRefreshRspackPlugin(),
|
|
240
|
+
isEnvironmentDevelopment && new TsCheckerRspackPlugin({
|
|
241
|
+
async: true,
|
|
242
|
+
formatter: "basic",
|
|
243
|
+
devServer: false,
|
|
244
|
+
typescript: {
|
|
245
|
+
mode: "write-tsbuildinfo",
|
|
246
|
+
diagnosticOptions: {
|
|
247
|
+
semantic: true,
|
|
248
|
+
syntactic: false,
|
|
249
|
+
declaration: false,
|
|
250
|
+
global: false
|
|
251
|
+
},
|
|
252
|
+
configOverwrite: { compilerOptions: {
|
|
253
|
+
incremental: true,
|
|
254
|
+
skipLibCheck: true,
|
|
255
|
+
noEmit: true
|
|
256
|
+
} }
|
|
257
|
+
}
|
|
258
|
+
})
|
|
259
|
+
],
|
|
260
|
+
performance: false
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
//#endregion
|
|
264
|
+
//#region Service.ts
|
|
265
|
+
var Service = class {
|
|
266
|
+
initialized = false;
|
|
267
|
+
mode = DEVELOPMENT;
|
|
268
|
+
commands = {
|
|
269
|
+
dev: startDev,
|
|
270
|
+
build: startBuild
|
|
271
|
+
};
|
|
272
|
+
projectOptions;
|
|
273
|
+
webpackRawConfigFns = [];
|
|
274
|
+
async init(mode) {
|
|
275
|
+
if (this.initialized) return;
|
|
276
|
+
this.initialized = true;
|
|
277
|
+
this.mode = mode;
|
|
278
|
+
this.loadEnv(mode);
|
|
279
|
+
await this.loaderUserConfig();
|
|
280
|
+
}
|
|
281
|
+
async run(name) {
|
|
282
|
+
let command = this.commands[name];
|
|
283
|
+
if (!command && name) throw new Error(`command "${name}" does not exist.`);
|
|
284
|
+
let mode = name === "dev" ? DEVELOPMENT : PRODUCTION;
|
|
285
|
+
await this.init(mode);
|
|
286
|
+
await command({
|
|
287
|
+
mode,
|
|
288
|
+
service: this,
|
|
289
|
+
rspackConfig: this.resolveRspackConfig()
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
async loaderUserConfig() {
|
|
293
|
+
const configPath = findEntryFile(paths_default.config);
|
|
294
|
+
if (configPath) {
|
|
295
|
+
const res = await import(configPath);
|
|
296
|
+
this.handleLoadedUserConfig(res.default);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
resolveRspackConfig() {
|
|
300
|
+
if (!this.initialized) throw new Error("Service must call init() before calling resolveRspackConfig().");
|
|
301
|
+
let baseConfig = createRspackConfig();
|
|
302
|
+
for (const fn of this.webpackRawConfigFns) if (typeof fn === "function") {
|
|
303
|
+
const res = fn(baseConfig);
|
|
304
|
+
if (res) baseConfig = merge(baseConfig, res);
|
|
305
|
+
}
|
|
306
|
+
return baseConfig;
|
|
307
|
+
}
|
|
308
|
+
loadEnv(mode) {
|
|
309
|
+
process.env.NODE_ENV = mode;
|
|
310
|
+
}
|
|
311
|
+
handleLoadedUserConfig(data) {
|
|
312
|
+
this.projectOptions = data;
|
|
313
|
+
if (data.configureRspack) this.webpackRawConfigFns.push(data.configureRspack);
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
//#endregion
|
|
4
317
|
//#region index.ts
|
|
5
|
-
process.on("unhandledRejection",
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
318
|
+
process.on("unhandledRejection", consoleError);
|
|
319
|
+
const args = parseArgs({
|
|
320
|
+
args: process.argv.slice(2),
|
|
321
|
+
strict: false,
|
|
322
|
+
allowPositionals: true
|
|
10
323
|
});
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
324
|
+
const service = new Service();
|
|
325
|
+
const command = args.positionals[0];
|
|
326
|
+
try {
|
|
327
|
+
await service.run(command);
|
|
328
|
+
} catch (error) {
|
|
329
|
+
consoleError(error);
|
|
330
|
+
}
|
|
331
|
+
function consoleError(value) {
|
|
332
|
+
if (value instanceof Error) console.log(styleText("red", value.message));
|
|
333
|
+
else console.log(styleText("red", String(value)));
|
|
19
334
|
process.exit(1);
|
|
20
|
-
} else {
|
|
21
|
-
const script = args[scriptIndex];
|
|
22
|
-
setEnv({
|
|
23
|
-
NODE_ENV: script === "dev" ? DEVELOPMENT : PRODUCTION,
|
|
24
|
-
SCRIPT: script
|
|
25
|
-
});
|
|
26
|
-
await scriptLoaders[script]();
|
|
27
335
|
}
|
|
28
336
|
//#endregion
|
|
29
337
|
export {};
|
package/dist/main.d.ts
CHANGED
|
@@ -19,12 +19,12 @@ declare const _default: {
|
|
|
19
19
|
declare function getEnv(): string;
|
|
20
20
|
declare function isDevelopment(): boolean;
|
|
21
21
|
declare function isProduction(): boolean;
|
|
22
|
-
declare function isBuild(): boolean;
|
|
23
22
|
//#endregion
|
|
24
23
|
//#region main.d.ts
|
|
24
|
+
type ConfigureRspack = (config: Configuration) => Configuration;
|
|
25
25
|
type ReactScriptsConfig = {
|
|
26
|
-
configureRspack?:
|
|
26
|
+
configureRspack?: ConfigureRspack;
|
|
27
27
|
};
|
|
28
28
|
declare function defineConfig(config: ReactScriptsConfig): ReactScriptsConfig;
|
|
29
29
|
//#endregion
|
|
30
|
-
export { ReactScriptsConfig, defineConfig, getEnv,
|
|
30
|
+
export { ConfigureRspack, ReactScriptsConfig, defineConfig, getEnv, isDevelopment, isProduction, _default as paths };
|
package/dist/main.js
CHANGED
|
@@ -12,9 +12,6 @@ function isDevelopment() {
|
|
|
12
12
|
function isProduction() {
|
|
13
13
|
return getEnv() === PRODUCTION;
|
|
14
14
|
}
|
|
15
|
-
function isBuild() {
|
|
16
|
-
return process.env.SCRIPT === "build";
|
|
17
|
-
}
|
|
18
15
|
//#endregion
|
|
19
16
|
//#region paths.ts
|
|
20
17
|
const appDirectory = fs.realpathSync(process.cwd());
|
|
@@ -40,4 +37,4 @@ function defineConfig(config) {
|
|
|
40
37
|
return config;
|
|
41
38
|
}
|
|
42
39
|
//#endregion
|
|
43
|
-
export { defineConfig, getEnv,
|
|
40
|
+
export { defineConfig, getEnv, isDevelopment, isProduction, paths_default as paths };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import pth from "node:path";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
//#region utils/env.ts
|
|
4
|
+
const DEVELOPMENT = "development";
|
|
5
|
+
const PRODUCTION = "production";
|
|
6
|
+
function getEnv() {
|
|
7
|
+
return process.env.NODE_ENV || "development";
|
|
8
|
+
}
|
|
9
|
+
function isDevelopment() {
|
|
10
|
+
return getEnv() === DEVELOPMENT;
|
|
11
|
+
}
|
|
12
|
+
function isProduction() {
|
|
13
|
+
return getEnv() === PRODUCTION;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region paths.ts
|
|
17
|
+
const appDirectory = fs.realpathSync(process.cwd());
|
|
18
|
+
function resolveApp(relativePath) {
|
|
19
|
+
return pth.resolve(appDirectory, relativePath);
|
|
20
|
+
}
|
|
21
|
+
var paths_default = {
|
|
22
|
+
appSrc: resolveApp("src"),
|
|
23
|
+
appPath: resolveApp("."),
|
|
24
|
+
appBuild: resolveApp("build"),
|
|
25
|
+
appIndexJs: resolveApp("src/index.tsx"),
|
|
26
|
+
publicUrlOrPath: isDevelopment() ? "/" : "./",
|
|
27
|
+
appHtml: resolveApp("public/index.html"),
|
|
28
|
+
appTsConfig: resolveApp("./tsconfig.json"),
|
|
29
|
+
appNodeModules: resolveApp("node_modules"),
|
|
30
|
+
appPackageJson: resolveApp("package.json"),
|
|
31
|
+
appPublic: resolveApp("public"),
|
|
32
|
+
config: resolveApp("react-scripts.config")
|
|
33
|
+
};
|
|
34
|
+
//#endregion
|
|
35
|
+
export { isDevelopment as a, getEnv as i, DEVELOPMENT as n, isProduction as o, PRODUCTION as r, paths_default as t };
|
package/dist/scripts/build.js
CHANGED
|
@@ -1,195 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import fs from "node:fs/promises";
|
|
4
|
-
import { exit } from "node:process";
|
|
5
|
-
import { rspack } from "@rspack/core";
|
|
6
|
-
import path from "node:path";
|
|
7
|
-
import fs$1 from "node:fs";
|
|
8
|
-
import prettyBytes from "pretty-bytes";
|
|
9
|
-
import zlib from "node:zlib";
|
|
10
|
-
//#region utils/fs-extra.ts
|
|
11
|
-
async function emptyDirectory(directory) {
|
|
12
|
-
await fs.rm(directory, {
|
|
13
|
-
recursive: true,
|
|
14
|
-
force: true
|
|
15
|
-
});
|
|
16
|
-
await fs.mkdir(directory, { recursive: true });
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* 读取文件夹下的所有文件
|
|
20
|
-
* @param path
|
|
21
|
-
* @returns
|
|
22
|
-
*/
|
|
23
|
-
async function readdir(path$1) {
|
|
24
|
-
let fileList = [];
|
|
25
|
-
const files = await fs.readdir(path$1);
|
|
26
|
-
if (files.length === 0) return [];
|
|
27
|
-
for (const file of files) {
|
|
28
|
-
const filePath = path.join(path$1, file);
|
|
29
|
-
if ((await fs.stat(filePath)).isDirectory()) fileList = [...fileList, ...await readdir(filePath)];
|
|
30
|
-
else fileList.push(filePath);
|
|
31
|
-
}
|
|
32
|
-
return fileList;
|
|
33
|
-
}
|
|
34
|
-
//#endregion
|
|
35
|
-
//#region utils/gzip-size.ts
|
|
36
|
-
function getOptions(options) {
|
|
37
|
-
return {
|
|
38
|
-
level: 9,
|
|
39
|
-
...options
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
function gzipSync(input, options) {
|
|
43
|
-
return zlib.gzipSync(input, getOptions(options)).length;
|
|
44
|
-
}
|
|
45
|
-
//#endregion
|
|
46
|
-
//#region utils/strip-ansi.ts
|
|
47
|
-
function ansiRegex(options = {}) {
|
|
48
|
-
const { onlyFirst } = options;
|
|
49
|
-
const pattern = [String.raw`[\u001B\u009B][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d\/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\u0007)`, String.raw`(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))`].join("|");
|
|
50
|
-
return new RegExp(pattern, onlyFirst ? void 0 : "g");
|
|
51
|
-
}
|
|
52
|
-
function stripAnsi(string) {
|
|
53
|
-
const regex = ansiRegex();
|
|
54
|
-
return string.replace(regex, "");
|
|
55
|
-
}
|
|
56
|
-
//#endregion
|
|
57
|
-
//#region utils/file-size-reporter.ts
|
|
58
|
-
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
|
|
59
|
-
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
|
|
60
|
-
/**
|
|
61
|
-
* 是不是可以读取的资源
|
|
62
|
-
* @param asset
|
|
63
|
-
* @returns
|
|
64
|
-
*/
|
|
65
|
-
function canReadAsset(asset) {
|
|
66
|
-
return /\.(js|css)$/.test(asset) && !/service-worker\.js/.test(asset) && !/precache-manifest\.[\da-f]+\.js/.test(asset);
|
|
67
|
-
}
|
|
68
|
-
function removeFileNameHash(buildFolder, fileName) {
|
|
69
|
-
return fileName.replace(buildFolder, "").replaceAll("\\", "/").replace(/\/?(.*)(\.[\da-f]+)(\.chunk)?(\.js|\.css)/, (match, p1, p2, p3, p4) => p1 + p4);
|
|
70
|
-
}
|
|
71
|
-
async function measureFileSizesBeforeBuild(buildFolder) {
|
|
72
|
-
const result = {
|
|
73
|
-
root: buildFolder,
|
|
74
|
-
sizes: {}
|
|
75
|
-
};
|
|
76
|
-
try {
|
|
77
|
-
const files = await readdir(buildFolder);
|
|
78
|
-
if (Array.isArray(files) && files.length > 0) {
|
|
79
|
-
const canReadAssetFiles = files.filter((item) => canReadAsset(item));
|
|
80
|
-
for (const fileName of canReadAssetFiles) {
|
|
81
|
-
const stats = fs$1.statSync(fileName);
|
|
82
|
-
const fileKey = removeFileNameHash(buildFolder, fileName);
|
|
83
|
-
result.sizes[fileKey] = stats.size;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
} catch {
|
|
87
|
-
return result;
|
|
88
|
-
}
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
function getDifferenceLabel(currentSize, previousSize) {
|
|
92
|
-
const FIFTY_KILOBYTES = 1024 * 50;
|
|
93
|
-
const difference = currentSize - previousSize;
|
|
94
|
-
const fileSize = Number.isNaN(difference) ? "0" : prettyBytes(difference);
|
|
95
|
-
if (difference >= FIFTY_KILOBYTES) return styleText("red", "+" + fileSize);
|
|
96
|
-
else if (difference < FIFTY_KILOBYTES && difference > 0) return styleText("yellow", "+" + fileSize);
|
|
97
|
-
else if (difference < 0) return styleText("green", fileSize);
|
|
98
|
-
else return "";
|
|
99
|
-
}
|
|
100
|
-
function printFileSizesAfterBuild(stats, previousSizeMap) {
|
|
101
|
-
let suggestBundleSplitting = false;
|
|
102
|
-
const root = previousSizeMap.root;
|
|
103
|
-
const sizes = previousSizeMap.sizes;
|
|
104
|
-
const statCompilation = stats.toJson({
|
|
105
|
-
all: false,
|
|
106
|
-
assets: true
|
|
107
|
-
});
|
|
108
|
-
if (statCompilation.assets) {
|
|
109
|
-
const assets = [];
|
|
110
|
-
const gzipLabelLengthList = [];
|
|
111
|
-
const sizeLabelLengthList = [];
|
|
112
|
-
for (const asset of statCompilation.assets) {
|
|
113
|
-
if (!canReadAsset(asset.name)) continue;
|
|
114
|
-
const fileContents = fs$1.readFileSync(path.join(root, asset.name));
|
|
115
|
-
const previousSize = sizes[removeFileNameHash(root, asset.name)];
|
|
116
|
-
const difference = getDifferenceLabel(asset.size, previousSize);
|
|
117
|
-
const sizeLabel = prettyBytes(asset.size) + (difference ? " (" + difference + ")" : "");
|
|
118
|
-
const gzipLabel = "gzip : " + prettyBytes(gzipSync(fileContents));
|
|
119
|
-
const sizeLabelLength = stripAnsi(sizeLabel).length;
|
|
120
|
-
const gzipLabelLength = stripAnsi(gzipLabel).length;
|
|
121
|
-
assets.push({
|
|
122
|
-
folder: path.join(path.basename(paths_default.appBuild), path.dirname(asset.name)),
|
|
123
|
-
name: path.basename(asset.name),
|
|
124
|
-
size: asset.size,
|
|
125
|
-
sizeLabel,
|
|
126
|
-
gzipLabel,
|
|
127
|
-
sizeLabelLength,
|
|
128
|
-
gzipLabelLength
|
|
129
|
-
});
|
|
130
|
-
gzipLabelLengthList.push(gzipLabelLength);
|
|
131
|
-
sizeLabelLengthList.push(sizeLabelLength);
|
|
132
|
-
}
|
|
133
|
-
assets.sort((a, b) => b.size - a.size);
|
|
134
|
-
const longestSizeLabelLength = Math.max.apply(void 0, sizeLabelLengthList);
|
|
135
|
-
const longestGzipSizeLabelLength = Math.max.apply(void 0, gzipLabelLengthList);
|
|
136
|
-
for (const asset of assets) {
|
|
137
|
-
let sizeLabel = asset.sizeLabel;
|
|
138
|
-
let gzipLabel = asset.gzipLabel;
|
|
139
|
-
const sizeLabelLength = asset.sizeLabelLength;
|
|
140
|
-
const gzipLabelLength = asset.gzipLabelLength;
|
|
141
|
-
if (sizeLabelLength < longestSizeLabelLength) sizeLabel += " ".repeat(longestSizeLabelLength - sizeLabelLength);
|
|
142
|
-
if (gzipLabelLength < longestGzipSizeLabelLength) gzipLabel += " ".repeat(longestGzipSizeLabelLength - gzipLabelLength);
|
|
143
|
-
const maxRecommendedSize = asset.name.indexOf("main.") === 0 ? WARN_AFTER_BUNDLE_GZIP_SIZE : WARN_AFTER_CHUNK_GZIP_SIZE;
|
|
144
|
-
const isLarge = maxRecommendedSize && asset.size > maxRecommendedSize;
|
|
145
|
-
if (isLarge && path.extname(asset.name) === ".js") suggestBundleSplitting = true;
|
|
146
|
-
console.log(" " + (isLarge ? styleText("yellow", sizeLabel) : sizeLabel) + " " + gzipLabel, " " + styleText("dim", asset.folder + path.sep) + styleText("cyan", asset.name));
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (suggestBundleSplitting) {
|
|
150
|
-
console.log();
|
|
151
|
-
console.log(styleText("yellow", "The bundle size is significantly larger than recommended."));
|
|
152
|
-
console.log(styleText("yellow", "You can also analyze the project dependencies: https://goo.gl/LeUzfb"));
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
//#endregion
|
|
156
|
-
//#region scripts/build.ts
|
|
157
|
-
async function startBuild() {
|
|
158
|
-
try {
|
|
159
|
-
const previousFileSizes = await measureFileSizesBeforeBuild(paths_default.appBuild);
|
|
160
|
-
await emptyDirectory(paths_default.appBuild);
|
|
161
|
-
await copyPublicFolder();
|
|
162
|
-
const stats = await build();
|
|
163
|
-
console.log(styleText("green", "Compiled successfully.\n"));
|
|
164
|
-
console.log("File sizes after gzip:\n");
|
|
165
|
-
if (stats) printFileSizesAfterBuild(stats, previousFileSizes);
|
|
166
|
-
console.log();
|
|
167
|
-
} catch (error) {
|
|
168
|
-
console.log(styleText("red", "Failed to compile.\n"));
|
|
169
|
-
if (error && error.message) console.log(error.message);
|
|
170
|
-
exit(1);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
function copyPublicFolder() {
|
|
174
|
-
return fs.cp(paths_default.appPublic, paths_default.appBuild, {
|
|
175
|
-
recursive: true,
|
|
176
|
-
filter: (source) => source !== paths_default.appHtml
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Create the production build and print the deployment instructions.
|
|
181
|
-
* @returns
|
|
182
|
-
*/
|
|
183
|
-
async function build() {
|
|
184
|
-
console.log("Creating an optimized production build...");
|
|
185
|
-
const compiler = rspack(await createRspackConfig());
|
|
186
|
-
return new Promise((resolve, reject) => {
|
|
187
|
-
compiler.run((error, stats) => {
|
|
188
|
-
if (error) return reject(error);
|
|
189
|
-
return resolve(stats);
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
await startBuild();
|
|
194
|
-
//#endregion
|
|
195
|
-
export {};
|
|
1
|
+
import { t as startBuild } from "../build-CoW3Nti_.js";
|
|
2
|
+
export { startBuild };
|