javascript - Webpack resolve.alias does not work with typescript?
問(wèn)題描述
環(huán)境:
- node v6.9.2- typescript v2.4.1- webpack v1.15.0- awesome-typescript-loader v3.2.1
問(wèn)題:
在tsx中報(bào)錯(cuò) Module not found ’tools’...
import { toFieldValue } from ’@utils/tools’;
在webpack的配置中添加resolve.alias
resolve : { alias: { // 這里配置沒(méi)有問(wèn)題, 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', }))];
問(wèn)題解答
回答1:應(yīng)該是可以使用alias的,樓主可以參考我的react-typescript模版項(xiàng)目,https://github.com/devlee/tpl...
樓主這不能用,分析原因是tsconfig里的paths應(yīng)該是路徑,而不是直接是源文件吧
回答2:解決了~ tool.js 必須要聲明一個(gè)要聲明一個(gè)對(duì)應(yīng)的tool.d.ts文件。醉了醉了~~~
相關(guān)文章:
1. vim - docker中新的ubuntu12.04鏡像,運(yùn)行vi提示,找不到命名.2. docker-compose中volumes的問(wèn)題3. python 多進(jìn)程 或者 多線程下如何高效的同步數(shù)據(jù)?4. php - 想要遠(yuǎn)程推送emjio ios端怎么搞 需要怎么配合5. node.js - 問(wèn)個(gè)問(wèn)題 Uncaught (in promise)6. 一個(gè)走錯(cuò)路的23歲傻小子的提問(wèn)7. android - 添加multidex后在部分機(jī)型上產(chǎn)生anr的問(wèn)題,該如何解決8. angular.js - node.js中下載的angulae無(wú)法引入9. docker-compose 為何找不到配置文件?10. java - Hibernate查詢的數(shù)據(jù)是存放在session中嗎?
