@uni_toolkit/unplugin-json-optimization 0.0.10 → 0.0.12

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.
@@ -2,11 +2,21 @@
2
2
  import { createUnplugin } from "unplugin";
3
3
  import { createFilter } from "@rollup/pluginutils";
4
4
  import path from "path";
5
+ function replaceRequirePaths(code, pathMapper) {
6
+ return code.replace(/require\s*\(\s*(["'])([^"']+)\1\s*\)/g, (match, quote, path2) => {
7
+ const newPath = pathMapper(path2);
8
+ if (newPath && newPath !== path2) {
9
+ return `require(${quote}${newPath}${quote})`;
10
+ }
11
+ return match;
12
+ });
13
+ }
5
14
  var unpluginFactory = (options = {
6
15
  includes: ["**/*.json"],
7
16
  excludes: ["pages.json", "**/uni_modules/**/*.json"]
8
17
  }) => {
9
18
  const jsonFiles = /* @__PURE__ */ new Set();
19
+ const pathMap = /* @__PURE__ */ new Map();
10
20
  const inputDir = process.env.UNI_INPUT_DIR;
11
21
  return {
12
22
  name: "unplugin-json-optimization",
@@ -29,6 +39,22 @@ var unpluginFactory = (options = {
29
39
  const relativePath = path.relative(inputDir, id);
30
40
  const { dir, name } = path.parse(relativePath);
31
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
+ });
32
58
  }
33
59
  }
34
60
  }
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, (match, quote, path2) => {
43
+ const newPath = pathMapper(path2);
44
+ if (newPath && newPath !== path2) {
45
+ return `require(${quote}${newPath}${quote})`;
46
+ }
47
+ return match;
48
+ });
49
+ }
41
50
  var unpluginFactory = (options = {
42
51
  includes: ["**/*.json"],
43
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",
@@ -65,6 +75,22 @@ var unpluginFactory = (options = {
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
@@ -2,7 +2,7 @@ import {
2
2
  src_default,
3
3
  unplugin,
4
4
  unpluginFactory
5
- } from "./chunk-5WLD335P.js";
5
+ } from "./chunk-ZDVJORCZ.js";
6
6
  export {
7
7
  src_default as default,
8
8
  unplugin,
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, (match, quote, path2) => {
44
+ const newPath = pathMapper(path2);
45
+ if (newPath && newPath !== path2) {
46
+ return `require(${quote}${newPath}${quote})`;
47
+ }
48
+ return match;
49
+ });
50
+ }
42
51
  var unpluginFactory = (options = {
43
52
  includes: ["**/*.json"],
44
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",
@@ -66,6 +76,22 @@ var unpluginFactory = (options = {
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
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-5WLD335P.js";
3
+ } from "./chunk-ZDVJORCZ.js";
4
4
 
5
5
  // src/vite.ts
6
6
  import { createVitePlugin } from "unplugin";
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, (match, quote, path2) => {
44
+ const newPath = pathMapper(path2);
45
+ if (newPath && newPath !== path2) {
46
+ return `require(${quote}${newPath}${quote})`;
47
+ }
48
+ return match;
49
+ });
50
+ }
42
51
  var unpluginFactory = (options = {
43
52
  includes: ["**/*.json"],
44
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",
@@ -66,6 +76,22 @@ var unpluginFactory = (options = {
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
  }
@@ -1,7 +1,7 @@
1
- import * as _unplugin from 'unplugin';
1
+ import * as webpack from 'webpack';
2
2
  import { Options } from './types.cjs';
3
3
  import '@rollup/pluginutils';
4
4
 
5
- declare const _default: (options?: Options | undefined) => _unplugin.WebpackPluginInstance;
5
+ declare const _default: (options?: Options | undefined) => webpack.WebpackPluginInstance;
6
6
 
7
7
  export { _default as default };
package/dist/webpack.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import * as _unplugin from 'unplugin';
1
+ import * as webpack from 'webpack';
2
2
  import { Options } from './types.js';
3
3
  import '@rollup/pluginutils';
4
4
 
5
- declare const _default: (options?: Options | undefined) => _unplugin.WebpackPluginInstance;
5
+ declare const _default: (options?: Options | undefined) => webpack.WebpackPluginInstance;
6
6
 
7
7
  export { _default as default };
package/dist/webpack.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-5WLD335P.js";
3
+ } from "./chunk-ZDVJORCZ.js";
4
4
 
5
5
  // src/webpack.ts
6
6
  import { createWebpackPlugin } from "unplugin";
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.10",
4
+ "version": "0.0.12",
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",
@@ -71,7 +71,7 @@
71
71
  }
72
72
  },
73
73
  "dependencies": {
74
- "@rollup/pluginutils": "^4.2.0",
74
+ "@rollup/pluginutils": "^5.3.0",
75
75
  "unplugin": "^2.3.4"
76
76
  },
77
77
  "devDependencies": {