将Typescript 2.3模块发布到NPM用于Angular 4消费

时间:2017-05-24 17:30:02

标签: javascript node.js angular typescript typescript2.2

Writing NPM modules in Typescript 中有相关说明,但它已过时,现在有很多不同的答案可能会或可能不适用于Angular。 Jason Aden在youtube上有关于如何发布Angular Components的非常好的演示,但在这种情况下,我只对发布vanilla typescript类感兴趣。

1 个答案:

答案 0 :(得分:2)

我完成了测试这个的原型。我使用了来自this article - creating npm modules written in typescript的代码和来自Jason Aden's Packaging Angular Presentation的汇总建议。

请注意,tsconfig-cjs.json部分是这样的,除了支持webpack和Angular之外,我们还支持commonjs + es5工作流程。

Github存储库

TS Module Producer Project

步骤

项目设置

初始化项目(TS Module Producer)
mkdir tsmproducer
cd tsmproducer
# Initialize the project's package.json
npm init -y

替换package.json

    {
      "name": "tsmproducer",
      "version": "1.0.0",
      "description": "Example typescript npm project supporting es6 modules via the module package.json attribute as well as commonjs",
      "main": "target/main/cjs/index.js",
      "types": "target/main/esm/index.d.ts",
      "module": "target/main/esm/fesm.js",
      "scripts": {
        "build-cjs": "rm -rf target/main/cjs && tsc -p tsconfig-cjs.json",
        "build-esm": "rm -rf target/main/esm && tsc -p tsconfig-esm.json && rollup target/main/esm/index.js -o target/main/esm/fesm.js",
        "build-all": "npm run build-cjs && npm run build-esm"
      },
      "keywords": [
        "typescript",
        "commonjs",
        "npm",
        "modules",
        "es6",
        "es2015",
        "publishing",
        "rollup",
        "treeshaking"
      ],
      "author": "Ole Ersoy",
      "license": "MIT",
      "devDependencies": {
        "rollup": "^0.41.6",
        "typescript": "^2.3.4"
      }
    }

下载依赖项

npm i
npm i -g rollup
npm i -g typescript    


# Make typescript source directory
mkdir -p src/main/ts

为es6和commonjs

创建typescript编译器设置
# tsconfig-esm.json

    {
        "compilerOptions": {
            "declaration": true,
            "module": "es2015",
            "target": "es5",
            "rootDir": "src/main/ts",
            "outDir": "target/main/esm"
        },
        "exclude": [
            "node_modules"
        ]
    }

# tsconfig-cjs.json

  {
      "compilerOptions": {
          "module": "commonjs",
          "target": "es5",
          "rootDir": "src/main/ts",
          "outDir": "target/main/cjs"
      },
      "exclude": [
          "node_modules"
      ]
  }

创建打字稿测试导出:

# src/main/ts/printSubtract.ts

    export function printSubtract(...numbers: number[]) {
        console.log(`Subtracting the numbers: ${numbers.join(', ')}`);
    }

# src/main/ts/printAdd.ts

    export function printAdd(...numbers: number[]) {
        console.log(`Adding the numbers: ${numbers.join(', ')}`);
    }

# src/main/ts/index.ts

    export * from './printAdd';
    export * from './printSubtract';

编译

npm run build-all

创建消费者项目

mkdir tsmconsumer (TS Module Consumer)
cd tsmconsumer
npm init -y
npm i ../tsmproducer

创建模块消费者

touch index.ts

index.ts中尝试以下操作:

    import {printAdd, printSubtract} from 'tsmproducer';
    printAdd(1, 2);
    printSubtract(2, 1);

摘要

  • 汇总产生的FESM(Flat EcmaScript模块)通过package.json's module属性公开。
  • 生成Typescript声明文件并将其放在target/main/esm目录中,并通过package.json's types属性公开。
  • 通过package.json's main属性启用CommonJS模块支持。