@sepveneto/plugin-mp-router-view 0.4.4 → 0.5.0

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/rollup.cjs CHANGED
@@ -67,7 +67,7 @@ module.exports = __toCommonJS(rollup_exports);
67
67
  var import_unplugin2 = require("unplugin");
68
68
 
69
69
  // src/index.ts
70
- var import_node_path4 = __toESM(require("path"), 1);
70
+ var import_node_path5 = __toESM(require("path"), 1);
71
71
  var import_unplugin = require("unplugin");
72
72
 
73
73
  // src/util/parse.ts
@@ -5591,17 +5591,17 @@ function withTrailingSlash(input = "", respectQueryAndFragment) {
5591
5591
  if (hasTrailingSlash(input, true)) {
5592
5592
  return input || "/";
5593
5593
  }
5594
- let path7 = input;
5594
+ let path8 = input;
5595
5595
  let fragment = "";
5596
5596
  const fragmentIndex = input.indexOf("#");
5597
5597
  if (fragmentIndex >= 0) {
5598
- path7 = input.slice(0, fragmentIndex);
5598
+ path8 = input.slice(0, fragmentIndex);
5599
5599
  fragment = input.slice(fragmentIndex);
5600
- if (!path7) {
5600
+ if (!path8) {
5601
5601
  return fragment;
5602
5602
  }
5603
5603
  }
5604
- const [s0, ...s] = path7.split("?");
5604
+ const [s0, ...s] = path8.split("?");
5605
5605
  return s0 + "/" + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
5606
5606
  }
5607
5607
  function isNonEmptyURL(url) {
@@ -5635,8 +5635,8 @@ var import_node_path = __toESM(require("path"), 1);
5635
5635
  var import_node_v8 = __toESM(require("v8"), 1);
5636
5636
  var import_node_util = require("util");
5637
5637
  var BUILTIN_MODULES = new Set(import_node_module.builtinModules);
5638
- function normalizeSlash(path7) {
5639
- return path7.replace(/\\/g, "/");
5638
+ function normalizeSlash(path8) {
5639
+ return path8.replace(/\\/g, "/");
5640
5640
  }
5641
5641
  var own$1 = {}.hasOwnProperty;
5642
5642
  var classRegExp = /^([A-Z][a-z\d]*)+$/;
@@ -5752,8 +5752,8 @@ codes.ERR_INVALID_PACKAGE_CONFIG = createError(
5752
5752
  * @param {string} [base]
5753
5753
  * @param {string} [message]
5754
5754
  */
5755
- (path7, base, message) => {
5756
- return `Invalid package config ${path7}${base ? ` while importing ${base}` : ""}${message ? `. ${message}` : ""}`;
5755
+ (path8, base, message) => {
5756
+ return `Invalid package config ${path8}${base ? ` while importing ${base}` : ""}${message ? `. ${message}` : ""}`;
5757
5757
  },
5758
5758
  Error
5759
5759
  );
@@ -5785,8 +5785,8 @@ codes.ERR_MODULE_NOT_FOUND = createError(
5785
5785
  * @param {string} base
5786
5786
  * @param {boolean} [exactUrl]
5787
5787
  */
5788
- (path7, base, exactUrl = false) => {
5789
- return `Cannot find ${exactUrl ? "module" : "package"} '${path7}' imported from ${base}`;
5788
+ (path8, base, exactUrl = false) => {
5789
+ return `Cannot find ${exactUrl ? "module" : "package"} '${path8}' imported from ${base}`;
5790
5790
  },
5791
5791
  Error
5792
5792
  );
@@ -5837,8 +5837,8 @@ codes.ERR_UNKNOWN_FILE_EXTENSION = createError(
5837
5837
  * @param {string} extension
5838
5838
  * @param {string} path
5839
5839
  */
5840
- (extension, path7) => {
5841
- return `Unknown file extension "${extension}" for ${path7}`;
5840
+ (extension, path8) => {
5841
+ return `Unknown file extension "${extension}" for ${path8}`;
5842
5842
  },
5843
5843
  TypeError
5844
5844
  );
@@ -6218,9 +6218,9 @@ Default "index" lookups for the main are deprecated for ES modules.`,
6218
6218
  );
6219
6219
  }
6220
6220
  }
6221
- function tryStatSync(path7) {
6221
+ function tryStatSync(path8) {
6222
6222
  try {
6223
- return (0, import_node_fs.statSync)(path7);
6223
+ return (0, import_node_fs.statSync)(path8);
6224
6224
  } catch (e) {
6225
6225
  }
6226
6226
  }
@@ -7075,10 +7075,10 @@ _head = new WeakMap();
7075
7075
  _tail = new WeakMap();
7076
7076
  _size = new WeakMap();
7077
7077
  var findUpStop = Symbol("findUpStop");
7078
- function _resolve2(path7, options = {}) {
7078
+ function _resolve2(path8, options = {}) {
7079
7079
  if (options.platform === "auto" || !options.platform)
7080
7080
  options.platform = import_node_process2.default.platform === "win32" ? "win32" : "posix";
7081
- const modulePath = resolvePathSync(path7, {
7081
+ const modulePath = resolvePathSync(path8, {
7082
7082
  url: options.paths
7083
7083
  });
7084
7084
  if (options.platform === "win32")
@@ -7179,12 +7179,13 @@ function addToHeader(code, header2) {
7179
7179
  function addToFooter(code, footer2) {
7180
7180
  return code.replace(/(\s*)(<\/template>)(?!(([\s\S]*)(<\/template>)))/, (p) => footer2 + p);
7181
7181
  }
7182
+ var pageMap = /* @__PURE__ */ new Map();
7182
7183
  function getPages(collectMode) {
7183
7184
  const jsonStr = import_node_fs3.default.readFileSync(import_node_path3.default.resolve(INPUT_DIR, "pages.json"), "utf-8");
7184
7185
  const pagesJson = parseJson(jsonStr);
7185
7186
  const { pages, subPackages = [] } = pagesJson;
7186
7187
  const entryList = [...collectEntry(collectMode, pages), ...collectEntry(collectMode, subPackages)];
7187
- return entryList;
7188
+ return { entryList, pageMap };
7188
7189
  }
7189
7190
  function collectEntry(collectMode, json, root = "") {
7190
7191
  const entryList = [];
@@ -7197,6 +7198,7 @@ function collectEntry(collectMode, json, root = "") {
7197
7198
  return;
7198
7199
  }
7199
7200
  const _path = import_node_path3.default.resolve(INPUT_DIR, `${root}${root ? "/" : ""}${item.path}`);
7201
+ pageMap.set(_path, item);
7200
7202
  if (collectMode && typeof collectMode === "boolean" && !item["ROUTER_VIEW_EXCLUDE"]) {
7201
7203
  entryList.push(_path);
7202
7204
  } else if (typeof collectMode === "string" && item[collectMode]) {
@@ -7216,9 +7218,9 @@ function combineCode(code, header2, footer2) {
7216
7218
  code = addToFooter(code, footer2);
7217
7219
  return code;
7218
7220
  }
7219
- function transform(path7, code, entryPages, level) {
7221
+ function transform(path8, code, entryPages, level) {
7220
7222
  logLevel = level;
7221
- if (isAppVue(path7)) {
7223
+ if (isAppVue(path8)) {
7222
7224
  log(`\u57FA\u4E8E\u5E73\u53F0${process.env.UNI_PLATFORM}, \u5904\u7406App.vue...`, logLevel);
7223
7225
  const { origin, tpl } = getTemplate(code);
7224
7226
  const { count, before, after } = countRouterView(tpl);
@@ -7230,28 +7232,55 @@ function transform(path7, code, entryPages, level) {
7230
7232
  header = before;
7231
7233
  footer = after;
7232
7234
  return origin;
7233
- } else if (isEntryPage(path7, entryPages)) {
7234
- log(`\u5904\u7406 ${path7} ...`, logLevel);
7235
+ } else if (isEntryPage(path8, entryPages)) {
7236
+ log(`\u5904\u7406 ${path8} ...`, logLevel);
7235
7237
  code = combineCode(code, header, footer);
7236
7238
  }
7237
7239
  return code;
7238
7240
  }
7239
7241
 
7242
+ // src/util/pagemeta.ts
7243
+ var import_node_path4 = __toESM(require("path"), 1);
7244
+
7245
+ // src/util/constant.ts
7246
+ var PAGE_META = "$$__PageMeta";
7247
+
7248
+ // src/util/pagemeta.ts
7249
+ var scriptRegex = /<script\b[^>]*>/;
7250
+ function injectPageMeta(code, id, pageMap2) {
7251
+ const ext = import_node_path4.default.extname(id);
7252
+ const key = id.replace(new RegExp(`${ext}$`), "");
7253
+ const injection = `
7254
+ var __g =
7255
+ typeof globalThis !== 'undefined' ? globalThis :
7256
+ typeof self !== 'undefined' ? self :
7257
+ typeof window !== 'undefined' ? window :
7258
+ typeof global !== 'undefined' ? global :
7259
+ Function('return this')();
7260
+
7261
+ __g[Symbol.for('${PAGE_META}')] = ${JSON.stringify(pageMap2.get(key))}
7262
+ `;
7263
+ return code.replace(scriptRegex, (match) => match + injection);
7264
+ }
7265
+
7240
7266
  // src/index.ts
7241
7267
  var NAME = "unplugin-mp-router-view";
7242
7268
  var unpluginFactory = (options) => {
7243
7269
  var _a2, _b2;
7244
7270
  const log2 = (_a2 = options == null ? void 0 : options.log) != null ? _a2 : "error";
7245
- const entryPages = getPages((_b2 = options == null ? void 0 : options.collect) != null ? _b2 : true);
7271
+ const { entryList: entryPages, pageMap: pageMap2 } = getPages((_b2 = options == null ? void 0 : options.collect) != null ? _b2 : true);
7246
7272
  return {
7247
7273
  name: NAME,
7248
7274
  enforce: "pre",
7249
7275
  transformInclude(id) {
7250
- const _id = import_node_path4.default.resolve(id);
7276
+ const _id = import_node_path5.default.resolve(id);
7251
7277
  return isAppVue(_id) || isEntryPage(_id, entryPages);
7252
7278
  },
7253
7279
  transform(code, id) {
7254
- const res = transform(import_node_path4.default.resolve(id), code, entryPages, log2);
7280
+ let res = transform(import_node_path5.default.resolve(id), code, entryPages, log2);
7281
+ if (options == null ? void 0 : options.inject) {
7282
+ res = injectPageMeta(code, id, pageMap2);
7283
+ }
7255
7284
  return res;
7256
7285
  }
7257
7286
  };
package/dist/rollup.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-UB2NVQE4.js";
3
+ } from "./chunk-34IBKFGI.js";
4
+ import "./chunk-M3QQKYK3.js";
4
5
 
5
6
  // src/rollup.ts
6
7
  import { createRollupPlugin } from "unplugin";
package/dist/type.d.d.ts CHANGED
@@ -33,4 +33,4 @@ interface PageJSON {
33
33
  globalStyle: Record<string, string>
34
34
  }
35
35
 
36
- export { PageJSON };
36
+ export { Page, PageJSON };
package/dist/types.d.ts CHANGED
@@ -3,6 +3,10 @@ type Level = 'error' | 'log' | undefined | boolean;
3
3
  interface Options {
4
4
  log?: Level;
5
5
  collect?: string | boolean | ((path: string) => boolean);
6
+ /**
7
+ * 是否向每一个路由页面注入该路由的元数据
8
+ */
9
+ inject?: boolean;
6
10
  }
7
11
 
8
12
  export { Options };
package/dist/vite.cjs CHANGED
@@ -67,7 +67,7 @@ module.exports = __toCommonJS(vite_exports);
67
67
  var import_unplugin2 = require("unplugin");
68
68
 
69
69
  // src/index.ts
70
- var import_node_path4 = __toESM(require("path"), 1);
70
+ var import_node_path5 = __toESM(require("path"), 1);
71
71
  var import_unplugin = require("unplugin");
72
72
 
73
73
  // src/util/parse.ts
@@ -5591,17 +5591,17 @@ function withTrailingSlash(input = "", respectQueryAndFragment) {
5591
5591
  if (hasTrailingSlash(input, true)) {
5592
5592
  return input || "/";
5593
5593
  }
5594
- let path7 = input;
5594
+ let path8 = input;
5595
5595
  let fragment = "";
5596
5596
  const fragmentIndex = input.indexOf("#");
5597
5597
  if (fragmentIndex >= 0) {
5598
- path7 = input.slice(0, fragmentIndex);
5598
+ path8 = input.slice(0, fragmentIndex);
5599
5599
  fragment = input.slice(fragmentIndex);
5600
- if (!path7) {
5600
+ if (!path8) {
5601
5601
  return fragment;
5602
5602
  }
5603
5603
  }
5604
- const [s0, ...s] = path7.split("?");
5604
+ const [s0, ...s] = path8.split("?");
5605
5605
  return s0 + "/" + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
5606
5606
  }
5607
5607
  function isNonEmptyURL(url) {
@@ -5635,8 +5635,8 @@ var import_node_path = __toESM(require("path"), 1);
5635
5635
  var import_node_v8 = __toESM(require("v8"), 1);
5636
5636
  var import_node_util = require("util");
5637
5637
  var BUILTIN_MODULES = new Set(import_node_module.builtinModules);
5638
- function normalizeSlash(path7) {
5639
- return path7.replace(/\\/g, "/");
5638
+ function normalizeSlash(path8) {
5639
+ return path8.replace(/\\/g, "/");
5640
5640
  }
5641
5641
  var own$1 = {}.hasOwnProperty;
5642
5642
  var classRegExp = /^([A-Z][a-z\d]*)+$/;
@@ -5752,8 +5752,8 @@ codes.ERR_INVALID_PACKAGE_CONFIG = createError(
5752
5752
  * @param {string} [base]
5753
5753
  * @param {string} [message]
5754
5754
  */
5755
- (path7, base, message) => {
5756
- return `Invalid package config ${path7}${base ? ` while importing ${base}` : ""}${message ? `. ${message}` : ""}`;
5755
+ (path8, base, message) => {
5756
+ return `Invalid package config ${path8}${base ? ` while importing ${base}` : ""}${message ? `. ${message}` : ""}`;
5757
5757
  },
5758
5758
  Error
5759
5759
  );
@@ -5785,8 +5785,8 @@ codes.ERR_MODULE_NOT_FOUND = createError(
5785
5785
  * @param {string} base
5786
5786
  * @param {boolean} [exactUrl]
5787
5787
  */
5788
- (path7, base, exactUrl = false) => {
5789
- return `Cannot find ${exactUrl ? "module" : "package"} '${path7}' imported from ${base}`;
5788
+ (path8, base, exactUrl = false) => {
5789
+ return `Cannot find ${exactUrl ? "module" : "package"} '${path8}' imported from ${base}`;
5790
5790
  },
5791
5791
  Error
5792
5792
  );
@@ -5837,8 +5837,8 @@ codes.ERR_UNKNOWN_FILE_EXTENSION = createError(
5837
5837
  * @param {string} extension
5838
5838
  * @param {string} path
5839
5839
  */
5840
- (extension, path7) => {
5841
- return `Unknown file extension "${extension}" for ${path7}`;
5840
+ (extension, path8) => {
5841
+ return `Unknown file extension "${extension}" for ${path8}`;
5842
5842
  },
5843
5843
  TypeError
5844
5844
  );
@@ -6218,9 +6218,9 @@ Default "index" lookups for the main are deprecated for ES modules.`,
6218
6218
  );
6219
6219
  }
6220
6220
  }
6221
- function tryStatSync(path7) {
6221
+ function tryStatSync(path8) {
6222
6222
  try {
6223
- return (0, import_node_fs.statSync)(path7);
6223
+ return (0, import_node_fs.statSync)(path8);
6224
6224
  } catch (e) {
6225
6225
  }
6226
6226
  }
@@ -7075,10 +7075,10 @@ _head = new WeakMap();
7075
7075
  _tail = new WeakMap();
7076
7076
  _size = new WeakMap();
7077
7077
  var findUpStop = Symbol("findUpStop");
7078
- function _resolve2(path7, options = {}) {
7078
+ function _resolve2(path8, options = {}) {
7079
7079
  if (options.platform === "auto" || !options.platform)
7080
7080
  options.platform = import_node_process2.default.platform === "win32" ? "win32" : "posix";
7081
- const modulePath = resolvePathSync(path7, {
7081
+ const modulePath = resolvePathSync(path8, {
7082
7082
  url: options.paths
7083
7083
  });
7084
7084
  if (options.platform === "win32")
@@ -7179,12 +7179,13 @@ function addToHeader(code, header2) {
7179
7179
  function addToFooter(code, footer2) {
7180
7180
  return code.replace(/(\s*)(<\/template>)(?!(([\s\S]*)(<\/template>)))/, (p) => footer2 + p);
7181
7181
  }
7182
+ var pageMap = /* @__PURE__ */ new Map();
7182
7183
  function getPages(collectMode) {
7183
7184
  const jsonStr = import_node_fs3.default.readFileSync(import_node_path3.default.resolve(INPUT_DIR, "pages.json"), "utf-8");
7184
7185
  const pagesJson = parseJson(jsonStr);
7185
7186
  const { pages, subPackages = [] } = pagesJson;
7186
7187
  const entryList = [...collectEntry(collectMode, pages), ...collectEntry(collectMode, subPackages)];
7187
- return entryList;
7188
+ return { entryList, pageMap };
7188
7189
  }
7189
7190
  function collectEntry(collectMode, json, root = "") {
7190
7191
  const entryList = [];
@@ -7197,6 +7198,7 @@ function collectEntry(collectMode, json, root = "") {
7197
7198
  return;
7198
7199
  }
7199
7200
  const _path = import_node_path3.default.resolve(INPUT_DIR, `${root}${root ? "/" : ""}${item.path}`);
7201
+ pageMap.set(_path, item);
7200
7202
  if (collectMode && typeof collectMode === "boolean" && !item["ROUTER_VIEW_EXCLUDE"]) {
7201
7203
  entryList.push(_path);
7202
7204
  } else if (typeof collectMode === "string" && item[collectMode]) {
@@ -7216,9 +7218,9 @@ function combineCode(code, header2, footer2) {
7216
7218
  code = addToFooter(code, footer2);
7217
7219
  return code;
7218
7220
  }
7219
- function transform(path7, code, entryPages, level) {
7221
+ function transform(path8, code, entryPages, level) {
7220
7222
  logLevel = level;
7221
- if (isAppVue(path7)) {
7223
+ if (isAppVue(path8)) {
7222
7224
  log(`\u57FA\u4E8E\u5E73\u53F0${process.env.UNI_PLATFORM}, \u5904\u7406App.vue...`, logLevel);
7223
7225
  const { origin, tpl } = getTemplate(code);
7224
7226
  const { count, before, after } = countRouterView(tpl);
@@ -7230,28 +7232,55 @@ function transform(path7, code, entryPages, level) {
7230
7232
  header = before;
7231
7233
  footer = after;
7232
7234
  return origin;
7233
- } else if (isEntryPage(path7, entryPages)) {
7234
- log(`\u5904\u7406 ${path7} ...`, logLevel);
7235
+ } else if (isEntryPage(path8, entryPages)) {
7236
+ log(`\u5904\u7406 ${path8} ...`, logLevel);
7235
7237
  code = combineCode(code, header, footer);
7236
7238
  }
7237
7239
  return code;
7238
7240
  }
7239
7241
 
7242
+ // src/util/pagemeta.ts
7243
+ var import_node_path4 = __toESM(require("path"), 1);
7244
+
7245
+ // src/util/constant.ts
7246
+ var PAGE_META = "$$__PageMeta";
7247
+
7248
+ // src/util/pagemeta.ts
7249
+ var scriptRegex = /<script\b[^>]*>/;
7250
+ function injectPageMeta(code, id, pageMap2) {
7251
+ const ext = import_node_path4.default.extname(id);
7252
+ const key = id.replace(new RegExp(`${ext}$`), "");
7253
+ const injection = `
7254
+ var __g =
7255
+ typeof globalThis !== 'undefined' ? globalThis :
7256
+ typeof self !== 'undefined' ? self :
7257
+ typeof window !== 'undefined' ? window :
7258
+ typeof global !== 'undefined' ? global :
7259
+ Function('return this')();
7260
+
7261
+ __g[Symbol.for('${PAGE_META}')] = ${JSON.stringify(pageMap2.get(key))}
7262
+ `;
7263
+ return code.replace(scriptRegex, (match) => match + injection);
7264
+ }
7265
+
7240
7266
  // src/index.ts
7241
7267
  var NAME = "unplugin-mp-router-view";
7242
7268
  var unpluginFactory = (options) => {
7243
7269
  var _a2, _b2;
7244
7270
  const log2 = (_a2 = options == null ? void 0 : options.log) != null ? _a2 : "error";
7245
- const entryPages = getPages((_b2 = options == null ? void 0 : options.collect) != null ? _b2 : true);
7271
+ const { entryList: entryPages, pageMap: pageMap2 } = getPages((_b2 = options == null ? void 0 : options.collect) != null ? _b2 : true);
7246
7272
  return {
7247
7273
  name: NAME,
7248
7274
  enforce: "pre",
7249
7275
  transformInclude(id) {
7250
- const _id = import_node_path4.default.resolve(id);
7276
+ const _id = import_node_path5.default.resolve(id);
7251
7277
  return isAppVue(_id) || isEntryPage(_id, entryPages);
7252
7278
  },
7253
7279
  transform(code, id) {
7254
- const res = transform(import_node_path4.default.resolve(id), code, entryPages, log2);
7280
+ let res = transform(import_node_path5.default.resolve(id), code, entryPages, log2);
7281
+ if (options == null ? void 0 : options.inject) {
7282
+ res = injectPageMeta(code, id, pageMap2);
7283
+ }
7255
7284
  return res;
7256
7285
  }
7257
7286
  };
package/dist/vite.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-UB2NVQE4.js";
3
+ } from "./chunk-34IBKFGI.js";
4
+ import "./chunk-M3QQKYK3.js";
4
5
 
5
6
  // src/vite.ts
6
7
  import { createVitePlugin } from "unplugin";
package/dist/webpack.cjs CHANGED
@@ -67,7 +67,7 @@ module.exports = __toCommonJS(webpack_exports);
67
67
  var import_unplugin2 = require("unplugin");
68
68
 
69
69
  // src/index.ts
70
- var import_node_path4 = __toESM(require("path"), 1);
70
+ var import_node_path5 = __toESM(require("path"), 1);
71
71
  var import_unplugin = require("unplugin");
72
72
 
73
73
  // src/util/parse.ts
@@ -5591,17 +5591,17 @@ function withTrailingSlash(input = "", respectQueryAndFragment) {
5591
5591
  if (hasTrailingSlash(input, true)) {
5592
5592
  return input || "/";
5593
5593
  }
5594
- let path7 = input;
5594
+ let path8 = input;
5595
5595
  let fragment = "";
5596
5596
  const fragmentIndex = input.indexOf("#");
5597
5597
  if (fragmentIndex >= 0) {
5598
- path7 = input.slice(0, fragmentIndex);
5598
+ path8 = input.slice(0, fragmentIndex);
5599
5599
  fragment = input.slice(fragmentIndex);
5600
- if (!path7) {
5600
+ if (!path8) {
5601
5601
  return fragment;
5602
5602
  }
5603
5603
  }
5604
- const [s0, ...s] = path7.split("?");
5604
+ const [s0, ...s] = path8.split("?");
5605
5605
  return s0 + "/" + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
5606
5606
  }
5607
5607
  function isNonEmptyURL(url) {
@@ -5635,8 +5635,8 @@ var import_node_path = __toESM(require("path"), 1);
5635
5635
  var import_node_v8 = __toESM(require("v8"), 1);
5636
5636
  var import_node_util = require("util");
5637
5637
  var BUILTIN_MODULES = new Set(import_node_module.builtinModules);
5638
- function normalizeSlash(path7) {
5639
- return path7.replace(/\\/g, "/");
5638
+ function normalizeSlash(path8) {
5639
+ return path8.replace(/\\/g, "/");
5640
5640
  }
5641
5641
  var own$1 = {}.hasOwnProperty;
5642
5642
  var classRegExp = /^([A-Z][a-z\d]*)+$/;
@@ -5752,8 +5752,8 @@ codes.ERR_INVALID_PACKAGE_CONFIG = createError(
5752
5752
  * @param {string} [base]
5753
5753
  * @param {string} [message]
5754
5754
  */
5755
- (path7, base, message) => {
5756
- return `Invalid package config ${path7}${base ? ` while importing ${base}` : ""}${message ? `. ${message}` : ""}`;
5755
+ (path8, base, message) => {
5756
+ return `Invalid package config ${path8}${base ? ` while importing ${base}` : ""}${message ? `. ${message}` : ""}`;
5757
5757
  },
5758
5758
  Error
5759
5759
  );
@@ -5785,8 +5785,8 @@ codes.ERR_MODULE_NOT_FOUND = createError(
5785
5785
  * @param {string} base
5786
5786
  * @param {boolean} [exactUrl]
5787
5787
  */
5788
- (path7, base, exactUrl = false) => {
5789
- return `Cannot find ${exactUrl ? "module" : "package"} '${path7}' imported from ${base}`;
5788
+ (path8, base, exactUrl = false) => {
5789
+ return `Cannot find ${exactUrl ? "module" : "package"} '${path8}' imported from ${base}`;
5790
5790
  },
5791
5791
  Error
5792
5792
  );
@@ -5837,8 +5837,8 @@ codes.ERR_UNKNOWN_FILE_EXTENSION = createError(
5837
5837
  * @param {string} extension
5838
5838
  * @param {string} path
5839
5839
  */
5840
- (extension, path7) => {
5841
- return `Unknown file extension "${extension}" for ${path7}`;
5840
+ (extension, path8) => {
5841
+ return `Unknown file extension "${extension}" for ${path8}`;
5842
5842
  },
5843
5843
  TypeError
5844
5844
  );
@@ -6218,9 +6218,9 @@ Default "index" lookups for the main are deprecated for ES modules.`,
6218
6218
  );
6219
6219
  }
6220
6220
  }
6221
- function tryStatSync(path7) {
6221
+ function tryStatSync(path8) {
6222
6222
  try {
6223
- return (0, import_node_fs.statSync)(path7);
6223
+ return (0, import_node_fs.statSync)(path8);
6224
6224
  } catch (e) {
6225
6225
  }
6226
6226
  }
@@ -7075,10 +7075,10 @@ _head = new WeakMap();
7075
7075
  _tail = new WeakMap();
7076
7076
  _size = new WeakMap();
7077
7077
  var findUpStop = Symbol("findUpStop");
7078
- function _resolve2(path7, options = {}) {
7078
+ function _resolve2(path8, options = {}) {
7079
7079
  if (options.platform === "auto" || !options.platform)
7080
7080
  options.platform = import_node_process2.default.platform === "win32" ? "win32" : "posix";
7081
- const modulePath = resolvePathSync(path7, {
7081
+ const modulePath = resolvePathSync(path8, {
7082
7082
  url: options.paths
7083
7083
  });
7084
7084
  if (options.platform === "win32")
@@ -7179,12 +7179,13 @@ function addToHeader(code, header2) {
7179
7179
  function addToFooter(code, footer2) {
7180
7180
  return code.replace(/(\s*)(<\/template>)(?!(([\s\S]*)(<\/template>)))/, (p) => footer2 + p);
7181
7181
  }
7182
+ var pageMap = /* @__PURE__ */ new Map();
7182
7183
  function getPages(collectMode) {
7183
7184
  const jsonStr = import_node_fs3.default.readFileSync(import_node_path3.default.resolve(INPUT_DIR, "pages.json"), "utf-8");
7184
7185
  const pagesJson = parseJson(jsonStr);
7185
7186
  const { pages, subPackages = [] } = pagesJson;
7186
7187
  const entryList = [...collectEntry(collectMode, pages), ...collectEntry(collectMode, subPackages)];
7187
- return entryList;
7188
+ return { entryList, pageMap };
7188
7189
  }
7189
7190
  function collectEntry(collectMode, json, root = "") {
7190
7191
  const entryList = [];
@@ -7197,6 +7198,7 @@ function collectEntry(collectMode, json, root = "") {
7197
7198
  return;
7198
7199
  }
7199
7200
  const _path = import_node_path3.default.resolve(INPUT_DIR, `${root}${root ? "/" : ""}${item.path}`);
7201
+ pageMap.set(_path, item);
7200
7202
  if (collectMode && typeof collectMode === "boolean" && !item["ROUTER_VIEW_EXCLUDE"]) {
7201
7203
  entryList.push(_path);
7202
7204
  } else if (typeof collectMode === "string" && item[collectMode]) {
@@ -7216,9 +7218,9 @@ function combineCode(code, header2, footer2) {
7216
7218
  code = addToFooter(code, footer2);
7217
7219
  return code;
7218
7220
  }
7219
- function transform(path7, code, entryPages, level) {
7221
+ function transform(path8, code, entryPages, level) {
7220
7222
  logLevel = level;
7221
- if (isAppVue(path7)) {
7223
+ if (isAppVue(path8)) {
7222
7224
  log(`\u57FA\u4E8E\u5E73\u53F0${process.env.UNI_PLATFORM}, \u5904\u7406App.vue...`, logLevel);
7223
7225
  const { origin, tpl } = getTemplate(code);
7224
7226
  const { count, before, after } = countRouterView(tpl);
@@ -7230,28 +7232,55 @@ function transform(path7, code, entryPages, level) {
7230
7232
  header = before;
7231
7233
  footer = after;
7232
7234
  return origin;
7233
- } else if (isEntryPage(path7, entryPages)) {
7234
- log(`\u5904\u7406 ${path7} ...`, logLevel);
7235
+ } else if (isEntryPage(path8, entryPages)) {
7236
+ log(`\u5904\u7406 ${path8} ...`, logLevel);
7235
7237
  code = combineCode(code, header, footer);
7236
7238
  }
7237
7239
  return code;
7238
7240
  }
7239
7241
 
7242
+ // src/util/pagemeta.ts
7243
+ var import_node_path4 = __toESM(require("path"), 1);
7244
+
7245
+ // src/util/constant.ts
7246
+ var PAGE_META = "$$__PageMeta";
7247
+
7248
+ // src/util/pagemeta.ts
7249
+ var scriptRegex = /<script\b[^>]*>/;
7250
+ function injectPageMeta(code, id, pageMap2) {
7251
+ const ext = import_node_path4.default.extname(id);
7252
+ const key = id.replace(new RegExp(`${ext}$`), "");
7253
+ const injection = `
7254
+ var __g =
7255
+ typeof globalThis !== 'undefined' ? globalThis :
7256
+ typeof self !== 'undefined' ? self :
7257
+ typeof window !== 'undefined' ? window :
7258
+ typeof global !== 'undefined' ? global :
7259
+ Function('return this')();
7260
+
7261
+ __g[Symbol.for('${PAGE_META}')] = ${JSON.stringify(pageMap2.get(key))}
7262
+ `;
7263
+ return code.replace(scriptRegex, (match) => match + injection);
7264
+ }
7265
+
7240
7266
  // src/index.ts
7241
7267
  var NAME = "unplugin-mp-router-view";
7242
7268
  var unpluginFactory = (options) => {
7243
7269
  var _a2, _b2;
7244
7270
  const log2 = (_a2 = options == null ? void 0 : options.log) != null ? _a2 : "error";
7245
- const entryPages = getPages((_b2 = options == null ? void 0 : options.collect) != null ? _b2 : true);
7271
+ const { entryList: entryPages, pageMap: pageMap2 } = getPages((_b2 = options == null ? void 0 : options.collect) != null ? _b2 : true);
7246
7272
  return {
7247
7273
  name: NAME,
7248
7274
  enforce: "pre",
7249
7275
  transformInclude(id) {
7250
- const _id = import_node_path4.default.resolve(id);
7276
+ const _id = import_node_path5.default.resolve(id);
7251
7277
  return isAppVue(_id) || isEntryPage(_id, entryPages);
7252
7278
  },
7253
7279
  transform(code, id) {
7254
- const res = transform(import_node_path4.default.resolve(id), code, entryPages, log2);
7280
+ let res = transform(import_node_path5.default.resolve(id), code, entryPages, log2);
7281
+ if (options == null ? void 0 : options.inject) {
7282
+ res = injectPageMeta(code, id, pageMap2);
7283
+ }
7255
7284
  return res;
7256
7285
  }
7257
7286
  };
package/dist/webpack.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-UB2NVQE4.js";
3
+ } from "./chunk-34IBKFGI.js";
4
+ import "./chunk-M3QQKYK3.js";
4
5
 
5
6
  // src/webpack.ts
6
7
  import { createWebpackPlugin } from "unplugin";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sepveneto/plugin-mp-router-view",
3
3
  "type": "module",
4
- "version": "0.4.4",
4
+ "version": "0.5.0",
5
5
  "description": "Register global imports on demand for Vite and Webpack",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/SepVeneto/mp-plugin#readme",
@@ -25,6 +25,11 @@
25
25
  "require": "./dist/webpack.cjs",
26
26
  "import": "./dist/webpack.js"
27
27
  },
28
+ "./helper": {
29
+ "types": "./dist/helper/index.d.ts",
30
+ "require": "./dist/helper/index.cjs",
31
+ "import": "./dist/helper/index.js"
32
+ },
28
33
  "./loader": {
29
34
  "types": "./dist/loader.d.ts",
30
35
  "require": "./dist/loader.cjs",