[英]How do I compile my TypeScript code for Node.js to one file?
I want to compile TypeScript to one file when using with Node.js.我想在与 Node.js 一起使用时将 TypeScript 编译为一个文件。
I have tried configuring "tsconfig.json" like this:我试过像这样配置“tsconfig.json”:
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": false,
"sourceMap": false,
"outFile": "./app/index.js",
"pretty": true,
"types": [
"node"
]
}",
...but when I try with module" set to
"commonjs"`, I get the error: ...但是当我尝试将module" set to
"commonjs"` 时,出现错误:
error TS6082: Only 'amd' and 'system' modules are supported alongside --outFile.
If I change it to "module": "system"
, I get this error when running the file in node
:如果我将其更改为"module": "system"
,则在node
运行文件时会出现此错误:
ReferenceError: System is not defined
If I change module
to "amd"
, I get this error when running the file in node
:如果我将module
更改为"amd"
,则在node
运行文件时会出现此错误:
ReferenceError: define is not defined
It is not possible to bundle Node.js modules into a single file with the TypeScript compiler alone: each file in the CommonJS module system (used by Node.js) is treated as a single module, and cannot be joined together without proper module bundling techniques found in the many JavaScript code bundlers out there (such as Browserify, Webpack, Rollup, Parceljs, ...).单独使用 TypeScript 编译器无法将 Node.js 模块捆绑到单个文件中:CommonJS 模块系统(由 Node.js 使用)中的每个文件都被视为单个模块,如果没有适当的模块捆绑,则无法连接在一起在许多 JavaScript 代码打包器(例如 Browserify、Webpack、Rollup、Parceljs 等)中发现的技术。
Other module systems such as SystemJS do not have this limitation, and module definitions can be concatenated into a single file using just the TypeScript compiler: tsc ... --outfile bundle.js -module system
.其他模块系统如 SystemJS 没有这个限制,模块定义可以只使用 TypeScript 编译器连接成一个文件: tsc ... --outfile bundle.js -module system
。 However, they are not supported by Node.js on the fly.但是,它们不受 Node.js 动态支持。
For the actual solution, there are two reasonable choices: either configure your project to bundle the solution using a separate tool (Browserify, Webpack, ...), or include an implementation of SystemJS into your Node.js application (instructions here ).对于实际的解决方案,有两个合理的选择:要么配置您的项目以使用单独的工具(Browserify、Webpack 等)捆绑解决方案,要么将SystemJS的实现包含到您的 Node.js 应用程序中(此处的说明)。
I will also end with a side note: consider evaluating whether you really need to bundle your server-sided code.我还将以附注结束:考虑评估您是否真的需要捆绑您的服务器端代码。 Bundling is typically performed in frontend projects to reduce the size of client-sided resources, although it can be done as well for server applications in resource-efficient scenarios.捆绑通常在前端项目中执行以减少客户端资源的大小,尽管它也可以在资源高效的场景中为服务器应用程序完成。
@E_net4 is right. @E_net4是对的。 Currently, it is not possible to build modules into a single file with the TypeScript compiler alone.目前,无法单独使用 TypeScript 编译器将模块构建到单个文件中。 This article describes how to do so with webpack where the file structure is: 本文介绍了如何使用 webpack 执行此操作,其中文件结构为:
├── index.js
├── package.json
├── src
│ ├── add-message
│ │ └── index.ts
│ ├── index.ts
│ └── upcase-messages
│ └── index.ts
├── tsconfig.json
└── webpack.config.js
webpack.config.js
const nodeExternals = require('webpack-node-externals'); module.exports = { entry: './src/index.ts', output: { filename: 'index.js', // <-- Important libraryTarget: 'this' // <-- Important }, target: 'node', // <-- Important module: { rules: [ { test: /\\.tsx?$/, loader: 'ts-loader', options: { transpileOnly: true } } ] }, resolve: { extensions: [ '.ts', '.tsx', '.js' ] }, externals: [nodeExternals()] // <-- Important };
tsconfig.json
{ "compilerOptions": { "target": "es6", "module": "commonjs", "outDir": "./", "noImplicitAny": true, "strictNullChecks": true }, "include": [ "src/**/*.ts" ], "exclude": [ "node_modules" ] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.