admin管理员组文章数量:1356245
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.
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.
2 Answers
Reset to default 6 +50Inline 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;
}
Here is a modification/extension of @Luze's appoach, documentation here:
myPluginFile.js
:
const axios = require('axios').default
module.exports = {
install: function (less, pluginManager, functions) {
functions.add("blue", function () {
const headers = {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
timestamp: +new Date(),
}
try {
const method = 'get'
const url = 'https://myAPI./myFile.json'
const options = { headers: { ...headers } }
const { data } = await axios[method](url, {}, options)
return data
} catch (error) {
console.info('Error', { msg: error.message })
}
});
},
};
myStyleFile.less
:
@plugin "myPluginFile";
/* Style the body */
body {
color: blue();
}
本文标签:
版权声明:本文标题:javascript - How to replace the deprecated javascriptEnabled option in less-loader with a new plugin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744032272a2579131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论