Итак, я хочу скомпилировать файлы TS в один файл JS, который может понять браузер. Как я могу это сделать? Когда я ссылаюсь на индекс. js в index.html
файле, браузер говорит, что index.js:1 Uncaught ReferenceError: define is not defined at index.js:1
. Как заставить компилятор TypeScript создать один файл JS, который может запустить браузер? Заранее спасибо. Независимо от типа генерации кода модуля, компилятор не создает файл, который браузер умеет обрабатывать.
index.ts
import { doSomething } from "./utils";
import { FooBar } from "./foobar";
const foobar = new FooBar();
foobar.foo();
doSomething();
tsconfig. json
{
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "amd", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outFile": "./index.js", /* Concatenate and emit output to single file. */
"strict": true, /* Enable all strict type-checking options. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
index. js
define("foobar", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var FooBar = /** @class */ (function () {
function FooBar() {
}
FooBar.prototype.foo = function () {
console.log(this);
};
return FooBar;
}());
exports.FooBar = FooBar;
});
define("utils", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function doSomething() {
console.log('doSomethingg');
}
exports.doSomething = doSomething;
});
define("index", ["require", "exports", "utils", "foobar"], function (require, exports, utils_1, foobar_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var foobar = new foobar_1.FooBar();
foobar.foo();
utils_1.doSomething();
});
//# sourceMappingURL=index.js.map