问题
There are related instructions in Writing NPM modules in Typescript , however it's dated and there are now a lot of different answers which may or may not work for Angular. There's also a very good presentation by Jason Aden on youtube on how to publish Angular Components, but in this case I'm only interested in publishing vanilla typescript classes.
回答1:
I finished a prototype for testing this out. I used code from this article - creating npm modules written in typescript and the rollup suggestion from Jason Aden's Packaging Angular Presentation.
Note that the tsconfig-cjs.json
part of this is there so that in addition to supporting webpack and Angular, we also support commonjs + es5 workflows.
Github Repository
TS Module Producer Project
Steps
Project Setup
Initialize the Project (The TS Module Producer)mkdir tsmproducer
cd tsmproducer
# Initialize the project's package.json
npm init -y
Replace 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"
}
}
Download dependencies
npm i
npm i -g rollup
npm i -g typescript
# Make typescript source directory
mkdir -p src/main/ts
Create typescript compiler settings for es6 and commonjs
# 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"
]
}
Create typescript test exports:
# 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';
Compile
npm run build-all
Create a consumer project
mkdir tsmconsumer (TS Module Consumer)
cd tsmconsumer
npm init -y
npm i ../tsmproducer
Create a module consumer
touch index.ts
In index.ts
try the following:
import {printAdd, printSubtract} from 'tsmproducer';
printAdd(1, 2);
printSubtract(2, 1);
Summary
- The FESM (Flat EcmaScript module) produced by rollup is exposed via
package.json's module
attribute. - Typescript declaration files are produced and placed in the
target/main/esm
directory and exposed viapackage.json's types
attribute. - CommonJS module support is enabled via
package.json's main
attribute.
来源:https://stackoverflow.com/questions/44165084/publishing-typescript-2-3-modules-to-npm-for-angular-4-consumption