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. sql語句 - mysql中關聯表查詢問題2. css - chrome下a標簽嵌套img 顯示會多個小箭頭?3. javascript - 求解答:實例對象調用constructor,此時constructor內的this的指向?4. javascript - iframe 為什么加載網頁的時候滾動條這樣顯示?5. javascript - 如何將一個div始終固定在某個位置;無論屏幕和分辨率怎么變化;div位置始終不變6. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。7. javascript - 有什么比較好的網頁版shell前端組件?8. html - vue項目中用到了elementUI問題9. mysql updtae追加數據sql語句10. javascript - 原生canvas中如何獲取到觸摸事件的canvas內坐標?
