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

Using import.meta.url #518

Open
JamiesWhiteShirt opened this issue Mar 2, 2023 · 0 comments · May be fixed by #524
Open

Using import.meta.url #518

JamiesWhiteShirt opened this issue Mar 2, 2023 · 0 comments · May be fixed by #524

Comments

@JamiesWhiteShirt
Copy link

With preconstruct dev, using import.meta.url results in SyntaxError: Cannot use 'import.meta' outside a module when loaded in Node.

With preconstruct build, import.meta.url is transformed to the following in the CommonJS output, which loads just fine:

(typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('dist/{outputName}.js', document.baseURI).href))

I figure that in the case of preconstruct build, Rollup is doing the transform, while with preconstruct dev Babel is the only thing transforming the code, leaving import.meta.url untouched. It seems Babel with @babel/plugin-transform-modules-commonjs is like this by design:

babel/babel#9299 (comment)

If you want to transform it, you'll need another plugin (that we don't offically provide because the spec doesn't say what should there be inside that object).


babel-plugin-transform-import-meta partially resolves the issue. It allows consuming the module as CommonJS, but due to how Babel must be configured with Preconstruct, adding it to the Babel config breaks other build types. Since the Babel config applies to ALL build types including ESM, the transform applies to the ESM output, where __filename is invalid. Thus, the following error happens when the ESM build is loaded in Node: ReferenceError: __filename is not defined in ES module scope.

Would it make sense for Preconstruct to support using import.meta.url with preconstruct dev by adding babel-plugin-transform-import-meta in the Babel config used in the require hook? I did a quick test and it seems both preconstruct dev and preconstruct build work fine as a result.

@emmatown emmatown linked a pull request Mar 6, 2023 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant