manifest.json
中不能使用ts:
"background": {"service_worker": "background.ts"}
只能使用js
"background": {"service_worker": "background.js"}
在vite.config.js
中增加以下配置,可以将background.ts
转换成background.js
export default defineConfig({build: {rollupOptions: {input: {main: path.resolve(__dirname, 'index.html'),background: path.resolve(__dirname, 'src/utils/background.ts')},output: {entryFileNames: '[name].js'},external: ['express'] // 添加 express 到外部依赖}}
})
参考资料:
- Chrome extension with Typescript
- How to use TypeScript import in Chrome extension background page?
- background.ts shows an error “Service worker registraion failed” but when i change it backgrounds.js it works fine in manifest.json