lhk*_*lhk 6 javascript google-chrome-extension google-chrome-devtools source-maps parceljs
我正在为Chrome开发一个Webextension,代码是用Typescript编写的,所以我需要源映射。
该扩展程序与ParcelJS捆绑在一起,但我认为我的问题与捆绑器无关。
从Chrome 70更新到72后,源地图不再起作用。作为最小的示例,我可以使用以下代码在MacOS 14和Ubuntu 18.04,Chrome 72上重现该问题。
这个问题似乎只限于Chrome72。不幸的是,在撰写本文时,这是当前的稳定版本:
为了方便起见,我建立了一个github仓库。克隆它并运行以下命令(对不起,我不确定是否需要在全球范围内安装包裹捆绑器。为方便起见,我总是这样做)
git clone https://github.com/lhk/contentscript_smap.git
cd contentscript_smap
# npm install -g parcel-bundler
npm install -d
parcel build manifest.json
Run Code Online (Sandbox Code Playgroud)
要遵循Stackoverflow规则(如果您链接到代码,则还应该在问题中包含它,该链接有时可能会断开):
content.ts:
console.log('log from typescript')
Run Code Online (Sandbox Code Playgroud)
manifest.json:
{
"manifest_version": 2,
"name": "sourcemaps messed up",
"version": "0.0.1",
"description": "",
"author": "",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"./content.ts"
]
}
],
"permissions": [
"activeTab",
"<all_urls>"
]
}
Run Code Online (Sandbox Code Playgroud)
package.json:
{
"name": "contentscript_smap",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lhk/contentscript_smap.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/lhk/contentscript_smap/issues"
},
"homepage": "https://github.com/lhk/contentscript_smap#readme",
"dependencies": {
"parcel-bundler": "^1.11.0",
"parcel-plugin-web-extension": "^1.5.1",
"typescript": "^3.3.3"
}
}
Run Code Online (Sandbox Code Playgroud)
Parcel将捆绑扩展并产生一个dist/
文件夹,您可以在该文件夹中将其安装在Firefox和Chrome中。
Firefox运行良好(查看对内容.TS的源代码引用):
Chrome找不到源地图:
不仅仅是控制台仅显示编译的源代码,还不是源映射的原始源代码。我根本无法在Chrome中找到打字稿代码。
Firefox 也有类似的问题:无法在弹出窗口和后台脚本中加载 Sourcemap。文档中甚至提到了这一点,解决方法是将源映射托管在本地网络服务器上。
所以我尝试了一下,发现它可以解决我的 Chrome 问题。希望您可以通过以下步骤重现它:
npm install local-web-server
"content_security_policy": "script-src 'self' https://127.0.0.1:8000/; object-src 'self'"
parcel build src/manifest.json --public-url=https://127.0.0.1:8000/
cd dist/
并ws -https
设置本地服务器现在在 Chrome 中重新加载扩展程序,它就可以工作了(好吧,至少对我来说)。