Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import.meta.url Support #208

Closed
evanplaice opened this issue Jul 1, 2020 · 3 comments
Closed

import.meta.url Support #208

evanplaice opened this issue Jul 1, 2020 · 3 comments

Comments

@evanplaice
Copy link

I have a Monaco Editor web component that needs URLs provided so it knows where to side-load the language-server-protocol service workers from

For context, this is an ESM -> ESM transform with the following command

esbuild --format=esm --bundle src/wc-monaco-editor.js --outfile=index.js

Here's the original source

const monacoDir = new URL('monaco/', import.meta.url)
self.MonacoEnvironment = {
  getWorkerUrl: function (moduleId, label) {
    if (label === 'json') {
      return `${monacoDir}json.worker.js`
    }
    if (label === 'css') {
      return `${monacoDir}css.worker.js`
    }
    if (label === 'html') {
      return `${monacoDir}html.worker.js`
    }
    if (label === 'typescript' || label === 'javascript') {
      return `${monacoDir}ts.worker.js`
    }
    return `${monacoDir}editor.worker.js`
  }
}

Which gets transformed to

const import_meta = {};
const monacoDir = new URL("monaco/", import_meta.url); // <-- import_meta.url isn't defined
self.MonacoEnvironment = {
  getWorkerUrl: function(moduleId, label) {
    if (label === "json") {
      return `${monacoDir}json.worker.js`;
    }
    if (label === "css") {
      return `${monacoDir}css.worker.js`;
    }
    if (label === "html") {
      return `${monacoDir}html.worker.js`;
    }
    if (label === "typescript" || label === "javascript") {
      return `${monacoDir}ts.worker.js`;
    }
    return `${monacoDir}editor.worker.js`;
  }
};

Note: RollupJS can bundle the same code without issue because it doesn't transform import.meta.url


BTW, tc39/proposal-import-meta is officially Stage 4

@evanw
Copy link
Owner

evanw commented Jul 1, 2020

I was under the impression that import.meta was potentially for information from the bundler about the original source file, but it looks like I'm mistaken. Both Rollup and Parcel 2 appear to pass import.meta through unmodified. I will change esbuild to do that too.

@evanplaice
Copy link
Author

evanplaice commented Jul 2, 2020

Nope, import.meta.url is just a const pointing to the absolute path/url of the source it's being called from. If bundling relocates stuff, the onus is on the person doing the bundling to reconcile the differences (via copying or symlinking).

Webpack is is at an unfortunate disadvantage due to their heavy reliance on faux modules and magical kitchen sink builds.

Bundlers that more-or-less respect the structure of the original source can pass it through as-is.

@evanw evanw closed this as completed in ac97be7 Jul 2, 2020
@evanw
Copy link
Owner

evanw commented Jul 2, 2020

This should be working now as of version 0.5.17.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants