如何解决Typescript AMD 模块 - 脚本未运行
我正在开发我的 Chrome 扩展程序,我想将打字稿添加到我的项目中。到目前为止,我已经能够以这种方式构建我的应用程序而没有任何编译/运行时错误(使用 require.js):
tsconfig-bg.json(与我为内容脚本和弹出窗口构建脚本的方式相同):
{
"compilerOptions": {
"module": "AMD","target": "ES6","outDir": "build","outFile": "build/background.js","rootDir": "src"
},"files": [
"src/consts.ts","src/background.ts",],}
manifest.json:
"background": {
"scripts": ["require.js","background.js"],"persistent": false
},"content_scripts": [
{
"matches": ["<all_urls>"],"js": ["require.js","content.js"]
}
],"browser_action": {
"default_popup": "popup.html","default_title": "__MSG_appName__"
}
background.ts:
import { Consts } from "./consts"
console.log("It is running!!");
consts.ts:
export const Consts = { ... }
控制台或任何地方都没有任何错误。但问题是我的脚本都没有运行。单击扩展程序的图标时只会出现弹出窗口,但即使加载了所有资源,也不会在后台、内容或弹出窗口中运行 js 脚本。我想我应该以某种方式定义入口点,但我该怎么做?
我现在应该做什么?
解决方法
AFAIK 似乎不可能在不使用 Webpack、Rollup 或其他工具的情况下在浏览器中使用由 Typescript 构建的 .js 脚本。即使
"compilerOptions": {
"module": "none"
},
我需要的一切都是工作类型控制,所以我必须使用 import/export
关键字,结果:
Object.defineProperty(exports,"__esModule",{ value: true });
const consts_1 = require("./consts");
和浏览器没有 require()
功能。
如果这是真的,那简直就是愚蠢:(
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。