You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
The text was updated successfully, but these errors were encountered:
With
preconstruct dev
, usingimport.meta.url
results inSyntaxError: 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:I figure that in the case of
preconstruct build
, Rollup is doing the transform, while withpreconstruct dev
Babel is the only thing transforming the code, leavingimport.meta.url
untouched. It seems Babel with@babel/plugin-transform-modules-commonjs
is like this by design:babel/babel#9299 (comment)
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
withpreconstruct dev
by addingbabel-plugin-transform-import-meta
in the Babel config used in the require hook? I did a quick test and it seems bothpreconstruct dev
andpreconstruct build
work fine as a result.The text was updated successfully, but these errors were encountered: