javascript - 如何配置 vue 应用程序(使用 vue-cli)以将 nonce 属性添加到生成的脚本标签?
问题描述
我有一个使用 vue-cli 编译的 vue 应用程序。我的 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');
},
};
我想nonce="<%= nonce %>"
为它生成的每个脚本标签添加 webpack。我看到 webpack 有一个__webpack_nonce__
变量,但我尝试在 vue.config.js 文件的许多部分设置它。我尝试将它添加到chainWebpack() 和configWebpack()。我已经尝试将它添加到 vue/home/main.ts 文件中。似乎没有任何效果。如何将随机数属性添加到脚本标签?
这是 vue 2.6.x 和 vue cli 4.5.x
解决方案
我最终编写了自己的 webpack 插件,因为我的用例比 script-ext-html-webpack-plugin 支持的要复杂一些。我需要${nonce}
标题标签和<%= nonce %>
正文标签。我的插件代码是一个简单的基于 @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;
});
}
}
更新后的 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');
},
};
推荐阅读
- node.js - ReferenceError:nodejs中未定义窗口
- javascript - Object.create() 方法是执行浅拷贝吗?
- python - 附加到字典
- angular - Updating data without passing id param in URL
- python - Python输出方法
- c++ - DirectShow:为 YV12 渲染
- python - 将字符串转换为 Dict / JSON
- css - 根据智能手机屏幕大小调整字体大小 React native
- botframework - Microsoft Bot Framework:确定用户的 Web 聊天会话可用性
- python - 从一个列表中找到一个元素并将其与另一个列表中的元素进行比较