How to replace the deprecated javascriptEnabled option in less-loader with a new plugin

你离开我真会死。 提交于 2019-12-24 22:04:20

问题


I need to use inline js for my less files, and previously had a webpack config with something like this to enable inline js:

module.exports = {
  ...
  module: {
    ...
    rules: [
      ...
      {
        test: /\.less$/,
        use: [
          { loader: 'style-loader' },
          { loader: 'css-loader' },
          {
            loader: 'less-loader',
            options: { javascriptEnabled: true },
          },
        ],
      },
    ],
  },
};

However the javascriptEnabled option has been deprecated and the replacement for this is to use the @plugin syntax and use a js plugin. However, I am a bit confused by the docs and how exactly to implement a plugin and which plugin should be implemented in my webpack config to replace this now deprecated option so I can still use inline js. How can I go about doing this? Thanks.


回答1:


Inline javascript has been deprecated for security concerns. It was vulnerable to code injection. Therefor it is strongly advised to not use inline javascript.

You could use an older version from before javascriptEnabled was deprecated if you really wanted to use it that badly, but I suppose that answer would be too simple. So here is this.

I did some research and I guess you got your idea to use plugins from this question. My guess is that the author here didn't mean to replace javascriptEnabled from less-loader with a webpack plugin to achieve a similar way of writing inline javascript. I guess he meant that every piece of inline javascript should be rewritten as a less plugin for security reasons.

If you think about it that way, the docs suddenly make more sense.

You could replace your inline javascript with different Less plugins like the docs you provided show:

// my-plugin.js
install: function(less, pluginManager, functions) {
    functions.add('pi', function() {
        return Math.PI;
    });
}
// etc

If you were to use this in your stylesheet:

@plugin "my-plugin";
.show-me-pi {
  value: pi();
}

You would get:

.show-me-pi {
  value: 3.141592653589793;
}


来源:https://stackoverflow.com/questions/58343406/how-to-replace-the-deprecated-javascriptenabled-option-in-less-loader-with-a-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!