@dcloudio/uni-cli-shared 3.0.0-alpha-3021320211109002 → 3.0.0-alpha-3021320211115001

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.
Files changed (107) hide show
  1. package/dist/constants.d.ts +3 -0
  2. package/dist/constants.js +4 -1
  3. package/dist/easycom.d.ts +7 -1
  4. package/dist/easycom.js +25 -6
  5. package/dist/env/define.d.ts +3 -0
  6. package/dist/env/define.js +6 -0
  7. package/dist/exports.d.ts +1 -0
  8. package/dist/exports.js +8 -0
  9. package/dist/filter.d.ts +16 -0
  10. package/dist/filter.js +60 -0
  11. package/dist/hbx/alias.js +9 -1
  12. package/dist/hbx/env.js +4 -1
  13. package/dist/hbx/index.js +0 -7
  14. package/dist/hbx/log.d.ts +2 -0
  15. package/dist/hbx/log.js +36 -2
  16. package/dist/index.d.ts +4 -2
  17. package/dist/index.js +4 -2
  18. package/dist/json/app/manifest/confusion.d.ts +3 -0
  19. package/dist/json/app/manifest/confusion.js +56 -14
  20. package/dist/json/app/manifest/env.d.ts +1 -1
  21. package/dist/json/app/manifest/index.d.ts +1 -0
  22. package/dist/json/app/manifest/index.js +5 -1
  23. package/dist/json/app/manifest/nvue.d.ts +3 -3
  24. package/dist/json/app/pages/index.d.ts +0 -1
  25. package/dist/json/app/pages/index.js +0 -11
  26. package/dist/json/manifest.d.ts +1 -1
  27. package/dist/json/manifest.js +9 -3
  28. package/dist/json/mp/index.d.ts +1 -0
  29. package/dist/json/mp/index.js +11 -0
  30. package/dist/json/mp/jsonFile.d.ts +8 -6
  31. package/dist/json/mp/jsonFile.js +68 -16
  32. package/dist/json/mp/pages.d.ts +1 -0
  33. package/dist/json/mp/pages.js +17 -3
  34. package/dist/json/mp/types.d.ts +1 -0
  35. package/dist/json/pages.js +0 -4
  36. package/dist/logs/console.js +5 -2
  37. package/dist/logs/format.js +6 -0
  38. package/dist/messages/index.d.ts +3 -0
  39. package/dist/messages/index.js +3 -0
  40. package/dist/mp/constants.d.ts +2 -0
  41. package/dist/mp/constants.js +5 -0
  42. package/dist/mp/event.d.ts +2 -1
  43. package/dist/mp/event.js +18 -3
  44. package/dist/mp/imports.d.ts +5 -0
  45. package/dist/mp/imports.js +63 -0
  46. package/dist/mp/index.d.ts +5 -0
  47. package/dist/mp/index.js +7 -0
  48. package/dist/mp/nvue.d.ts +1 -0
  49. package/dist/mp/nvue.js +18 -0
  50. package/dist/mp/style.d.ts +1 -0
  51. package/dist/mp/style.js +9 -0
  52. package/dist/mp/template.d.ts +55 -0
  53. package/dist/mp/template.js +73 -0
  54. package/dist/mp/transformImports.d.ts +14 -0
  55. package/dist/mp/transformImports.js +175 -0
  56. package/dist/postcss/plugins/stylePluginScoped.d.ts +1 -1
  57. package/dist/postcss/plugins/uniapp.d.ts +1 -1
  58. package/dist/postcss/plugins/uniapp.js +51 -21
  59. package/dist/preprocess/context.js +1 -0
  60. package/dist/utils.d.ts +9 -1
  61. package/dist/utils.js +93 -8
  62. package/dist/vite/features.d.ts +1 -1
  63. package/dist/vite/index.d.ts +2 -3
  64. package/dist/vite/plugins/copy.d.ts +1 -1
  65. package/dist/vite/plugins/cssScoped.d.ts +1 -1
  66. package/dist/vite/plugins/inject.d.ts +1 -1
  67. package/dist/vite/plugins/inject.js +2 -2
  68. package/dist/vite/plugins/vitejs/config.d.ts +1 -1
  69. package/dist/vite/plugins/vitejs/index.d.ts +1 -1
  70. package/dist/vite/plugins/vitejs/plugin.d.ts +1 -1
  71. package/dist/vite/plugins/vitejs/plugins/asset.d.ts +1 -1
  72. package/dist/vite/plugins/vitejs/plugins/asset.js +28 -21
  73. package/dist/vite/plugins/vitejs/plugins/css.d.ts +4 -4
  74. package/dist/vite/plugins/vitejs/plugins/css.js +15 -24
  75. package/dist/vite/plugins/vitejs/server/moduleGraph.d.ts +1 -1
  76. package/dist/vite/plugins/vitejs/server/moduleGraph.js +0 -3
  77. package/dist/vite/utils/ast.d.ts +1 -1
  78. package/dist/vite/utils/index.d.ts +1 -1
  79. package/dist/vite/utils/plugin.d.ts +4 -4
  80. package/dist/vite/utils/plugin.js +2 -2
  81. package/dist/vue/index.d.ts +2 -0
  82. package/dist/vue/index.js +14 -0
  83. package/dist/vue/transforms/index.d.ts +10 -0
  84. package/dist/vue/transforms/index.js +36 -0
  85. package/dist/vue/transforms/transformComponent.d.ts +3 -0
  86. package/dist/vue/transforms/transformComponent.js +26 -0
  87. package/dist/vue/transforms/transformEvent.d.ts +2 -0
  88. package/dist/vue/transforms/transformEvent.js +22 -0
  89. package/dist/vue/transforms/transformPageHead.d.ts +2 -0
  90. package/dist/vue/transforms/transformPageHead.js +11 -0
  91. package/dist/vue/transforms/transformRef.d.ts +2 -0
  92. package/dist/vue/transforms/transformRef.js +32 -0
  93. package/dist/vue/transforms/transformTag.d.ts +2 -0
  94. package/dist/vue/transforms/transformTag.js +18 -0
  95. package/dist/vue/transforms/vModel.d.ts +12 -0
  96. package/dist/vue/transforms/vModel.js +33 -0
  97. package/dist/vue/transforms/vOn.d.ts +15 -0
  98. package/dist/vue/transforms/vOn.js +88 -0
  99. package/dist/vue/utils.d.ts +9 -0
  100. package/dist/vue/utils.js +66 -0
  101. package/lib/nvue.css +34 -0
  102. package/package.json +27 -6
  103. package/LICENSE +0 -202
  104. package/dist/json/app/pages/nvue.d.ts +0 -1
  105. package/dist/json/app/pages/nvue.js +0 -88
  106. package/dist/renderjs.d.ts +0 -16
  107. package/dist/renderjs.js +0 -44
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.transformVueComponentImports = void 0;
7
+ const parser_1 = require("@babel/parser");
8
+ const types_1 = require("@babel/types");
9
+ const shared_1 = require("@vue/shared");
10
+ const estree_walker_1 = require("estree-walker");
11
+ const magic_string_1 = __importDefault(require("magic-string"));
12
+ const messages_1 = require("../messages");
13
+ const constants_1 = require("../constants");
14
+ const utils_1 = require("../utils");
15
+ async function transformVueComponentImports(code, importer, { root, resolve, global, dynamicImport, babelParserPlugins, }) {
16
+ if (!global && !code.includes(constants_1.BINDING_COMPONENTS)) {
17
+ return { code, usingComponents: {} };
18
+ }
19
+ const s = new magic_string_1.default(code);
20
+ const scriptAst = (0, parser_1.parse)(code, {
21
+ plugins: [...(babelParserPlugins || [])],
22
+ sourceType: 'module',
23
+ }).program;
24
+ const imports = findVueComponentImports(scriptAst.body, global
25
+ ? parseGlobalComponents(scriptAst)
26
+ : parseComponents(scriptAst, findBindingComponents(scriptAst.body)));
27
+ const usingComponents = {};
28
+ for (let i = 0; i < imports.length; i++) {
29
+ const { tag, import: { start, end, specifiers: [specifier], source, }, } = imports[i];
30
+ const resolveId = await resolve(source.value, importer);
31
+ if (resolveId) {
32
+ s.overwrite(start, end, dynamicImport(specifier.local.name, resolveId.id) + ';');
33
+ const componentName = (0, shared_1.hyphenate)(tag);
34
+ if (!usingComponents[componentName]) {
35
+ usingComponents[componentName] =
36
+ '/' + (0, utils_1.removeExt)((0, utils_1.normalizeMiniProgramFilename)(resolveId.id, root));
37
+ }
38
+ }
39
+ }
40
+ return { code: s.toString(), usingComponents };
41
+ }
42
+ exports.transformVueComponentImports = transformVueComponentImports;
43
+ /**
44
+ * 解析编译器生成的 bindingComponents
45
+ * @param ast
46
+ * @returns
47
+ */
48
+ function findBindingComponents(ast) {
49
+ for (const node of ast) {
50
+ if (!(0, types_1.isVariableDeclaration)(node)) {
51
+ continue;
52
+ }
53
+ const declarator = node.declarations[0];
54
+ if ((0, types_1.isIdentifier)(declarator.id) &&
55
+ declarator.id.name === constants_1.BINDING_COMPONENTS) {
56
+ const bindingComponents = JSON.parse(declarator.init.value);
57
+ return Object.keys(bindingComponents).reduce((bindings, tag) => {
58
+ const binding = bindingComponents[tag];
59
+ bindings[binding.name] = {
60
+ tag,
61
+ type: binding.type,
62
+ };
63
+ return bindings;
64
+ }, {});
65
+ }
66
+ }
67
+ return {};
68
+ }
69
+ /**
70
+ * 查找全局组件定义:app.component('component-a',{})
71
+ * @param ast
72
+ * @returns
73
+ */
74
+ function parseGlobalComponents(ast) {
75
+ const bindingComponents = {};
76
+ estree_walker_1.walk(ast, {
77
+ enter(child) {
78
+ if (!(0, types_1.isCallExpression)(child)) {
79
+ return;
80
+ }
81
+ const { callee } = child;
82
+ // .component
83
+ if (!(0, types_1.isMemberExpression)(callee) ||
84
+ !(0, types_1.isIdentifier)(callee.property) ||
85
+ callee.property.name !== 'component') {
86
+ return;
87
+ }
88
+ // .component('component-a',{})
89
+ const args = child.arguments;
90
+ if (args.length !== 2) {
91
+ return;
92
+ }
93
+ const [name, value] = args;
94
+ if (!(0, types_1.isStringLiteral)(name)) {
95
+ return console.warn(messages_1.M['mp.component.args[0]']);
96
+ }
97
+ if (!(0, types_1.isIdentifier)(value)) {
98
+ return console.warn(messages_1.M['mp.component.args[1]']);
99
+ }
100
+ bindingComponents[value.name] = {
101
+ tag: name.value,
102
+ type: 'unknown',
103
+ };
104
+ },
105
+ });
106
+ return bindingComponents;
107
+ }
108
+ /**
109
+ * 从 components 中查找定义的组件,修改 bindingComponents
110
+ * @param ast
111
+ * @param bindingComponents
112
+ */
113
+ function parseComponents(ast, bindingComponents) {
114
+ ;
115
+ estree_walker_1.walk(ast, {
116
+ enter(child) {
117
+ if (!(0, types_1.isObjectExpression)(child)) {
118
+ return;
119
+ }
120
+ const componentsProp = child.properties.find((prop) => (0, types_1.isObjectProperty)(prop) &&
121
+ (0, types_1.isIdentifier)(prop.key) &&
122
+ prop.key.name === 'components');
123
+ if (!componentsProp) {
124
+ return;
125
+ }
126
+ const componentsExpr = componentsProp.value;
127
+ if (!(0, types_1.isObjectExpression)(componentsExpr)) {
128
+ return;
129
+ }
130
+ componentsExpr.properties.forEach((prop) => {
131
+ if (!(0, types_1.isObjectProperty)(prop)) {
132
+ return;
133
+ }
134
+ if (!(0, types_1.isIdentifier)(prop.key) && !(0, types_1.isStringLiteral)(prop.key)) {
135
+ return;
136
+ }
137
+ if (!(0, types_1.isIdentifier)(prop.value)) {
138
+ return;
139
+ }
140
+ const tag = (0, types_1.isIdentifier)(prop.key) ? prop.key.name : prop.key.value;
141
+ const name = findBindingComponent(tag, bindingComponents);
142
+ if (name) {
143
+ bindingComponents[prop.value.name] = bindingComponents[name];
144
+ }
145
+ });
146
+ },
147
+ });
148
+ return bindingComponents;
149
+ }
150
+ function findBindingComponent(tag, bindingComponents) {
151
+ return Object.keys(bindingComponents).find((name) => {
152
+ const componentTag = bindingComponents[name].tag;
153
+ const camelName = (0, shared_1.camelize)(componentTag);
154
+ const PascalName = (0, shared_1.capitalize)(camelName);
155
+ return tag === componentTag || tag === camelName || tag === PascalName;
156
+ });
157
+ }
158
+ function findVueComponentImports(ast, bindingComponents) {
159
+ const imports = [];
160
+ for (let i = 0; i < ast.length; i++) {
161
+ const node = ast[i];
162
+ if (!(0, types_1.isImportDeclaration)(node)) {
163
+ continue;
164
+ }
165
+ if (node.specifiers.length !== 1) {
166
+ continue;
167
+ }
168
+ const { name } = node.specifiers[0].local;
169
+ if (!bindingComponents[name]) {
170
+ continue;
171
+ }
172
+ imports.push({ tag: bindingComponents[name].tag, import: node });
173
+ }
174
+ return imports;
175
+ }
@@ -1,3 +1,3 @@
1
- import { PluginCreator } from 'postcss';
1
+ import type { PluginCreator } from 'postcss';
2
2
  declare const scopedPlugin: PluginCreator<string>;
