less-loader icon indicating copy to clipboard operation
less-loader copied to clipboard

Module build failed: 'variable is undefined' for deeply nested variables

Open mergederg opened this issue 8 years ago • 1 comments

  • Operating System:
  • Node Version: 11.1.0
  • NPM Version: 6.4.1
  • webpack Version: 3.8.1
  • less-loader Version: 4.1.0

This issue is for a:

  • [X] bug
  • [ ] feature request
  • [ ] modification request

Code

CLI Command
  $ npx webpack --config webpack.config.js
webpack.config.js
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
    mode: 'production',
    entry: {
        theentry: './webpack.entryjs.js'
    },
    output: {
        path: path.resolve(__dirname, 'WEBPACK_ASSETS'),
        filename: '[name].js'
    },
    resolve: {
        modules: ['./', 'node_modules'], // TODO: May not be doing anything ...
    },
    module: {
        strictExportPresence: false, // TODO: May also not be doing anything...
        rules: [{
            test: /\.less$/,
            use: [{
                loader: 'less-loader'
            }]
        }]
    },
    plugins: [
        new MiniCssExtractPlugin(),
    ]
};

Expected Behavior

Webpack reads LESS into the bundle in some form, and does not error out. Variables should not be lost en-route to the bundle.

Actual Behavior

Webpack errors out complaining of missing variables (which are defined inside explicitly imported files).

How Do We Reproduce?

Create a 'top.less' file. Include in it:

@import "../../../../shared/less/toBreak.less";
@import "../../../../shared/less/toBreak2.less";

Four folders above top.less, create '/shared/less' and put this inside toBreak.less:

@screenXs:480px;
@screenSm:640px;
@screenMd:1024px;
@screenLg:1400px;
  
@screenXsMin:480px;
@screenSmMin:640px;
@screenMdMin:1024px;
@screenLgMin:1400px;

@screenXsMax:639px;
@screenSmMax:1023px;
@screenMdMax:1399px;

And in toBreak2:

@screen-xs:@screenXs;
@screen-sm:@screenSm;
@screen-md:@screenMd;
@screen-lg:@screenLg;
  
@screen-xs-min:@screenXsMin;
@screen-sm-min:@screenSmMin;
@screen-md-min:@screenMdMin;
@screen-lg-min:@screenLgMin;

@screen-xs-max:@screenXsMax;
@screen-sm-max:@screenSmMax;
@screen-md-max:@screenMdMax;

Now, require the original JS file in the entry point...

@import "top.less"

Upon running, should see something like: @screen-xs:@screenXS: ^ Variable @screenXs is undefined

Sounds similar to this old issue: https://github.com/webpack-contrib/less-loader/issues/47

mergederg avatar Nov 07 '18 21:11 mergederg

Configuration error on my part, not realizing less-loader had to be chained with css-loader in my type of scenario.

mergederg avatar Nov 20 '18 16:11 mergederg