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. boot2docker无法启动2. java - web端百度网盘的一个操作为什么要分两次请求服务器, 有什么好处吗3. python - 怎样在linux下开发flask web应用时查看代码出错(traceback)的地方。4. docker内创建jenkins访问另一个容器下的服务器问题5. 登录界面URL在服务器上无法找到6. javascript - vue-cli创建的vue项目去哪里找啊?7. javascript - 表单ajax提交后跳转,手机按返回又进入这个表单页了!!8. python - 用 requests 登陆战网失败9. css - 如何使一个 inline-block 元素的子元素自动填充10. 发送验证码显示服务器连接失败

网公网安备