@chialab/esbuild-plugin-meta-url
Advanced tools
Comparing version 0.11.36 to 0.11.38
@@ -22,3 +22,3 @@ import path from 'path'; | ||
*/ | ||
function findIdentifierValue(node, id, program) { | ||
export function findIdentifierValue(node, id, program) { | ||
const identifier = program.body | ||
@@ -59,2 +59,36 @@ .filter( | ||
/** | ||
* @param {*} node The acorn node. | ||
* @param {*} ast The ast program. | ||
* @return The path value. | ||
*/ | ||
export function getMetaUrl(node, ast) { | ||
if (node.type === 'MemberExpression') { | ||
node = node.object; | ||
} | ||
if (!node.callee || node.callee.type !== 'Identifier' || node.callee.name !== 'URL') { | ||
return; | ||
} | ||
if (node.arguments.length !== 2) { | ||
return; | ||
} | ||
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; | ||
} | ||
if (arg2.object.type !== 'MetaProperty' || | ||
arg2.property.type !== 'Identifier' || | ||
arg2.property.name !== 'url') { | ||
return; | ||
} | ||
return arg1.value; | ||
} | ||
/** | ||
* Instantiate a plugin that converts URL references into static import | ||
@@ -118,25 +152,3 @@ * in order to handle assets bundling. | ||
NewExpression(node) { | ||
if (!node.callee || node.callee.type !== 'Identifier' || node.callee.name !== 'URL') { | ||
return; | ||
} | ||
if (node.arguments.length !== 2) { | ||
return; | ||
} | ||
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; | ||
} | ||
if (arg2.object.type !== 'MetaProperty' || | ||
arg2.property.type !== 'Identifier' || | ||
arg2.property.name !== 'url') { | ||
return; | ||
} | ||
const value = arg1.value; | ||
const value = getMetaUrl(node, ast); | ||
if (typeof value !== 'string' || isUrl(value)) { | ||
@@ -143,0 +155,0 @@ return; |
{ | ||
"name": "@chialab/esbuild-plugin-meta-url", | ||
"type": "module", | ||
"version": "0.11.36", | ||
"version": "0.11.38", | ||
"description": "A file loader plugin for esbuild for constructed URLs using import metadata.", | ||
@@ -38,5 +38,5 @@ "main": "lib/index.js", | ||
"@chialab/esbuild-helpers": "^0.11.13", | ||
"@chialab/esbuild-plugin-emit": "^0.11.29", | ||
"@chialab/esbuild-plugin-transform": "^0.11.35", | ||
"@chialab/estransform": "^0.11.13", | ||
"@chialab/esbuild-plugin-emit": "^0.11.38", | ||
"@chialab/esbuild-plugin-transform": "^0.11.38", | ||
"@chialab/estransform": "^0.11.38", | ||
"@chialab/node-resolve": "^0.11.19", | ||
@@ -48,3 +48,3 @@ "esbuild": "^0.12.0" | ||
}, | ||
"gitHead": "79afb6a0a6aaea80a12413a4ad3f9863a3126933" | ||
"gitHead": "875e5933fc9f8fd3727ff65a90154c6d85ded668" | ||
} |
/** | ||
* @typedef {{ resolve?: typeof defaultResolve }} PluginOptions | ||
*/ | ||
/** | ||
* 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. | ||
*/ | ||
export function findIdentifierValue(node: any, id: string, program: any): any; | ||
/** | ||
* @param {*} node The acorn node. | ||
* @param {*} ast The ast program. | ||
* @return The path value. | ||
*/ | ||
export function getMetaUrl(node: any, ast: any): any; | ||
/** | ||
* Instantiate a plugin that converts URL references into static import | ||
@@ -3,0 +23,0 @@ * in order to handle assets bundling. |
10945
188