Skip to content

Commit

Permalink
fix: 修复 export 没加上 .js 后缀的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jan 29, 2024
1 parent 8b2a2ff commit 4137558
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions haoma-compile.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs-extra'
import { defineBabelPlugin, getCompileConfig } from 'haoma'
import pa from 'path'
import { FirstParameter } from './src/types'

const removeIsTypePlugin = defineBabelPlugin(t => ({
visitor: {
Expand Down Expand Up @@ -29,26 +30,30 @@ const removeIsTypePlugin = defineBabelPlugin(t => ({
},
}))

const addExtensionPlugin = defineBabelPlugin(() => ({
visitor: {
ImportDeclaration: {
exit(path, { filename }) {
const modulePath = path.node.source.value
if (modulePath.startsWith('.')) {
for (const ext of ['.ts', '/index.ts', '.js', '/index.js']) {
const modulePathWithExt = modulePath + ext
const resolvedPath = pa.join(
pa.dirname(filename),
modulePathWithExt,
)
if (fs.pathExistsSync(resolvedPath)) {
path.node.source.value = modulePathWithExt.replace(/\.ts$/, '.js')
break
}
const addExtension: ReturnType<
FirstParameter<typeof defineBabelPlugin>
>['visitor']['ImportDeclaration'] = {
exit(path, { filename }) {
if (path.node.source) {
const modulePath = path.node.source.value
if (modulePath.startsWith('.')) {
for (const ext of ['.ts', '/index.ts', '.js', '/index.js']) {
const modulePathWithExt = modulePath + ext
const resolvedPath = pa.join(pa.dirname(filename), modulePathWithExt)
if (fs.pathExistsSync(resolvedPath)) {
path.node.source.value = modulePathWithExt.replace(/\.ts$/, '.js')
break
}
}
},
},
}
}
},
}
const addExtensionPlugin = defineBabelPlugin(() => ({
visitor: {
ImportDeclaration: addExtension,
ExportAllDeclaration: addExtension as any,
ExportNamedDeclaration: addExtension as any,
},
}))

Expand Down

0 comments on commit 4137558

Please sign in to comment.