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. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””2. Docker for Mac 創建的dnsmasq容器連不上/不工作的問題3. io - java 文件操作,如何向指定的位置插入內容 (不是替換內容) ?4. dockerfile - [docker build image失敗- npm install]5. javascript - vue使用videojs+videojs-contrib-hls報錯6. javascript - webpack --hot 熱重載無效的問題7. golang - 用IDE看docker源碼時的小問題8. Python 爬蟲 遇到的問題(手淘問大家)9. nignx - docker內nginx 80端口被占用10. mysql如何配置遠程php外網鏈接數據庫
