Skip to content

Commit

Permalink
feat!: resolve relative imports for mjs files (resolves #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 24, 2021
1 parent 321a1c7 commit 86b6175
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
36 changes: 31 additions & 5 deletions src/make.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import globby from 'globby'
import { resolve, extname, join, basename, dirname } from 'upath'
import { emptyDir, mkdirp, copyFile, readFile, writeFile, unlink } from 'fs-extra'
import { InputFile, LoaderOptions, createLoader, OutputFile, LoaderResult } from './loader'
import { InputFile, LoaderOptions, createLoader, OutputFile } from './loader'
import { getDeclarations } from './utils/dts'

export interface MkdistOptions extends LoaderOptions {
Expand Down Expand Up @@ -49,6 +49,12 @@ export async function mkdist (options: MkdistOptions /* istanbul ignore next */
outputs.push(...await loadFile(file) || [])
}

// Normalize output extensions
for (const output of outputs.filter(o => o.extension)) {
const renamed = basename(output.path, extname(output.path)) + output.extension
output.path = join(dirname(output.path), renamed)
}

// Generate declarations
const dtsOutputs = outputs.filter(o => o.declaration)
if (dtsOutputs.length) {
Expand All @@ -58,13 +64,33 @@ export async function mkdist (options: MkdistOptions /* istanbul ignore next */
}
}

// Resolve relative imports
const outPaths = new Set(outputs.map(o => o.path))
const resolveExts = ['', '/index.mjs', '/index.js', '.mjs', '.ts']
const resolveId = (from: string, id: string = '') => {
if (!id.startsWith('.')) {
return id
}
for (const ext of resolveExts) {
// TODO: Resolve relative ../ via ufo
if (outPaths.has(join(dirname(from), id + ext))) {
return id + ext
}
}
return id
}
for (const output of outputs.filter(o => o.extension === '.mjs')) {
// Resolve import statements
output.contents = output.contents!.replace(
/(import|export)(.* from ['"])(.*)(['"])/g,
(_, type, head, id, tail) => type + head + resolveId(output.path, id) + tail
)
}

// Write outputs
const writtenFiles: string[] = []
await Promise.all(outputs.map(async (output) => {
let outFile = join(options.distDir, output.path)
if (typeof output.extension === 'string') {
outFile = join(dirname(outFile), basename(outFile, extname(outFile)) + output.extension)
}
const outFile = join(options.distDir, output.path)
await mkdirp(dirname(outFile))
if (output.raw) {
await copyFile(output.srcPath!, outFile)
Expand Down
1 change: 1 addition & 0 deletions test/fixture/src/bar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'bar'
1 change: 1 addition & 0 deletions test/fixture/src/foo.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const a = undefined

export const test = () => a ?? 2
6 changes: 4 additions & 2 deletions test/fixture/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const a: string = 'works'
import bar from './bar'

const foo: string = 'foo'

export const importFoo = () => import('path')

export default () => a
export default () => foo + bar

0 comments on commit 86b6175

Please sign in to comment.