Skip to content

Commit

Permalink
refactor: improve mkdist implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 24, 2021
1 parent 0606b22 commit 321a1c7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
10 changes: 9 additions & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export interface OutputFile {
path: string
srcPath?: string
extension?: string
contents: string
contents?: string
declaration?: boolean
raw?: boolean
}

export type LoaderResult = OutputFile[] | undefined
Expand Down Expand Up @@ -56,6 +57,13 @@ export function createLoader (loaderOptions: CreateLoaderOptions = {}) {
return outputs
}
}
return [
{
path: input.path,
srcPath: input.srcPath,
raw: true
}
]
}

return {
Expand Down
63 changes: 25 additions & 38 deletions src/make.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export async function mkdist (options: MkdistOptions /* istanbul ignore next */
await mkdirp(options.distDir)
}

// Scan input files
const filePaths = await globby('**', { absolute: false, cwd: options.srcDir })

const files: InputFile[] = filePaths.map((path) => {
const srcPath = resolve(options.srcDir, path)
return {
Expand All @@ -36,56 +36,43 @@ export async function mkdist (options: MkdistOptions /* istanbul ignore next */
}
})

const writtenFiles: string[] = []

// Create loader
const { loadFile } = createLoader({
format: options.format,
ext: options.ext,
declaration: options.declaration
})

const writeOutput = async (output: OutputFile) => {
let outFile = join(options.distDir, output.path)
if (typeof output.extension === 'string') {
outFile = join(dirname(outFile), basename(outFile, extname(outFile)) + output.extension)
}
await mkdirp(dirname(outFile))
await writeFile(outFile, output.contents, 'utf8')
writtenFiles.push(outFile)
// Use loaders to get output files
const outputs: OutputFile[] = []
for (const file of files) {
outputs.push(...await loadFile(file) || [])
}

const declarations: LoaderResult = []
for (const file of files) {
const outputs = await loadFile(file)
if (file.srcPath && (!outputs || !outputs.length)) {
const outFile = join(options.distDir, file.path)
await mkdirp(dirname(outFile))
await copyFile(file.srcPath, outFile)
writtenFiles.push(outFile)
} else {
for (const output of outputs /* istanbul ignore next */ || []) {
if (output.declaration) {
declarations.push(output)
continue
}
await writeOutput(output)
}
// Generate declarations
const dtsOutputs = outputs.filter(o => o.declaration)
if (dtsOutputs.length) {
const declarations = await getDeclarations(new Map(dtsOutputs.map(o => [o.srcPath!, o.contents || ''])))
for (const output of dtsOutputs) {
output.contents = declarations[output.srcPath!] || ''
}
}

if (declarations.length) {
const input: Record<string, string> = {}
for (const d of declarations) {
input[d.srcPath!] = d.contents
// 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 res = await getDeclarations(input)
for (const d of declarations) {
if (res[d.srcPath!]) {
d.contents = res[d.srcPath!]
await writeOutput(d)
}
await mkdirp(dirname(outFile))
if (output.raw) {
await copyFile(output.srcPath!, outFile)
} else {
await writeFile(outFile, output.contents, 'utf8')
}
}
writtenFiles.push(outFile)
}))

return {
writtenFiles
Expand Down
10 changes: 5 additions & 5 deletions src/utils/dts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export async function getDeclarations (input: Record<string, string>) {
const vfs: Map<string, string> = new Map(Object.entries(input))

export async function getDeclarations (vfs: Map<string, string>) {
const ts = await import('typescript')

const inputFiles = [...vfs.keys()]

const compilerOptions = {
allowJs: true,
declaration: true,
Expand All @@ -21,12 +21,12 @@ export async function getDeclarations (input: Record<string, string>) {
return _readFile(filename)
}

const program = ts.createProgram!(Object.keys(input), compilerOptions, tsHost)
const program = ts.createProgram!(inputFiles, compilerOptions, tsHost)
await program.emit()

const output: Record<string, string> = {}

for (const filename in input) {
for (const filename of inputFiles) {
const dtsFilename = filename.replace(/\.(ts|js)$/, '.d.ts')
output[filename] = vfs.get(dtsFilename) || ''
}
Expand Down

0 comments on commit 321a1c7

Please sign in to comment.