【问题标题】:How to run typescript compiler as a package.json script without grunt or gulp如何在没有 grunt 或 gulp 的情况下将 typescript 编译器作为 package.json 脚本运行
【发布时间】:2015-10-23 08:32:43
【问题描述】:

我不想使用 grunt 或 gulp 来编译 ts 文件。我只想在我的 package.json 中这样做:

  "scripts": {
    "build": "tsc main.ts dist/"
  },

有可能吗?

【问题讨论】:

    标签: typescript package.json


    【解决方案1】:

    "build": "tsc main.ts dist/"

    强烈建议您使用tsconfig.json,然后使用-p 编译器选项来构建您的代码。 看:Compilation-Context

    设置

    这里是使用 tsc 和 NPM 脚本的设置

    初始化

    npm init
    npm install typescript --save
    

    然后在你的 package.json 添加一些脚本

    "scripts": {
        "build": "tsc -p .",
        "start": "npm run build -- -w"
    },
    

    使用

    • 仅用于构建:npm run build
    • 建筑+现场观看:npm start

    享受?

    【讨论】:

    • 链接现在已损坏。你有新的吗?
    • 只需上一级目录,排除文件名,项目将加载:github.com/TypeStrong/atom-typescript/tree/master/docs
    • 生成 tsconfig.json : yarn run tsc --init (如果你使用 npm,则将 yarn 替换为 npm)
    • 请注意,构建脚本中的./src 应该指向包含您的 tsconfig 的目录。非常感谢这个:)
    • 这应该是: npm install --save-dev typescript 你不想让 typescript 成为运行时依赖
    【解决方案2】:

    如果要编译运行,可以使用ts-node module

    npm install --save-dev ts-node 
    npm install --save-dev typescript
    

    然后运行:

    "scripts": {
        "start": "ts-node index.ts"
    },
    

    index.ts 已导入的所有其他 typescripts 文件(以及从 index.ts 依赖项中的导入)都将被编译和执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 2010-10-19
      • 2021-11-15
      • 2016-04-17
      • 2018-12-10
      • 2021-03-12
      • 1970-01-01
      相关资源
      最近更新 更多