如何使用新插件替换less-loader中已弃用的javascriptEnabled选项 [英] How to replace the deprecated javascriptEnabled option in less-loader with a new plugin
问题描述
我需要对较少的文件使用内联js,并且以前有一个webpack配置,其中包含以下内容以启用内联js:
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 },
},
],
},
],
},
};
但是不建议使用javascriptEnabled
选项,其替代品是使用@plugin
语法并使用js插件.但是,我对 docs 以及如何正确实现插件感到困惑以及应该在我的webpack配置中实现哪个插件来替换现在不推荐使用的选项,这样我仍然可以使用内联js.我该怎么做呢?谢谢.
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.
推荐答案
出于安全考虑,不推荐使用内嵌javascript.它容易受到代码注入的攻击.因此,强烈建议不要使用内联javascript.
Inline javascript has been deprecated for security concerns. It was vulnerable to code injection. Therefor it is strongly advised to not use inline javascript.
如果您真的想严重使用它,可以使用不推荐使用的较早版本的javascriptEnabled
,但是我认为答案太简单了.这是这里.
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.
我做了一些研究,我想您是从这个问题开始使用plugins
的.我的猜测是,这里的作者并不是要用webpack
插件替换less-loader
中的javascriptEnabled
来实现类似的编写内联javascript的方式.我猜他的意思是出于安全原因,每段嵌入式JavaScript都应重写为less plugin
.
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.
您可以将内联javascript替换为其他Less plugins
,例如您提供的文档所示:
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();
}
您会得到:
.show-me-pi {
value: 3.141592653589793;
}
这篇关于如何使用新插件替换less-loader中已弃用的javascriptEnabled选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!