Reputation: 865
I have a very simple .ts file which declares some variable. ex:
var myvar = {'a':'b'};
I'd like to include this directly in another .ts file, so that other file can consume the variable directly, and it's all contained in one place (no require, etc.) The resulting js file that gets generated from the other file, needs to be used both in browser and node context.
I'd done it like this:
fileA.ts:
var myvar = {'a':'b'};
export default myvar;
fileB.ts (file which I want fileA.ts imported in to) has this at top:
import myvar from './fileA';
The problem is that the file that gets output, fileB.js, looks like this (in place of where I put the import statement):
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const myvar_1 = __importDefault(require("./fileA"));
I can't have the require statements. Is there a way to use ts, to directly 'copy in' the contents of one file in to the .js file that's being created?
Upvotes: 0
Views: 377
Reputation: 51579
Is there a way to use ts, to directly 'copy in' the contents of one file in to the .js file that's being created
Not exactly. There is no way to "copy" or "include" the contents of one source file into another source file.
However, with compiler option --module
set to none
, the program can be split into multiple files, which will be combined by the compiler into one output .js
file.
The big caveat is that you can't use import
and export
at the top level at all in your program. Instead, all the files share one common global scope where you can define global variables. You can find an example of such program organization in the "Namespaces" chapter of the documentation, in the "splitting across files" part.
Upvotes: 1