@dcloudio/vite-plugin-uni 0.0.1-nvue3.3030820220125001
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/LICENSE +202 -0
- package/bin/uni.js +22 -0
- package/dist/cli/action.d.ts +4 -0
- package/dist/cli/action.js +76 -0
- package/dist/cli/build.d.ts +5 -0
- package/dist/cli/build.js +147 -0
- package/dist/cli/index.d.ts +20 -0
- package/dist/cli/index.js +50 -0
- package/dist/cli/nvue.d.ts +1 -0
- package/dist/cli/nvue.js +24 -0
- package/dist/cli/server.d.ts +4 -0
- package/dist/cli/server.js +120 -0
- package/dist/cli/utils.d.ts +25 -0
- package/dist/cli/utils.js +189 -0
- package/dist/config/build.d.ts +3 -0
- package/dist/config/build.js +35 -0
- package/dist/config/css.d.ts +3 -0
- package/dist/config/css.js +28 -0
- package/dist/config/define.d.ts +3 -0
- package/dist/config/define.js +15 -0
- package/dist/config/index.d.ts +3 -0
- package/dist/config/index.js +36 -0
- package/dist/config/optimizeDeps.d.ts +3 -0
- package/dist/config/optimizeDeps.js +18 -0
- package/dist/config/resolve.d.ts +4 -0
- package/dist/config/resolve.js +33 -0
- package/dist/configResolved/env.d.ts +2 -0
- package/dist/configResolved/env.js +28 -0
- package/dist/configResolved/index.d.ts +25 -0
- package/dist/configResolved/index.js +59 -0
- package/dist/configResolved/options.d.ts +3 -0
- package/dist/configResolved/options.js +19 -0
- package/dist/configResolved/plugins/index.d.ts +8 -0
- package/dist/configResolved/plugins/index.js +54 -0
- package/dist/configResolved/plugins/json.d.ts +3 -0
- package/dist/configResolved/plugins/json.js +33 -0
- package/dist/configResolved/plugins/pre.d.ts +3 -0
- package/dist/configResolved/plugins/pre.js +51 -0
- package/dist/configResolved/plugins/preCss.d.ts +7 -0
- package/dist/configResolved/plugins/preCss.js +41 -0
- package/dist/configResolved/plugins/preVue.d.ts +6 -0
- package/dist/configResolved/plugins/preVue.js +146 -0
- package/dist/configResolved/plugins/resolveId.d.ts +15 -0
- package/dist/configResolved/plugins/resolveId.js +37 -0
- package/dist/configResolved/plugins/ssr.d.ts +3 -0
- package/dist/configResolved/plugins/ssr.js +63 -0
- package/dist/configResolved/plugins/static.d.ts +9 -0
- package/dist/configResolved/plugins/static.js +44 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +81 -0
- package/dist/plugins/copy.d.ts +3 -0
- package/dist/plugins/copy.js +55 -0
- package/dist/resolveId/index.d.ts +3 -0
- package/dist/resolveId/index.js +16 -0
- package/dist/utils/easycom.d.ts +2 -0
- package/dist/utils/easycom.js +37 -0
- package/dist/utils/filter.d.ts +1 -0
- package/dist/utils/filter.js +15 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +15 -0
- package/dist/utils/plugin.d.ts +26 -0
- package/dist/utils/plugin.js +115 -0
- package/dist/utils/polyfill.d.ts +4 -0
- package/dist/utils/polyfill.js +28 -0
- package/dist/vue/index.d.ts +1 -0
- package/dist/vue/index.js +13 -0
- package/dist/vue/options.d.ts +10 -0
- package/dist/vue/options.js +117 -0
- package/package.json +62 -0
|
@@ -0,0 +1,19 @@
|
|
|
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.initOptions = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const jsonc_parser_1 = require("jsonc-parser");
|
|
10
|
+
function resolveBase() {
|
|
11
|
+
const manifest = (0, jsonc_parser_1.parse)(fs_1.default.readFileSync(path_1.default.join(process.env.UNI_INPUT_DIR, 'manifest.json'), 'utf8'));
|
|
12
|
+
return (manifest.h5 && manifest.h5.router && manifest.h5.router.base) || '/';
|
|
13
|
+
}
|
|
14
|
+
function initOptions(options, config) {
|
|
15
|
+
options.base = resolveBase();
|
|
16
|
+
options.outputDir = process.env.UNI_OUTPUT_DIR;
|
|
17
|
+
options.assetsDir = config.build.assetsDir;
|
|
18
|
+
}
|
|
19
|
+
exports.initOptions = initOptions;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ResolvedConfig } from 'vite';
|
|
2
|
+
import { FilterPattern } from '@rollup/pluginutils';
|
|
3
|
+
import { VitePluginUniResolvedOptions } from '../..';
|
|
4
|
+
export interface UniPluginFilterOptions extends VitePluginUniResolvedOptions {
|
|
5
|
+
include?: FilterPattern;
|
|
6
|
+
exclude?: FilterPattern;
|
|
7
|
+
}
|
|
8
|
+
export declare function initPlugins(config: ResolvedConfig, options: VitePluginUniResolvedOptions): void;
|
|
@@ -0,0 +1,54 @@
|
|
|
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.initPlugins = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const debug_1 = __importDefault(require("debug"));
|
|
9
|
+
const shared_1 = require("@vue/shared");
|
|
10
|
+
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
11
|
+
const pre_1 = require("./pre");
|
|
12
|
+
const json_1 = require("./json");
|
|
13
|
+
const preCss_1 = require("./preCss");
|
|
14
|
+
const static_1 = require("./static");
|
|
15
|
+
const preVue_1 = require("./preVue");
|
|
16
|
+
const ssr_1 = require("./ssr");
|
|
17
|
+
const resolveId_1 = require("./resolveId");
|
|
18
|
+
const debugPlugin = (0, debug_1.default)('uni:plugin');
|
|
19
|
+
const UNI_H5_RE = /@dcloudio\/uni-h5/;
|
|
20
|
+
const uniPrePluginOptions = {
|
|
21
|
+
exclude: [...uni_cli_shared_1.COMMON_EXCLUDE, UNI_H5_RE],
|
|
22
|
+
};
|
|
23
|
+
const uniPreCssPluginOptions = {
|
|
24
|
+
exclude: [...uni_cli_shared_1.COMMON_EXCLUDE, UNI_H5_RE],
|
|
25
|
+
};
|
|
26
|
+
function initPlugins(config, options) {
|
|
27
|
+
const plugins = config.plugins;
|
|
28
|
+
addPlugin(plugins, (0, resolveId_1.uniResolveIdPlugin)(options), 'vite:resolve', 'pre');
|
|
29
|
+
addPlugin(plugins, (0, pre_1.uniPrePlugin)(config, (0, shared_1.extend)(uniPrePluginOptions, options)), 0, 'pre');
|
|
30
|
+
addPlugin(plugins, (0, preCss_1.uniPreCssPlugin)(config, (0, shared_1.extend)(uniPreCssPluginOptions, options)), 'vite:css');
|
|
31
|
+
addPlugin(plugins, (0, preVue_1.uniPreVuePlugin)(), 'vite:vue', 'pre');
|
|
32
|
+
addPlugin(plugins, (0, ssr_1.uniSSRPlugin)(config, (0, shared_1.extend)({ exclude: [...uni_cli_shared_1.COMMON_EXCLUDE] }, options)), 'vite:vue');
|
|
33
|
+
addPlugin(plugins, (0, json_1.uniJsonPlugin)(options), 'vite:json', 'pre');
|
|
34
|
+
addPlugin(plugins, (0, static_1.uniStaticPlugin)(options, config), 'vite:asset', 'pre');
|
|
35
|
+
if ((0, uni_cli_shared_1.isInHBuilderX)()) {
|
|
36
|
+
try {
|
|
37
|
+
require(path_1.default.resolve(process.env.UNI_HBUILDERX_PLUGINS, 'uni_helpers/lib/bytenode'));
|
|
38
|
+
const { V } = require(path_1.default.join(process.env.UNI_HBUILDERX_PLUGINS, 'uni_helpers'));
|
|
39
|
+
addPlugin(plugins, V({ dir: process.env.UNI_INPUT_DIR }), 0, 'pre');
|
|
40
|
+
}
|
|
41
|
+
catch (e) { }
|
|
42
|
+
}
|
|
43
|
+
if (process.env.DEBUG) {
|
|
44
|
+
debugPlugin(plugins.length);
|
|
45
|
+
debugPlugin(plugins.map((p) => p.name));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.initPlugins = initPlugins;
|
|
49
|
+
function addPlugin(plugins, plugin, index, type = 'post') {
|
|
50
|
+
if (typeof index === 'string') {
|
|
51
|
+
index = plugins.findIndex((plugin) => plugin.name === index);
|
|
52
|
+
}
|
|
53
|
+
return plugins.splice(index + (type === 'pre' ? 0 : 1), 0, plugin);
|
|
54
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uniJsonPlugin = void 0;
|
|
4
|
+
const jsonc_parser_1 = require("jsonc-parser");
|
|
5
|
+
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
6
|
+
const jsonExtRE = /\.json($|\?)(?!commonjs-proxy)/;
|
|
7
|
+
const SPECIAL_QUERY_RE = /[\?&](?:worker|sharedworker|raw|url)\b/;
|
|
8
|
+
function uniJsonPlugin(options) {
|
|
9
|
+
return {
|
|
10
|
+
name: 'uni:json',
|
|
11
|
+
transform(code, id) {
|
|
12
|
+
if (!jsonExtRE.test(id))
|
|
13
|
+
return null;
|
|
14
|
+
if (SPECIAL_QUERY_RE.test(id))
|
|
15
|
+
return null;
|
|
16
|
+
if (id.endsWith('.json.js'))
|
|
17
|
+
return null;
|
|
18
|
+
// preprocess
|
|
19
|
+
if (code.includes('#endif')) {
|
|
20
|
+
code = (0, uni_cli_shared_1.preJs)(code);
|
|
21
|
+
}
|
|
22
|
+
let jsonObj = (0, jsonc_parser_1.parse)(code);
|
|
23
|
+
if ((0, uni_cli_shared_1.isUniAppLocaleFile)(id)) {
|
|
24
|
+
jsonObj = jsonObj.common || {};
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
code: JSON.stringify(jsonObj),
|
|
28
|
+
map: null,
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.uniJsonPlugin = uniJsonPlugin;
|
|
@@ -0,0 +1,51 @@
|
|
|
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.uniPrePlugin = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const debug_1 = __importDefault(require("debug"));
|
|
9
|
+
const pluginutils_1 = require("@rollup/pluginutils");
|
|
10
|
+
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
11
|
+
const debugPreJs = (0, debug_1.default)('uni:pre-js');
|
|
12
|
+
const debugPreHtml = (0, debug_1.default)('uni:pre-html');
|
|
13
|
+
const debugPreJsTry = (0, debug_1.default)('uni:pre-js-try');
|
|
14
|
+
const PRE_JS_EXTNAME = ['.json', '.css'].concat(uni_cli_shared_1.EXTNAME_VUE).concat(uni_cli_shared_1.EXTNAME_JS);
|
|
15
|
+
const PRE_HTML_EXTNAME = uni_cli_shared_1.EXTNAME_VUE;
|
|
16
|
+
function uniPrePlugin(config, options) {
|
|
17
|
+
const filter = (0, pluginutils_1.createFilter)(options.include, options.exclude);
|
|
18
|
+
return {
|
|
19
|
+
name: 'uni:pre',
|
|
20
|
+
transform(code, id) {
|
|
21
|
+
if (!filter(id)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const { filename, query } = (0, uni_cli_shared_1.parseVueRequest)(id);
|
|
25
|
+
const extname = path_1.default.extname(filename);
|
|
26
|
+
const isHtml = query.type === 'template' || PRE_HTML_EXTNAME.includes(extname);
|
|
27
|
+
const isJs = PRE_JS_EXTNAME.includes(extname);
|
|
28
|
+
const isPre = isHtml || isJs;
|
|
29
|
+
if (isPre) {
|
|
30
|
+
debugPreJsTry(id);
|
|
31
|
+
}
|
|
32
|
+
const hasEndif = isPre && code.includes('#endif');
|
|
33
|
+
if (!hasEndif) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (isHtml) {
|
|
37
|
+
code = (0, uni_cli_shared_1.preHtml)(code);
|
|
38
|
+
debugPreHtml(id);
|
|
39
|
+
}
|
|
40
|
+
if (isJs) {
|
|
41
|
+
code = (0, uni_cli_shared_1.preJs)(code);
|
|
42
|
+
debugPreJs(id);
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
code,
|
|
46
|
+
map: (0, uni_cli_shared_1.withSourcemap)(config) ? this.getCombinedSourcemap() : null,
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
exports.uniPrePlugin = uniPrePlugin;
|
|
@@ -0,0 +1,41 @@
|
|
|
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.uniPreCssPlugin = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const pluginutils_1 = require("@rollup/pluginutils");
|
|
9
|
+
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
10
|
+
const debugPre = (0, debug_1.default)('uni:pre-css');
|
|
11
|
+
const debugPreTry = (0, debug_1.default)('uni:pre-css-try');
|
|
12
|
+
const cssLangs = `\\.(css|less|sass|scss|styl|stylus|postcss)($|\\?)`;
|
|
13
|
+
const cssLangRE = new RegExp(cssLangs);
|
|
14
|
+
/**
|
|
15
|
+
* preprocess css
|
|
16
|
+
* @param options
|
|
17
|
+
*/
|
|
18
|
+
function uniPreCssPlugin(config, options) {
|
|
19
|
+
const filter = (0, pluginutils_1.createFilter)(options.include, options.exclude);
|
|
20
|
+
return {
|
|
21
|
+
name: 'uni:pre-css',
|
|
22
|
+
transform(code, id) {
|
|
23
|
+
if (!cssLangRE.test(id)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (!filter(id)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
debugPreTry(id);
|
|
30
|
+
if (!code.includes('#endif')) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
debugPre(id);
|
|
34
|
+
return {
|
|
35
|
+
code: (0, uni_cli_shared_1.preJs)(code),
|
|
36
|
+
map: (0, uni_cli_shared_1.withSourcemap)(config) ? this.getCombinedSourcemap() : null,
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.uniPreCssPlugin = uniPreCssPlugin;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
import { RootNode, ElementNode } from '@vue/compiler-core';
|
|
3
|
+
export declare function uniPreVuePlugin(): Plugin;
|
|
4
|
+
export declare function parseBlockCode(ast: RootNode, code: string): string;
|
|
5
|
+
export declare function parseWxsNodes(ast: RootNode): ElementNode[];
|
|
6
|
+
export declare function parseWxsCode(wxsNodes: ElementNode[], code: string): string;
|
|
@@ -0,0 +1,146 @@
|
|
|
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.parseWxsCode = exports.parseWxsNodes = exports.parseBlockCode = exports.uniPreVuePlugin = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const debug_1 = __importDefault(require("debug"));
|
|
9
|
+
const compiler_sfc_1 = require("@vue/compiler-sfc");
|
|
10
|
+
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
11
|
+
const debugPreVue = (0, debug_1.default)('uni:pre-vue');
|
|
12
|
+
const BLOCK_RE = /<\/block>/;
|
|
13
|
+
const WXS_LANG_RE = /lang=["|'](renderjs|wxs)["|']/;
|
|
14
|
+
const WXS_ATTRS = ['wxs', 'renderjs'];
|
|
15
|
+
function uniPreVuePlugin() {
|
|
16
|
+
let isNVue = false;
|
|
17
|
+
return {
|
|
18
|
+
name: 'uni:pre-vue',
|
|
19
|
+
config(config) {
|
|
20
|
+
isNVue = config.nvue;
|
|
21
|
+
},
|
|
22
|
+
async transform(code, id) {
|
|
23
|
+
const { filename, query } = (0, uni_cli_shared_1.parseVueRequest)(id);
|
|
24
|
+
if (query.vue) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (!uni_cli_shared_1.EXTNAME_VUE.includes(path_1.default.extname(filename))) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
// 清空当前页面已缓存的 filter 信息
|
|
31
|
+
(0, uni_cli_shared_1.clearMiniProgramTemplateFilter)((0, uni_cli_shared_1.removeExt)((0, uni_cli_shared_1.normalizeMiniProgramFilename)(id, process.env.UNI_INPUT_DIR)));
|
|
32
|
+
const hasBlock = BLOCK_RE.test(code);
|
|
33
|
+
// nvue 不支持 renderjs,wxs
|
|
34
|
+
const hasWxs = !isNVue && WXS_LANG_RE.test(code);
|
|
35
|
+
if (!hasBlock && !hasWxs) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
debugPreVue(id);
|
|
39
|
+
const watchFiles = [];
|
|
40
|
+
const errors = [];
|
|
41
|
+
let ast = (0, uni_cli_shared_1.parseVue)(code, errors);
|
|
42
|
+
if (hasBlock) {
|
|
43
|
+
code = parseBlockCode(ast, code);
|
|
44
|
+
}
|
|
45
|
+
if (hasWxs) {
|
|
46
|
+
if (hasBlock) {
|
|
47
|
+
// 重新解析新的 code
|
|
48
|
+
ast = (0, uni_cli_shared_1.parseVue)(code, errors);
|
|
49
|
+
}
|
|
50
|
+
const wxsNodes = parseWxsNodes(ast);
|
|
51
|
+
code = parseWxsCode(wxsNodes, code);
|
|
52
|
+
// add watch
|
|
53
|
+
for (const wxsNode of wxsNodes) {
|
|
54
|
+
const srcProp = wxsNode.props.find((prop) => prop.type === 6 /* ATTRIBUTE */ && prop.name === 'src');
|
|
55
|
+
if (srcProp && srcProp.value) {
|
|
56
|
+
const resolveId = await this.resolve(srcProp.value.content, id);
|
|
57
|
+
if (resolveId) {
|
|
58
|
+
watchFiles.push(resolveId.id);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// if (errors.length) {
|
|
64
|
+
// this.error(errors.join('\n'))
|
|
65
|
+
// }
|
|
66
|
+
watchFiles.forEach((file) => this.addWatchFile(file));
|
|
67
|
+
return {
|
|
68
|
+
code,
|
|
69
|
+
map: null,
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
exports.uniPreVuePlugin = uniPreVuePlugin;
|
|
75
|
+
function traverseChildren({ children }, blockNodes) {
|
|
76
|
+
children.forEach((node) => traverseNode(node, blockNodes));
|
|
77
|
+
}
|
|
78
|
+
function traverseNode(node, blockNodes) {
|
|
79
|
+
if ((0, uni_cli_shared_1.isElementNode)(node) && node.tag === 'block') {
|
|
80
|
+
blockNodes.push(node);
|
|
81
|
+
}
|
|
82
|
+
if (node.type === 10 /* IF_BRANCH */ ||
|
|
83
|
+
node.type === 11 /* FOR */ ||
|
|
84
|
+
node.type === 1 /* ELEMENT */ ||
|
|
85
|
+
node.type === 0 /* ROOT */) {
|
|
86
|
+
traverseChildren(node, blockNodes);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function parseBlockCode(ast, code) {
|
|
90
|
+
const blockNodes = [];
|
|
91
|
+
traverseNode(ast, blockNodes);
|
|
92
|
+
if (blockNodes.length) {
|
|
93
|
+
return parseBlockNode(code, blockNodes);
|
|
94
|
+
}
|
|
95
|
+
return code;
|
|
96
|
+
}
|
|
97
|
+
exports.parseBlockCode = parseBlockCode;
|
|
98
|
+
const BLOCK_END_LEN = '</block>'.length;
|
|
99
|
+
const BLOCK_START_LEN = '<block'.length;
|
|
100
|
+
function parseBlockNode(code, blocks) {
|
|
101
|
+
const magicString = new compiler_sfc_1.MagicString(code);
|
|
102
|
+
blocks.forEach(({ loc }) => {
|
|
103
|
+
const startOffset = loc.start.offset;
|
|
104
|
+
const endOffset = loc.end.offset;
|
|
105
|
+
magicString.overwrite(startOffset, startOffset + BLOCK_START_LEN, '<template');
|
|
106
|
+
magicString.overwrite(endOffset - BLOCK_END_LEN, endOffset, '</template>');
|
|
107
|
+
});
|
|
108
|
+
return magicString.toString();
|
|
109
|
+
}
|
|
110
|
+
function parseWxsNodes(ast) {
|
|
111
|
+
return ast.children.filter((node) => node.type === 1 /* ELEMENT */ &&
|
|
112
|
+
node.tag === 'script' &&
|
|
113
|
+
node.props.find((prop) => prop.name === 'lang' &&
|
|
114
|
+
prop.type === 6 /* ATTRIBUTE */ &&
|
|
115
|
+
prop.value &&
|
|
116
|
+
WXS_ATTRS.includes(prop.value.content)));
|
|
117
|
+
}
|
|
118
|
+
exports.parseWxsNodes = parseWxsNodes;
|
|
119
|
+
function parseWxsCode(wxsNodes, code) {
|
|
120
|
+
if (wxsNodes.length) {
|
|
121
|
+
code = parseWxsNode(code, wxsNodes);
|
|
122
|
+
}
|
|
123
|
+
return code;
|
|
124
|
+
}
|
|
125
|
+
exports.parseWxsCode = parseWxsCode;
|
|
126
|
+
const SCRIPT_END_LEN = '</script>'.length;
|
|
127
|
+
const SCRIPT_START_LEN = '<script'.length;
|
|
128
|
+
function parseWxsNode(code, nodes) {
|
|
129
|
+
const magicString = new compiler_sfc_1.MagicString(code);
|
|
130
|
+
nodes.forEach(({ loc, props }) => {
|
|
131
|
+
const langAttr = props.find((prop) => prop.name === 'lang');
|
|
132
|
+
const moduleAttr = props.find((prop) => prop.name === 'module');
|
|
133
|
+
const startOffset = loc.start.offset;
|
|
134
|
+
const endOffset = loc.end.offset;
|
|
135
|
+
const lang = langAttr.value.content;
|
|
136
|
+
const langStartOffset = langAttr.loc.start.offset;
|
|
137
|
+
magicString.overwrite(startOffset, startOffset + SCRIPT_START_LEN, '<' + lang); // <renderjs or <wxs
|
|
138
|
+
magicString.overwrite(langStartOffset, langStartOffset + ('lang="' + lang + '"').length, ''); // remove lang="renderjs" or lang="wxs"
|
|
139
|
+
magicString.overwrite(endOffset - SCRIPT_END_LEN, endOffset, '</' + lang + '>'); //</renderjs> or </wxs>
|
|
140
|
+
if (moduleAttr) {
|
|
141
|
+
const moduleStartOffset = moduleAttr.loc.start.offset;
|
|
142
|
+
magicString.overwrite(moduleStartOffset, moduleStartOffset + 'module'.length, 'name'); // module="echarts" => name="echarts"
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
return magicString.toString();
|
|
146
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
import { VitePluginUniResolvedOptions } from '../..';
|
|
3
|
+
declare const BUILT_IN_MODULES: {
|
|
4
|
+
'vue-router': string;
|
|
5
|
+
vuex: string;
|
|
6
|
+
'vue-i18n': string;
|
|
7
|
+
'@dcloudio/uni-app': string;
|
|
8
|
+
'@dcloudio/uni-stat': string;
|
|
9
|
+
'@dcloudio/uni-cloud': string;
|
|
10
|
+
'@dcloudio/uni-i18n': string;
|
|
11
|
+
'@dcloudio/uni-shared': string;
|
|
12
|
+
};
|
|
13
|
+
export declare type BuiltInModulesKey = keyof typeof BUILT_IN_MODULES;
|
|
14
|
+
export declare function uniResolveIdPlugin(options: VitePluginUniResolvedOptions): Plugin;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
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.uniResolveIdPlugin = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const debug_1 = __importDefault(require("debug"));
|
|
9
|
+
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
10
|
+
const debugResolve = (0, debug_1.default)('uni:resolve-id');
|
|
11
|
+
const BUILT_IN_MODULES = {
|
|
12
|
+
'vue-router': 'dist/vue-router.esm-bundler.js',
|
|
13
|
+
vuex: 'dist/vuex.esm-bundler.js',
|
|
14
|
+
'vue-i18n': 'dist/vue-i18n.esm-bundler.js',
|
|
15
|
+
'@dcloudio/uni-app': 'dist/uni-app.es.js',
|
|
16
|
+
'@dcloudio/uni-stat': 'dist/uni-stat.es.js',
|
|
17
|
+
'@dcloudio/uni-cloud': 'dist/uni-cloud.es.js',
|
|
18
|
+
'@dcloudio/uni-i18n': 'dist/uni-i18n.es.js',
|
|
19
|
+
'@dcloudio/uni-shared': 'dist/uni-shared.es.js',
|
|
20
|
+
};
|
|
21
|
+
function uniResolveIdPlugin(options) {
|
|
22
|
+
const resolveCache = {};
|
|
23
|
+
return {
|
|
24
|
+
name: 'uni:resolve-id',
|
|
25
|
+
resolveId(id) {
|
|
26
|
+
const cache = resolveCache[id];
|
|
27
|
+
if (cache) {
|
|
28
|
+
debugResolve('cache', id, cache);
|
|
29
|
+
return cache;
|
|
30
|
+
}
|
|
31
|
+
if (BUILT_IN_MODULES[id]) {
|
|
32
|
+
return (resolveCache[id] = (0, uni_cli_shared_1.resolveBuiltIn)(path_1.default.join(id, BUILT_IN_MODULES[id])));
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
exports.uniResolveIdPlugin = uniResolveIdPlugin;
|
|
@@ -0,0 +1,63 @@
|
|
|
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.uniSSRPlugin = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
+
const estree_walker_1 = require("estree-walker");
|
|
10
|
+
const pluginutils_1 = require("@rollup/pluginutils");
|
|
11
|
+
const compiler_sfc_1 = require("@vue/compiler-sfc");
|
|
12
|
+
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
13
|
+
const debugSSR = (0, debug_1.default)('uni:ssr');
|
|
14
|
+
const KEYED_FUNC_RE = /(ssrRef|shallowSsrRef)/;
|
|
15
|
+
function uniSSRPlugin(config, options) {
|
|
16
|
+
const filter = (0, pluginutils_1.createFilter)(options.include, options.exclude);
|
|
17
|
+
return {
|
|
18
|
+
name: 'uni:ssr',
|
|
19
|
+
transform(code, id) {
|
|
20
|
+
if (!filter(id))
|
|
21
|
+
return null;
|
|
22
|
+
if (!KEYED_FUNC_RE.test(code)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
debugSSR('try', id);
|
|
26
|
+
const ast = this.parse(code);
|
|
27
|
+
const s = new compiler_sfc_1.MagicString(code);
|
|
28
|
+
(0, estree_walker_1.walk)(ast, {
|
|
29
|
+
enter(node) {
|
|
30
|
+
if (!(0, uni_cli_shared_1.isCallExpression)(node)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const { callee, arguments: args } = node;
|
|
34
|
+
if (args.length !== 1) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const name = (0, uni_cli_shared_1.isIdentifier)(callee)
|
|
38
|
+
? callee.name
|
|
39
|
+
: (0, uni_cli_shared_1.isMemberExpression)(callee) && (0, uni_cli_shared_1.isIdentifier)(callee.property)
|
|
40
|
+
? callee.property.name
|
|
41
|
+
: '';
|
|
42
|
+
if (name !== 'ssrRef' && name !== 'shallowSsrRef') {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const { end } = node;
|
|
46
|
+
const key = id + '-' + node.end;
|
|
47
|
+
debugSSR(key, name);
|
|
48
|
+
s.appendLeft(end - 1, ", '" + createKey(`${id}-${end}`) + "'");
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
return {
|
|
52
|
+
code: s.toString(),
|
|
53
|
+
map: (0, uni_cli_shared_1.withSourcemap)(config) ? s.generateMap().toString() : null,
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
exports.uniSSRPlugin = uniSSRPlugin;
|
|
59
|
+
function createKey(source) {
|
|
60
|
+
const hash = crypto_1.default.createHash('md5');
|
|
61
|
+
hash.update(source);
|
|
62
|
+
return hash.digest('base64').toString();
|
|
63
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Plugin, ResolvedConfig } from 'vite';
|
|
2
|
+
import { UniPluginFilterOptions } from '.';
|
|
3
|
+
/**
|
|
4
|
+
* 提供static等目录静态资源加载
|
|
5
|
+
* @param _options
|
|
6
|
+
* @param config
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare function uniStaticPlugin(_options: UniPluginFilterOptions, config: ResolvedConfig): Plugin;
|
|
@@ -0,0 +1,44 @@
|
|
|
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.uniStaticPlugin = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
9
|
+
const utils_1 = require("../../utils");
|
|
10
|
+
const debugStatic = (0, debug_1.default)('uni:static');
|
|
11
|
+
/**
|
|
12
|
+
* 提供static等目录静态资源加载
|
|
13
|
+
* @param _options
|
|
14
|
+
* @param config
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
function uniStaticPlugin(_options, config) {
|
|
18
|
+
const filter = (0, utils_1.createPublicFileFilter)();
|
|
19
|
+
return {
|
|
20
|
+
name: 'uni:static',
|
|
21
|
+
resolveId(id) {
|
|
22
|
+
if (!config.assetsInclude((0, uni_cli_shared_1.cleanUrl)(id))) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const publicFile = filter(id);
|
|
26
|
+
if (publicFile) {
|
|
27
|
+
debugStatic(id);
|
|
28
|
+
return id;
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
async load(id) {
|
|
32
|
+
if (!config.assetsInclude((0, uni_cli_shared_1.cleanUrl)(id))) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (filter(id)) {
|
|
36
|
+
return `export default ${JSON.stringify(fileToUrl(id, config))}`;
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.uniStaticPlugin = uniStaticPlugin;
|
|
42
|
+
function fileToUrl(id, config) {
|
|
43
|
+
return config.base + id.slice(1);
|
|
44
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite';
|
|
2
|
+
import type { Options as VueOptions } from '@vitejs/plugin-vue';
|
|
3
|
+
import type { Options as ViteLegacyOptions } from '@vitejs/plugin-legacy';
|
|
4
|
+
import type { VueJSXPluginOptions } from '@vue/babel-plugin-jsx';
|
|
5
|
+
import { CopyOptions } from '@dcloudio/uni-cli-shared';
|
|
6
|
+
export interface VitePluginUniOptions {
|
|
7
|
+
inputDir?: string;
|
|
8
|
+
outputDir?: string;
|
|
9
|
+
vueOptions?: VueOptions;
|
|
10
|
+
vueJsxOptions?: (VueJSXPluginOptions & {
|
|
11
|
+
babelPlugins?: any[];
|
|
12
|
+
}) | boolean;
|
|
13
|
+
viteLegacyOptions?: ViteLegacyOptions | false;
|
|
14
|
+
}
|
|
15
|
+
export interface VitePluginUniResolvedOptions extends VitePluginUniOptions {
|
|
16
|
+
base: string;
|
|
17
|
+
command: ResolvedConfig['command'];
|
|
18
|
+
platform: UniApp.PLATFORM;
|
|
19
|
+
inputDir: string;
|
|
20
|
+
outputDir: string;
|
|
21
|
+
assetsDir: string;
|
|
22
|
+
devServer?: ViteDevServer;
|
|
23
|
+
copyOptions?: Required<CopyOptions>;
|
|
24
|
+
}
|
|
25
|
+
export { runDev, runBuild } from './cli/action';
|
|
26
|
+
export default function uniPlugin(rawOptions?: VitePluginUniOptions): Plugin[];
|