【发布时间】:2016-11-07 17:47:35
【问题描述】:
我最近开始使用 typescript,不明白如何使用 webpack 构建一个 typescript 库,该库将在另一个 typescript 模块中使用。
也是这个库,旨在在浏览器中运行。
目前我已经完成了以下结构和构建输出:
lib
├── build
│ ├── typings // tsc output with declaration:true
│ │ ├── Bar.d.ts
│ │ └── Foo.d.ts
│ └── lib.bundle.js //webpack bundle file
├── src
│ ├── Bar.ts
│ └── Foo.ts
├── bower.json
├── package.json
└── tconfig.json
webpack 配置:
webpackOptions = {
resolve: { extensions: ['', '.ts'] },
module: {
loaders: [{ test: /\.ts$/, exclude: [/node_modules/,/out/,/.*\.d\.ts/],include:[/\.ts$/], loaders: ['ts-loader']}]
},
ts:{
compilerOptions:compilerOptions
},
output: {
library:"mylib",
libraryTarget:'commonjs',
filename: 'mylib.bundle.js'
}
}
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"outDir":"out",
"declaration": true
},
"exclude": [
"node_modules",
"bower_components",
"typings/global",
"build"
]
}
还有 Foo.ts 例如:
export class Foo{
foo(){}
}
Bar.ts:
import {Foo} from './Foo'
export class Bar{
public foo:Foo = new Foo();
}
所以我有以下构建输出:
- webpack 包
- 每个文件的打字稿声明
问题是:
- 如何在另一个浏览器应用程序中通过 bower 导入/使用这个库(也将使用 typescript 和 webpack)?
- 在使用此库的应用程序中将使用哪种导入语法(例如
import .. from或require)? - 也许我根本不应该使用 webpack 来捆绑库?
更新: 使用 NPM 管理 typescript 模块之间的本地依赖项,如 @basarat 所建议的,没有凉亭,一切正常
【问题讨论】:
标签: typescript webpack