Pla*_*fan 5 javascript node.js typescript webpack
我有 2 个打字稿项目,其中一个依赖于另一个。我可以通过在 tsconfig 中设置 "declaration": true 来生成打字稿声明文件(这适用于两个项目)。我还将“outDir”设置为“./dist”。我可以看到大量生成的声明文件发送到 dist 文件夹。
我也在使用 webpack。但是我如何让 wepack 将所有这些声明文件包含在包中以便客户端可以使用它?
所以我的 tsconfig 看起来像这样:
{
"compilerOptions": {
"allowJs": true,
"alwaysStrict": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "Node",
"noImplicitAny": true,
"sourceMap": true,
"strictNullChecks": true,
"target": "es5",
"declaration": true,
"outDir": "./dist",
"types": [
"mocha", "node", "./dist/index.d.ts"
],
"lib": [
"es5",
"es2015",
"es6",
"dom"
]
},
"include": [
"lib/**/*", "tests/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
然而,这是行不通的。看到如下错误信息:
ERROR in /Users/Plastikfan/dev/github/js/jaxom/tsconfig.json
[tsl] ERROR
TS2688: Cannot find type definition file for './dist/index.d.ts'.
Run Code Online (Sandbox Code Playgroud)
事实上,当我看到这个的时候:
"types": [
"mocha", "node", "./dist/index.d.ts"
],
Run Code Online (Sandbox Code Playgroud)
这让我担心,因为 /dist/index.d.ts 是 webpack/typescript 构建过程的输出,所以构建如何在构建过程开始时看到其内容,而它可能不存在。对我来说,这似乎不对,那么我如何声明“./dist/index.d.ts”是该项目的声明?
那么一旦这个问题解决了,typescript 客户端如何使用 typescript/javascript 包;即我有另一个项目(zenobia)也是一个 typescript 项目,它需要使用 npm 中的 typescript 项目,那么我需要做什么才能让它正常工作?Jaxom 是要使用的源打字稿项目,在它的索引文件中,它导出所有面向客户端的声明,实际上它看起来像这样:
index.ts:
export * from './types';
export * from './specService/spec-option-service.class';
export * from './converter/xpath-converter.class';
Run Code Online (Sandbox Code Playgroud)
与对应生成的 dist/index.d.ts 相同
在消费者方面(zenobia),我知道我需要某种三重斜杠指令和导入语句,但我在兜圈子,但没有得到正确的结果。
zenobia 中的 tsconfig 与 jaxom 相同(如我上面所示)。
客户端上的导入如下所示:
从 'jaxom' 导入 * 作为 Jaxom;
所以我希望导出的所有定义都可以在 Jaxom 上使用。这个导入应该可以工作,因为它是打字稿。如果是js,那么这对Nodejs项目不起作用。
我读过的文档要么对我来说含糊不清,要么我需要的解释介于 webpack 和 typescript 之间,既没有正确解释完整的集成,也没有做出假设。
在 Jaxom 中,我有 2 个 webpack 配置(1 个用于生产代码,另一个用于测试)。这是我在所有 webpack/typescript 项目中使用过/正在使用的模式。
Jaxom 的生产 webpack 配置:
{
"compilerOptions": {
"allowJs": true,
"alwaysStrict": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "Node",
"noImplicitAny": true,
"sourceMap": true,
"strictNullChecks": true,
"target": "es5",
"declaration": true,
"outDir": "./dist",
"types": [
"mocha", "node", "./dist/index.d.ts"
],
"lib": [
"es5",
"es2015",
"es6",
"dom"
]
},
"include": [
"lib/**/*", "tests/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
以及用于测试的 webpack 配置
ERROR in /Users/Plastikfan/dev/github/js/jaxom/tsconfig.json
[tsl] ERROR
TS2688: Cannot find type definition file for './dist/index.d.ts'.
Run Code Online (Sandbox Code Playgroud)
对于其他遇到这个问题的初学者来说,无法从客户端角度使用类型的问题是由源码包中的多个问题引起的,解释如下。
\n\n1)实际上,只有 package.json 才显示出一个问题,该问题未包含在原始帖子中,但它相当于:
\n\n有 2 个构建流,1 个用于源代码,另一个用于测试(在我的 package.json 中,它们由 2 个单独的脚本表示:“build:d”用于开发源代码 [生成./dist/jaxom-bundle。 js ] 构建和测试代码构建的“build:t” [生成./dist/jaxom-test-bundle.js ]。这两个脚本都依赖于一个“干净”的脚本,该脚本利用 rimraf 来删除现有的构建文件(在“dist/”文件夹中)。使用我当前的构建设置,构建测试会删除所有现有的构建文件,然后构建测试包。源包丢失。
这意味着当我打包构建时(npm pack用于创建发布到 npm 注册表的 tar 球),如果我在发布之前刚刚运行测试构建,则包含所有测试的测试捆绑将被打包。这不是我的本意。这种情况的简单修复是确保在 npm pack/publish 之前构建源包,这在您查看构建时很明显:即,您可以看到包含的类型文件。对于那些刚刚起步的开发人员来说,认识到这一点非常重要。在发布 npm 包(特别是新包)之前,您应该完成使用 npm pack 的这一步,然后提取其内容以查看其中的内容,或者只是仔细注意 npm pack 的结果和观察包装中的物品。
查看源包和测试包的 npm pack 的以下输出:
\n\n源包(./dist/jaxom-bundle.js):
\n\n\xce\xbb ~/dev/github/js/jaxom/ feature/define-exports* npm pack\nnpm notice \nnpm notice jaxom@0.0.1\nnpm notice === Tarball Contents === \nnpm notice 1.1kB LICENSE \nnpm notice 165.5kB dist/jaxom-bundle.js \nnpm notice 3.2kB package.json \nnpm notice 99B README.md \nnpm notice 455B typings/exceptions.d.ts \nnpm notice 133B typings/index.d.ts \nnpm notice 1.1kB typings/normaliser/normaliser.class.d.ts \nnpm notice 1.8kB typings/specService/spec-option-service.class.d.ts\nnpm notice 9.4kB typings/transformer/transformer.class.d.ts \nnpm notice 2.5kB typings/types.d.ts \nnpm notice 1.3kB typings/converter/xpath-converter.class.d.ts \nnpm notice 5.6kB typings/converter/xpath-converter.impl.d.ts \nnpm notice === Tarball Details === \nnpm notice name: jaxom \nnpm notice version: 0.0.1 \nnpm notice filename: jaxom-0.0.1.tgz \nnpm notice package size: 50.5 kB \nnpm notice unpacked size: 192.2 kB \nnpm notice shasum: c43be4000932201c0ca077aeb3f102bd0130eef5\nnpm notice integrity: sha512-96HaZbSHn7kCc[...]rS0K4pLxDMLPA==\nnpm notice total files: 12 \nnpm notice \njaxom-0.0.1.tgz\n\xce\xbb ~/dev/github/js/jaxom/\nRun Code Online (Sandbox Code Playgroud)\n\n测试包(./dist/jaxom-test-bundle.js)被测试类(spec.d.ts)污染:
\n\n\xce\xbb ~/dev/github/js/jaxom/ feature/define-exports* npm pack\nnpm notice \nnpm notice jaxom@0.0.1\nnpm notice === Tarball Contents === \nnpm notice 1.1kB LICENSE \nnpm notice 165.5kB dist/jaxom-bundle.js \nnpm notice 186.0kB dist/jaxom-test-bundle.js \nnpm notice 3.2kB package.json \nnpm notice 253.4kB dist/jaxom-test-bundle.js.map \nnpm notice 99B README.md \nnpm notice 455B typings/exceptions.d.ts \nnpm notice 455B typings/lib/exceptions.d.ts \nnpm notice 133B typings/index.d.ts \nnpm notice 133B typings/lib/index.d.ts \nnpm notice 1.1kB typings/lib/normaliser/normaliser.class.d.ts \nnpm notice 1.1kB typings/normaliser/normaliser.class.d.ts \nnpm notice 11B typings/tests/normaliser/normaliser.class.spec.d.ts \nnpm notice 1.8kB typings/lib/specService/spec-option-service.class.d.ts \nnpm notice 1.8kB typings/specService/spec-option-service.class.d.ts \nnpm notice 20B typings/tests/specService/spec-option-service.class.spec.d.ts\nnpm notice 329B typings/tests/test-helpers.d.ts \nnpm notice 9.4kB typings/lib/transformer/transformer.class.d.ts \nnpm notice 9.4kB typings/transformer/transformer.class.d.ts \nnpm notice 20B typings/tests/transformer/transformer.class.spec.d.ts \nnpm notice 2.5kB typings/lib/types.d.ts \nnpm notice 2.5kB typings/types.d.ts \nnpm notice 1.3kB typings/converter/xpath-converter.class.d.ts \nnpm notice 1.3kB typings/lib/converter/xpath-converter.class.d.ts \nnpm notice 20B typings/tests/converter/xpath-converter.class.spec.d.ts \nnpm notice 5.6kB typings/converter/xpath-converter.impl.d.ts \nnpm notice 5.6kB typings/lib/converter/xpath-converter.impl.d.ts \nnpm notice 20B typings/tests/converter/xpath-converter.impl.spec.d.ts \nnpm notice === Tarball Details === \nnpm notice name: jaxom \nnpm notice version: 0.0.1 \nnpm notice filename: jaxom-0.0.1.tgz \nnpm notice package size: 115.6 kB \nnpm notice unpacked size: 654.3 kB \nnpm notice shasum: 8433700d3378bf2ab4ff3798a93e7dbb98baee86\nnpm notice integrity: sha512-cQ9RUCLwHMOuq[...]xS4kMJe/4jPqQ==\nnpm notice total files: 28 \nnpm notice \njaxom-0.0.1.tgz\n\xce\xbb ~/dev/github/js/jaxom/\nRun Code Online (Sandbox Code Playgroud)\n\n我认为解决上述问题的最佳方法是使用一个特定的发布脚本,在发布到 npm 之前构建正确的包。我发现了几篇很好的文章来解释这一点:一步一步:构建和发布 NPM Typescript 包或The 30-Second Guide to Publication a TypeScript package to NPM
\n\n2) 输入一个单独的文件夹(我的错!)。
\n\n最好让类型生成到与主包相同的文件夹中,在本例中为 ./dist。
\n| 归档时间: |
|
| 查看次数: |
8267 次 |
| 最近记录: |