Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable inline javascript in LESS

I would like to use inline js in my less files but I get the following message:

Inline JavaScript is not enabled. Is it set in your options?

How can I enable that?

like image 614
pharkasbence Avatar asked Oct 13 '17 11:10

pharkasbence


People also ask

How do I use JavaScript inline?

Inline JavaScript can be achieved by using Script tag inside the body of the HTML, and instead of specifying the source(src=”…”) of the JavaScript file in the Script tag, we have to write all the JavaScript code inside the Script tag.

Is there inline JavaScript?

The "Inline JavaScript" filter reduces the number of requests made by a web page by inserting the contents of small external JavaScript resources directly into the HTML document. This can reduce the time it takes to display content to the user, especially in older browsers.

What does inline JavaScript mean?

When a script tag is used in the HTML file, it is called inlining. This means no external JS file is used instead javascript is put into an HTML file. Modern code has moved from manual coding and customized structures to templates that provide a framework for effective code creation processes.


4 Answers

I had same problem, I use webpack with less loader, I needed to add javascript option in less loader config:

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

I found in the sourcecode of less compiler: https://github.com/less/less.js/blob/3.x/bin/lessc

that they parse js less option in this way:

        case 'js':
            options.javascriptEnabled = true;
            break;
        case 'no-js':
            console.error('The "--no-js" argument is deprecated, as inline JavaScript ' + 
                'is disabled by default. Use "--js" to enable inline JavaScript (not recommended).');
            break;

So you should probably use '--js' in a static compilation ( command line ) or 'javascriptEnabled: true' in a dynamic compilation ( like webpack loader ) to enable javascript.

like image 98
Davide Carpini Avatar answered Oct 24 '22 10:10

Davide Carpini


Just to update the accepted answer,

From 3.11.1, if you use just options, it will throw :

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? }

In [email protected], it's not just options that should be used, but lessOptions like this :

{
    test: /\.less$/,
    use: [{
        loader: "style-loader"
    }, {
        loader: "css-loader"
    }, {
        loader: "less-loader",
        options: {
          lessOptions: {
             javascriptEnabled: true
          }
        }
    }]
}
like image 34
CtrlX Avatar answered Oct 24 '22 10:10

CtrlX


Updated: May 2020

For less-loader version 6.1.0^.

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^.

like image 10
Jonathon Tech Avatar answered Oct 24 '22 09:10

Jonathon Tech


If you're using the lessc the cli interface then you just need --js on the end.

lessc --js ./styles/theme.less ./styles/theme.css
like image 4
Ben Winding Avatar answered Oct 24 '22 08:10

Ben Winding