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

feat(h5): support disable tree shaking

上级 23cbb4c5
......@@ -72,3 +72,7 @@ export function getRouterOptions(manifestJson: Record<string, any>): {
} {
return extend({}, manifestJson.h5?.router)
}
export function isEnableTreeShaking(manifestJson: Record<string, any>) {
return manifestJson.h5?.optimization?.treeShaking?.enable !== false
}
......@@ -16,7 +16,13 @@ import { walk } from 'estree-walker'
import { extend } from '@vue/shared'
import MagicString from 'magic-string'
import { isProperty, isReference, isMemberExpression, isJsFile } from '../utils'
import {
isProperty,
isReference,
isMemberExpression,
isJsFile,
isAssignmentExpression,
} from '../utils'
interface Scope {
parent: Scope
......@@ -46,6 +52,7 @@ export function uniViteInjectPlugin(options: InjectOptions): Plugin {
delete modules.sourceMap
delete modules.callback
const reassignments = new Set<string>()
const modulesMap = new Map<string, string | [string, string]>()
const namespaceModulesMap = new Map<string, string | [string, string]>()
Object.keys(modules).forEach((name) => {
......@@ -105,7 +112,12 @@ export function uniViteInjectPlugin(options: InjectOptions): Plugin {
const newImports = new Map()
function handleReference(node: BaseNode, name: string, keypath: string) {
function handleReference(
node: BaseNode,
name: string,
keypath: string,
parent?: BaseNode
) {
let mod = modulesMap.get(keypath)
if (!mod && hasNamespace) {
const mods = keypath.split('.')
......@@ -128,8 +140,15 @@ export function uniViteInjectPlugin(options: InjectOptions): Plugin {
if (mod && !imports.has(name) && !scope.contains(name)) {
if (typeof mod === 'string') mod = [mod, 'default']
if (mod[0] === id) return false
const hash = `${keypath}:${mod[0]}:${mod[1]}`
// 当 API 被覆盖定义后,不再摇树
if (reassignments.has(hash)) {
return false
}
if (parent && isAssignmentExpression(parent)) {
reassignments.add(hash)
return false
}
const importLocalName =
name === keypath ? name : makeLegalIdentifier(`$inject_${keypath}`)
......@@ -184,7 +203,7 @@ export function uniViteInjectPlugin(options: InjectOptions): Plugin {
if (isReference(node, parent)) {
const { name, keypath } = flatten(node)
const handled = handleReference(node, name, keypath)
const handled = handleReference(node, name, keypath, parent)
if (handled) {
this.skip()
}
......
......@@ -4,6 +4,7 @@ import type {
Property,
Identifier,
CallExpression,
AssignmentExpression,
MemberExpression,
MethodDefinition,
ExportSpecifier,
......@@ -25,6 +26,10 @@ export const isProperty = (node: BaseNode): node is Property =>
export const isIdentifier = (node: BaseNode): node is Identifier =>
node.type === 'Identifier'
export const isAssignmentExpression = (
node: BaseNode
): node is AssignmentExpression => node.type === 'AssignmentExpression'
export const isCallExpression = (node: BaseNode): node is CallExpression =>
node.type === 'CallExpression'
......
......@@ -10,6 +10,8 @@ import {
buildInCssSet,
uniViteInjectPlugin,
isCombineBuiltInCss,
isEnableTreeShaking,
parseManifestJsonOnce,
} from '@dcloudio/uni-cli-shared'
const apiJson = require(path.resolve(__dirname, '../../lib/api.json'))
......@@ -49,17 +51,27 @@ export function uniInjectPlugin(): Plugin {
}
})
}
const injectPlugin = uniViteInjectPlugin(
extend(uniInjectPluginOptions, {
callback,
})
)
let injectPlugin: Plugin
return {
name: 'vite:uni-h5-inject',
apply: 'build',
enforce: 'post',
configResolved(config) {
resolvedConfig = config
const enableTreeShaking = isEnableTreeShaking(
parseManifestJsonOnce(process.env.UNI_INPUT_DIR)
)
if (!enableTreeShaking) {
// 不启用摇树优化,移除 wx、uni 等 API 配置
delete uniInjectPluginOptions['wx.']
delete uniInjectPluginOptions['uni.']
}
injectPlugin = uniViteInjectPlugin(
extend(uniInjectPluginOptions, {
callback,
})
)
},
transform(code, id) {
return injectPlugin.transform!.call(this, code, id)
......
......@@ -11,6 +11,8 @@ import {
normalizePagesRoute,
normalizePagePath,
normalizePath,
isEnableTreeShaking,
parseManifestJsonOnce,
} from '@dcloudio/uni-cli-shared'
export function uniPagesJsonPlugin(): Plugin {
......@@ -79,8 +81,11 @@ function getGlobal(ssr?: boolean) {
// 兼容 wx 对象
function registerGlobalCode(config: ResolvedConfig, ssr?: boolean) {
const name = getGlobal(ssr)
if (config.command === 'build' && !ssr) {
// 非SSR的发行模式,补充全局 uni 对象
const enableTreeShaking = isEnableTreeShaking(
parseManifestJsonOnce(process.env.UNI_INPUT_DIR)
)
if (enableTreeShaking && config.command === 'build' && !ssr) {
// 非 SSR 的发行模式,补充全局 uni 对象
return `${name}.uni = {};${name}.wx = {}`
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册