Comparing version 0.1.6 to 0.1.7
@@ -5,2 +5,4 @@ # Changelog | ||
### [0.1.7](https://github.com/unjs/mkdist/compare/v0.1.6...v0.1.7) (2021-04-14) | ||
### [0.1.6](https://github.com/unjs/mkdist/compare/v0.1.5...v0.1.6) (2021-04-09) | ||
@@ -7,0 +9,0 @@ |
161
dist/cli.js
@@ -38,63 +38,2 @@ #!/usr/bin/env node | ||
const compilerOptions = { | ||
allowJs: true, | ||
declaration: true, | ||
incremental: true, | ||
skipLibCheck: true, | ||
emitDeclarationOnly: true | ||
}; | ||
let _ts; | ||
async function getTs() { | ||
if (!_ts) { | ||
try { | ||
_ts = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('typescript')); }); | ||
} catch (err) { | ||
console.warn("[mkdist] Could not load `typescript` for generating types. Do you have it installed?"); | ||
throw err; | ||
} | ||
} | ||
return _ts; | ||
} | ||
const vfs = new Map(); | ||
let _tsHost; | ||
async function getTsHost() { | ||
if (!_tsHost) { | ||
const ts = await getTs(); | ||
_tsHost = ts.createCompilerHost(compilerOptions); | ||
} | ||
_tsHost.writeFile = (fileName, declaration) => { | ||
vfs.set(fileName, declaration); | ||
}; | ||
const _readFile = _tsHost.readFile; | ||
_tsHost.readFile = (filename) => { | ||
if (vfs.has(filename)) { | ||
return vfs.get(filename); | ||
} | ||
return _readFile(filename); | ||
}; | ||
return _tsHost; | ||
} | ||
async function getDeclaration(contents, filename = "_input.ts") { | ||
const dtsFilename = filename.replace(/\.(ts|js)$/, ".d.ts"); | ||
if (vfs.has(dtsFilename)) { | ||
return vfs.get(dtsFilename); | ||
} | ||
try { | ||
const ts = await getTs(); | ||
const host = await getTsHost(); | ||
if (vfs.has(filename)) { | ||
throw new Error("Race condition for generating " + filename); | ||
} | ||
vfs.set(filename, contents); | ||
const program = ts.createProgram([filename], compilerOptions, host); | ||
await program.emit(); | ||
const result = vfs.get(dtsFilename); | ||
vfs.delete(filename); | ||
return result; | ||
} catch (err) { | ||
console.warn(`Could not generate declaration file for ${filename}:`, err); | ||
return ""; | ||
} | ||
} | ||
const jsLoader = async (input, {options}) => { | ||
@@ -105,13 +44,12 @@ var _a; | ||
} | ||
const output = []; | ||
let contents = await input.getContents(); | ||
const declaration = []; | ||
if (options.declaration && !((_a = input.srcPath) == null ? void 0 : _a.endsWith(".d.ts"))) { | ||
const dtsContents = await getDeclaration(contents, input.srcPath); | ||
if (dtsContents) { | ||
declaration.push({ | ||
contents: dtsContents, | ||
path: input.path, | ||
extension: ".d.ts" | ||
}); | ||
} | ||
output.push({ | ||
contents, | ||
srcPath: input.srcPath, | ||
path: input.path, | ||
extension: ".d.ts", | ||
declaration: true | ||
}); | ||
} | ||
@@ -124,10 +62,8 @@ if (input.extension === ".ts") { | ||
} | ||
return [ | ||
{ | ||
contents, | ||
path: input.path, | ||
extension: ".js" | ||
}, | ||
...declaration | ||
]; | ||
output.push({ | ||
contents, | ||
path: input.path, | ||
extension: ".js" | ||
}); | ||
return output; | ||
}; | ||
@@ -146,3 +82,3 @@ | ||
const extension = "." + lang; | ||
const [scriptFile, ...declaration] = await loadFile({ | ||
const files = await loadFile({ | ||
getContents: () => script, | ||
@@ -153,2 +89,3 @@ path: `${input.path}${extension}`, | ||
}) || []; | ||
const scriptFile = files.find((f) => f.extension === ".js"); | ||
if (!scriptFile) { | ||
@@ -163,3 +100,3 @@ return; | ||
}, | ||
...declaration | ||
...files.filter((f) => f !== scriptFile) | ||
]; | ||
@@ -188,2 +125,33 @@ }; | ||
async function getDeclarations(input) { | ||
const vfs = new Map(Object.entries(input)); | ||
const ts = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('typescript')); }); | ||
const compilerOptions = { | ||
allowJs: true, | ||
declaration: true, | ||
incremental: true, | ||
skipLibCheck: true, | ||
emitDeclarationOnly: true | ||
}; | ||
const tsHost = ts.createCompilerHost(compilerOptions); | ||
tsHost.writeFile = (fileName, declaration) => { | ||
vfs.set(fileName, declaration); | ||
}; | ||
const _readFile = tsHost.readFile; | ||
tsHost.readFile = (filename) => { | ||
if (vfs.has(filename)) { | ||
return vfs.get(filename); | ||
} | ||
return _readFile(filename); | ||
}; | ||
const program = ts.createProgram(Object.keys(input), compilerOptions, tsHost); | ||
await program.emit(); | ||
const output = {}; | ||
for (const filename in input) { | ||
const dtsFilename = filename.replace(/\.(ts|js)$/, ".d.ts"); | ||
output[filename] = vfs.get(dtsFilename) || ""; | ||
} | ||
return output; | ||
} | ||
async function mkdist(options = {}) { | ||
@@ -214,2 +182,12 @@ options.rootDir = upath.resolve(process.cwd(), options.rootDir || "."); | ||
}); | ||
const writeOutput = async (output) => { | ||
let outFile = upath.join(options.distDir, output.path); | ||
if (typeof output.extension === "string") { | ||
outFile = upath.join(upath.dirname(outFile), upath.basename(outFile, upath.extname(outFile)) + output.extension); | ||
} | ||
await fsExtra.mkdirp(upath.dirname(outFile)); | ||
await fsExtra.writeFile(outFile, output.contents, "utf8"); | ||
writtenFiles.push(outFile); | ||
}; | ||
const declarations = []; | ||
for (const file of files) { | ||
@@ -224,12 +202,23 @@ const outputs = await loadFile(file); | ||
for (const output of outputs || []) { | ||
let outFile = upath.join(options.distDir, output.path); | ||
if (typeof output.extension === "string") { | ||
outFile = upath.join(upath.dirname(outFile), upath.basename(outFile, upath.extname(outFile)) + output.extension); | ||
if (output.declaration) { | ||
declarations.push(output); | ||
continue; | ||
} | ||
await fsExtra.mkdirp(upath.dirname(outFile)); | ||
await fsExtra.writeFile(outFile, output.contents, "utf8"); | ||
writtenFiles.push(outFile); | ||
await writeOutput(output); | ||
} | ||
} | ||
} | ||
if (declarations.length) { | ||
const input = {}; | ||
for (const d of declarations) { | ||
input[d.srcPath] = d.contents; | ||
} | ||
const res = await getDeclarations(input); | ||
for (const d of declarations) { | ||
if (res[d.srcPath]) { | ||
d.contents = res[d.srcPath]; | ||
await writeOutput(d); | ||
} | ||
} | ||
} | ||
return { | ||
@@ -236,0 +225,0 @@ writtenFiles |
@@ -12,4 +12,6 @@ interface InputFile { | ||
path: string; | ||
srcPath?: string; | ||
extension?: string; | ||
contents: string; | ||
declaration?: boolean; | ||
} | ||
@@ -16,0 +18,0 @@ declare type LoaderResult = OutputFile[] | undefined; |
@@ -36,63 +36,2 @@ 'use strict'; | ||
const compilerOptions = { | ||
allowJs: true, | ||
declaration: true, | ||
incremental: true, | ||
skipLibCheck: true, | ||
emitDeclarationOnly: true | ||
}; | ||
let _ts; | ||
async function getTs() { | ||
if (!_ts) { | ||
try { | ||
_ts = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('typescript')); }); | ||
} catch (err) { | ||
console.warn("[mkdist] Could not load `typescript` for generating types. Do you have it installed?"); | ||
throw err; | ||
} | ||
} | ||
return _ts; | ||
} | ||
const vfs = new Map(); | ||
let _tsHost; | ||
async function getTsHost() { | ||
if (!_tsHost) { | ||
const ts = await getTs(); | ||
_tsHost = ts.createCompilerHost(compilerOptions); | ||
} | ||
_tsHost.writeFile = (fileName, declaration) => { | ||
vfs.set(fileName, declaration); | ||
}; | ||
const _readFile = _tsHost.readFile; | ||
_tsHost.readFile = (filename) => { | ||
if (vfs.has(filename)) { | ||
return vfs.get(filename); | ||
} | ||
return _readFile(filename); | ||
}; | ||
return _tsHost; | ||
} | ||
async function getDeclaration(contents, filename = "_input.ts") { | ||
const dtsFilename = filename.replace(/\.(ts|js)$/, ".d.ts"); | ||
if (vfs.has(dtsFilename)) { | ||
return vfs.get(dtsFilename); | ||
} | ||
try { | ||
const ts = await getTs(); | ||
const host = await getTsHost(); | ||
if (vfs.has(filename)) { | ||
throw new Error("Race condition for generating " + filename); | ||
} | ||
vfs.set(filename, contents); | ||
const program = ts.createProgram([filename], compilerOptions, host); | ||
await program.emit(); | ||
const result = vfs.get(dtsFilename); | ||
vfs.delete(filename); | ||
return result; | ||
} catch (err) { | ||
console.warn(`Could not generate declaration file for ${filename}:`, err); | ||
return ""; | ||
} | ||
} | ||
const jsLoader = async (input, {options}) => { | ||
@@ -103,13 +42,12 @@ var _a; | ||
} | ||
const output = []; | ||
let contents = await input.getContents(); | ||
const declaration = []; | ||
if (options.declaration && !((_a = input.srcPath) == null ? void 0 : _a.endsWith(".d.ts"))) { | ||
const dtsContents = await getDeclaration(contents, input.srcPath); | ||
if (dtsContents) { | ||
declaration.push({ | ||
contents: dtsContents, | ||
path: input.path, | ||
extension: ".d.ts" | ||
}); | ||
} | ||
output.push({ | ||
contents, | ||
srcPath: input.srcPath, | ||
path: input.path, | ||
extension: ".d.ts", | ||
declaration: true | ||
}); | ||
} | ||
@@ -122,10 +60,8 @@ if (input.extension === ".ts") { | ||
} | ||
return [ | ||
{ | ||
contents, | ||
path: input.path, | ||
extension: ".js" | ||
}, | ||
...declaration | ||
]; | ||
output.push({ | ||
contents, | ||
path: input.path, | ||
extension: ".js" | ||
}); | ||
return output; | ||
}; | ||
@@ -144,3 +80,3 @@ | ||
const extension = "." + lang; | ||
const [scriptFile, ...declaration] = await loadFile({ | ||
const files = await loadFile({ | ||
getContents: () => script, | ||
@@ -151,2 +87,3 @@ path: `${input.path}${extension}`, | ||
}) || []; | ||
const scriptFile = files.find((f) => f.extension === ".js"); | ||
if (!scriptFile) { | ||
@@ -161,3 +98,3 @@ return; | ||
}, | ||
...declaration | ||
...files.filter((f) => f !== scriptFile) | ||
]; | ||
@@ -186,2 +123,33 @@ }; | ||
async function getDeclarations(input) { | ||
const vfs = new Map(Object.entries(input)); | ||
const ts = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('typescript')); }); | ||
const compilerOptions = { | ||
allowJs: true, | ||
declaration: true, | ||
incremental: true, | ||
skipLibCheck: true, | ||
emitDeclarationOnly: true | ||
}; | ||
const tsHost = ts.createCompilerHost(compilerOptions); | ||
tsHost.writeFile = (fileName, declaration) => { | ||
vfs.set(fileName, declaration); | ||
}; | ||
const _readFile = tsHost.readFile; | ||
tsHost.readFile = (filename) => { | ||
if (vfs.has(filename)) { | ||
return vfs.get(filename); | ||
} | ||
return _readFile(filename); | ||
}; | ||
const program = ts.createProgram(Object.keys(input), compilerOptions, tsHost); | ||
await program.emit(); | ||
const output = {}; | ||
for (const filename in input) { | ||
const dtsFilename = filename.replace(/\.(ts|js)$/, ".d.ts"); | ||
output[filename] = vfs.get(dtsFilename) || ""; | ||
} | ||
return output; | ||
} | ||
async function mkdist(options = {}) { | ||
@@ -212,2 +180,12 @@ options.rootDir = upath.resolve(process.cwd(), options.rootDir || "."); | ||
}); | ||
const writeOutput = async (output) => { | ||
let outFile = upath.join(options.distDir, output.path); | ||
if (typeof output.extension === "string") { | ||
outFile = upath.join(upath.dirname(outFile), upath.basename(outFile, upath.extname(outFile)) + output.extension); | ||
} | ||
await fsExtra.mkdirp(upath.dirname(outFile)); | ||
await fsExtra.writeFile(outFile, output.contents, "utf8"); | ||
writtenFiles.push(outFile); | ||
}; | ||
const declarations = []; | ||
for (const file of files) { | ||
@@ -222,12 +200,23 @@ const outputs = await loadFile(file); | ||
for (const output of outputs || []) { | ||
let outFile = upath.join(options.distDir, output.path); | ||
if (typeof output.extension === "string") { | ||
outFile = upath.join(upath.dirname(outFile), upath.basename(outFile, upath.extname(outFile)) + output.extension); | ||
if (output.declaration) { | ||
declarations.push(output); | ||
continue; | ||
} | ||
await fsExtra.mkdirp(upath.dirname(outFile)); | ||
await fsExtra.writeFile(outFile, output.contents, "utf8"); | ||
writtenFiles.push(outFile); | ||
await writeOutput(output); | ||
} | ||
} | ||
} | ||
if (declarations.length) { | ||
const input = {}; | ||
for (const d of declarations) { | ||
input[d.srcPath] = d.contents; | ||
} | ||
const res = await getDeclarations(input); | ||
for (const d of declarations) { | ||
if (res[d.srcPath]) { | ||
d.contents = res[d.srcPath]; | ||
await writeOutput(d); | ||
} | ||
} | ||
} | ||
return { | ||
@@ -234,0 +223,0 @@ writtenFiles |
{ | ||
"name": "mkdist", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "Lightweight file-to-file transformer", | ||
@@ -17,3 +17,3 @@ "repository": "unjs/mkdist", | ||
"build": "siroc build", | ||
"dev": "yarn mkdist test/fixture", | ||
"dev": "yarn mkdist test/fixture -d", | ||
"lint": "eslint --ext .ts .", | ||
@@ -20,0 +20,0 @@ "mkdist": "jiti src/cli", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21702
478