提交 24dfc6f6 编写于 作者: fxy060608's avatar fxy060608

refactor(h5): renderjs

上级 a9950fbe
......@@ -11,5 +11,6 @@ export * from './easycom'
export * from './constants'
export * from './preprocess'
export * from './postcss'
export * from './renderjs'
export { checkUpdate } from './checkUpdate'
import { parseVueRequest } from './vite/utils/url'
const WXS_RE = /vue&type=wxs/
export function isWxs(id: string) {
return WXS_RE.test(id)
}
const RENDERJS_RE = /vue&type=renderjs/
export function isRenderjs(id: string) {
return RENDERJS_RE.test(id)
}
export function parseRenderjs(id: string) {
if (isWxs(id)) {
return {
type: 'wxs',
name: (parseVueRequest(id).query as any).name as string,
} as const
}
if (isRenderjs(id)) {
return {
type: 'renderjs',
name: (parseVueRequest(id).query as any).name as string,
} as const
}
return {
type: '',
name: '',
} as const
}
export function missingModuleName(type: 'wxs' | 'renderjs', code: string) {
return `<script module="missing module name" lang="${type}">
${code}
</script>`
}
......@@ -7,6 +7,7 @@ const inject_1 = require("./plugins/inject");
const mainJs_1 = require("./plugins/mainJs");
const manifestJson_1 = require("./plugins/manifestJson");
const pagesJson_1 = require("./plugins/pagesJson");
const renderjs_1 = require("./plugins/renderjs");
const resolveId_1 = require("./plugins/resolveId");
const setup_1 = require("./plugins/setup");
const ssr_1 = require("./plugins/ssr");
......@@ -20,5 +21,6 @@ exports.default = [
css_1.uniCssPlugin(),
ssr_1.uniSSRPlugin(),
setup_1.uniSetupPlugin(),
renderjs_1.uniRenderjsPlugin(),
plugin_1.UniH5Plugin,
];
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uniRenderjsPlugin = void 0;
const debug_1 = __importDefault(require("debug"));
const compiler_sfc_1 = require("@vue/compiler-sfc");
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
const debugRenderjs = debug_1.default('vite:uni:renderjs');
function uniRenderjsPlugin() {
return {
name: 'vite:uni-h5-renderjs',
transform(code, id) {
const { type, name } = uni_cli_shared_1.parseRenderjs(id);
if (!type) {
return;
}
debugRenderjs(id);
if (!name) {
this.error(uni_cli_shared_1.missingModuleName(type, code));
}
return `${compiler_sfc_1.rewriteDefault(code.replace(/module\.exports\s*=/, 'export default '), '_sfc_' + type)}
${type === 'renderjs' ? genRenderjsCode(name) : genWxsCode(name)}`;
},
};
}
exports.uniRenderjsPlugin = uniRenderjsPlugin;
function genRenderjsCode(name) {
return `export default Comp => {
if(!Comp.mixins){Comp.mixins = []}
Comp.mixins.push({beforeCreate(){ this['${name}'] = this }})
Comp.mixins.push(_sfc_renderjs)
}`;
}
function genWxsCode(name) {
return `export default Comp => {
if(!Comp.mixins){Comp.mixins = []}
Comp.mixins.push({beforeCreate(){ this['${name}'] = _sfc_wxs }})
}`;
}
......@@ -5,6 +5,7 @@ import { uniInjectPlugin } from './plugins/inject'
import { uniMainJsPlugin } from './plugins/mainJs'
import { uniManifestJsonPlugin } from './plugins/manifestJson'
import { uniPagesJsonPlugin } from './plugins/pagesJson'
import { uniRenderjsPlugin } from './plugins/renderjs'
import { uniResolveIdPlugin } from './plugins/resolveId'
import { uniSetupPlugin } from './plugins/setup'
import { uniSSRPlugin } from './plugins/ssr'
......@@ -19,5 +20,6 @@ export default [
uniCssPlugin(),
uniSSRPlugin(),
uniSetupPlugin(),
uniRenderjsPlugin(),
UniH5Plugin,
]
import debug from 'debug'
import { Plugin } from 'vite'
import { rewriteDefault } from '@vue/compiler-sfc'
import { missingModuleName, parseRenderjs } from '@dcloudio/uni-cli-shared'
const debugRenderjs = debug('vite:uni:renderjs')
export function uniRenderjsPlugin(): Plugin {
return {
name: 'vite:uni-h5-renderjs',
transform(code, id) {
const { type, name } = parseRenderjs(id)
if (!type) {
return
}
debugRenderjs(id)
if (!name) {
this.error(missingModuleName(type, code))
}
return `${rewriteDefault(
code.replace(/module\.exports\s*=/, 'export default '),
'_sfc_' + type
)}
${type === 'renderjs' ? genRenderjsCode(name) : genWxsCode(name)}`
},
}
}
function genRenderjsCode(name: string) {
return `export default Comp => {
if(!Comp.mixins){Comp.mixins = []}
Comp.mixins.push({beforeCreate(){ this['${name}'] = this }})
Comp.mixins.push(_sfc_renderjs)
}`
}
function genWxsCode(name: string) {
return `export default Comp => {
if(!Comp.mixins){Comp.mixins = []}
Comp.mixins.push({beforeCreate(){ this['${name}'] = _sfc_wxs }})
}`
}
......@@ -12,7 +12,6 @@ import { uniPreCssPlugin } from './preCss'
import { uniEasycomPlugin } from './easycom'
import { uniStaticPlugin } from './static'
import { uniRenderjsPlugin } from './renderjs'
import { uniPreVuePlugin } from './preVue'
import { uniSSRPlugin } from './ssr'
import { uniResolveIdPlugin } from './resolveId'
......@@ -60,7 +59,6 @@ export function initPlugins(
'vite:css'
)
addPlugin(plugins, uniPreVuePlugin(), 'vite:vue', 'pre')
addPlugin(plugins, uniRenderjsPlugin(), 'vite:vue')
addPlugin(
plugins,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册