From 6d68e45271ce86a285f463bec11a97d98f7f179a Mon Sep 17 00:00:00 2001 From: mapengda Date: Fri, 12 Mar 2021 15:03:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BB=86=E8=8A=82=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __test__/index.spec.ts | 11 +++++++---- package.json | 2 +- src/replace/exports.ts | 1 - src/replace/require.ts | 8 +++----- src/utils/base.ts | 20 ++++++++++++++------ 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/__test__/index.spec.ts b/__test__/index.spec.ts index 256810e..da240c4 100644 --- a/__test__/index.spec.ts +++ b/__test__/index.spec.ts @@ -2,7 +2,7 @@ import { transformFileBase } from "../src/utils/base"; const content = `const babel = require('babel') const types = require('babel').types -const { parser } = require('babel') +const { parser, generate, } = require('babel') const a = 123; const b = 'sad'; @@ -13,12 +13,14 @@ exports.a = a exports.b = b exports.c = c exports.d = d +export const e = 123 -module.exports = a` +exports["default"] = "test" +module.exports = exports["default"]` const expectStr = `import babel from 'babel'; import { types } from 'babel'; -import { parser } from 'babel'; +import { parser, generate } from 'babel'; const _a = 123; const _b = 'sad'; const _c = true; @@ -29,7 +31,8 @@ export const a = _a; export const b = _b; export const c = _c; export const d = _d; -export default _a;` +export const e = 123; +export default "test";` it("快照", () => { const { code } = transformFileBase(content) diff --git a/package.json b/package.json index 019252a..353b779 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cjs2esmodule", - "version": "1.0.1", + "version": "1.0.2", "main": "lib/index.js", "description": "将commonjs转为es module,可在vite中使用,也可直接转换文件", "license": "MIT", diff --git a/src/replace/exports.ts b/src/replace/exports.ts index cde81fe..db2befa 100644 --- a/src/replace/exports.ts +++ b/src/replace/exports.ts @@ -2,7 +2,6 @@ import * as t from '@babel/types' import { getName, getSafe } from '../utils/get'; export function replaceExports(node, path) { - const objName = getSafe(node, 'left.object.name'); const name = getName(node, 'left.property') diff --git a/src/replace/require.ts b/src/replace/require.ts index 19a3ba2..3a62498 100644 --- a/src/replace/require.ts +++ b/src/replace/require.ts @@ -1,11 +1,12 @@ import * as t from '@babel/types' +import { getName } from '../utils/get' export function replaceWithProp(ctr, path) { // const cmp = require('react').Component // ctr.id.name: cmp // ctr.init.property.name: Component // ctr.init.object.arguments[0].name: react - if (ctr.init.object.callee && ctr.init.object.callee.name === 'require') { + if (getName(ctr, `init.object.callee`) === 'require') { path.replaceWith( t.importDeclaration( [ @@ -22,7 +23,7 @@ export function replaceWithDefault(ctr, path) { // const react1 = require('react') // ctr.id.name: react1 // ctr.init.object.arguments[0].name: react - if (ctr.init.callee.name === 'require') { + if (getName(ctr, `init.callee`) === 'require') { path.replaceWith( t.importDeclaration( [ @@ -37,8 +38,6 @@ export function replaceWithDefault(ctr, path) { export function replaceWithRest(ctr, path) { // const { react: react1 } = require('react') - // ctr.id.name: react1 - // ctr.init.object.arguments[0].name: react if (ctr.init.callee.name === 'require') { path.replaceWith( @@ -54,7 +53,6 @@ export function replaceWithRest(ctr, path) { export function replaceWithRequire(path) { // require('react') if (path.node.callee.name === 'require') { - const arg = path.node.arguments[0] path.parentPath.replaceWith( t.importDeclaration( [], diff --git a/src/utils/base.ts b/src/utils/base.ts index b0a76eb..e54323c 100644 --- a/src/utils/base.ts +++ b/src/utils/base.ts @@ -32,20 +32,28 @@ export const visitors = { AssignmentExpression(path) { try { const { node } = path - replaceExports(node, path) + replaceExports(node, path) } catch(err) { console.log(err) } }, } -export const transformFileBase = (src, id = '') => { - const ast = parser.parse(src); - traverse(ast, visitors) +export const transformFileBase = (src) => { + try { + const ast = parser.parse(src); + traverse(ast, visitors); - return generate(ast,); + return generate(ast,); + } catch(err) { + // sourceType 默认为 script,如果报错,则用 module 解析 + const ast = parser.parse(src, { sourceType: "module" }); + traverse(ast, visitors); + + return generate(ast,); + } } export function isCjsFile(content) { - return /(exports[\.\[]|module\.exports)/g.test(content) + return /(exports[\.\[]|module\.exports|require\()/g.test(content) } \ No newline at end of file