Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Fix(Pack): Wrong path resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Mar 9, 2023
1 parent 1523f1f commit 76921cc
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 35 deletions.
2 changes: 2 additions & 0 deletions packages/aronrepo/src/commands/pack.ts
Expand Up @@ -162,6 +162,8 @@ program.command('pack [entryPaths...]')
}
if (options.watch) {
await ctx.watch()
} else {
await ctx.dispose()
}
if (options.serve) {
await ctx.serve()
Expand Down
71 changes: 41 additions & 30 deletions packages/aronrepo/src/utils/esbuild-plugin-fill-module-ext.ts
Expand Up @@ -9,37 +9,48 @@ export function createFillModuleExtPlugin(outext = '.js', outdir = 'src'): Plugi
return {
name: 'fill-module-ext',
setup(build) {
const started: any = {}
started.promise = new Promise(resolve => {
started.resolve = resolve
})
build.onStart(() => {
started.resolve(true)
})
build.onLoad({ filter: /\.ts$/ }, async (args) => {
const content = await fs.promises.readFile(args.path, { encoding: 'utf8' })
return {
contents: content
.replace(/((?:(?:import|export)(?:.*from | ))|(?:(?:import))\()'((\.(?:\.)?\/.*)|\.)'/gmi,
(...matches) => {
const modulePath: string = matches[2]
const parsedModulePath = path.parse(modulePath)
if (parsedModulePath.ext) {
return matches[1]
}
let cd = '../'
if (parsedModulePath.dir.startsWith('../')) {
cd = '../../'
}
const foundModuleSourcePath = fg.sync([
path.join(args.path, parsedModulePath.dir, cd, parsedModulePath.name) + '.{ts,js,mjs,jsx,tsx}',
path.join(args.path, parsedModulePath.dir, cd, parsedModulePath.name, 'index.{ts,js,mjs,jsx,tsx}')
])[0]
if (!foundModuleSourcePath) {
return matches[0]
}
let targetModulePath = path.relative(resolvedOutdir, path.changeExt(foundModuleSourcePath, outext))
if (parsedModulePath.dir === '.' || parsedModulePath.dir === '') {
targetModulePath = './' + targetModulePath
} else {
targetModulePath = path.join(parsedModulePath.dir, targetModulePath)
}
return `${matches[1]}'${targetModulePath}'`
}),
loader: 'ts',
if (await started.promise === true) {
const content = await fs.promises.readFile(args.path, { encoding: 'utf8' })
const currentDirPath = path.dirname(args.path)
return {
contents: content
.replace(/((?:(?:import|export)(?:.*from | ))|(?:(?:import))\()'((\.(?:\.)?\/.*)|\.)'/gmi,
(...matches) => {
const currentPath = args.path
const modulePath: string = matches[2]
const parsedModulePath = path.parse(modulePath)
if (parsedModulePath.ext) {
return matches[1]
}
const targetDir = path.resolve(currentDirPath, modulePath)
const foundModuleSourcePath = fg.sync([
targetDir + '.{ts,js,mjs,jsx,tsx}',
path.join(targetDir, 'index.{ts,js,mjs,jsx,tsx}')
])[0]
if (!foundModuleSourcePath) {
return matches[0]
}
let targetModulePath = path.relative(resolvedOutdir, path.changeExt(foundModuleSourcePath, outext))
const parsedTargetModulePath = path.parse(targetModulePath)
if (modulePath === '.' || modulePath === './') {
targetModulePath = './index' + outext
} else if (parsedTargetModulePath.name === parsedModulePath.name) {
targetModulePath = modulePath + outext
} else {
targetModulePath = modulePath + '/index' + outext
}
return `${matches[1]}'${targetModulePath}'`
}),
loader: 'ts',
}
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion packages/aronrepo/tests/suffix-module-ext/src/a.ts
Expand Up @@ -2,5 +2,6 @@ export * from './b'
export * from './d'
export * from '.'
import { dIndex } from './d'
console.log(dIndex);
import { ff } from './f/ff'
console.log(dIndex, ff);
(() => import('./c'))()
2 changes: 2 additions & 0 deletions packages/aronrepo/tests/suffix-module-ext/src/d/dd.ts
@@ -0,0 +1,2 @@
import { ff } from '../f/ff'
console.log(ff)
4 changes: 2 additions & 2 deletions packages/aronrepo/tests/suffix-module-ext/src/d/index.ts
@@ -1,4 +1,4 @@
export const dIndex = 'dIndex'
export * from '../e'
import { f } from '../f/f'
console.log(f)
import { ff } from '../f/ff'
console.log(ff)
1 change: 0 additions & 1 deletion packages/aronrepo/tests/suffix-module-ext/src/f/f.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/aronrepo/tests/suffix-module-ext/src/f/ff.ts
@@ -0,0 +1 @@
export const ff = 'ff'
2 changes: 1 addition & 1 deletion packages/aronrepo/tests/suffix-module-ext/test.ts
Expand Up @@ -10,7 +10,7 @@ it('suffix es module with outext', () => {
], { cwd: __dirname })
expectFileIncludes('dist/esm/d/index.js', [
'"../e.js"',
'"../f/f"'
'"../f/ff.js"'
], { cwd: __dirname })
})

0 comments on commit 76921cc

Please sign in to comment.