@chialab/esbuild-rna
Advanced tools
Comparing version 0.15.14 to 0.15.17
140
lib/index.js
import path from 'path'; | ||
import crypto from 'crypto'; | ||
import { mkdir, readFile, writeFile, rm } from 'fs/promises'; | ||
import { mkdir, readFile, writeFile } from 'fs/promises'; | ||
import commondir from 'commondir'; | ||
import { appendSearchParam, escapeRegexBody } from '@chialab/node-resolve'; | ||
@@ -51,3 +52,3 @@ import { loadSourcemap, inlineSourcemap, mergeSourcemaps } from '@chialab/estransform'; | ||
/** | ||
* @typedef {{ code: string, map?: import('@chialab/estransform').SourceMap|null, resolveDir?: string }} OnTransformResult | ||
* @typedef {{ code: string, map?: import('@chialab/estransform').SourceMap|null, resolveDir?: string, errors?: import('esbuild').Message[], warnings?: import('esbuild').Message[] }} OnTransformResult | ||
*/ | ||
@@ -113,21 +114,13 @@ | ||
/** | ||
* Create file path replacing esbuild patterns. | ||
* @see https://esbuild.github.io/api/#chunk-names | ||
* @param {string} pattern The esbuild pattern. | ||
* @param {string} filePath The full file path. | ||
* @param {Buffer|string} buffer The file contents. | ||
* Get the base out path. | ||
* @param {string} cwd The current working directory. | ||
* @param {string[] | Record<string, string>} entryPoints The entry points. | ||
* @return {string} | ||
*/ | ||
export function computeName(pattern, filePath, buffer) { | ||
const inputFile = path.basename(filePath); | ||
function getOutBase(cwd, entryPoints) { | ||
const files = (Array.isArray(entryPoints) ? entryPoints : Object.values(entryPoints)) | ||
.map((entry) => (path.isAbsolute(entry) ? entry : path.resolve(cwd, entry))) | ||
.map((entry) => path.dirname(entry)); | ||
return `${pattern | ||
.replace('[name]', path.basename(inputFile, path.extname(inputFile))) | ||
.replace('[ext]', path.extname(inputFile)) | ||
.replace('[hash]', () => { | ||
const hash = crypto.createHash('sha1'); | ||
hash.update(/** @type {Buffer} */ (buffer)); | ||
return hash.digest('hex').substr(0, 8); | ||
}) | ||
}${path.extname(inputFile)}`; | ||
return commondir(files); | ||
} | ||
@@ -143,3 +136,3 @@ | ||
const { esbuild } = build; | ||
const { stdin, sourceRoot, absWorkingDir, outdir, outfile, loader = {}, write = true } = build.initialOptions; | ||
const { stdin, sourceRoot, absWorkingDir, outdir, outfile, outbase, entryPoints = [], loader = {}, write = true } = build.initialOptions; | ||
const loaders = { | ||
@@ -165,5 +158,6 @@ ...DEFAULT_LOADERS, | ||
const workingDir = absWorkingDir || process.cwd(); | ||
const rootDir = sourceRoot || absWorkingDir || process.cwd(); | ||
const rootDir = sourceRoot || workingDir; | ||
const outDir = /** @type {string} */(outdir || (outfile && path.dirname(outfile))); | ||
const fullOutDir = /** @type {string} */(outDir && path.resolve(workingDir, outDir)); | ||
const outBase = outbase || (entryPoints.length ? getOutBase(workingDir, entryPoints) : workingDir); | ||
@@ -188,2 +182,6 @@ const rnaBuild = { | ||
/** | ||
* Compute the outbase dir. | ||
*/ | ||
outBase, | ||
/** | ||
* Compute the build root dir. | ||
@@ -205,2 +203,24 @@ */ | ||
/** | ||
* Create file path replacing esbuild patterns. | ||
* @see https://esbuild.github.io/api/#chunk-names | ||
* @param {string} pattern The esbuild pattern. | ||
* @param {string} filePath The full file path. | ||
* @param {Buffer|string} buffer The file contents. | ||
* @return {string} | ||
*/ | ||
computeName(pattern, filePath, buffer) { | ||
const inputFile = path.basename(filePath); | ||
return `${pattern | ||
.replace('[name]', path.basename(inputFile, path.extname(inputFile))) | ||
.replace('[ext]', path.extname(inputFile)) | ||
.replace('[dir]', path.relative(outBase, path.dirname(filePath))) | ||
.replace('[hash]', () => { | ||
const hash = crypto.createHash('sha1'); | ||
hash.update(/** @type {Buffer} */(buffer)); | ||
return hash.digest('hex').substr(0, 8); | ||
}) | ||
}${path.extname(inputFile)}`; | ||
}, | ||
/** | ||
* Iterate build.onLoad hooks in order to programmatically load file contents. | ||
@@ -255,2 +275,4 @@ * @param {OnLoadArgs} args The load arguments. | ||
const maps = []; | ||
const warnings = []; | ||
const errors = []; | ||
for (const { options, callback } of transform) { | ||
@@ -273,2 +295,8 @@ const { namespace: optionsNamespace = 'file', filter } = options; | ||
code = result.code; | ||
if (result.warnings) { | ||
warnings.push(...result.warnings); | ||
} | ||
if (result.errors) { | ||
errors.push(...result.errors); | ||
} | ||
if (result.map) { | ||
@@ -288,2 +316,4 @@ maps.push(result.map); | ||
resolveDir, | ||
warnings, | ||
errors, | ||
}; | ||
@@ -305,2 +335,4 @@ } | ||
resolveDir, | ||
warnings, | ||
errors, | ||
}; | ||
@@ -317,5 +349,18 @@ }, | ||
buffer = buffer || await readFile(source); | ||
if (!buffer) { | ||
const result = await rnaBuild.load({ | ||
pluginData: null, | ||
namespace: 'file', | ||
suffix: '', | ||
path: source, | ||
}); | ||
const computedName = computeName(assetNames, source, buffer); | ||
if (result.contents) { | ||
buffer = Buffer.from(result.contents); | ||
} else { | ||
buffer = await readFile(source); | ||
} | ||
} | ||
const computedName = rnaBuild.computeName(assetNames, source, buffer); | ||
const outputFile = path.join(fullOutDir, computedName); | ||
@@ -576,3 +621,2 @@ const bytes = buffer.length; | ||
build.onEnd(async (buildResult) => { | ||
const loaders = { ...DEFAULT_LOADERS, ...(build.initialOptions.loader || {}) }; | ||
const rnaResult = /** @type {Result} */ (buildResult); | ||
@@ -582,52 +626,2 @@ rnaResult.dependencies = state.dependencies; | ||
rnaBuild.files.forEach((result) => assignToResult(rnaResult, result)); | ||
if (buildResult.outputFiles && buildResult.outputFiles.length) { | ||
const mainFile = buildResult.outputFiles[0].path; | ||
const mainExt = path.extname(mainFile); | ||
if (mainExt !== '.js') { | ||
const jsFile = buildResult.outputFiles[1]; | ||
if (jsFile && jsFile.path.endsWith('.js')) { | ||
let realFileName = path.join(path.dirname(jsFile.path), path.basename(jsFile.path, '.js')); | ||
if (path.extname(realFileName) !== mainExt) { | ||
realFileName += mainExt; | ||
} | ||
buildResult.outputFiles[0].path = realFileName; | ||
buildResult.outputFiles.splice(1, 1); | ||
} | ||
} | ||
} | ||
if (buildResult.metafile) { | ||
// remove .js outputs for non js entryPoints | ||
const outputs = { ...buildResult.metafile.outputs }; | ||
for (const outputKey in outputs) { | ||
const output = outputs[outputKey]; | ||
if (!output.entryPoint) { | ||
continue; | ||
} | ||
const entryPoint = path.resolve(rootDir, output.entryPoint.split('?')[0]); | ||
const dependencies = Object.keys(output.inputs) | ||
.map((input) => path.resolve(rootDir, input.split('?')[0])); | ||
rnaBuild.collectDependencies(entryPoint, dependencies); | ||
if (path.extname(outputKey) === '.js') { | ||
const entryLoader = loaders[path.extname(entryPoint)] || 'file'; | ||
if (entryLoader !== 'file' && entryLoader !== 'css') { | ||
continue; | ||
} | ||
if (write) { | ||
const fullOutputKey = path.join(workingDir, outputKey); | ||
await rm(fullOutputKey); | ||
try { | ||
await rm(`${fullOutputKey}.map`); | ||
} catch (err) { | ||
// | ||
} | ||
} | ||
delete buildResult.metafile.outputs[outputKey]; | ||
} | ||
} | ||
} | ||
}); | ||
@@ -634,0 +628,0 @@ } |
{ | ||
"name": "@chialab/esbuild-rna", | ||
"type": "module", | ||
"version": "0.15.14", | ||
"version": "0.15.17", | ||
"description": "A framework for esbuild plugins with transform and emit capabilities.", | ||
@@ -31,10 +31,12 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@chialab/estransform": "^0.15.14", | ||
"@chialab/node-resolve": "^0.15.9" | ||
"@chialab/estransform": "^0.15.17", | ||
"@chialab/node-resolve": "^0.15.9", | ||
"commondir": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"@types/commondir": "^1.0.0", | ||
"esbuild": "^0.14.8", | ||
"typescript": "^4.3.0" | ||
}, | ||
"gitHead": "a48a62a5bbec43f2cc45d260e8f5f4e2b2b52759" | ||
"gitHead": "2cf976d115889891248373ca6253d1e9eca79339" | ||
} |
/// <reference types="node" /> | ||
/** | ||
* Create file path replacing esbuild patterns. | ||
* @see https://esbuild.github.io/api/#chunk-names | ||
* @param {string} pattern The esbuild pattern. | ||
* @param {string} filePath The full file path. | ||
* @param {Buffer|string} buffer The file contents. | ||
* @return {string} | ||
*/ | ||
export function computeName(pattern: string, filePath: string, buffer: Buffer | string): string; | ||
/** | ||
* Enrich the esbuild build with a transformation pipeline and emit methods. | ||
@@ -33,2 +24,6 @@ * @param {import('esbuild').PluginBuild} build The esbuild build. | ||
/** | ||
* Compute the outbase dir. | ||
*/ | ||
outBase: string; | ||
/** | ||
* Compute the build root dir. | ||
@@ -52,2 +47,11 @@ */ | ||
/** | ||
* Create file path replacing esbuild patterns. | ||
* @see https://esbuild.github.io/api/#chunk-names | ||
* @param {string} pattern The esbuild pattern. | ||
* @param {string} filePath The full file path. | ||
* @param {Buffer|string} buffer The file contents. | ||
* @return {string} | ||
*/ | ||
computeName(pattern: string, filePath: string, buffer: Buffer | string): string; | ||
/** | ||
* Iterate build.onLoad hooks in order to programmatically load file contents. | ||
@@ -65,2 +69,4 @@ * @param {OnLoadArgs} args The load arguments. | ||
resolveDir: string | undefined; | ||
warnings: import("esbuild").Message[]; | ||
errors: import("esbuild").Message[]; | ||
}>; | ||
@@ -137,2 +143,4 @@ /** | ||
resolveDir?: string; | ||
errors?: import('esbuild').Message[]; | ||
warnings?: import('esbuild').Message[]; | ||
}; | ||
@@ -139,0 +147,0 @@ export type TransformCallback = (args: import('esbuild').OnLoadArgs & { |
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
901
36858
3
3
+ Addedcommondir@^1.0.1
+ Addedcommondir@1.0.1(transitive)