Rollup+TypeScript路径别名不生效,尝试多种方案仍未解决
Rollup构建中路径别名无法正确映射的问题
我目前使用Rollup作为打包工具,在构建过程中路径别名无法被正确映射。例如,示例中的导入语句本应被解析为:
import {FlowType} from '正确的目标路径'
我已经尝试了谷歌能找到的所有解决方案,但问题仍未解决。
以下是我的tsconfig配置:
// tsconfig.json { "compilerOptions": { "useDefineForClassFields": true, "noUnusedLocals": true, "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, "declaration": true, "target": "ES5", "lib": ["dom", "DOM.Iterable", "ESNext"], "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "strict": true, "forceConsistentCasingInFileNames": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "noEmit": false, "jsx": "react-jsx", "baseUrl": ".", "plugins": [ { "transform": "typescript-transform-paths" }, { "transform": "typescript-transform-paths", "afterDeclarations": true } ], "paths": { "@models/*" : ["src/models/*"], "@constants/*" : ["src/constants/*"], "@components/*" : ["src/components/*"], "@abi/*" : ["src/abi/*"], "@styles/*": ["src/styles/*"], "@utils/*": ["src/utils/*"], "@assets/*" : ["src/assets/*"], "@core/*" : ["src/core/*"], "@contexts/*" : ["src/contexts/*"], "@hooks/*" : ["src/hooks/*"], "@HOC/*" : ["src/HOC/*"] } }, "include": ["src", "test/main.tsx", "test/App.tsx"], "references": [{ "path": "./tsconfig.node.json" }], "exclude": ["dist","node_modules","test"] }
我的rollup.config.js配置如下:
import typescript from "@rollup/plugin-typescript"; import pkg from "./package.json" import alias from '@rollup/plugin-alias' import path from 'path' import { typescriptPaths } from 'rollup-plugin-typescript-paths' import json from '@rollup/plugin-json'; import dts from 'rollup-plugin-dts' import tsConfig from './tsconfig.json' import resolve from "rollup-plugin-node-resolve" import babel from "rollup-plugin-babel" import peerDepsExternal from "rollup-plugin-peer-deps-external" import scss from 'rollup-plugin-scss' export default { input: "src/index.ts", output: [ { file: pkg.main, format: "cjs"}, { file: pkg.module, format: "es" }, ], plugins: [ peerDepsExternal(), babel({ exclude: "node_modules/**", extensions: ['.ts', '.tsx'], include: ["src/**/*"] }), typescript({ outDir: 'dist', tsconfig : './tsconfig.json', }), scss({fileName: 'bundle.css'}), alias({ resolve: ['.ts','.tsx','.json','.js', '.jsx'], entries: [ { find : '@components/*', replacement : path.resolve(__dirname, 'src/components') }, { find : '@constants/*', replacement : path.resolve(__dirname, 'src/constants') }, { find : '@models/*', replacement : path.resolve(__dirname, 'src/models') }, { find : '@abi/*', replacement : path.resolve(__dirname, 'src/abi'), }, { find : '@styles/*', replacement : path.resolve(__dirname, 'src/styles') }, { find : '@utils/*' , replacement : path.resolve(__dirname , 'src/utils') }, { find : "@assets/*" , replacement : path.resolve(__dirname , 'src/assets') }, { find : "@core/*" , replacement : path.resolve(__dirname, "src/core") }, { find : "@contexts/*", replacement: path.resolve(__dirname, "src/contexts"), }, { find : "@hooks/*", replacement : path.resolve(__dirname, "src/hooks") }, { find : "@HOC/*", replacement : path.resolve(__dirname, "src/HOC") }, ], }), typescriptPaths({ preserveExtensions: true }), json() ] };
内容的提问来源于stack exchange,提问作者Jisang Seo
相关产品推荐
相关产品推荐

