@uni_toolkit/unplugin-json-optimization 0.0.10-alpha.1762396524408 → 0.0.11
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/chunk-E4HM7IM2.js +70 -0
- package/dist/index.cjs +30 -4
- package/dist/index.js +1 -1
- package/dist/vite.cjs +30 -4
- package/dist/vite.js +1 -1
- package/dist/webpack.cjs +30 -4
- package/dist/webpack.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-EUN6NT5L.js +0 -44
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createUnplugin } from "unplugin";
|
|
3
|
+
import { createFilter } from "@rollup/pluginutils";
|
|
4
|
+
import path from "path";
|
|
5
|
+
function replaceRequirePaths(code, pathMapper) {
|
|
6
|
+
return code.replace(/require\s*\(\s*(["'])([^"']+)\1\s*\)/g, (_, quote, path2) => {
|
|
7
|
+
const newPath = pathMapper(path2);
|
|
8
|
+
if (newPath && newPath !== path2) {
|
|
9
|
+
return `require(${quote}${newPath}${quote})`;
|
|
10
|
+
}
|
|
11
|
+
return code;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
var unpluginFactory = (options = {
|
|
15
|
+
includes: ["**/*.json"],
|
|
16
|
+
excludes: ["pages.json", "**/uni_modules/**/*.json"]
|
|
17
|
+
}) => {
|
|
18
|
+
const jsonFiles = /* @__PURE__ */ new Set();
|
|
19
|
+
const pathMap = /* @__PURE__ */ new Map();
|
|
20
|
+
const inputDir = process.env.UNI_INPUT_DIR;
|
|
21
|
+
return {
|
|
22
|
+
name: "unplugin-json-optimization",
|
|
23
|
+
enforce: "post",
|
|
24
|
+
load(id) {
|
|
25
|
+
if (!id.endsWith(".json") || !createFilter(options.includes, options.excludes)(id)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
if (!jsonFiles.has(id)) {
|
|
29
|
+
jsonFiles.add(id);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
generateBundle(_, bundle) {
|
|
33
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
34
|
+
if (chunk.type !== "chunk" || !chunk.moduleIds || !chunk.moduleIds.length || fileName.includes("/")) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const id = chunk.moduleIds.find((id2) => jsonFiles.has(id2));
|
|
38
|
+
if (id) {
|
|
39
|
+
const relativePath = path.relative(inputDir, id);
|
|
40
|
+
const { dir, name } = path.parse(relativePath);
|
|
41
|
+
chunk.fileName = `${path.join(dir, name)}.js`;
|
|
42
|
+
pathMap.set(fileName, chunk.fileName);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const keys = Array.from(pathMap.keys());
|
|
46
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
47
|
+
if (chunk.type !== "chunk") {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (keys.some((key) => chunk.code.includes(key))) {
|
|
51
|
+
chunk.code = replaceRequirePaths(chunk.code, (originalPath) => {
|
|
52
|
+
const name = path.relative(process.cwd(), path.resolve(path.dirname(fileName), originalPath));
|
|
53
|
+
if (keys.includes(name)) {
|
|
54
|
+
return path.relative(path.dirname(fileName), pathMap.get(name));
|
|
55
|
+
}
|
|
56
|
+
return originalPath;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
var unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
|
|
64
|
+
var src_default = unplugin;
|
|
65
|
+
|
|
66
|
+
export {
|
|
67
|
+
unpluginFactory,
|
|
68
|
+
unplugin,
|
|
69
|
+
src_default
|
|
70
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -38,11 +38,21 @@ module.exports = __toCommonJS(src_exports);
|
|
|
38
38
|
var import_unplugin = require("unplugin");
|
|
39
39
|
var import_pluginutils = require("@rollup/pluginutils");
|
|
40
40
|
var import_node_path = __toESM(require("path"), 1);
|
|
41
|
+
function replaceRequirePaths(code, pathMapper) {
|
|
42
|
+
return code.replace(/require\s*\(\s*(["'])([^"']+)\1\s*\)/g, (_, quote, path2) => {
|
|
43
|
+
const newPath = pathMapper(path2);
|
|
44
|
+
if (newPath && newPath !== path2) {
|
|
45
|
+
return `require(${quote}${newPath}${quote})`;
|
|
46
|
+
}
|
|
47
|
+
return code;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
41
50
|
var unpluginFactory = (options = {
|
|
42
51
|
includes: ["**/*.json"],
|
|
43
|
-
excludes: ["pages.json", "**/uni_modules
|
|
52
|
+
excludes: ["pages.json", "**/uni_modules/**/*.json"]
|
|
44
53
|
}) => {
|
|
45
54
|
const jsonFiles = /* @__PURE__ */ new Set();
|
|
55
|
+
const pathMap = /* @__PURE__ */ new Map();
|
|
46
56
|
const inputDir = process.env.UNI_INPUT_DIR;
|
|
47
57
|
return {
|
|
48
58
|
name: "unplugin-json-optimization",
|
|
@@ -56,15 +66,31 @@ var unpluginFactory = (options = {
|
|
|
56
66
|
}
|
|
57
67
|
},
|
|
58
68
|
generateBundle(_, bundle) {
|
|
59
|
-
for (const [, chunk] of Object.entries(bundle)) {
|
|
60
|
-
if (chunk.type !== "chunk" || !chunk.moduleIds) {
|
|
69
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
70
|
+
if (chunk.type !== "chunk" || !chunk.moduleIds || !chunk.moduleIds.length || fileName.includes("/")) {
|
|
61
71
|
continue;
|
|
62
72
|
}
|
|
63
73
|
const id = chunk.moduleIds.find((id2) => jsonFiles.has(id2));
|
|
64
|
-
if (id
|
|
74
|
+
if (id) {
|
|
65
75
|
const relativePath = import_node_path.default.relative(inputDir, id);
|
|
66
76
|
const { dir, name } = import_node_path.default.parse(relativePath);
|
|
67
77
|
chunk.fileName = `${import_node_path.default.join(dir, name)}.js`;
|
|
78
|
+
pathMap.set(fileName, chunk.fileName);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const keys = Array.from(pathMap.keys());
|
|
82
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
83
|
+
if (chunk.type !== "chunk") {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (keys.some((key) => chunk.code.includes(key))) {
|
|
87
|
+
chunk.code = replaceRequirePaths(chunk.code, (originalPath) => {
|
|
88
|
+
const name = import_node_path.default.relative(process.cwd(), import_node_path.default.resolve(import_node_path.default.dirname(fileName), originalPath));
|
|
89
|
+
if (keys.includes(name)) {
|
|
90
|
+
return import_node_path.default.relative(import_node_path.default.dirname(fileName), pathMap.get(name));
|
|
91
|
+
}
|
|
92
|
+
return originalPath;
|
|
93
|
+
});
|
|
68
94
|
}
|
|
69
95
|
}
|
|
70
96
|
}
|
package/dist/index.js
CHANGED
package/dist/vite.cjs
CHANGED
|
@@ -39,11 +39,21 @@ var import_unplugin2 = require("unplugin");
|
|
|
39
39
|
var import_unplugin = require("unplugin");
|
|
40
40
|
var import_pluginutils = require("@rollup/pluginutils");
|
|
41
41
|
var import_node_path = __toESM(require("path"), 1);
|
|
42
|
+
function replaceRequirePaths(code, pathMapper) {
|
|
43
|
+
return code.replace(/require\s*\(\s*(["'])([^"']+)\1\s*\)/g, (_, quote, path2) => {
|
|
44
|
+
const newPath = pathMapper(path2);
|
|
45
|
+
if (newPath && newPath !== path2) {
|
|
46
|
+
return `require(${quote}${newPath}${quote})`;
|
|
47
|
+
}
|
|
48
|
+
return code;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
42
51
|
var unpluginFactory = (options = {
|
|
43
52
|
includes: ["**/*.json"],
|
|
44
|
-
excludes: ["pages.json", "**/uni_modules
|
|
53
|
+
excludes: ["pages.json", "**/uni_modules/**/*.json"]
|
|
45
54
|
}) => {
|
|
46
55
|
const jsonFiles = /* @__PURE__ */ new Set();
|
|
56
|
+
const pathMap = /* @__PURE__ */ new Map();
|
|
47
57
|
const inputDir = process.env.UNI_INPUT_DIR;
|
|
48
58
|
return {
|
|
49
59
|
name: "unplugin-json-optimization",
|
|
@@ -57,15 +67,31 @@ var unpluginFactory = (options = {
|
|
|
57
67
|
}
|
|
58
68
|
},
|
|
59
69
|
generateBundle(_, bundle) {
|
|
60
|
-
for (const [, chunk] of Object.entries(bundle)) {
|
|
61
|
-
if (chunk.type !== "chunk" || !chunk.moduleIds) {
|
|
70
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
71
|
+
if (chunk.type !== "chunk" || !chunk.moduleIds || !chunk.moduleIds.length || fileName.includes("/")) {
|
|
62
72
|
continue;
|
|
63
73
|
}
|
|
64
74
|
const id = chunk.moduleIds.find((id2) => jsonFiles.has(id2));
|
|
65
|
-
if (id
|
|
75
|
+
if (id) {
|
|
66
76
|
const relativePath = import_node_path.default.relative(inputDir, id);
|
|
67
77
|
const { dir, name } = import_node_path.default.parse(relativePath);
|
|
68
78
|
chunk.fileName = `${import_node_path.default.join(dir, name)}.js`;
|
|
79
|
+
pathMap.set(fileName, chunk.fileName);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const keys = Array.from(pathMap.keys());
|
|
83
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
84
|
+
if (chunk.type !== "chunk") {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (keys.some((key) => chunk.code.includes(key))) {
|
|
88
|
+
chunk.code = replaceRequirePaths(chunk.code, (originalPath) => {
|
|
89
|
+
const name = import_node_path.default.relative(process.cwd(), import_node_path.default.resolve(import_node_path.default.dirname(fileName), originalPath));
|
|
90
|
+
if (keys.includes(name)) {
|
|
91
|
+
return import_node_path.default.relative(import_node_path.default.dirname(fileName), pathMap.get(name));
|
|
92
|
+
}
|
|
93
|
+
return originalPath;
|
|
94
|
+
});
|
|
69
95
|
}
|
|
70
96
|
}
|
|
71
97
|
}
|
package/dist/vite.js
CHANGED
package/dist/webpack.cjs
CHANGED
|
@@ -39,11 +39,21 @@ var import_unplugin2 = require("unplugin");
|
|
|
39
39
|
var import_unplugin = require("unplugin");
|
|
40
40
|
var import_pluginutils = require("@rollup/pluginutils");
|
|
41
41
|
var import_node_path = __toESM(require("path"), 1);
|
|
42
|
+
function replaceRequirePaths(code, pathMapper) {
|
|
43
|
+
return code.replace(/require\s*\(\s*(["'])([^"']+)\1\s*\)/g, (_, quote, path2) => {
|
|
44
|
+
const newPath = pathMapper(path2);
|
|
45
|
+
if (newPath && newPath !== path2) {
|
|
46
|
+
return `require(${quote}${newPath}${quote})`;
|
|
47
|
+
}
|
|
48
|
+
return code;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
42
51
|
var unpluginFactory = (options = {
|
|
43
52
|
includes: ["**/*.json"],
|
|
44
|
-
excludes: ["pages.json", "**/uni_modules
|
|
53
|
+
excludes: ["pages.json", "**/uni_modules/**/*.json"]
|
|
45
54
|
}) => {
|
|
46
55
|
const jsonFiles = /* @__PURE__ */ new Set();
|
|
56
|
+
const pathMap = /* @__PURE__ */ new Map();
|
|
47
57
|
const inputDir = process.env.UNI_INPUT_DIR;
|
|
48
58
|
return {
|
|
49
59
|
name: "unplugin-json-optimization",
|
|
@@ -57,15 +67,31 @@ var unpluginFactory = (options = {
|
|
|
57
67
|
}
|
|
58
68
|
},
|
|
59
69
|
generateBundle(_, bundle) {
|
|
60
|
-
for (const [, chunk] of Object.entries(bundle)) {
|
|
61
|
-
if (chunk.type !== "chunk" || !chunk.moduleIds) {
|
|
70
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
71
|
+
if (chunk.type !== "chunk" || !chunk.moduleIds || !chunk.moduleIds.length || fileName.includes("/")) {
|
|
62
72
|
continue;
|
|
63
73
|
}
|
|
64
74
|
const id = chunk.moduleIds.find((id2) => jsonFiles.has(id2));
|
|
65
|
-
if (id
|
|
75
|
+
if (id) {
|
|
66
76
|
const relativePath = import_node_path.default.relative(inputDir, id);
|
|
67
77
|
const { dir, name } = import_node_path.default.parse(relativePath);
|
|
68
78
|
chunk.fileName = `${import_node_path.default.join(dir, name)}.js`;
|
|
79
|
+
pathMap.set(fileName, chunk.fileName);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const keys = Array.from(pathMap.keys());
|
|
83
|
+
for (const [fileName, chunk] of Object.entries(bundle)) {
|
|
84
|
+
if (chunk.type !== "chunk") {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (keys.some((key) => chunk.code.includes(key))) {
|
|
88
|
+
chunk.code = replaceRequirePaths(chunk.code, (originalPath) => {
|
|
89
|
+
const name = import_node_path.default.relative(process.cwd(), import_node_path.default.resolve(import_node_path.default.dirname(fileName), originalPath));
|
|
90
|
+
if (keys.includes(name)) {
|
|
91
|
+
return import_node_path.default.relative(import_node_path.default.dirname(fileName), pathMap.get(name));
|
|
92
|
+
}
|
|
93
|
+
return originalPath;
|
|
94
|
+
});
|
|
69
95
|
}
|
|
70
96
|
}
|
|
71
97
|
}
|
package/dist/webpack.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uni_toolkit/unplugin-json-optimization",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.11",
|
|
5
5
|
"description": "A plugin to optimize json files generation",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/uni-toolkit/uni-toolkit/tree/main/packages/unplugin-json-optimization",
|
package/dist/chunk-EUN6NT5L.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import { createUnplugin } from "unplugin";
|
|
3
|
-
import { createFilter } from "@rollup/pluginutils";
|
|
4
|
-
import path from "path";
|
|
5
|
-
var unpluginFactory = (options = {
|
|
6
|
-
includes: ["**/*.json"],
|
|
7
|
-
excludes: ["pages.json", "**/uni_modules/*.json"]
|
|
8
|
-
}) => {
|
|
9
|
-
const jsonFiles = /* @__PURE__ */ new Set();
|
|
10
|
-
const inputDir = process.env.UNI_INPUT_DIR;
|
|
11
|
-
return {
|
|
12
|
-
name: "unplugin-json-optimization",
|
|
13
|
-
enforce: "post",
|
|
14
|
-
load(id) {
|
|
15
|
-
if (!id.endsWith(".json") || !createFilter(options.includes, options.excludes)(id)) {
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
if (!jsonFiles.has(id)) {
|
|
19
|
-
jsonFiles.add(id);
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
generateBundle(_, bundle) {
|
|
23
|
-
for (const [, chunk] of Object.entries(bundle)) {
|
|
24
|
-
if (chunk.type !== "chunk" || !chunk.moduleIds) {
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
const id = chunk.moduleIds.find((id2) => jsonFiles.has(id2));
|
|
28
|
-
if (id && !id.includes("/")) {
|
|
29
|
-
const relativePath = path.relative(inputDir, id);
|
|
30
|
-
const { dir, name } = path.parse(relativePath);
|
|
31
|
-
chunk.fileName = `${path.join(dir, name)}.js`;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
};
|
|
37
|
-
var unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
|
|
38
|
-
var src_default = unplugin;
|
|
39
|
-
|
|
40
|
-
export {
|
|
41
|
-
unpluginFactory,
|
|
42
|
-
unplugin,
|
|
43
|
-
src_default
|
|
44
|
-
};
|