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

fix(prebundle): 修复部分小程序不支持语法问题 fix #12414 #12491

Merged
merged 5 commits into from
Sep 20, 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
24 changes: 24 additions & 0 deletions packages/taro-jd/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IPluginContext } from '@tarojs/service'
import { isString } from '@tarojs/shared'

import JD from './program'

Expand All @@ -14,4 +15,27 @@ export default (ctx: IPluginContext) => {
await program.start()
}
})

ctx.modifyRunnerOpts(({ opts }) => {
if (!opts?.compiler) return

if (isString(opts.compiler)) {
opts.compiler = {
type: opts.compiler
}
}
const { compiler } = opts
if (compiler.type === 'webpack5') {
compiler.prebundle ||= {}
const prebundleOptions = compiler.prebundle
if (prebundleOptions.enable === false) return
prebundleOptions.swc ||= {
jsc: {
target: 'es5'
}
}
prebundleOptions.exclude ||= []
prebundleOptions.include ||= []
}
})
}
25 changes: 25 additions & 0 deletions packages/taro-swan/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IPluginContext } from '@tarojs/service'
import { isString } from '@tarojs/shared'

import Swan from './program'

Expand All @@ -14,4 +15,28 @@ export default (ctx: IPluginContext) => {
await program.start()
}
})

ctx.modifyRunnerOpts(({ opts }) => {
if (!opts?.compiler) return

if (isString(opts.compiler)) {
opts.compiler = {
type: opts.compiler
}
}
const { compiler } = opts
if (compiler.type === 'webpack5') {
compiler.prebundle ||= {}
const prebundleOptions = compiler.prebundle
if (prebundleOptions.enable === false) return
prebundleOptions.swc ||= {
jsc: {
// Note: 由于百度小程序不支持 ES2015,所以这里需要将 ES5 (模拟器环境可无该问题)
target: 'es5'
}
}
prebundleOptions.exclude ||= []
prebundleOptions.include ||= []
}
})
}
1 change: 1 addition & 0 deletions packages/taro-webpack5-prebundle/src/prebundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export default class BasePrebundle<T extends IPrebundleConfig = IPrebundleConfig

await scanImports({
appPath: this.appPath,
chain: this.chain,
customEsbuildConfig: this.customEsbuildConfig,
entries,
include,
Expand Down
8 changes: 8 additions & 0 deletions packages/taro-webpack5-prebundle/src/prebundle/scanImports.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import esbuild, { Loader } from 'esbuild'
import fs from 'fs'
import path from 'path'
import Chain from 'webpack-chain'

import {
externalModule,
getDefines,
getResolve,
isExclude,
isOptimizeIncluded,
Expand All @@ -26,6 +28,7 @@ import {

interface ScanImportsConfig {
appPath: string
chain: Chain
entries: string[]
include: string[]
exclude: string[]
Expand All @@ -34,6 +37,7 @@ interface ScanImportsConfig {

export async function scanImports ({
appPath,
chain,
entries,
include = [],
exclude = [],
Expand All @@ -55,6 +59,10 @@ deps: CollectedDeps = new Map()
format: 'esm',
loader: defaultEsbuildLoader,
write: false,
define: {
...getDefines(chain),
define: 'false'
},
plugins: [
scanImportsPlugin,
...customPlugins
Expand Down