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

Failed to build when webpack alias for asset is an array of paths instead of a single path #10

Closed
Rush opened this issue Feb 9, 2022 · 5 comments
Labels
enhancement New feature or request
Milestone

Comments

@Rush
Copy link

Rush commented Feb 9, 2022

See my full repro here: https://github.com/Rush/webdiscus-repro and run npm run reproduce.

Or to reproduce by hand, setup the resolve alias as a list of paths, instead of a single path

resolve: {
  alias: {
    assets: [ assetPath1, assetPath2 ]

and then require the asset from pug

Module not found: Error: Can't resolve '/code/webdiscus-pug-node-example/assets,/code/webdiscus-pug-node-example/assets2/fonts/ibm-plex-sans-v7-latin-regular.woff2' in '/code/webdiscus-pug-node-example/server/views'

Requiring same asset from JS/TS works fine via:

require('assets/fonts/ibm-plex-sans-v7-latin-regular.woff2');
@webdiscus webdiscus added the enhancement New feature or request label Feb 9, 2022
@webdiscus webdiscus changed the title Failed to build when asset path for require is an array of paths instead of a single path Failed to build when webpack alias for asset is an array of paths instead of a single path Feb 9, 2022
@webdiscus webdiscus added this to the in development milestone Feb 9, 2022
@Rush
Copy link
Author

Rush commented Feb 10, 2022

Thanks for adding it to the "in development" milestone. :) I hope I will not have to wait for long as I'm slightly blocked, but not too worry too much :)

@webdiscus webdiscus modified the milestones: in development, testing Feb 10, 2022
webdiscus added a commit that referenced this issue Feb 10, 2022
fix optional prefix of alias in request when an alias name self contains the prefix
@webdiscus
Copy link
Owner

hallo @Rush

thank you for very important issue. I have enhanced the resolving of alias for array.

You can see possible usage cases in the fixture and resolve.alias for compile method.

⚠️ Limitation for compile method and alias as an array

Usage of a variable in request don't work if alias is an array:

resolve: {
  alias: {
     Images: ['/path_1/', '/path_2/'],     
  }
}
- var basename = 'logo';
img(src=require('Images/' + basename + '-c1.jpeg')) <-- does not work

img(src=require('Images/logo.jpeg')) <-- works

This is limitation of webpack compiler self, webpack don't recognize an asset request contained a variable. But the webpack resolver resolve this assets correct. This limitation does not affect the render and html methods.

New version is 1.7.1

webdiscus added a commit that referenced this issue Feb 10, 2022
fix optional prefix of alias in request when an alias name self contains the prefix
@Rush
Copy link
Author

Rush commented Feb 10, 2022

Awesome! THe workaround for not using variables in the require expression is simply to prepare utility data structures. Such as:

- const statusIcons = { draft: require('images/status/draft.png'), reject: require('images/status/reject.png'), review: require('images/status/review.png'), approve: require('images/status/review.png') };

@webdiscus
Copy link
Owner

Awesome! The workaround for not using variables in the require expression is simply to prepare utility data structures. Such as:

- const statusIcons = { draft: require('images/status/draft.png'), reject: require('images/status/reject.png'), review: require('images/status/review.png'), approve: require('images/status/review.png') };

Thanks, it works too :-)

webdiscus added a commit that referenced this issue Feb 10, 2022
fix optional prefix of alias in request when an alias name self contains the prefix
@webdiscus webdiscus modified the milestones: testing, done Feb 10, 2022
@Rush
Copy link
Author

Rush commented Feb 15, 2022

Fixed, works great now. Thank you!

@Rush Rush closed this as completed Feb 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants