【发布时间】:2018-06-11 08:14:33
【问题描述】:
使用 Webpack 编译后重新加载 Chrome 扩展时出现此错误:
Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".
at new Function (<anonymous>)
at evalExpression (compiler.js:33919)
at jitStatements (compiler.js:33937)
at JitCompiler.webpackJsonp.../../../compiler/esm5/compiler.js.JitCompiler._interpretOrJit (compiler.js:34520)
at JitCompiler.webpackJsonp.../../../compiler/esm5/compiler.js.JitCompiler._compileTemplate (compiler.js:34448)
at compiler.js:34347
at Set.forEach (<anonymous>)
at JitCompiler.webpackJsonp.../../../compiler/esm5/compiler.js.JitCompiler._compileComponents (compiler.js:34347)
at compiler.js:34217
at Object.then (compiler.js:474)
我的 CSP 授予 unsafe-eval 权限。
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
如何允许在我的代码中使用eval()(因为 Webpack 使用它来生成源映射)?
【问题讨论】:
-
我可以避免这个问题的一种方法是使用
ng build --prod,但产品构建需要更长的时间 -
将
devtool: 'cheap-module-source-map'添加到您的 Webpack 配置中,通过覆盖默认源映射来解决问题。
标签: javascript google-chrome webpack google-chrome-extension