-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Add ES Modules support to Parcel Plugins #7639
Comments
Unfortunately it's pretty much impossible to support if you also want caching. Parcel needs to hook into the module system to intercept all requests so that it can keep track of all of the files a module loads. That way it can invalidate the cache when a dev dependency changes. But with ESM, node doesn't offer a good way of doing this yet. |
Why does all the example plugin code on the docs use import if one cannot actually use import to write a plugin? As one cannot use import outside of a module. Is there something I am missing in the docs? Example (taken from https://parceljs.org/plugin-system/namer/) |
Any update on this? |
This confused me tremendously and was massive headache for me. Fixed it by using require. I may be wrong, but I think the example plugin code is run through a bundler which converts import to require before being imported and used by parcel. |
Indeed the documentation examples are very confusing if |
That was indeed the intended meaning (and how it works for the core plugins, which are transpiled with Babel) when writing the docs. But I see how it can be misleading. |
Yes, I figured the examples were taken from the Parcel source itself, which of course is transpiled when Parcel is built. it is difficult to know this though. I got the example Resolver plugin working just now by rewriting it with Node const plugin = require('@parcel/plugin');
const Resolver = plugin.Resolver;
module.exports = new Resolver({ ... }); I'm not sure how I would set up Parcel to transpile my plugin when it is loaded. Babel is of course available (for Parcel to use when transpiling my app). From the above comment, I understand this wouldn't be a good idea anyway. |
Until parcel supports ES modules for parcel plugins, the docs must be fixed/updated. At least, a warning note in the docs would help tremendously to avoid the confusion. |
@Slevinski I've hacked together a few plugins. One was a rudimentary transformer to substitute environment variables (e.g., I'm unsure how simple an example would help you-- but, I ripped the code out of my simple transformer to show you my setup below, in case it's helpful. For convenience, I put it in the project directly via an npm workspace but that would be fairly easy to change (i.e., if you want to ship an actual package). The parcel docs are quite good for the actual Parcel API.
const plugin = require('@parcel/plugin');
const Transformer = plugin.Transformer;
module.exports = new Transformer({
async loadConfig({config}) {
// If config is needed..
},
async transform(opts) {
let asset = opts.asset;
if (asset.type === 'html') {
// Do something with an HTML asset..
}
return [asset];
}
});
Main
|
@richardkmichael Thanks for the example. I was able to coble something together and get it published. It's working with a mithril project. I tried a test project with static html files, but neither the parcel dev server nor the build would load. I'll investigate more later, but for now I'm able to move forward with my project. |
🙋 Feature request
Add ES Modules support to Parcel Plugins
🤔 Expected Behavior
Parcel Plugins ought to be able to use es6 module syntax.
import
,export
, etc.😯 Current Behavior
🔦 Context
require()
is currently used when loading plugins:The text was updated successfully, but these errors were encountered: