@vnejs/build 0.0.35 → 0.0.36

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/build",
3
- "version": "0.0.35",
3
+ "version": "0.0.36",
4
4
  "description": "Build tools for @vnejs",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -15,35 +15,13 @@
15
15
  "keywords": [],
16
16
  "author": "",
17
17
  "license": "ISC",
18
- "browserslist": {
19
- "production": [
20
- ">0.5%",
21
- "not dead",
22
- "not op_mini all"
23
- ],
24
- "development": [
25
- "last 1 chrome version",
26
- "last 1 firefox version",
27
- "last 1 safari version"
28
- ]
29
- },
30
18
  "dependencies": {
31
- "commander": "13.1.0",
32
19
  "@bem-react/classname": "1.5.12",
20
+ "@vitejs/plugin-react": "^5.1.2",
21
+ "commander": "13.1.0",
33
22
  "lodash.merge": "4.6.2",
34
- "webpack": "5.107.2",
35
- "html-webpack-plugin": "5.6.7",
36
- "core-js": "3.49.0",
37
- "style-loader": "4.0.0",
38
- "css-loader": "7.1.4",
39
23
  "react": "~19.2.7",
40
24
  "react-dom": "~19.2.7",
41
- "babel-loader": "10.1.1",
42
- "base64-inline-loader": "2.0.1",
43
- "@babel/core": "7.29.7",
44
- "@babel/plugin-transform-class-properties": "7.29.7",
45
- "@babel/preset-env": "7.29.7",
46
- "@babel/preset-react": "7.29.7",
47
- "@babel/preset-typescript": "7.29.7"
25
+ "vite": "^7.0.4"
48
26
  }
49
27
  }
package/src/cli.js CHANGED
@@ -26,6 +26,6 @@ program
26
26
  .command("client")
27
27
  .option("-w, --watch", "watch mode", false)
28
28
  .option("-q, --quiet", "no console mode", false)
29
- .action(require("./client")(getRootDir()));
29
+ .action(async (options) => require("./client")(getRootDir())(options));
30
30
 
31
31
  program.parse();
@@ -1,9 +1,21 @@
1
- const webpack = require("webpack");
1
+ const { build } = require("vite");
2
2
 
3
- module.exports = (rootDir) => (options) =>
4
- webpack(require("./webpack.config")(rootDir, options), (err, stats) => {
5
- if (err || stats.hasErrors()) {
6
- stats.hasErrors() && console.error(stats.toJson().errors);
7
- stats.hasWarnings() && console.warn(stats.toJson().warnings);
3
+ const createConfig = require("./vite.config");
4
+
5
+ module.exports = (rootDir) => async (options) => {
6
+ const config = createConfig(rootDir, options);
7
+
8
+ try {
9
+ const result = await build(config);
10
+
11
+ if (result && typeof result.close === "function") {
12
+ const closeWatcher = () => result.close();
13
+
14
+ process.on("SIGINT", closeWatcher);
15
+ process.on("SIGTERM", closeWatcher);
8
16
  }
9
- });
17
+ } catch (error) {
18
+ console.error(error);
19
+ process.exit(1);
20
+ }
21
+ };
@@ -0,0 +1,120 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+
4
+ const entryDir = "entry";
5
+
6
+ const getVnejsScopedPackageNames = (rootPath, scopePrefix) => {
7
+ let dir = rootPath;
8
+
9
+ while (dir !== path.dirname(dir)) {
10
+ const scopedDir = path.join(dir, "node_modules", "@vnejs");
11
+
12
+ if (fs.existsSync(scopedDir)) {
13
+ return fs
14
+ .readdirSync(scopedDir)
15
+ .filter((name) => name.startsWith(scopePrefix))
16
+ .map((name) => `@vnejs/${name}`)
17
+ .filter((packageName) => {
18
+ try {
19
+ require.resolve(`${packageName}/package.json`, { paths: [rootPath] });
20
+ return true;
21
+ } catch {
22
+ return false;
23
+ }
24
+ })
25
+ .sort();
26
+ }
27
+
28
+ dir = path.dirname(dir);
29
+ }
30
+
31
+ return [];
32
+ };
33
+
34
+ const getModOrder = (modDir, gamePath) => {
35
+ const indexJsonPath = path.join(gamePath, modDir, "index.json");
36
+
37
+ if (!fs.existsSync(indexJsonPath)) return 0;
38
+
39
+ try {
40
+ const { order } = JSON.parse(fs.readFileSync(indexJsonPath, "utf8"));
41
+
42
+ return typeof order === "number" && Number.isFinite(order) ? order : 0;
43
+ } catch {
44
+ return 0;
45
+ }
46
+ };
47
+
48
+ const getGameModIndexJsonFiles = (rootPath) => {
49
+ const gamePath = path.join(rootPath, "game");
50
+
51
+ if (!fs.existsSync(gamePath)) return [];
52
+
53
+ return fs
54
+ .readdirSync(gamePath)
55
+ .map((modDir) => path.join(gamePath, modDir, "index.json"))
56
+ .filter((file) => fs.existsSync(file));
57
+ };
58
+
59
+ const getGameModScripts = (rootPath) => {
60
+ const gamePath = path.join(rootPath, "game");
61
+
62
+ if (!fs.existsSync(gamePath)) return [];
63
+
64
+ return fs
65
+ .readdirSync(gamePath)
66
+ .filter((modDir) => fs.existsSync(path.join(gamePath, modDir, "scripts", "index.js")))
67
+ .sort((modA, modB) => {
68
+ const orderDiff = getModOrder(modB, gamePath) - getModOrder(modA, gamePath);
69
+
70
+ return orderDiff !== 0 ? orderDiff : modA.localeCompare(modB);
71
+ })
72
+ .map((modDir) => path.join(gamePath, modDir, "scripts", "index.js"));
73
+ };
74
+
75
+ const resolvePackageDir = (name, rootPath) => path.dirname(require.resolve(`${name}/package.json`, { paths: [rootPath] }));
76
+
77
+ const importStatementPattern = /^[ \t]*import\s[\s\S]*?;[ \t]*(?:\n|$)/gm;
78
+
79
+ const toImportStatements = (targets) => targets.map((target) => `import ${JSON.stringify(target)};`);
80
+
81
+ // Порядок side-effect импортов важен: vendors → uis → bundle → entry/index.js → game/*/scripts → тело модуля.
82
+ const orderEntryImports = (code, { vendorImports = [], leadingImports = [], trailingImports = [] } = {}) => {
83
+ const leading = toImportStatements([...vendorImports, ...leadingImports]);
84
+ const trailing = toImportStatements(trailingImports);
85
+ const matches = [...code.matchAll(importStatementPattern)];
86
+
87
+ if (!matches.length) {
88
+ return [...leading, ...trailing, code].filter(Boolean).join("\n");
89
+ }
90
+
91
+ const lastImport = matches.at(-1);
92
+ const splitAt = lastImport.index + lastImport[0].length;
93
+ const originalImports = code.slice(0, splitAt).trimEnd();
94
+ const body = code.slice(splitAt).trimStart();
95
+
96
+ return [...leading, originalImports, ...trailing, body].filter(Boolean).join("\n");
97
+ };
98
+
99
+ const cleanClientOutput = (distPath, isClientOutput) => {
100
+ if (!fs.existsSync(distPath)) return;
101
+
102
+ fs.readdirSync(distPath).forEach((file) => {
103
+ if (!isClientOutput(file)) return;
104
+
105
+ fs.rmSync(path.join(distPath, file), { force: true, recursive: true });
106
+ });
107
+ };
108
+
109
+ const vendorImports = ["react", "react-dom", "@bem-react/classname"];
110
+
111
+ module.exports = {
112
+ entryDir,
113
+ vendorImports,
114
+ getVnejsScopedPackageNames,
115
+ getGameModIndexJsonFiles,
116
+ getGameModScripts,
117
+ resolvePackageDir,
118
+ orderEntryImports,
119
+ cleanClientOutput,
120
+ };
@@ -0,0 +1,130 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const react = require("@vitejs/plugin-react").default;
4
+
5
+ const {
6
+ entryDir,
7
+ vendorImports,
8
+ getVnejsScopedPackageNames,
9
+ getGameModIndexJsonFiles,
10
+ getGameModScripts,
11
+ resolvePackageDir,
12
+ orderEntryImports,
13
+ cleanClientOutput,
14
+ } = require("./utils");
15
+
16
+ const buildChunkPattern = /^(index|vendors|uis|bundle|web)\.[a-zA-Z0-9_-]+\.js$/;
17
+
18
+ const isClientOutput = (asset) => asset === "index.html" || buildChunkPattern.test(asset) || asset.startsWith("assets/");
19
+
20
+ const isVendorModule = (id) =>
21
+ /[/\\](react|react-dom)([/\\]|$)/.test(id) || id.includes("@bem-react/classname");
22
+
23
+ const isBundleModule = (id) => /(@vnejs[/\\]bundles\.|[/\\]bundles[/\\])/.test(id);
24
+
25
+ const isUisModule = (id) => /(@vnejs[/\\]uis\.|[/\\]uis[/\\])/.test(id);
26
+
27
+ module.exports = (rootPath, options = {}) => {
28
+ const isDev = process.env.NODE_ENV === "development";
29
+ const entryRoot = path.join(rootPath, entryDir);
30
+ const distPath = path.join(rootPath, "dist");
31
+ const entryFile = path.join(entryRoot, "index.js");
32
+ const htmlFile = path.join(entryRoot, "index.html");
33
+
34
+ if (!fs.existsSync(entryFile)) {
35
+ throw new Error(`entry file not found: ${entryFile}`);
36
+ }
37
+
38
+ if (!fs.existsSync(htmlFile)) {
39
+ throw new Error(`html template not found: ${htmlFile}`);
40
+ }
41
+
42
+ const uisPackages = getVnejsScopedPackageNames(rootPath, "uis.");
43
+ const bundlePackages = getVnejsScopedPackageNames(rootPath, "bundles.");
44
+
45
+ const buildOrderedEntry = (code) =>
46
+ orderEntryImports(code, {
47
+ vendorImports,
48
+ leadingImports: [...uisPackages, ...bundlePackages],
49
+ trailingImports: getGameModScripts(rootPath),
50
+ });
51
+
52
+ return {
53
+ configFile: false,
54
+ root: entryRoot,
55
+ base: "./",
56
+ mode: isDev ? "development" : "production",
57
+ publicDir: false,
58
+ logLevel: options.quiet ? "warn" : "info",
59
+ define: {
60
+ "process.env.NODE_ENV": JSON.stringify(isDev ? "development" : "production"),
61
+ },
62
+ resolve: {
63
+ alias: {
64
+ react: resolvePackageDir("react", rootPath),
65
+ "react-dom": resolvePackageDir("react-dom", rootPath),
66
+ },
67
+ extensions: [".js", ".jsx", ".ts", ".tsx", ".css"],
68
+ },
69
+ plugins: [
70
+ react({ include: /\.(jsx|tsx|js|ts)$/ }),
71
+ {
72
+ name: "vnejs-build:order-entry",
73
+ transform(code, id) {
74
+ if (path.normalize(id) === path.normalize(entryFile)) {
75
+ return buildOrderedEntry(code);
76
+ }
77
+ },
78
+ },
79
+ {
80
+ name: "vnejs-build:html-entry",
81
+ transformIndexHtml: {
82
+ order: "pre",
83
+ handler(html) {
84
+ if (html.includes("./index.js")) return html;
85
+
86
+ return html.replace("</body>", ' <script type="module" src="./index.js"></script>\n </body>');
87
+ },
88
+ },
89
+ },
90
+ {
91
+ name: "vnejs-build:clean-output",
92
+ buildStart() {
93
+ cleanClientOutput(distPath, isClientOutput);
94
+ },
95
+ },
96
+ options.watch && {
97
+ name: "vnejs-build:watch-mod-index",
98
+ buildStart() {
99
+ getGameModIndexJsonFiles(rootPath).forEach((file) => this.addWatchFile(file));
100
+ },
101
+ },
102
+ ].filter(Boolean),
103
+ build: {
104
+ outDir: path.relative(entryRoot, distPath) || "dist",
105
+ emptyOutDir: false,
106
+ minify: !isDev,
107
+ watch: options.watch ? {} : null,
108
+ assetsInlineLimit: (filePath) => (/\.(jpe?g|png|gif|svg)$/i.test(filePath) ? Number.POSITIVE_INFINITY : 4096),
109
+ rollupOptions: {
110
+ input: htmlFile,
111
+ output: {
112
+ entryFileNames: "[name].[hash].js",
113
+ chunkFileNames: "[name].[hash].js",
114
+ assetFileNames: (assetInfo) => {
115
+ const name = assetInfo.names?.[0] || assetInfo.name || "";
116
+
117
+ if (/\.(ttf|eot|woff2?)$/i.test(name)) return "assets/fonts/[name].[hash][extname]";
118
+
119
+ return "assets/[name].[hash][extname]";
120
+ },
121
+ manualChunks(id) {
122
+ if (isBundleModule(id)) return "bundle";
123
+ if (isUisModule(id)) return "uis";
124
+ if (isVendorModule(id)) return "vendors";
125
+ },
126
+ },
127
+ },
128
+ },
129
+ };
130
+ };
package/src/data/index.js CHANGED
@@ -2,7 +2,7 @@ const fs = require("fs");
2
2
  const path = require("path");
3
3
  const merge = require("lodash.merge");
4
4
 
5
- const { runBuildData } = require("./utils");
5
+ const { runBuildData, buildModsJson } = require("./utils");
6
6
 
7
7
  const buildDataFunc = (gameDir, distDir) => {
8
8
  let result = {};
@@ -19,7 +19,7 @@ const buildDataFunc = (gameDir, distDir) => {
19
19
  fs.writeFileSync(path.join(distDir, `label.${label.replaceAll("/", "|")}.json`), JSON.stringify(result.label[label]));
20
20
  });
21
21
 
22
- fs.writeFileSync(path.join(distDir, "mods.json"), JSON.stringify(fs.readdirSync(gameDir)));
22
+ fs.writeFileSync(path.join(distDir, "mods.json"), JSON.stringify(buildModsJson(gameDir)));
23
23
  };
24
24
 
25
25
  const cleanJsonFiles = (distDir) => {
package/src/data/utils.js CHANGED
@@ -151,4 +151,31 @@ const runBuildData = ({ modDir } = {}) => {
151
151
  return result;
152
152
  };
153
153
 
154
- module.exports = { runBuildData };
154
+ const readModIndexJson = (modPath) => {
155
+ const indexJsonPath = path.join(modPath, "index.json");
156
+
157
+ if (!fs.existsSync(indexJsonPath)) return {};
158
+
159
+ try {
160
+ return JSON.parse(fs.readFileSync(indexJsonPath, "utf8"));
161
+ } catch {
162
+ return {};
163
+ }
164
+ };
165
+
166
+ const buildModsJson = (gameDir) =>
167
+ fs.readdirSync(gameDir).reduce((mods, modDir) => {
168
+ const modPath = path.join(gameDir, modDir);
169
+
170
+ if (!fs.statSync(modPath).isDirectory()) return mods;
171
+
172
+ mods[modDir] = {
173
+ ...readModIndexJson(modPath),
174
+ hasMediaDir: fs.existsSync(path.join(modPath, "media")),
175
+ hasPublicDir: fs.existsSync(path.join(modPath, "public")),
176
+ };
177
+
178
+ return mods;
179
+ }, {});
180
+
181
+ module.exports = { runBuildData, buildModsJson };
@@ -1,139 +0,0 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const webpack = require("webpack");
4
- const HtmlWebpackPlugin = require("html-webpack-plugin");
5
-
6
- const buildRoot = path.join(__dirname, "../..");
7
- const entryDir = "entry";
8
- const isDev = process.env.NODE_ENV === "development";
9
-
10
- const vnejsPattern = `${path.sep}node_modules${path.sep}@vnejs${path.sep}`;
11
-
12
- const shouldBabelTranspile = (resourcePath) => {
13
- if (resourcePath.includes(`${path.sep}node_modules${path.sep}`)) {
14
- if (!resourcePath.includes(vnejsPattern)) return false;
15
-
16
- return !/[\\/]dist[\\/]/.test(resourcePath);
17
- }
18
-
19
- return true;
20
- };
21
-
22
- const getScriptLoader = () => ({
23
- loader: `babel-loader`,
24
- options: {
25
- root: buildRoot,
26
- plugins: [`@babel/plugin-transform-class-properties`],
27
- presets: [
28
- [`@babel/preset-typescript`, { allowDeclareFields: true }],
29
- [`@babel/preset-react`, { runtime: "automatic" }],
30
- [`@babel/preset-env`, { bugfixes: true, corejs: 3, modules: "commonjs", useBuiltIns: `usage` }],
31
- ],
32
- cacheDirectory: true,
33
- caller: { target: "clientside", name: "babel-loader" },
34
- },
35
- });
36
-
37
- const getCssLoader = () => [{ loader: `style-loader` }, { loader: `css-loader` }];
38
-
39
- const webpackChunkPattern = /^(index|vendors|uis|bundle)\.[a-f0-9]+\.js$/;
40
-
41
- const isWebpackOutput = (asset) => asset === "index.html" || webpackChunkPattern.test(asset) || asset.startsWith("assets/");
42
-
43
- const getVnejsScopedPackageNames = (rootPath, scopePrefix) => {
44
- let dir = rootPath;
45
-
46
- while (dir !== path.dirname(dir)) {
47
- const scopedDir = path.join(dir, "node_modules", "@vnejs");
48
-
49
- if (fs.existsSync(scopedDir)) {
50
- return fs
51
- .readdirSync(scopedDir)
52
- .filter((name) => name.startsWith(scopePrefix))
53
- .map((name) => `@vnejs/${name}`)
54
- .filter((packageName) => {
55
- try {
56
- require.resolve(`${packageName}/package.json`, { paths: [rootPath] });
57
- return true;
58
- } catch {
59
- return false;
60
- }
61
- })
62
- .sort();
63
- }
64
-
65
- dir = path.dirname(dir);
66
- }
67
-
68
- return [];
69
- };
70
-
71
- module.exports = (rootPath, options) => {
72
- const distPath = path.join(rootPath, "dist");
73
- const gamePath = path.join(rootPath, "game");
74
- const entry = [path.join(rootPath, entryDir, "index.js")];
75
-
76
- fs.readdirSync(gamePath).forEach((modDir) => {
77
- const scriptPath = path.join(gamePath, modDir, "scripts", "index.js");
78
-
79
- fs.existsSync(scriptPath) && entry.push(scriptPath);
80
- });
81
-
82
- const resolvePackageDir = (name) => path.dirname(require.resolve(`${name}/package.json`, { paths: [rootPath] }));
83
-
84
- const uisPackages = getVnejsScopedPackageNames(rootPath, "uis.");
85
- const bundlePackages = getVnejsScopedPackageNames(rootPath, "bundles.");
86
-
87
- const indexDependOn = ["vendors"];
88
-
89
- uisPackages.length && indexDependOn.push("uis");
90
- bundlePackages.length && indexDependOn.push("bundle");
91
-
92
- const htmlChunks = [...indexDependOn, "index"];
93
-
94
- const webpackEntry = { index: { import: entry, chunkLoading: false, dependOn: indexDependOn }, vendors: [`react`, `react-dom`, `@bem-react/classname`] };
95
-
96
- uisPackages.length && (webpackEntry.uis = { import: uisPackages, dependOn: "vendors" });
97
-
98
- if (bundlePackages.length) {
99
- const bundleDependOn = ["vendors"];
100
-
101
- uisPackages.length && bundleDependOn.push("uis");
102
-
103
- webpackEntry.bundle = { import: bundlePackages, dependOn: bundleDependOn };
104
- }
105
-
106
- return {
107
- watch: options.watch,
108
- mode: isDev ? "development" : "production",
109
- target: "web",
110
- entry: webpackEntry,
111
- optimization: { minimize: true },
112
- resolve: {
113
- extensions: [".js", ".jsx", ".ts", ".tsx", ".css"],
114
- alias: { "react": resolvePackageDir("react"), "react-dom": resolvePackageDir("react-dom") },
115
- },
116
- output: {
117
- path: distPath,
118
- filename: "[name].[contenthash].js",
119
- clean: { keep: (asset) => !isWebpackOutput(asset) },
120
- },
121
- plugins: [
122
- new webpack.DefinePlugin({ "process.env.NODE_ENV": `"${isDev ? "development" : "production"}"` }),
123
- new HtmlWebpackPlugin({ filename: "index.html", template: path.join(rootPath, entryDir, "index.html"), chunks: htmlChunks, chunksSortMode: "manual" }),
124
- ],
125
-
126
- module: {
127
- rules: [
128
- { test: /\.m?(t|j)sx?$/, exclude: (resourcePath) => !shouldBabelTranspile(resourcePath), use: getScriptLoader() },
129
- { test: /\.css$/, use: getCssLoader() },
130
- {
131
- test: /\.(ttf|eot|woff2?)$/,
132
- type: "asset/resource",
133
- generator: { filename: "assets/fonts/[name].[contenthash][ext]" },
134
- },
135
- { test: /\.(jpe?g|png|gif|svg)$/, use: [{ loader: `base64-inline-loader` }] },
136
- ],
137
- },
138
- };
139
- };