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

Support static replacement from transformers #5405

Open
101arrowz opened this issue Nov 30, 2020 · 8 comments
Open

Support static replacement from transformers #5405

101arrowz opened this issue Nov 30, 2020 · 8 comments

Comments

@101arrowz
Copy link
Member

101arrowz commented Nov 30, 2020

🙋 feature request

Support custom replacement of build-time constants.

🤔 Expected Behavior

It is possible to run custom replacements on JavaScript from a transformer.

😯 Current Behavior

It is not possible to run custom replacements on JavaScript from a transformer.

💁 Possible Solution

Make it possible to run custom replacements on JavaScript from a transformer.

To be more serious: right now, only the JS transformer has the ability to run said replacements, so I think adding a meta property for custom replacement keys and values should make this possible.

🔦 Context

Support Vue's runtime globals from the Vue transformer as mentioned in this comment.

@mischnic
Copy link
Member

Support Vue's runtime globals from the Vue transformer as mentioned in this comment.

Asking once again: in which files should this replacement be done?

@101arrowz
Copy link
Member Author

Just in Vue's internal files, nowhere else. Sorry for not clarifying.

@mischnic
Copy link
Member

so I think adding a meta property for custom replacement keys and values should make this possible.

These would be a kind of global configuration. The Vue transformer would certainly not be able to set them...

One possibility would be setting them similar to these config options: https://v2.parceljs.org/features/node-emulation/#%F0%9F%94%A7-disabling-these-features. I think this would be fine for Vue's usecase?

@101arrowz
Copy link
Member Author

101arrowz commented Nov 30, 2020

The reason those don't work is that the globals are not defined on process.env. I was thinking something along the lines of reading the Vue entry file with the Vue transformer, returning it as a dependency with a meta property replacements: { __VUE_OPTIONS_API__: true } and forwarding the meta property to child dependencies from the JS transformer. Would that be too hacky?

@mischnic
Copy link
Member

The reason those don't work is that the globals are not defined on process.env

I was thinking of:

{
  "name": "my-project",
  "dependencies": {
    ...
  },
  "@parcel/transformer-js": {
    "define": {
      "__VUE_OPTIONS_API__": false,
	}
  }
}

forwarding the meta property to child dependencies from the JS transformer. Would that be too hacky?

Propagating the meta properties unconditionally to every child recursively? That's not an option

@mischnic
Copy link
Member

mischnic commented Dec 1, 2020

@parcel/transformer-js

We also want to prevent feature creep on JS transformer though. The alternative would be a specific transformer for this (which could also reuse the previous ASTs).

@mischnic
Copy link
Member

I just ran into another usecase of replacing globals.
Maybe allowing transformers to set asset.meta.jsGlobals = {"__XYZ__": true}; would make sense (and then the JS transformer would apply that).

@devongovett
Copy link
Member

What types of values would be supported? Just JSON stringifyable types?

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

No branches or pull requests

3 participants