Skip to content

Commit

Permalink
fix: 细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
mapengda committed Mar 12, 2021
1 parent 13f217d commit 6d68e45
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
11 changes: 7 additions & 4 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 0 additions & 1 deletion src/replace/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
8 changes: 3 additions & 5 deletions src/replace/require.ts
Original file line number Diff line number Diff line change
@@ -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(
[
Expand All @@ -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(
[
Expand All @@ -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(
Expand All @@ -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(
[],
Expand Down
20 changes: 14 additions & 6 deletions src/utils/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6d68e45

Please sign in to comment.