Open caoxiemeihao opened 2 years ago
@evanw is there a solution planned for this? This prevents us from bumping versions of several dependencies, including some critical ones :(
Is it still not resolved?
Why this need?
We use this to dynamically determine path for import outside of javascript.
esbuild options:
...
platform: "browser",
format: "esm",
bundle: true,
treeShaking: true,
splitting: true,
sourcemap: true,
...
Consider this example:
function inject_style(text: string) {
let newText = text;
const pathSearch = new RegExp(/(\\[START_RESOLVE_PATH\\])(?<RelativePath>.*)(\\[END_RESOLVE_PATH\\])/g);
const foundResources = text.matchAll(pathSearch);
for (const iterator of foundResources) {
const relPath = iterator["groups"].RelativePath;
const replaceRegexp = new RegExp(`(\\\\[START_RESOLVE_PATH\\\\])($\{relPath})(\\\\[END_RESOLVE_PATH\\\\])`);
const resolvedPath = import.meta.resolve(relPath);
newText = newText.replace(replaceRegexp, resolvedPath);
}
const style = window.document.createElement("style");
const node = window.document.createTextNode(newText);
style.appendChild(node);
window.document.head.appendChild(style);
}
in a custom plugin:
[custom esbuild plugin setup]....
onResolve({ filter: /^__style_helper__$/ }, () => {
return { path: "styleImporter.js", namespace: "style-helper" };
});
//if you pass he function as a string it will be correct, however if you pass the "function".toString() it will be "import_meta"
onLoad({ filter: /.*/, namespace: "style-helper" }, () => ({
contents: `export ${inject_style.toString().replace("import_meta","import.meta")}`,
}));
When i use
esbuild
to transpileimport.meat.url
incjs
format, I always don't get the correct result.Reproduce
Expected