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

chore(helper): 升级 swc 与 esbuild #14825

Merged
merged 12 commits into from
Nov 14, 2023
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
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ packages/taro-runtime/src/polyfill

packages/taro-plugin-react-devtools/src/backend
packages/taro-plugin-vue-devtools/src/backend

packages/taro-helper/swc
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@babel/runtime": "^7.14.5",
"@babel/traverse": "^7.14.5",
"@commitlint/cli": "^17.6.6",
"@commitlint/config-conventional": "^16.2.1",
"@commitlint/config-conventional": "^17.6.6",
"@tarojs/components": "workspace:*",
"@tarojs/helper": "workspace:*",
"@tarojs/mini-runner": "workspace:*",
Expand Down
4 changes: 3 additions & 1 deletion packages/postcss-unit-transform/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const config = require('../../.eslintrc.js')

module.exports = {
parser: '@typescript-eslint/parser',
parser: config.parser,
plugins: [
'@typescript-eslint'
],
Expand Down
4 changes: 0 additions & 4 deletions packages/postcss-unit-transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@
"dependencies": {
"postcss": "^6.0.21",
"typescript": "^4.7.4"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.17.0",
"@typescript-eslint/parser": "^5.17.0"
}
}
2 changes: 1 addition & 1 deletion packages/taro-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@types/node": "^14.14.31",
"change-case": "^4.1.2",
"csstype": "^3.1.1",
"esbuild": "^0.14.27",
"esbuild": "~0.19.5",
"jquery": "^3.4.1",
"lodash": "^4.17.21",
"miniapp-types": "1.6.0",
Expand Down
1 change: 0 additions & 1 deletion packages/taro-helper/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
swc/**/.cargo
swc/**/target/*
!swc/**/target/wasm32-wasi
swc/**/target/wasm32-wasi/*
Expand Down
6 changes: 3 additions & 3 deletions packages/taro-helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@
"@babel/register": "^7.14.5",
"@babel/runtime": "^7.14.5",
"@babel/traverse": "^7.14.5",
"@swc/core": "1.3.23",
"@swc/register": "^0.1.10",
"@swc/core": "1.3.96",
"@swc/register": "0.1.10",
"ansi-escapes": "^4.3.2",
"chalk": "3.0.0",
"chokidar": "^3.3.1",
"cross-spawn": "^7.0.3",
"debug": "4.3.4",
"dotenv": "^16.0.3",
"dotenv-expand": "^9.0.0",
"esbuild": "^0.14.27",
"esbuild": "~0.19.5",
"find-yarn-workspace-root": "2.0.0",
"fs-extra": "^8.0.1",
"lodash": "^4.17.21",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default defineAppConfig({
pages: [
'pages/index/index',
],
].map(item => item),
window: {
backgroundTextStyle: 'light',
navigationBarBackgroundColor: '#fff',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import t from '@/utils/i18n.ts'

export default definePageConfig({
navigationBarTitleText: t()
})
5 changes: 5 additions & 0 deletions packages/taro-helper/src/__tests__/__mocks__/page.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config = {
navigationBarTitleText: 'index'
}

export default config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default definePageConfig({
navigationBarTitleText: IS_BUILD_COMPONENT ? 'comp' : 'index'
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default definePageConfig({
navigationBarTitleText: 'index'
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config = definePageConfig({
navigationBarTitleText: 'index'
})

module.exports = config
3 changes: 3 additions & 0 deletions packages/taro-helper/src/__tests__/__mocks__/utils/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const t = () => 'i18n'

export default t
44 changes: 42 additions & 2 deletions packages/taro-helper/src/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,57 @@ describe('readConfig', () => {
navigationBarTextStyle: 'black'
}
}
const pageConfig = {
navigationBarTitleText: 'index'
}

test('read config without tips', async () => {
test('read app config without tips', async () => {
const result = readConfig(path.join(__dirname, './__mocks__/app.config.ts'))
expect(result).toEqual(config)
})

test('read config with tips', async () => {
test('read app config with tips', async () => {
const result = readConfig(path.join(__dirname, './__mocks__/app.define.config.ts'))
expect(result).toEqual(config)
})

test('read page config without tips', async () => {
const result = readConfig(path.join(__dirname, './__mocks__/page.config.ts'))
expect(result).toEqual(pageConfig)
})

test('read page config with tips', async () => {
const result = readConfig(path.join(__dirname, './__mocks__/page.define.config.ts'))
expect(result).toEqual(pageConfig)
})

test('read page config with module.exports', async () => {
const result = readConfig(path.join(__dirname, './__mocks__/page.es5.config.ts'))
expect(result).toEqual(pageConfig)
})

test('read page config with alias', async () => {
const result = readConfig(path.join(__dirname, './__mocks__/page.alias.config.ts'), {
alias: {
'@/utils': path.resolve(__dirname, './__mocks__/utils'),
}
})
expect(result).toEqual({
navigationBarTitleText: 'i18n'
})
})

test('read page config with defineConstants', async () => {
const result = readConfig(path.join(__dirname, './__mocks__/page.define-constants.config.ts'), {
defineConstants: {
IS_BUILD_COMPONENT: 'true'
}
})
expect(result).toEqual({
navigationBarTitleText: 'comp'
})
})

test('read config with import', async () => {
const logSpy = jest.spyOn(console, 'log')
logSpy.mockImplementation(() => {})
Expand Down
1 change: 1 addition & 0 deletions packages/taro-helper/src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function requireWithEsbuild (id: string, {
// 导致这些模块报错(如 lodash)。目前的办法是把 define 置为 false,不支持 AMD 导出。
define: 'false'
}),
alias: customConfig.alias || {},
entryPoints: [id],
format: 'esm',
loader: defaults(customConfig.loader, defaultEsbuildLoader),
Expand Down
18 changes: 0 additions & 18 deletions packages/taro-helper/src/swcRegister.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
import { CallExpression } from '@swc/core'
import { Visitor } from '@swc/core/Visitor.js'

export class InjectDefineConfigHeader extends Visitor {
visitTsType (expression) {
return expression
}

visitCallExpression (expression: CallExpression) {
const callee = expression.callee
if (callee.type === 'Identifier' && (callee.value === 'definePageConfig' || callee.value === 'defineAppConfig')) {
return expression.arguments[0].expression
}

return expression
}
}

interface ICreateSwcRegisterParam {
only
plugins?: [string, any][]
Expand Down
11 changes: 10 additions & 1 deletion packages/taro-helper/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,22 @@ export function readPageConfig (configPath: string) {
return result
}

export function readConfig (configPath: string) {
interface IReadConfigOptions {
defineConstants?: Record<string, any>
alias?: Record<string, any>
}

export function readConfig<T extends IReadConfigOptions> (configPath: string, options: T = {} as T) {
let result: any = {}
if (fs.existsSync(configPath)) {
if (REG_JSON.test(configPath)) {
result = fs.readJSONSync(configPath)
} else {
result = requireWithEsbuild(configPath, {
customConfig: {
define: options.defineConstants || {},
alias: options.alias || {},
},
customSwcConfig: {
jsc: {
parser: {
Expand Down
5 changes: 5 additions & 0 deletions packages/taro-helper/swc/plugin-define-config/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# These command aliases are not final, may change
[alias]
# Alias to build actual plugin binary for the specified target.
build-wasi = "build --target wasm32-wasi"
build-wasm32 = "build --target wasm32-unknown-unknown"
Loading