Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vue3): 允许用户对vue-loader传入配置项 #11694

Merged
merged 4 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions packages/taro-plugin-vue3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import { modifyH5WebpackChain } from './webpack.h5'

import type { IPluginContext } from '@tarojs/service'

type CompilerOptions = {
isCustomElement: (tag: string) => boolean
whitespace: 'condense' | 'preserve'
delimiters: string[]
comments: boolean
nodeTransforms: ((...args: any) => void)[]
}
export interface IConfig {
mini?: {
compilerOptions: {
isCustomElement: (tag: string) => boolean
whitespace: 'condense' | 'preserve'
delimiters: string[]
comments: boolean
nodeTransforms: ((...args: any) => void)[]
}
compilerOptions: CompilerOptions
},
vueLoaderOption?: {
compilerOptions: CompilerOptions
[key: string]: any
}
}

Expand All @@ -27,10 +32,10 @@ export default (ctx: IPluginContext, config: IConfig = {}) => {

if (process.env.TARO_ENV === 'h5') {
// H5
modifyH5WebpackChain(ctx, chain)
modifyH5WebpackChain(ctx, chain, config)
} else {
// 小程序
modifyMiniWebpackChain(ctx, chain, data, config.mini)
modifyMiniWebpackChain(ctx, chain, data, config)
}
})
}
Expand Down
10 changes: 7 additions & 3 deletions packages/taro-plugin-vue3/src/webpack.h5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { getVueLoaderPath } from './index'

import type { IPluginContext } from '@tarojs/service'
import type { RootNode, TemplateChildNode, ElementNode } from '@vue/compiler-core'
import type { IConfig } from './index'

export function modifyH5WebpackChain (ctx: IPluginContext, chain) {
export function modifyH5WebpackChain (ctx: IPluginContext, chain, config: IConfig) {
// vue3 tsx 使用原生组件
setAlias(chain)
setStyleLoader(ctx, chain)
setVueLoader(chain)
setVueLoader(chain, config)
setLoader(chain)
setTaroApiLoader(chain)
}
Expand All @@ -34,7 +35,7 @@ function setStyleLoader (ctx: IPluginContext, chain) {
})
}

function setVueLoader (chain) {
function setVueLoader (chain, config: IConfig) {
const vueLoaderPath = getVueLoaderPath()

// plugin
Expand All @@ -43,6 +44,7 @@ function setVueLoader (chain) {
.plugin('vueLoaderPlugin')
.use(VueLoaderPlugin)

const compilerOptions = config.vueLoaderOption?.compilerOptions || {}
// loader
const vueLoaderOption = {
transformAssetUrls: {
Expand All @@ -59,7 +61,9 @@ function setVueLoader (chain) {
'taro-image': 'src',
'taro-cover-image': 'src'
},
...(config.vueLoaderOption ?? {}),
compilerOptions: {
...compilerOptions,
// https://github.com/vuejs/vue-next/blob/master/packages/compiler-core/src/options.ts
nodeTransforms: [(node: RootNode | TemplateChildNode) => {
if (node.type === 1 /* ELEMENT */) {
Expand Down
14 changes: 5 additions & 9 deletions packages/taro-plugin-vue3/src/webpack.mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import type { IConfig } from './index'

const CUSTOM_WRAPPER = 'custom-wrapper'

type MiniConfig = IConfig['mini']

export function modifyMiniWebpackChain (_ctx: IPluginContext, chain, data, config: MiniConfig) {
export function modifyMiniWebpackChain (_ctx: IPluginContext, chain, data, config: IConfig) {
setVueLoader(chain, data, config)
setLoader(chain)
setDefinePlugin(chain)
}

function setVueLoader (chain, data, config: MiniConfig) {
function setVueLoader (chain, data, config: IConfig) {
const vueLoaderPath = getVueLoaderPath()

// plugin
Expand All @@ -26,6 +24,7 @@ function setVueLoader (chain, data, config: MiniConfig) {
.plugin('vueLoaderPlugin')
.use(VueLoaderPlugin)

const compilerOptions = config.vueLoaderOption?.compilerOptions || config.mini?.compilerOptions || {}
// loader
const vueLoaderOption: any = {
optimizeSSR: false,
Expand All @@ -37,11 +36,8 @@ function setVueLoader (chain, data, config: MiniConfig) {
image: 'src',
'cover-image': 'src'
},
compilerOptions: {}
}

if (config?.compilerOptions) {
vueLoaderOption.compilerOptions = Object.assign({}, config.compilerOptions)
...(config.vueLoaderOption ?? {}),
compilerOptions
}

vueLoaderOption.compilerOptions.nodeTransforms ||= []
Expand Down