Siv*_*iva 13 typescript reactjs tsconfig ts-loader typescript-typings
编辑 1:将 GitHub URL 添加到项目中
编辑2:删除baseUrl
修复tsconfig.json
所有问题并使用相对导入效果很好。
链接:Github
如何生成带有相对路径而不是 的打字稿声明文件alias
?
我正在 UMD 模式下创建一个库(samplelibrary)并将其发布到 npm 中。打包的 npm 库只有build
文件夹(带有类型),package.json
并且README.md
当我尝试在另一个打字稿应用程序中使用该库时,由于生成的类型声明文件无效,构建失败。类型声明文件包含别名而不是相对路径。
编译日志:
ERROR in /workspace/myproject/node_modules/samplelibrary/build/typings/src/foo.d.ts(403,17):
TS2307: Cannot find module 'utils/bar
如何解决这个问题?
实际创建的声明文件foo.d.ts:
declare const Foo: {
bar: typeof import("utils/bar");
}
Run Code Online (Sandbox Code Playgroud)
预期文件:
declare const Foo: {
bar: typeof import("./utils/bar");
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"moduleResolution": "node",
"jsx": "react",
"sourceMap": true,
"rootDir": "./",
"baseUrl": "./src",
"paths": {
"@samplecompany/sampleproject": ["./"]
},
"outDir": "build",
"removeComments": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"strictNullChecks": true,
"declaration": true,
"declarationDir": "typings",
"importHelpers": true
},
"files": ["types/untyped-modules.d.ts"],
"include": [
"src/**/*",
"test/**/*",
"build/**/*",
"styleguide-renderer/**/*"
],
"exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)
文件夹结构:
root
-src
-utils
-bar.ts
-foo.ts
Run Code Online (Sandbox Code Playgroud)
实用程序/bar.ts
export const bar = {
hello: "World"
}
Run Code Online (Sandbox Code Playgroud)
src/foo.ts
import { bar } from "./utils/bar.ts
export default const Foo = {
bar
};
Run Code Online (Sandbox Code Playgroud)
好吧,如果没有重大的技巧,它似乎就行不通,但最终,我想通了。所以问题是类型声明路径需要重写。为此,您需要一些捆绑器/编译器来为您完成此操作。我猜想有各种包可以用于tsc
基础编译,但就我而言,我使用的是 Rollup,所以没有那么多。好吧,我只找到了一个https://github.com/zerkalica/zerollup/tree/master/packages/ts-transform-paths
那里有说明可以帮助您使用它。但基本上你需要做的是:
yarn add -D @zerollup/ts-transform-paths ttypescript
rollup.config.js
tsconfig.json
的"plugins": [{ "transform": "@zerollup/ts-transform-paths" }]
现在我的路径从eg 重写@react
为"../../react/file"
. 耶!
为了更好地衡量,这是我的配置:
{
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"declaration": true,
"declarationDir": "./dist",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"baseUrl": ".",
"paths": {
"@context/*": ["src/context/*"],
"@pm/*": ["src/pm/*"],
"@react/*": ["src/react/*"],
"@typings/*": ["src/typings/*"]
},
"plugins": [{ "transform": "@zerollup/ts-transform-paths" }]
},
"include": ["src"],
"exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)
import alias from '@rollup/plugin-alias'
import typescript from 'rollup-plugin-typescript2'
import postcss from 'rollup-plugin-postcss'
import ttypescript from 'ttypescript'
import path from 'path'
import pkg from './package.json'
export default {
input: 'src/index.ts',
output: [
{
file: pkg.main,
format: 'cjs',
},
{
file: pkg.module,
format: 'es',
},
],
external: [
...Object.keys(pkg.peerDependencies || {}),
],
plugins: [
alias({
entries: [
{ find: '@context', replacement: path.resolve(__dirname, 'src/context') },
{ find: '@pm', replacement: path.resolve(__dirname, 'src/pm') },
{ find: '@react', replacement: path.resolve(__dirname, 'src/react') },
{ find: '@typings', replacement: path.resolve(__dirname, 'src/typings') },
]
}),
typescript({
typescript: ttypescript
}),
postcss()
],
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8279 次 |
最近记录: |