[英]How to configure a vue application (with vue-cli) to add nonce attributes to generated script tags?
I have a vue application that is compiled using vue-cli.我有一个使用 vue-cli 编译的 vue 应用程序。 My vue.config.js file looks like:
我的 vue.config.js 文件如下所示:
'use strict';
module.exports = {
publicPath: `${process.env.CDN_URL || ''}/dist/`,
lintOnSave: true,
transpileDependencies: [],
outputDir: '.tmp/dist',
pages: {
navigator: {
entry: 'vue/home/main.ts',
template: 'views/home/index.ejs',
// Will output to dist/views/home/index.ejs
filename: 'views/home/index.ejs',
},
},
chainWebpack(config) {
// Override the default loader for html-webpack-plugin so that it does not fallback to ejs-loader.
// ejs-loader will use ejs syntax against the template file to inject dynamic values before html-webpack-plugin runs
config.module
.rule('ejs')
.test(/\.ejs$/)
.use('html')
.loader('html-loader');
},
};
I would like to have webpack add nonce="<%= nonce %>"
for each script tag that it generates.我想让 webpack 为它生成的每个脚本标签添加
nonce="<%= nonce %>"
。 I see that webpack has a __webpack_nonce__
variable, but I've tried setting that in many parts of the vue.config.js file.我看到 webpack 有一个
__webpack_nonce__
变量,但我已经尝试在 vue.config.js 文件的许多部分设置它。 I've tried adding it to chainWebpack() and configWebpack().我试过将它添加到 chainWebpack() 和 configWebpack() 中。 I've tried adding it to the vue/home/main.ts file.
我试过将它添加到 vue/home/main.ts 文件中。 Nothing seems to work.
似乎没有任何效果。 How do I get nonce attributes added to the script tags?
如何将 nonce 属性添加到脚本标签中?
This is vue 2.6.x and vue cli 4.5.x这是 vue 2.6.x 和 vue cli 4.5.x
I ended up writing my own webpack plugin since my use case was a bit more complex than what script-ext-html-webpack-plugin could support.我最终编写了自己的 webpack 插件,因为我的用例比 script-ext-html-webpack-plugin 可以支持的要复杂一些。 I needed
${nonce}
for header tags and <%= nonce %>
for body tags.我需要
${nonce}
用于 header 标签和<%= nonce %>
用于正文标签。 My plugin code is a simple and based off script-ext-html-webpack-plugin mentioned by @Sphinx我的插件代码很简单,基于 @Sphinx 提到的 script-ext-html-webpack-plugin
const HtmlWebpackPlugin = require('html-webpack-plugin');
class AddNonceToScriptTagsWebpackPlugin {
apply(compiler) {
compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
const alterAssetTagGroups = compilation.hooks.htmlWebpackPluginAlterAssetTags || HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups;
alterAssetTagGroups.tap(this.constructor.name, (data) => {
data.head = this._addNonceAttribute(data.head || []);
data.body = this._addNonceAttribute(data.body || []);
return data;
});
});
}
_addNonceAttribute(tags) {
return tags.map((tag) => {
if (tag.tagName === 'script') {
tag.attributes = tag.attributes || {};
tag.attributes.nonce = '<%= nonce %>';
} else if (tag.tagName === 'link' && tag.attributes && tag.attributes.as === 'script') {
tag.attributes = tag.attributes || {};
// eslint-disable-next-line no-template-curly-in-string
tag.attributes.nonce = '${nonce}';
}
return tag;
});
}
}
The updated vue.config.js file then looks like:更新后的 vue.config.js 文件如下所示:
'use strict';
module.exports = {
publicPath: `${process.env.CDN_URL || ''}/dist/`,
lintOnSave: true,
transpileDependencies: [],
outputDir: '.tmp/dist',
pages: {
navigator: {
entry: 'vue/home/main.ts',
template: 'views/home/index.ejs',
// Will output to dist/views/home/index.ejs
filename: 'views/home/index.ejs',
},
},
configureWebpack: {
plugins: [
new AddNonceToScriptTagsWebpackPlugin(),
],
},
chainWebpack(config) {
// Override the default loader for html-webpack-plugin so that it does not fallback to ejs-loader.
// ejs-loader will use ejs syntax against the template file to inject dynamic values before html-webpack-plugin runs
config.module
.rule('ejs')
.test(/\.ejs$/)
.use('html')
.loader('html-loader');
},
};
One solution should be adds script-ext-html-webpack-plugin into the plugin list of webpack.prod.conf
file (or your own webpack configuration file).一种解决方案应该是将script-ext-html-webpack-plugin添加到
webpack.prod.conf
文件(或您自己的 webpack 配置文件)的插件列表中。
new ScriptExtHtmlWebpackPlugin({
custom: {
test: /\.js$/, // adjust this regex based on your demand
attribute: 'nonce',
value: '<%= nonce %>'
}
}),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.