Skip to content

Commit

Permalink
feat: 支持commonjs和es6的导入导出混用
Browse files Browse the repository at this point in the history
  • Loading branch information
qican77 committed Sep 8, 2023
1 parent 87c733e commit c611a69
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/taro-transformer-wx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import flowStrip from '@babel/plugin-transform-flow-strip-types'
import jsxPlugin from '@babel/plugin-transform-react-jsx'
import traverse, { Binding, NodePath } from '@babel/traverse'
import * as t from '@babel/types'
import babel_plugin_transform_commonjs from 'babel-plugin-transform-commonjs'
// import * as template from '@babel/template'
// const template = require('babel-template')
import { prettyPrint } from 'html'
Expand Down Expand Up @@ -227,6 +228,7 @@ function parseCode(code: string) {
[decorators, { legacy: true }],
dynamicImport,
optionalChaining,
babel_plugin_transform_commonjs,
],
}) as { ast: t.File }
).ast
Expand Down
1 change: 1 addition & 0 deletions packages/taroize/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@babel/types": "^7.21.4",
"babel-core": "^6.26.3",
"babel-generator": "^6.26.1",
"babel-plugin-transform-commonjs": "^1.1.6",
"babel-template": "^6.26.0",
"babel-traverse": "^6.26.0",
"babel-types": "^6.26.0",
Expand Down
53 changes: 53 additions & 0 deletions packages/taroize/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,59 @@ export function parseScript (
path.remove()
}
},
VariableDeclaration (path) {
const variableDeclarator = path.node.declarations[0]
variableDeclarator.init
if (t.isVariableDeclarator(variableDeclarator)) {
// 去除commonjs转es6生成的var module = {exports: {},}
if (
t.isIdentifier(variableDeclarator.id) &&
variableDeclarator.id.name === 'module' &&
t.isObjectExpression(variableDeclarator.init)
) {
const init: t.ObjectExpression = variableDeclarator.init
if (
init.properties &&
init.properties.length > 0 &&
t.isObjectProperty(init.properties[0]) &&
t.isIdentifier(init.properties[0].key) &&
init.properties[0].key.name === 'exports'
) {
path.remove()
}
}
// 去除commonjs转es6生成的var exports = module.exports
if (
t.isIdentifier(variableDeclarator.id) &&
variableDeclarator.id.name === 'exports' &&
t.isMemberExpression(variableDeclarator.init)
) {
const init: t.MemberExpression = variableDeclarator.init
if (
t.isIdentifier(init.object) &&
init.object.name === 'module' &&
t.isIdentifier(init.property) &&
init.property.name === 'exports'
) {
path.remove()
}
}
}
},
ExportDefaultDeclaration (path) {
const declaration = path.node.declaration
// 去除commonjs转es6生成的export default module.exports
if (t.isMemberExpression(declaration)) {
if (
t.isIdentifier(declaration.object) &&
declaration.object.name === 'module' &&
t.isIdentifier(declaration.property) &&
declaration.property.name === 'exports'
) {
path.remove()
}
}
},
}

traverse(ast, vistor)
Expand Down
3 changes: 3 additions & 0 deletions packages/taroize/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import presetTypescript from '@babel/preset-typescript'
import { default as template } from '@babel/template'
import { NodePath } from '@babel/traverse'
import * as t from '@babel/types'
import babel_plugin_transform_commonjs from 'babel-plugin-transform-commonjs'
import { camelCase, capitalize } from 'lodash'

export function isAliasThis (p: NodePath<t.Node>, name: string) {
Expand Down Expand Up @@ -62,6 +63,7 @@ export function parseCode (code: string, scriptPath?: string) {
[decorators, { legacy: true }],
dynamicImport,
optionalChaining,
babel_plugin_transform_commonjs,
],
}) as { ast: t.File }
).ast
Expand All @@ -81,6 +83,7 @@ export function parseCode (code: string, scriptPath?: string) {
[decorators, { legacy: true }],
dynamicImport,
optionalChaining,
babel_plugin_transform_commonjs,
],
}) as { ast: t.File }
).ast
Expand Down
10 changes: 7 additions & 3 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 c611a69

Please sign in to comment.