【问题标题】:How do I compile my TypeScript code for Node.js to one file?如何将 Node.js 的 TypeScript 代码编译为一个文件?
【发布时间】:2017-02-22 11:09:12
【问题描述】:

我想在使用 Node.js 时将 TypeScript 编译成一个文件。

我尝试过像这样配置“tsconfig.json”:

"compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "noImplicitAny": false,
    "sourceMap": false,
    "outFile": "./app/index.js",
    "pretty": true,
    "types": [
      "node"
    ]
  }",

...但是当我尝试使用 module" set to"commonjs"` 时,我得到了错误:

error TS6082: Only 'amd' and 'system' modules are supported alongside --outFile.

如果我将其更改为"module": "system",则在node 中运行文件时会出现此错误:

ReferenceError: System is not defined

如果我将module 更改为"amd",在node 中运行文件时会出现此错误:

ReferenceError: define is not defined

【问题讨论】:

    标签: node.js typescript typescript2.0


    【解决方案1】:

    仅使用 TypeScript 编译器无法将 Node.js 模块捆绑到单个文件中:CommonJS 模块系统(由 Node.js 使用)中的每个文件都被视为单个模块,如果没有,则无法将其连接在一起在许多 JavaScript 代码捆绑器(例如 Browserify、Webpack、Rollup、Parceljs 等)中都可以找到合适的模块捆绑技术。

    SystemJS 等其他模块系统没有此限制,模块定义可以仅使用 TypeScript 编译器连接到单个文件中:tsc ... --outfile bundle.js -module system。但是,Node.js 不即时支持它们。

    对于实际的解决方案,有两个合理的选择:或者将您的项目配置为使用单独的工具(Browserify、Webpack 等)捆绑解决方案,或者将 SystemJS 的实现包含到您的 Node.js 应用程序中(指令here)。

    我还将以附注结束:考虑评估您是否真的需要捆绑服务器端代码。捆绑通常在前端项目中执行以减少客户端资源的大小,尽管在资源节约型场景中也可以为服务器应用程序执行捆绑。

    【讨论】:

      【解决方案2】:

      @E_net4 是对的。目前,仅使用 TypeScript 编译器无法将模块构建到单个文件中。 This article 描述了如何在文件结构所在的 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"
        ]
      }
      

      【讨论】:

        猜你喜欢
        • 2018-07-04
        • 2013-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-23
        • 2012-10-30
        • 1970-01-01
        • 2012-10-07
        相关资源
        最近更新 更多