使用 typescript 在 chrome 扩展 mv 3 后台脚本中导入 es6 模块

jac*_*123 9 javascript google-chrome google-chrome-extension typescript babeljs

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

这是我的项目结构

|   .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
Run Code Online (Sandbox Code Playgroud)

我的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"
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的 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                          
  }
}
Run Code Online (Sandbox Code Playgroud)

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

const myFunction = ()=>{}
export default myFunction
Run Code Online (Sandbox Code Playgroud)

在我的 background.ts 文件中:

import myFunction from './background.utils/'
Run Code Online (Sandbox Code Playgroud)

但是 Chrome 说导出未定义,即使我在互联网上尝试了多种方法,例如将“type”:“module”添加到 mainifest.json 文件中或在 tsconfig.json 文件中添加远程“module”:“commonjs”。

你们知道为什么会发生这种情况吗?

期待收到你们的答复

太感谢了。