moonrailgun / snowpack-plugin-replace

replace your code
MIT License
3 stars 5 forks source link

Add docs for require.resolve in mjs files #3

Open Olli-palkkaus opened 2 years ago

Olli-palkkaus commented 2 years ago

Documentation says that the file parameter needs to be a full path:

In the new examples, mjs-files are used in the configuration and there require.resolve is not available. How to overcome this is not so evident: it is not so easy to find the right terms to google.

Suggest adding:

For Example: require.resolve('./index.js') ... or new URL("./index.js", import.meta.url) in snowpack.config.mjs

Also, it may be worth mentioning in which files the content is replaced:

All of the restrictions are understandable, but it would be good to add them in the docs.

Thanks for the plugin though! Once I understood what it does, it did what I needed.

moonrailgun commented 2 years ago

Hi @Olli-palkkaus , Thanks for your suggestion. But i have no idea about import.meta.url in snowpack.config.mjs.

Whats the output in tranforms runtime? And the path of file should be a string, else it will be ignore.

Olli-palkkaus commented 2 years ago

And the path of file should be a string, else it will be ignore.

Oh, you are correct. I thought I had fixed the issue with new URL("./index.js", import.meta.url), but it was actually completely ignored not being a string. Therefore it was applying the replace to ALL files and I mistakenly thought the filter was matching to the file that I was testing.

When I add new URL("./index.js", import.meta.url).toString() it no longer works, so the result of this is not exactly the same as what the file id is matching in the plugin. So my suggestion does not work. At least not in Windows. At the moment, I do not have an idea how to fix this. I am now just using __SNOWPACK_ENV__ instead because I can use that to do what I need without a plugin.

I took the idea from here: https://nodejs.org/api/esm.html#esm_no_require_resolve

moonrailgun commented 2 years ago

import.meta.url just work in browser runtime. But in snowpack , i dont know whats is it.

And for your other suggestion, i had update README file, pls check it again.

Thanks for your suggestion