简体   繁体   English

Typescript AMD 模块 - 脚本未运行

[英]Typescript AMD modules - scripts are not running

I'm working on my Chrome extension and I want to add typescript to my project.我正在开发我的 Chrome 扩展程序,我想将 typescript 添加到我的项目中。 So far I've been able to build my app without any compile/runtime errors (using require.js) this way:到目前为止,我已经能够以这种方式构建我的应用程序而没有任何编译/运行时错误(使用 require.js):

tsconfig-bg.json (the same way I'm building scripts for content script and popup): 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 : 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 :背景.ts

import { Consts } from "./consts"
console.log("It is running!!");

consts.ts :常量.ts

export const Consts = { ... }

There is not any error in console or wherever.控制台或任何地方都没有任何错误。 But the problem is none of my scripts is running.但问题是我的脚本都没有运行。 Only the popup will appear when clicking on extension's icon, but no js script is running in either background, content or popup even if all the resources are loaded.单击扩展程序的图标时只会出现弹出窗口,但即使加载了所有资源,也不会在后台、内容或弹出窗口中运行 js 脚本。 I guess I should define the entry point somehow, but how could I do that?我想我应该以某种方式定义入口点,但我该怎么做呢?

What I'm supposed to do now?我现在该怎么办?

AFAIK it seems like it is not possible to use.js scripts that have been builded by Typescript in browser without using Webpack, Rollup or others. AFAIK 似乎不可能在不使用 Webpack、Rollup 或其他的情况下在浏览器中使用由 Typescript 构建的.js 脚本。 Even if即使

  "compilerOptions": {
    "module": "none"
  },

Everything I need is working type control, so I must use import/export keywords which results in:我需要的一切都是工作类型控制,所以我必须使用import/export关键字,这会导致:

Object.defineProperty(exports, "__esModule", { value: true });
const consts_1 = require("./consts");

and browsers do not have require() function.并且浏览器没有require() function。

If it is really true, that's just plain stupid:(如果这是真的,那简直是愚蠢的:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM