【问题标题】:Import es6 module in chrome extension mv 3 background script with typescript使用打字稿在chrome扩展mv 3后台脚本中导入es6模块
【发布时间】:2022-01-08 04:10:54
【问题描述】:

我是构建 Chrome 扩展 mv3 的新手。现在我正在使用 Typescript 作为我的主要语言创建一个 extension。我尝试导入 Es6 模块,但是当我加载扩展程序时,Chrome 显示“Uncaught ReferenceError: exports is not defined”。

这是我的项目结构

|   .babelrc
|   manifest.json
|   package.json
|   tsconfig.json
|   webpack.config.js
|
+---public
|   +---html
|   |       index.html
|   |       popup.html
|   |
|   +---js
|   |       background.d.ts
|   |       background.js
|   |       background.js.map
|   |       background.utils.d.ts
|   |       background.utils.js
|   |       background.utils.js.map
|   |       index.html
|   |       main.js
|   |       popup.d.ts
|   |       popup.js
|   |       popup.js.map
|   |
|   \---styles
|           popup.css
|
\---src
    |   background.ts
    |   background.utils.ts
    |   popup.ts
    |
    \---@types
        \---background
                index.d.ts

我的 manifest.json 文件:

{
  "name": "Getting Started Example",
  "description": "Build an Extension!",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "./public/js/background.js",
    "persitent": true
  },
  "action": {
    "default_popup": "./public/html/popup.html"
  },
  "minimum_chrome_version": "92",
  "permissions": [
    "management",
    "scripting",
    "activeTab",
    "webRequest",
    "tabs",
    "webNavigation",
    "storage"
  ],
  "content_scripts": [
    {
      "matches": [
        "*://*.nytimes.com/*"
      ],
      "js": [
        "./public/js/popup.js"
      ]
    }
  ]
}

我的 tsconfig.json 文件:

{
  "compilerOptions": {
    "target": "es2016",                                  
    "module": "commonjs",                                
    "typeRoots": [
      "./node_modules/@types"
    ],                                 
    "sourceMap": true,                            
    "outDir": "./public/js",                      
    "sourceRoot": "./src",                        
    "esModuleInterop": true,                      
    "forceConsistentCasingInFileNames": true,     
    "strict": true,                               
    "skipLibCheck": true                          
  }
}

在我的 background.utils.ts 文件中:

const myFunction = ()=>{}
export default myFunction

在我的 background.ts 文件中:

import myFunction from './background.utils/'

但是 Chromes 说即使我在 Internet 上尝试了几种方法,例如在 mainifest.json 文件中添加“type”:“module”或在 tsconfig.json 文件中添加远程“module”:“commonjs”,也没有定义导出.

你们知道为什么会这样吗?

期待收到你们的答案

非常感谢。

【问题讨论】:

    标签: javascript typescript google-chrome google-chrome-extension babeljs


    【解决方案1】:

    您已经尝试在清单中插入“类型:模块”?

    "background": {
      "service_worker": "./public/js/background.js",
      "type": "module"
    },
    

    【讨论】:

    • 是的,我已经这样做了
    猜你喜欢
    • 2021-09-14
    • 2017-05-07
    • 2016-07-04
    • 1970-01-01
    • 2019-06-20
    • 2018-04-11
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多