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

require a variable #116

Closed
njleonzhang opened this issue Nov 14, 2018 · 2 comments
Closed

require a variable #116

njleonzhang opened this issue Nov 14, 2018 · 2 comments

Comments

@njleonzhang
Copy link

njleonzhang commented Nov 14, 2018

is there a method which I can require resource from a pug variable?

- var url = './image/xxxx.png'

img(src=require( #{ url } ), width='150px')

error:

  Syntax Error: Unexpected character '#'
@njleonzhang
Copy link
Author

njleonzhang commented Nov 15, 2018

After study, I find this is a questions about webpack. When require a dynamic file path, we must indicate the file extension literally. It works in this way:

- var url = './image/xxxx'

img(src=require(url + './png'), width='150px')

refer to: https://stackoverflow.com/questions/39463459/dynamic-file-path-in-require-with-webpack

@Serrulien
Copy link

Serrulien commented Jul 30, 2019

@njleonzhang You found a workaround but what you said is not the cause. The thing that made your require call work is the creation of a context and not the addition of the file extension.
Instead of using the #{} syntax, we have to use the attribute interpolation syntax.
As i described in #122 (comment), we can use string casting to pass a variable when calling require so as create a dynamic require context.

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

No branches or pull requests

2 participants