3
3
  export default scopedPlugin;
@@ -1,4 +1,4 @@
1
- import { Plugin } from 'postcss';
1
+ import type { Plugin } from 'postcss';
2
2
  interface UniAppCssProcessorOptions {
3
3
  page?: string;
4
4
  unit?: string;
@@ -4,12 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const shared_1 = require("@vue/shared");
7
- const postcss_1 = require("postcss");
8
7
  const postcss_selector_parser_1 = __importDefault(require("postcss-selector-parser"));
9
8
  const uni_shared_1 = require("@dcloudio/uni-shared");
10
- const defaultUniAppCssProcessorOptions = (0, shared_1.extend)({
11
- page: 'body',
12
- }, uni_shared_1.defaultRpx2Unit);
9
+ const manifest_1 = require("../../json/manifest");
10
+ const defaultUniAppCssProcessorOptions = (0, shared_1.extend)({}, uni_shared_1.defaultRpx2Unit);
13
11
  const BG_PROPS = [
14
12
  'background',
15
13
  'background-clip',
@@ -21,22 +19,14 @@ const BG_PROPS = [
21
19
  'background-size',
22
20
  'background-attachment',
23
21
  ];
24
- function transform(selector, page, state) {
22
+ function transform(selector, state, { rewriteTag }) {
25
23
  if (selector.type !== 'tag') {
26
24
  return;
27
25
  }
28
26
  const { value } = selector;
29
- if ((0, uni_shared_1.isBuiltInComponent)(value)) {
30
- selector.value = uni_shared_1.COMPONENT_SELECTOR_PREFIX + value;
31
- }
32
- else if (value === 'page') {
33
- if (!page) {
34
- return;
35
- }
36
- selector.value = page;
37
- if (page !== 'body') {
38
- state.bg = true;
39
- }
27
+ selector.value = rewriteTag(value);
28
+ if (value === 'page' && selector.value === 'uni-page-body') {
29
+ state.bg = true;
40
30
  }
41
31
  }
42
32
  function createBodyBackgroundRule(origRule) {
@@ -47,13 +37,14 @@ function createBodyBackgroundRule(origRule) {
47
37
  }
48
38
  });
49
39
  if (bgDecls.length) {
50
- origRule.after((0, postcss_1.rule)({ selector: 'body' }).append(bgDecls));
40
+ const { rule } = require('postcss');
41
+ origRule.after(rule({ selector: 'body' }).append(bgDecls));
51
42
  }
52
43
  }
53
- function walkRules(page) {
44
+ function walkRules(options) {
54
45
  return (rule) => {
55
46
  const state = { bg: false };
56
- rule.selector = (0, postcss_selector_parser_1.default)((selectors) => selectors.walk((selector) => transform(selector, page, state))).processSync(rule.selector);
47
+ rule.selector = (0, postcss_selector_parser_1.default)((selectors) => selectors.walk((selector) => transform(selector, state, options))).processSync(rule.selector);
57
48
  state.bg && createBodyBackgroundRule(rule);
58
49
  };
59
50
  }
@@ -66,8 +57,42 @@ function walkDecls(rpx2unit) {
66
57
  decl.value = rpx2unit(decl.value);
67
58
  };
68
59
  }
60
+ const baiduTags = {
61
+ navigator: 'nav',
62
+ };
63
+ function rewriteBaiduTags(tag) {
64
+ return baiduTags[tag] || tag;
65
+ }
66
+ function rewriteUniH5Tags(tag) {
67
+ if (tag === 'page') {
68
+ return 'uni-page-body';
69
+ }
70
+ if ((0, uni_shared_1.isBuiltInComponent)(tag)) {
71
+ return uni_shared_1.COMPONENT_SELECTOR_PREFIX + tag;
72
+ }
73
+ return tag;
74
+ }
75
+ function rewriteUniAppTags(tag) {
76
+ if (tag === 'page') {
77
+ return 'body';
78
+ }
79
+ if ((0, uni_shared_1.isBuiltInComponent)(tag)) {
80
+ return uni_shared_1.COMPONENT_SELECTOR_PREFIX + tag;
81
+ }
82
+ return tag;
83
+ }
84
+ const transforms = {
85
+ h5: rewriteUniH5Tags,
86
+ app: rewriteUniAppTags,
87
+ 'mp-baidu': rewriteBaiduTags,
88
+ };
69
89
  const uniapp = (opts) => {
70
- const { page, unit, unitRatio, unitPrecision } = (0, shared_1.extend)({}, defaultUniAppCssProcessorOptions, opts || {});
90
+ const platform = process.env.UNI_PLATFORM;
91
+ if (!opts) {
92
+ const inputDir = process.env.UNI_INPUT_DIR;
93
+ opts = (0, shared_1.extend)((0, manifest_1.parseRpx2UnitOnce)(inputDir, platform));
94
+ }
95
+ const { unit, unitRatio, unitPrecision } = (0, shared_1.extend)({}, defaultUniAppCssProcessorOptions, opts || {});
71
96
  const rpx2unit = (0, uni_shared_1.createRpx2Unit)(unit, unitRatio, unitPrecision);
72
97
  return {
73
98
  postcssPlugin: 'uni-app',
@@ -75,7 +100,12 @@ const uniapp = (opts) => {
75
100
  return {
76
101
  OnceExit(root) {
77
102
  root.walkDecls(walkDecls(rpx2unit));
78
- root.walkRules(walkRules(page));
103
+ const rewriteTag = transforms[platform];
104
+ if (rewriteTag) {
105
+ root.walkRules(walkRules({
106
+ rewriteTag,
107
+ }));
108
+ }
79
109
  },
80
110
  };
81
111
  },
@@ -14,6 +14,7 @@ const DEFAULT_KEYS = [
14
14
  'MP_ALIPAY',
15
15
  'MP_BAIDU',
16
16
  'MP_QQ',
17
+ 'MP_LARK',
17
18
  'MP_TOUTIAO',
18
19
  'MP_WEIXIN',
19
20
  'MP_KUAISHOU',
package/dist/utils.d.ts CHANGED
@@ -1,9 +1,17 @@
1
1
  export { default as hash } from 'hash-sum';
2
+ import type { SFCTemplateCompileOptions } from '@vue/compiler-sfc';
3
+ import { ElementNode, RootNode, TemplateChildNode } from '@vue/compiler-core';
2
4
  export declare const isWindows: boolean;
3
5
  export declare function normalizePath(id: string): string;
6
+ export declare function relativeFile(from: string, to: string): string;
7
+ export declare function checkElementNodeTag(node: RootNode | TemplateChildNode | null | undefined, tag: string): node is ElementNode;
4
8
  export declare const resolveMainPathOnce: (inputDir: string) => string;
9
+ export declare function resolveComponentsLibPath(): string;
10
+ export declare function getBuiltInPaths(): string[];
5
11
  export declare function resolveBuiltIn(path: string): string;
6
12
  export declare function normalizeIdentifier(str: string): string;
7
13
  export declare function normalizePagePath(pagePath: string, platform: UniApp.PLATFORM): string | undefined;
8
- export declare function removeExt(str: string, ext?: string): string;
14
+ export declare function removeExt(str: string): string;
9
15
  export declare function normalizeNodeModules(str: string): string;
16
+ export declare function normalizeMiniProgramFilename(filename: string, inputDir?: string): string;
17
+ export declare function createUniVueTransformAssetUrls(base: string): SFCTemplateCompileOptions['transformAssetUrls'];
package/dist/utils.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.normalizeNodeModules = exports.removeExt = exports.normalizePagePath = exports.normalizeIdentifier = exports.resolveBuiltIn = exports.resolveMainPathOnce = exports.normalizePath = exports.isWindows = exports.hash = void 0;
6
+ exports.createUniVueTransformAssetUrls = exports.normalizeMiniProgramFilename = exports.normalizeNodeModules = exports.removeExt = exports.normalizePagePath = exports.normalizeIdentifier = exports.resolveBuiltIn = exports.getBuiltInPaths = exports.resolveComponentsLibPath = exports.resolveMainPathOnce = exports.checkElementNodeTag = exports.relativeFile = exports.normalizePath = exports.isWindows = exports.hash = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const os_1 = __importDefault(require("os"));
9
9
  const path_1 = __importDefault(require("path"));
@@ -11,12 +11,21 @@ const shared_1 = require("@vue/shared");
11
11
  const uni_shared_1 = require("@dcloudio/uni-shared");
12
12
  var hash_sum_1 = require("hash-sum");
13
13
  Object.defineProperty(exports, "hash", { enumerable: true, get: function () { return __importDefault(hash_sum_1).default; } });
14
+ const debug_1 = __importDefault(require("debug"));
14
15
  const constants_1 = require("./constants");
15
16
  exports.isWindows = os_1.default.platform() === 'win32';
16
17
  function normalizePath(id) {
17
18
  return exports.isWindows ? id.replace(/\\/g, '/') : id;
18
19
  }
19
20
  exports.normalizePath = normalizePath;
21
+ function relativeFile(from, to) {
22
+ return normalizePath(path_1.default.relative(path_1.default.dirname(from), to));
23
+ }
24
+ exports.relativeFile = relativeFile;
25
+ function checkElementNodeTag(node, tag) {
26
+ return !!node && node.type === 1 /* ELEMENT */ && node.tag === tag;
27
+ }
28
+ exports.checkElementNodeTag = checkElementNodeTag;
20
29
  exports.resolveMainPathOnce = (0, uni_shared_1.once)((inputDir) => {
21
30
  const mainTsPath = path_1.default.resolve(inputDir, 'main.ts');
22
31
  if (fs_1.default.existsSync(mainTsPath)) {
@@ -24,8 +33,60 @@ exports.resolveMainPathOnce = (0, uni_shared_1.once)((inputDir) => {
24
33
  }
25
34
  return normalizePath(path_1.default.resolve(inputDir, 'main.js'));
26
35
  });
36
+ let componentsLibPath = '';
37
+ function resolveComponentsLibPath() {
38
+ if (!componentsLibPath) {
39
+ componentsLibPath = path_1.default.resolve(resolveBuiltIn('@dcloudio/uni-components/package.json'), '../lib');
40
+ }
41
+ return componentsLibPath;
42
+ }
43
+ exports.resolveComponentsLibPath = resolveComponentsLibPath;
44
+ const ownerModules = ['@dcloudio/uni-app', '@dcloudio/vite-plugin-uni'];
45
+ const paths = [];
46
+ function resolveNodeModulePath(modulePath) {
47
+ const nodeModulesPaths = [];
48
+ const nodeModulesPath = path_1.default.join(modulePath, 'node_modules');
49
+ if (fs_1.default.existsSync(nodeModulesPath)) {
50
+ nodeModulesPaths.push(nodeModulesPath);
51
+ }
52
+ const index = modulePath.lastIndexOf('node_modules');
53
+ if (index > -1) {
54
+ nodeModulesPaths.push(path_1.default.join(modulePath.substr(0, index), 'node_modules'));
55
+ }
56
+ return nodeModulesPaths;
57
+ }
58
+ function initPaths() {
59
+ const cliContext = process.env.UNI_CLI_CONTEXT;
60
+ if (cliContext) {
61
+ const pathSet = new Set();
62
+ pathSet.add(path_1.default.join(cliContext, 'node_modules'));
63
+ [`@dcloudio/uni-` + process.env.UNI_PLATFORM, ...ownerModules].forEach((ownerModule) => {
64
+ let pkgPath = '';
65
+ try {
66
+ pkgPath = require.resolve(ownerModule + '/package.json', {
67
+ paths: [cliContext],
68
+ });
69
+ }
70
+ catch (e) { }
71
+ if (pkgPath) {
72
+ resolveNodeModulePath(path_1.default.dirname(pkgPath)).forEach((nodeModulePath) => {
73
+ pathSet.add(nodeModulePath);
74
+ });
75
+ }
76
+ });
77
+ paths.push(...pathSet);
78
+ (0, debug_1.default)('uni-paths')(paths);
79
+ }
80
+ }
81
+ function getBuiltInPaths() {
82
+ if (!paths.length) {
83
+ initPaths();
84
+ }
85
+ return paths;
86
+ }
87
+ exports.getBuiltInPaths = getBuiltInPaths;
27
88
  function resolveBuiltIn(path) {
28
- return require.resolve(path, { paths: [process.env.UNI_CLI_CONTEXT] });
89
+ return require.resolve(path, { paths: getBuiltInPaths() });
29
90
  }
30
91
  exports.resolveBuiltIn = resolveBuiltIn;
31
92
  function normalizeIdentifier(str) {
@@ -47,12 +108,8 @@ function normalizePagePath(pagePath, platform) {
47
108
  console.error(`${pagePath} not found`);
48
109
  }
49
110
  exports.normalizePagePath = normalizePagePath;
50
- function removeExt(str, ext) {
51
- if (ext) {
52
- const reg = new RegExp(ext.replace(/\./, '\\.') + '$');
53
- return normalizePath(str.replace(reg, ''));
54
- }
55
- return normalizePath(str.replace(/\.\w+$/g, ''));
111
+ function removeExt(str) {
112
+ return str.split('?')[0].replace(/\.\w+$/g, '');
56
113
  }
57
114
  exports.removeExt = removeExt;
58
115
  const NODE_MODULES_REGEX = /(\.\.\/)?node_modules/g;
@@ -66,3 +123,31 @@ function normalizeNodeModules(str) {
66
123
  return str;
67
124
  }
68
125
  exports.normalizeNodeModules = normalizeNodeModules;
126
+ function normalizeMiniProgramFilename(filename, inputDir) {
127
+ if (!inputDir || !path_1.default.isAbsolute(filename)) {
128
+ return normalizeNodeModules(filename);
129
+ }
130
+ return normalizeNodeModules(path_1.default.relative(inputDir, filename));
131
+ }
132
+ exports.normalizeMiniProgramFilename = normalizeMiniProgramFilename;
133
+ function createUniVueTransformAssetUrls(base) {
134
+ return {
135
+ base,
136
+ tags: {
137
+ audio: ['src'],
138
+ video: ['src', 'poster'],
139
+ img: ['src'],
140
+ image: ['src'],
141
+ 'cover-image': ['src'],
142
+ // h5
143
+ 'v-uni-audio': ['src'],
144
+ 'v-uni-video': ['src', 'poster'],
145
+ 'v-uni-image': ['src'],
146
+ 'v-uni-cover-image': ['src'],
147
+ // nvue
148
+ 'u-image': ['src'],
149
+ 'u-video': ['src', 'poster'],
150
+ },
151
+ };
152
+ }
153
+ exports.createUniVueTransformAssetUrls = createUniVueTransformAssetUrls;
@@ -1,4 +1,4 @@
1
- import { ConfigEnv } from 'vite';
1
+ import type { ConfigEnv } from 'vite';
2
2
  export declare type FEATURE_DEFINES = ReturnType<typeof initFeatures>;
3
3
  interface InitFeaturesOptions {
4
4
  pagesJson: UniApp.PagesJson;
@@ -1,5 +1,5 @@
1
1
  import type { Plugin } from 'vite';
2
- import type { EmittedFile } from 'rollup';
2
+ import type { EmittedAsset } from 'rollup';
3
3
  import type { ParserOptions } from '@vue/compiler-core';
4
4
  import type { CompilerOptions, TemplateCompiler } from '@vue/compiler-sfc';
5
5
  import { UniViteCopyPluginOptions } from './plugins/copy';
@@ -14,14 +14,13 @@ interface UniVitePluginUniOptions {
14
14
  compiler?: TemplateCompiler;
15
15
  compilerOptions?: {
16
16
  miniProgram?: {
17
- emitFile?: (emittedFile: EmittedFile) => string;
17
+ emitFile?: (emittedFile: EmittedAsset) => string;
18
18
  };
19
19
  isNativeTag: ParserOptions['isNativeTag'];
20
20
  isCustomElement: ParserOptions['isCustomElement'];
21
21
  directiveTransforms?: CompilerOptions['directiveTransforms'];
22
22
  nodeTransforms?: CompilerOptions['nodeTransforms'];
23
23
  };
24
- transformEvent?: Record<string, string>;
25
24
  copyOptions?: CopyOptions | (() => CopyOptions);
26
25
  }
27
26
  export interface UniVitePlugin extends Plugin {
@@ -1,4 +1,4 @@
1
- import { Plugin } from 'vite';
1
+ import type { Plugin } from 'vite';
2
2
  import { FileWatcherOptions } from '../../watcher';
3
3
  export declare type UniViteCopyPluginTarget = Omit<FileWatcherOptions, 'verbose'>;
4
4
  export interface UniViteCopyPluginOptions {
@@ -1,4 +1,4 @@
1
- import { Plugin } from 'vite';
1
+ import type { Plugin } from 'vite';
2
2
  import { FilterPattern } from '@rollup/pluginutils';
3
3
  interface UniCssScopedPluginOptions {
4
4
  include?: FilterPattern;
@@ -1,4 +1,4 @@
1
- import { Plugin } from 'vite';
1
+ import type { Plugin } from 'vite';
2
2
  import { FilterPattern } from '@rollup/pluginutils';
3
3
  declare type Injectment = string | [string, string];
4
4
  export interface InjectOptions {
@@ -9,7 +9,7 @@ const debug_1 = __importDefault(require("debug"));
9
9
  const pluginutils_1 = require("@rollup/pluginutils");
10
10
  const estree_walker_1 = require("estree-walker");
11
11
  const shared_1 = require("@vue/shared");
12
- const compiler_sfc_1 = require("@vue/compiler-sfc");
12
+ const magic_string_1 = __importDefault(require("magic-string"));
13
13
  const utils_1 = require("../utils");
14
14
  const debugInject = (0, debug_1.default)('vite:uni:inject');
15
15
  const debugInjectTry = (0, debug_1.default)('vite:uni:inject-try');
@@ -74,7 +74,7 @@ function uniViteInjectPlugin(options) {
74
74
  });
75
75
  // analyse scopes
76
76
  let scope = (0, pluginutils_1.attachScopes)(ast, 'scope');
77
- const magicString = new compiler_sfc_1.MagicString(code);
77
+ const magicString = new magic_string_1.default(code);
78
78
  const newImports = new Map();
79
79
  function handleReference(node, name, keypath) {
80
80
  let mod = modulesMap.get(keypath);
@@ -1 +1 @@
1
- export { ResolvedConfig } from 'vite';
1
+ export type { ResolvedConfig } from 'vite';
@@ -1 +1 @@
1
- export { ResolveFn, ViteDevServer } from 'vite';
1
+ export type { ResolveFn, ViteDevServer } from 'vite';
@@ -1 +1 @@
1
- export { Plugin } from 'vite';
1
+ export type { Plugin } from 'vite';
@@ -10,7 +10,7 @@ export declare const chunkToEmittedAssetsMap: WeakMap<RenderedChunk, Set<string>
10
10
  export declare function assetPlugin(config: ResolvedConfig): Plugin;
11
11
  export declare function registerAssetToChunk(chunk: RenderedChunk, file: string): void;
12
12
  export declare function checkPublicFile(url: string, { publicDir }: ResolvedConfig): string | undefined;
13
- export declare function fileToUrl(id: string, config: ResolvedConfig, ctx: PluginContext): string;
13
+ export declare function fileToUrl(id: string, config: ResolvedConfig, ctx: PluginContext, canInline?: boolean): string;
14
14
  export declare function getAssetFilename(hash: string, config: ResolvedConfig): string | undefined;
15
15
  export declare function getAssetHash(content: Buffer): string;
16
16
  export declare function urlToBuiltUrl(url: string, importer: string, config: ResolvedConfig, pluginContext: PluginContext): string;
@@ -25,6 +25,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.urlToBuiltUrl = exports.getAssetHash = exports.getAssetFilename = exports.fileToUrl = exports.checkPublicFile = exports.registerAssetToChunk = exports.assetPlugin = exports.chunkToEmittedAssetsMap = exports.assetUrlRE = void 0;
26
26
  const path_1 = __importDefault(require("path"));
27
27
  const url_1 = require("url");
28
+ const lite_1 = __importDefault(require("mime/lite"));
28
29
  const fs_1 = __importStar(require("fs"));
29
30
  const utils_1 = require("../utils");
30
31
  const magic_string_1 = __importDefault(require("magic-string"));
@@ -132,8 +133,8 @@ function checkPublicFile(url, { publicDir }) {
132
133
  }
133
134
  }
134
135
  exports.checkPublicFile = checkPublicFile;
135
- function fileToUrl(id, config, ctx) {
136
- return fileToBuiltUrl(id, config, ctx);
136
+ function fileToUrl(id, config, ctx, canInline = false) {
137
+ return fileToBuiltUrl(id, config, ctx, false, canInline);
137
138
  }
138
139
  exports.fileToUrl = fileToUrl;
139
140
  function getAssetFilename(hash, config) {
@@ -145,7 +146,7 @@ exports.getAssetFilename = getAssetFilename;
145
146
  * Register an asset to be emitted as part of the bundle (if necessary)
146
147
  * and returns the resolved public URL
147
148
  */
148
- function fileToBuiltUrl(id, config, pluginContext, skipPublicCheck = false) {
149
+ function fileToBuiltUrl(id, config, pluginContext, skipPublicCheck = false, canInline = false) {
149
150
  if (!skipPublicCheck && checkPublicFile(id, config)) {
150
151
  return config.base + id.slice(1);
151
152
  }
@@ -157,27 +158,33 @@ function fileToBuiltUrl(id, config, pluginContext, skipPublicCheck = false) {
157
158
  const file = (0, utils_1.cleanUrl)(id);
158
159
  const content = fs_1.default.readFileSync(file);
159
160
  let url;
160
- const map = assetHashToFilenameMap.get(config);
161
- const contentHash = getAssetHash(content);
162
- const { search, hash } = (0, url_1.parse)(id);
163
- const postfix = (search || '') + (hash || '');
164
- const fileName = (0, utils_1.normalizePath)(path_1.default.posix.relative(process.env.UNI_INPUT_DIR, file));
165
- if (!map.has(contentHash)) {
166
- map.set(contentHash, fileName);
161
+ if (canInline && content.length < Number(config.build.assetsInlineLimit)) {
162
+ // base64 inlined as a string
163
+ url = `data:${lite_1.default.getType(file)};base64,${content.toString('base64')}`;
167
164
  }
168
- if (!fileName.includes('/static/')) {
169
- const emittedSet = emittedHashMap.get(config);
170
- if (!emittedSet.has(contentHash)) {
171
- pluginContext.emitFile({
172
- name: fileName,
173
- fileName,
174
- type: 'asset',
175
- source: content,
176
- });
177
- emittedSet.add(contentHash);
165
+ else {
166
+ const map = assetHashToFilenameMap.get(config);
167
+ const contentHash = getAssetHash(content);
168
+ const { search, hash } = (0, url_1.parse)(id);
169
+ const postfix = (search || '') + (hash || '');
170
+ const fileName = (0, utils_1.normalizePath)(path_1.default.posix.relative(process.env.UNI_INPUT_DIR, file));
171
+ if (!map.has(contentHash)) {
172
+ map.set(contentHash, fileName);
173
+ }
174
+ if (!fileName.includes('/static/')) {
175
+ const emittedSet = emittedHashMap.get(config);
176
+ if (!emittedSet.has(contentHash)) {
177
+ pluginContext.emitFile({
178
+ name: fileName,
179
+ fileName,
180
+ type: 'asset',
181
+ source: content,
182
+ });
183
+ emittedSet.add(contentHash);
184
+ }
178
185
  }
186
+ url = `__VITE_ASSET__${contentHash}__${postfix ? `$_${postfix}__` : ``}`;
179
187
  }
180
- url = `__VITE_ASSET__${contentHash}__${postfix ? `$_${postfix}__` : ``}`;
181
188
  cache.set(id, url);
182
189
  return url;
183
190
  }