@chialab/esbuild-plugin-meta-url
Advanced tools
Comparing version
@@ -6,3 +6,3 @@ import path from 'path'; | ||
import { pipe, walk, getOffsetFromLocation } from '@chialab/estransform'; | ||
import { getEntry, finalizeEntry, createFilter, getParentBuild } from '@chialab/esbuild-plugin-transform'; | ||
import { getEntry, finalizeEntry, createFilter, getParentBuild, transformError } from '@chialab/esbuild-plugin-transform'; | ||
@@ -121,44 +121,48 @@ /** | ||
await pipe(entry, { | ||
source: path.basename(args.path), | ||
sourcesContent: options.sourcesContent, | ||
}, async ({ magicCode, code, ast }) => { | ||
/** | ||
* @type {{ [key: string]: string }} | ||
*/ | ||
const ids = {}; | ||
try { | ||
await pipe(entry, { | ||
source: path.basename(args.path), | ||
sourcesContent: options.sourcesContent, | ||
}, async ({ magicCode, code, ast }) => { | ||
/** | ||
* @type {{ [key: string]: string }} | ||
*/ | ||
const ids = {}; | ||
/** | ||
* @type {Promise<void>[]} | ||
*/ | ||
const promises = []; | ||
walk(ast, { | ||
/** | ||
* @param {*} node | ||
* @type {Promise<void>[]} | ||
*/ | ||
NewExpression(node) { | ||
const value = getMetaUrl(node, ast); | ||
if (typeof value !== 'string' || isUrl(value)) { | ||
return; | ||
} | ||
const promises = []; | ||
promises.push((async () => { | ||
const resolvedPath = await resolve(value, args.path); | ||
const startOffset = getOffsetFromLocation(code, node.loc.start); | ||
const endOffset = getOffsetFromLocation(code, node.loc.end); | ||
if (!ids[resolvedPath]) { | ||
const entryPoint = emitFileOrChunk(build, resolvedPath); | ||
const { identifier } = prependImportStatement({ ast, magicCode, code }, entryPoint, value); | ||
ids[resolvedPath] = identifier; | ||
walk(ast, { | ||
/** | ||
* @param {*} node | ||
*/ | ||
NewExpression(node) { | ||
const value = getMetaUrl(node, ast); | ||
if (typeof value !== 'string' || isUrl(value)) { | ||
return; | ||
} | ||
magicCode.overwrite(startOffset, endOffset, `new URL(${ids[resolvedPath]}, ${getBaseUrl(build)})`); | ||
})()); | ||
}, | ||
promises.push((async () => { | ||
const resolvedPath = await resolve(value, args.path); | ||
const startOffset = getOffsetFromLocation(code, node.loc.start); | ||
const endOffset = getOffsetFromLocation(code, node.loc.end); | ||
if (!ids[resolvedPath]) { | ||
const entryPoint = emitFileOrChunk(build, resolvedPath); | ||
const { identifier } = prependImportStatement({ ast, magicCode, code }, entryPoint, value); | ||
ids[resolvedPath] = identifier; | ||
} | ||
magicCode.overwrite(startOffset, endOffset, `new URL(${ids[resolvedPath]}, ${getBaseUrl(build)})`); | ||
})()); | ||
}, | ||
}); | ||
await Promise.all(promises); | ||
}); | ||
} catch (error) { | ||
throw transformError(this.name, error); | ||
} | ||
await Promise.all(promises); | ||
}); | ||
return finalizeEntry(build, args.path); | ||
@@ -165,0 +169,0 @@ }); |
{ | ||
"name": "@chialab/esbuild-plugin-meta-url", | ||
"type": "module", | ||
"version": "0.12.9", | ||
"version": "0.12.11", | ||
"description": "A file loader plugin for esbuild for constructed URLs using import metadata.", | ||
@@ -39,4 +39,4 @@ "main": "lib/index.js", | ||
"@chialab/esbuild-plugin-emit": "^0.12.7", | ||
"@chialab/esbuild-plugin-transform": "^0.12.9", | ||
"@chialab/estransform": "^0.12.9", | ||
"@chialab/esbuild-plugin-transform": "^0.12.11", | ||
"@chialab/estransform": "^0.12.11", | ||
"@chialab/node-resolve": "^0.12.2", | ||
@@ -48,3 +48,3 @@ "esbuild": "^0.13.0" | ||
}, | ||
"gitHead": "eb87471a0254e6d7e72a7cca7f0713031c07a23e" | ||
"gitHead": "f74a8eb0dc5038d5d2783506e40c41904dd28472" | ||
} |
10722
2.85%182
2.25%