Webpack less-loader javascriptEnabled Error
Asked Answered
S

1

5

I was getting this error when I moved from less-loader version 6.0.0 to version 6.1.0 when trying to load my Ant Design library into my front-end with Webpack. I wanted to know if anyone else had this issue and resolved it (I answered it below).

Here was my LESS compilation configuration before the update:

module: { rules: [{
    test: /\.less$/,
    use: [
        { loader: "style-loader" },
        { loader: "css-loader" },
        {
            loader: "less-loader",
            options: {
                javascriptEnabled: true,
            }
        }
    ]
}]}
ERROR in ./node_modules/.pnpm/registry.npmjs.org/antd/[email protected][email protected]/node_modules/antd/dist/antd.less (./node_modules/.pnpm/registry.npmjs.org/css-loader/[email protected]/node_modules/css-loader/dist/cjs.js!./node_modules/.pnpm/registry.npmjs.org/less-loader/[email protected]/node_modules/less-loader/dist/cjs.js??ref--6-2!./node_modules/.pnpm/registry.npmjs.org/antd/[email protected][email protected]/node_modules/antd/dist/antd.less)
Module build failed (from ./node_modules/.pnpm/registry.npmjs.org/less-loader/[email protected]/node_modules/less-loader/dist/cjs.js):
ValidationError: Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
 - options has an unknown property 'javascriptEnabled'. These properties are valid:
   object { lessOptions?, prependData?, appendData?, sourceMap?, implementation? }
    at validate (/home/<path>/react-web/node_modules/.pnpm/registry.npmjs.org/schema-utils/2.6.6/node_modules/schema-utils/dist/validate.js:88:11)
    at Object.lessLoader (/home/<path>/react-web/node_modules/.pnpm/registry.npmjs.org/less-loader/[email protected]/node_modules/less-loader/dist/index.js:22:28)
 @ ./node_modules/.pnpm/registry.npmjs.org/antd/[email protected][email protected]/node_modules/antd/dist/antd.less 2:26-228
 @ ./src/index.tsx
 @ multi ./src/index.tsx
Samarium answered 8/5, 2020 at 19:11 Comment(0)
S
16

In "less-loader" version 6.1.0^ they made breaking changes to the loader that, if you used something like Ant Design (or other LESS and JS loaders) you would nomally add the javascriptEnabled: true flag to the "options" object in your Webpack configuration.

In version 6.1.0^ this was change to be placed in the lessOptions object under the options configuration for the less loader. Here is the solution I used, that works for my Webpack configuration bundle.

module: { rules: [{
    test: /\.less$/,
    use: [
        { loader: "style-loader" },
        { loader: "css-loader" },
        {
            loader: "less-loader",
            options: {
                lessOptions: {
                    javascriptEnabled: true,
                }
            }
        }
    ]
}]}

Notice that the javascriptEnabled flag is not under the top-level options object, but it, instead, under the lessOptions sub-object. That is the latest updated standard as of less-loader version 6.1.0^.

Samarium answered 8/5, 2020 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.