Skip to content

Commit

Permalink
feat(vite): 增加 Babel 处理的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Mar 14, 2023
1 parent 5f9a24c commit f0527ca
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/taro-vite-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"mv:comp": "node ./mv-comp.js"
},
"dependencies": {
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-inject": "^5.0.3",
"@tarojs/shared": "workspace:*",
"html-minifier": "^4.0.0",
Expand Down
38 changes: 36 additions & 2 deletions packages/taro-vite-runner/src/mini/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { babel, RollupBabelInputPluginOptions } from '@rollup/plugin-babel'
import inject from '@rollup/plugin-inject'
import { PLATFORMS } from '@tarojs/helper'
import path from 'path'

import type { PluginOption } from 'vite'
import type { MiniBuildConfig } from '../utils/types'

export default function (taroConfig: MiniBuildConfig): PluginOption {
export default function (appPath: string, taroConfig: MiniBuildConfig): PluginOption {
function getDefineOption () {
const {
env = {},
Expand Down Expand Up @@ -45,6 +47,37 @@ export default function (taroConfig: MiniBuildConfig): PluginOption {
})
}

function getBabelOption (taroConfig: MiniBuildConfig): RollupBabelInputPluginOptions {
const { compile = {} } = taroConfig
const babelOptions: RollupBabelInputPluginOptions = {
extensions: ['.js', '.jsx', 'ts', 'tsx', '.es6', '.es', '.mjs'],
babelHelpers: 'runtime',
skipPreflightCheck: true
}

if (compile.exclude?.length) {
babelOptions.exclude = [
...compile.exclude,
/css-loader/,
/node_modules[/\\](?!@tarojs)/
]
} else if (compile.include?.length) {
const sourceDir = path.join(appPath, taroConfig.sourceRoot || 'src')
babelOptions.include = [
...compile.include,
sourceDir,
/taro/
]
} else {
babelOptions.exclude = [
/css-loader/,
/node_modules[/\\](?!@tarojs)/
]
}

return babelOptions
}

return {
name: 'taro:vite-mini-config',
config: () => ({
Expand Down Expand Up @@ -84,7 +117,8 @@ export default function (taroConfig: MiniBuildConfig): PluginOption {
Element: ['@tarojs/runtime', 'TaroElement'],
SVGElement: ['@tarojs/runtime', 'SVGElement'],
MutationObserver: ['@tarojs/runtime', 'MutationObserver']
})
}),
babel(getBabelOption(taroConfig)),
]
},
commonjsOptions: {
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-vite-runner/src/mini/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { MiniBuildConfig } from '../utils/types'
export default function (appPath: string, taroConfig: MiniBuildConfig): PluginOption[] {
return [
pipelinePlugin(appPath, taroConfig),
configPlugin(taroConfig),
configPlugin(appPath, taroConfig),
entryPlugin(),
pagePlugin(),
emitPlugin()
Expand Down
29 changes: 27 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f0527ca

Please sign in to comment.