@chialab/esbuild-plugin-meta-url
Advanced tools
Comparing version 0.11.35 to 0.11.36
@@ -13,2 +13,47 @@ import path from 'path'; | ||
/** | ||
* Detect first level identifier for esbuild file loader imports. | ||
* File could be previously bundled using esbuild, so the first argument of a new URL(something, import.meta.url) | ||
* is not a literal anymore but an identifier. | ||
* Here, we are looking for its computed value. | ||
* @param {*} node The acorn node. | ||
* @param {string} id The name of the identifier. | ||
* @param {*} program The ast program. | ||
* @return {*} The init acorn node. | ||
*/ | ||
function findIdentifierValue(node, id, program) { | ||
const identifier = program.body | ||
.filter( | ||
/** | ||
* @param {*} child | ||
*/ | ||
(child) => child.type === 'VariableDeclaration' | ||
) | ||
.reduce( | ||
/** | ||
* @param {*[]} acc | ||
* @param {*} child | ||
*/ | ||
(acc, child) => [...acc, ...child.declarations], [] | ||
) | ||
.filter( | ||
/** | ||
* @param {*} child | ||
*/ | ||
(child) => child.type === 'VariableDeclarator' | ||
) | ||
.find( | ||
/** | ||
* @param {*} child | ||
*/ | ||
(child) => child.id && child.id.type === 'Identifier' && child.id.name === id | ||
); | ||
if (!identifier || !identifier.init || identifier.init.type !== 'Literal') { | ||
return node; | ||
} | ||
return identifier.init; | ||
} | ||
/** | ||
* Instantiate a plugin that converts URL references into static import | ||
@@ -76,15 +121,21 @@ * in order to handle assets bundling. | ||
if (node.arguments.length !== 2 || | ||
node.arguments[0].type !== 'Literal' || | ||
node.arguments[1].type !== 'MemberExpression') { | ||
if (node.arguments.length !== 2) { | ||
return; | ||
} | ||
if (node.arguments[1].object.type !== 'MetaProperty' || | ||
node.arguments[1].property.type !== 'Identifier' || | ||
node.arguments[1].property.name !== 'url') { | ||
const arg1 = node.arguments[0].type === 'Identifier' ? findIdentifierValue(node, node.arguments[0].name, ast) : node.arguments[0]; | ||
const arg2 = node.arguments[1]; | ||
if (arg1.type !== 'Literal' || | ||
arg2.type !== 'MemberExpression') { | ||
return; | ||
} | ||
const value = node.arguments[0].value; | ||
if (arg2.object.type !== 'MetaProperty' || | ||
arg2.property.type !== 'Identifier' || | ||
arg2.property.name !== 'url') { | ||
return; | ||
} | ||
const value = arg1.value; | ||
if (typeof value !== 'string' || isUrl(value)) { | ||
@@ -91,0 +142,0 @@ return; |
{ | ||
"name": "@chialab/esbuild-plugin-meta-url", | ||
"type": "module", | ||
"version": "0.11.35", | ||
"version": "0.11.36", | ||
"description": "A file loader plugin for esbuild for constructed URLs using import metadata.", | ||
@@ -47,3 +47,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "ea00bad06278de34a104d65a25c6dd8f4f422373" | ||
"gitHead": "79afb6a0a6aaea80a12413a4ad3f9863a3126933" | ||
} |
/** | ||
* @typedef {{ resolve?: typeof defaultResolve }} PluginOptions | ||
*/ | ||
/** | ||
* Instantiate a plugin that converts URL references into static import | ||
@@ -6,0 +3,0 @@ * in order to handle assets bundling. |
10333
157