javascript - Webpack resolve.alias does not work with typescript?
问题描述
环境:
- node v6.9.2- typescript v2.4.1- webpack v1.15.0- awesome-typescript-loader v3.2.1
问题:
在tsx中报错 Module not found ’tools’...
import { toFieldValue } from ’@utils/tools’;
在webpack的配置中添加resolve.alias
resolve : { alias: { // 这里配置没有问题, jsx中可以正常使用 '@utils': './src/utils' }}
jsx中引用(可以使用)
import { toFieldValue } from ’@utils/tools’tsx配置
{ 'compilerOptions': { 'outDir': './dist/', 'module': 'commonjs', 'target': 'es6', 'sourceMap': true, 'allowSyntheticDefaultImports': true, 'jsx': 'react', 'pretty': true, 'noImplicitAny': true, 'traceResolution': true, 'baseUrl': '.', 'paths': { '@utils/*': ['./src/utils/*'] }, 'lib': [ 'dom', 'es2015.promise', 'es5', 'es2015.iterable', 'es2015.generator', 'es2015.symbol', 'es7'] }, 'include': [ './src/**/*' ], 'exclude': [ 'node_modules' ], 'awesomeTypescriptLoaderOptions': { 'useBabel': true, 'useCache': true }}
webpack添加配置(webpackConfig是webpack的配置)
webpackConfig.resolve.plugins = [(new TsConfigPathsPlugin({ configFileName: 'tsconfig.json', compiler: 'typescript', }))];
问题解答
回答1:应该是可以使用alias的,楼主可以参考我的react-typescript模版项目,https://github.com/devlee/tpl...
楼主这不能用,分析原因是tsconfig里的paths应该是路径,而不是直接是源文件吧
回答2:解决了~ tool.js 必须要声明一个要声明一个对应的tool.d.ts文件。醉了醉了~~~
相关文章:
1. mysql - 如何减少使用或者不用LEFT JOIN查询?2. 视频文件不能播放,怎么办?3. Python爬虫如何爬取span和span中间的内容并分别存入字典里?4. python - 编码问题求助5. python - Scrapy存在内存泄漏的问题。6. python - 我在使用pip install -r requirements.txt下载时,为什么部分能下载,部分不能下载7. 网页爬虫 - python 爬取网站 并解析非json内容8. mysql - 分库分表、分区、读写分离 这些都是用在什么场景下 ,会带来哪些效率或者其他方面的好处9. node.js - nodejs开发中常用的连接mysql的库10. mysql - jdbc的问题
