From c611a69da610b39e7179f7473702a2c65434f2c0 Mon Sep 17 00:00:00 2001 From: qican77 Date: Fri, 8 Sep 2023 14:34:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81commonjs=E5=92=8Ces6?= =?UTF-8?q?=E7=9A=84=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA=E6=B7=B7=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-transformer-wx/src/index.ts | 2 + packages/taroize/package.json | 1 + packages/taroize/src/script.ts | 53 +++++++++++++++++++++++ packages/taroize/src/utils.ts | 3 ++ pnpm-lock.yaml | 10 +++-- 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/packages/taro-transformer-wx/src/index.ts b/packages/taro-transformer-wx/src/index.ts index eea77b6b01ee..7d5fdb88274d 100644 --- a/packages/taro-transformer-wx/src/index.ts +++ b/packages/taro-transformer-wx/src/index.ts @@ -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' @@ -227,6 +228,7 @@ function parseCode(code: string) { [decorators, { legacy: true }], dynamicImport, optionalChaining, + babel_plugin_transform_commonjs, ], }) as { ast: t.File } ).ast diff --git a/packages/taroize/package.json b/packages/taroize/package.json index ba2cb50f9e1c..acdbcffb2ba7 100644 --- a/packages/taroize/package.json +++ b/packages/taroize/package.json @@ -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", diff --git a/packages/taroize/src/script.ts b/packages/taroize/src/script.ts index c877dad52931..c036cb8eda40 100644 --- a/packages/taroize/src/script.ts +++ b/packages/taroize/src/script.ts @@ -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) diff --git a/packages/taroize/src/utils.ts b/packages/taroize/src/utils.ts index e873e81344a2..4a6f1aab9982 100644 --- a/packages/taroize/src/utils.ts +++ b/packages/taroize/src/utils.ts @@ -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, name: string) { @@ -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 @@ -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 diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index de2f76667b53..00faa40fa7c7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4388,6 +4388,9 @@ importers: babel-generator: specifier: ^6.26.1 version: registry.npmjs.org/babel-generator@6.26.1 + babel-plugin-transform-commonjs: + specifier: ^1.1.6 + version: registry.npmjs.org/babel-plugin-transform-commonjs@1.1.6(@babel/core@7.21.8) babel-template: specifier: ^6.26.0 version: registry.npmjs.org/babel-template@6.26.0 @@ -16060,8 +16063,6 @@ packages: babel-register: registry.npmjs.org/babel-register@6.26.0 babylon: registry.npmjs.org/babylon@6.18.0 require-from-string: registry.npmjs.org/require-from-string@2.0.2 - transitivePeerDependencies: - - supports-color dev: false registry.npmjs.org/babel-plugin-react-native-web@0.18.12: @@ -16982,6 +16983,7 @@ packages: resolution: {integrity: sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==, registry: https://registry.yarnpkg.com/, tarball: https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz} name: bindings version: 1.5.0 + requiresBuild: true dependencies: file-uri-to-path: registry.npmjs.org/file-uri-to-path@1.0.0 optional: true @@ -23144,6 +23146,7 @@ packages: resolution: {integrity: sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==, registry: https://registry.yarnpkg.com/, tarball: https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz} name: file-uri-to-path version: 1.0.0 + requiresBuild: true optional: true registry.npmjs.org/filelist@1.0.4: @@ -30714,6 +30717,7 @@ packages: resolution: {integrity: sha512-2ZTgtl0nJsO0KQCjEpxcIr5D+Yv90plTitZt9JBfQvVJDS5seMl3FOvsh3+9CoYWXf/1l5OaZzzF6nDm4cagaQ==, registry: https://registry.yarnpkg.com/, tarball: https://registry.npmjs.org/nan/-/nan-2.17.0.tgz} name: nan version: 2.17.0 + requiresBuild: true optional: true registry.npmjs.org/nanoclone@0.2.1: @@ -40339,7 +40343,7 @@ packages: mime-types: registry.npmjs.org/mime-types@2.1.35 range-parser: registry.npmjs.org/range-parser@1.2.1 schema-utils: registry.npmjs.org/schema-utils@4.0.1 - webpack: registry.npmjs.org/webpack@5.78.0(@swc/core@1.3.42)(webpack-cli@5.0.1) + webpack: registry.npmjs.org/webpack@5.78.0 registry.npmjs.org/webpack-dev-server@3.11.3(webpack@4.46.0): resolution: {integrity: sha512-3x31rjbEQWKMNzacUZRE6wXvUFuGpH7vr0lIEbYpMAG9BOxi0928QU1BBswOAP3kg3H1O4hiS+sq4YyAn6ANnA==, registry: https://registry.yarnpkg.com/, tarball: https://registry.npmjs.org/webpack-dev-server/-/webpack-dev-server-3.11.3.tgz}