parcel-bundler / parcel

The zero configuration build tool for the web. 📦🚀
https://parceljs.org
MIT License
43.5k stars 2.27k forks source link

Is it possible to "register" a transformer to automatically handle files with certain extension? #3867

Closed samuelgozi closed 4 years ago

samuelgozi commented 4 years ago

❔ Question

Is it possible to tell parcel "Hey I'll handle all files that match '*.test'" from within the transformer?

I know that the user can specify in his config:

{
  "transforms": {
        "*.svelte": [
            "parcel-transformer-svelte"
        ]
    }
}

But can I do that for them, like a register function or something?

🔦 Context

I'm migrating a transformer to v2 and trying to keep parcel "config free" when not necessary. I think that when the user explicitly installs a transformer, there is no need for them to specify which files it needs to handle, they know, and we know(the transformer) already.

🌍 Your Environment

Software Version(s)
Parcel 2.0.0-alpha.3.2
Node 12
npm/Yarn 1.19.1
Operating System Catalina
mischnic commented 4 years ago

Create a custom config and tell your user to do this:

{
  "extends": "parcel-config-svelte"
}

(Example config plugin https://github.com/parcel-bundler/parcel/tree/v2/packages/configs/default)

samuelgozi commented 4 years ago

@mischnic mmm... I'm not sure if I didn't understand the answer or you didn't understand the question.

What I meant, Is that I(the transformer creator) want to spare the need of my users(the ones using this transformer in their project) to add anything to their configuration files for my transformer to work with the files it supports.

So in other words, I just want the users of my transformers to add the transformer dependency to their package.json dependencies, and the transformer will by itself tell parcel what files it can handle.

And thanks for the swift reply!

mischnic commented 4 years ago

What I meant, Is that I(the transformer creator) want to spare the need of my users(the ones using this transformer in their project) to add anything to their configuration files for my transformer to work with the files it supports.

No, that isn't possible.

So in other words, I just want the users of my transformers to add the transformer dependency to their package.json dependencies, and the transformer will by itself tell parcel what files it can handle.

No, that isn't possible.

The only alternative to the transforms: *.svelte approach creating a custom config (https://github.com/parcel-bundler/parcel/issues/3867#issuecomment-561210422) which would make the config file "smaller".

samuelgozi commented 4 years ago

@mischnic Thank you very much for the answer. I have one last question. Is there any specific reason behind this? I'm asking because if not then I will open a feature request.

mischnic commented 4 years ago

The issue with Parcel 1's plugin system (which is what you want) is that if multiple plugins that want to process the same asset type will overwrite each other (or aren't in the correct order or shouldn't be run sequentially after all - e.g. should import "foo.md" import a html rendering, a react component, the plain file contents)