5

I'm trying to pass less variables within a webpack configuration to the less loader, naturally. For some reason the variable is not being passed ok. I can't figure the correct syntax.

The variable has a dynamic content that is determined at the build time, in the webpack config file. This is the relevant line (I've tried many variations of it):

loader: 'style!css?-minimize!less?-minimize&{modifyVars:{"resources-path-prefix":"' + pathPrefix + '"}}'

In the above example some pathPrefix is being determined at build time and we want to pass its value into less context, where it will be used in url() css directives.

The above doesn't work - nothing is passed into less, and the default variable value defined in less applies.

Can anyone show how to correctly pass the value into the less compilation process? Thanks!

3
  • What is your actual variable in less @resources-path-prefix or resources-path-prefix? I think you are missing the @ which is expected in modifyVars.
    – BenM
    Jul 13, 2016 at 19:43
  • In the less its @resources-path-prefix (as less variables should be declared) Jul 13, 2016 at 19:47
  • Ah whoops looks like the modifyVars accepts both with or without the @ symbol. Thought that might have been missing.
    – BenM
    Jul 13, 2016 at 19:56

3 Answers 3

12

So it was tough but we finally made it work(!). Arggh - so much time invested to trying and figure out the syntax.

Here's the task: we want at build time to determine a path that should use used as the base url for misc assets in less files (background images, using url() less function).

First, we determined the path in webpack config file. Its plain JS, but the escaping pattern for the path string was absolutely nuts. We probably invested hours just on this. Amazing. Here it is:

var assetsPath = (someCondition) ? '/assets' : "\\/127.0.0.1:8080/assets";

Next is the loader configuration for less files, using the assetsPath prefix set above:

{
    test: /\.less$/,
    exclude: /node_modules/,
    loader: 'style!css?minimize=false!less?{"modifyVars":{"assetspath":"\'' + assetsPath +'\'"}}'
}

Notice the escaping pattern above where using the assetsPath in the loader configuration.

Next, you need to make sure that an empty variable is being defined in the less files. We initialized it in our 'vars.less' file, with:

@assetspath: '';

Finally, in any relevant class, we can use the value being passed in build time like this:

background-image: url("@{assetspath}/images/facebook.png");
5

You can try to use the query section of the loader:

    loader: 'style!css?-minimize!less?-minimize',
    query: {
        modifyVars: {
           "resources-path-prefix": pathPrefix
        } 
    }
5
  • thanks for the swift response. However, that's not possible. You can't use this syntax with multiple loaders... . You immediately get: "Error: Cannot define 'query' and multiple loaders in loaders list" (just tested it). Jul 13, 2016 at 19:41
  • Ahh. got it. Consider instead using less as a separate loader after the other ones.
    – Naftali
    Jul 13, 2016 at 19:42
  • but the above uses 3 chained loaders - less, css and style (in that order). Do I need to mimic the 'linkage' in the separate mode somehow? (and if so, how?). Jul 13, 2016 at 19:46
  • Make a loader for just style!css?-minimize, then another 2nd one just for less?-minimize, I believe webpack loads them in order.
    – Naftali
    Jul 13, 2016 at 19:46
  • Tried. Separate loaders works but the less loader fails. I'm down to this: query: { modifyVars: { "resourcespathprefix": resourcesPathPrefix } } I've refactored the less file so the variable name would be all-small-letters (one less camel casing problem to consider). Still no-go, I get: Module build failed: Unrecognised input ... near lines: } @resourcespathprefix: 127.0.0.1:8080; It appears to me as if the variable interpolated value is missing commas but I cannot seem to find how to add them :-( Tried multiple syntaxes Jul 13, 2016 at 20:35
1

It's a different approach to this situation, but we managed to make things work by converting into Base64 every resource that the CSS files loaded. We had to do this because figuring out the hostname of the resources was possible much later down the line than the webpack config file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.