日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
前端常用開發(fā)工具的路徑解析配置

本文是 Webpack CheatSheet | Webpack 基礎(chǔ)與實(shí)踐清單的一部分,項(xiàng)目代碼可以參考 fe-boilerplate | 多技術(shù)棧前端項(xiàng)目模板。

路徑解析

隨著需求的迭代與功能的完善,我們的項(xiàng)目也會愈發(fā)龐大而復(fù)雜,目錄層級結(jié)構(gòu)也會不斷深化;以 React 實(shí)踐清單中討論的 React 項(xiàng)目組織方式為例,我們常會分為 components, containers, services, apis, ducks, store, i18n 等等目錄,如果全部以相對路徑方式引入,可能會變成這個(gè)樣子:

 
 
 
  1. import React from 'react'; 
  2. import { connect } from 'react-redux'; 
  3.  
  4. import { someConstant } from './../../config/constants'; 
  5. import MyComponent from './../../../components/MyComponent'; 
  6. import { myActionCreator } from './../../../ducks/someReducer'; 

毫無疑問,這樣繁多的引用不可避免地會導(dǎo)致代碼之間耦合度的增加,使得更難以重構(gòu)或者優(yōu)化。在適當(dāng)?shù)啬K劃分的基礎(chǔ)上,我們希望在跨模塊引用時(shí),能夠以絕對路徑的方式,譬如:

 
 
 
  1. import React from 'react'; 
  2. import { connect } from 'react-redux'; 
  3. import { someConstant } from 'Config/constants'; 
  4. import MyComponent from 'Components/MyComponent'; 
  5. import { myActionCreator } from 'Ducks/someReducer'; 

當(dāng)然,我們并不提倡過度地使用絕對路徑引入,對于相對關(guān)系固定的組件,還是應(yīng)該優(yōu)先使用相對路徑方式引入。

Webpack

如前文介紹,Webpack 允許我們使用 resolve.alias 來自定義路徑解析:

 
 
 
  1. module.resolve = { 
  2.   alias: { 
  3.     Config: path.resolve(__dirname, '..', 'src', 'config'), 
  4.     Components: path.resolve(__dirname, '..', 'src', 'components'), 
  5.     Ducks: path.resolve(__dirname, '..', 'src', 'ducks'), 
  6.     Shared: path.resolve(__dirname, '..', 'src', 'shared'), 
  7.     App: path.join(__dirname, '..', 'src') 
  8.   } 
  9. }; 

開發(fā)工具的支持是不可避免地因素,值得高興的是 VSCode 允許我們在 jsconfig.json 中配置解析規(guī)則,Auto-Import 這樣的自動(dòng)導(dǎo)入工具同樣能識別這些規(guī)則:

 
 
 
  1.   "compilerOptions": { 
  2.     "target": "es2017", 
  3.     "allowSyntheticDefaultImports": false, 
  4.     "baseUrl": "./", 
  5.     "paths": { 
  6.       "Config/*": ["src/config/*"], 
  7.       "Components/*": ["src/components/*"], 
  8.       "Ducks/*": ["src/ducks/*"], 
  9.       "Shared/*": ["src/shared/*"], 
  10.       "App/*": ["src/*"] 
  11.     } 
  12.   }, 
  13.   "exclude": ["node_modules", "dist"] 

ESLint

ESLint 同樣是前端開發(fā)不可或缺的部分,我們可以使用 eslint-import-resolver-webpack 來擴(kuò)展 eslint-import 的模塊解析,使用 npm 安裝該模塊之后進(jìn)行如下配置:

 
 
 
  1. --- 
  2. settings: 
  3.   import/resolver: webpack  # take all defaults 

或者指定文件名:

 
 
 
  1. --- 
  2. settings: 
  3.   import/resolver: 
  4.     webpack: 
  5.       config: 'webpack.dev.config.js' 
  6.       config-index: 1   # optional, take the config at index 1 

對于未使用 Webpack 的項(xiàng)目,則可以考慮使用 eslint-import-resolver-alias:

 
 
 
  1. // .eslintrc.js 
  2. module.exports = { 
  3.   settings: { 
  4.     'import/resolver': { 
  5.       alias: { 
  6.         map: [ 
  7.           ['babel-polyfill', 'babel-polyfill/dist/polyfill.min.js'], 
  8.           ['helper', './utils/helper'], 
  9.           ['material-ui/DatePicker', '../custom/DatePicker'], 
  10.           ['material-ui', 'material-ui-ie10'] 
  11.         ], 
  12.         extensions: ['.ts', '.js', '.jsx', '.json'] 
  13.       } 
  14.     } 
  15.   } 
  16. }; 

Jest

我們可以在 package.json 中的 jest 配置項(xiàng)中添加 moduleNameMapper 屬性:

 
 
 
  1. "jest": { 
  2.   "moduleNameMapper": { 
  3.     "^Config(.*)$": "/src/config$1", 
  4.     "^Components(.*)$": "/src/components$1", 
  5.     "^Ducks(.*)$": "/src/ducks$1", 
  6.     "^Shared(.*)$": "/src/shared$1", 
  7.     "^App(.*)$": "/src$1" 

TypeScript

TypeScript 的配置類似于 VSCode,在 tsconfig.json 的 compilerOptions 選項(xiàng)中添加如下配置:

 
 
 
  1.   "baseUrl": ".", 
  2.   "paths": { 
  3.     "c-apis/*": ["src/apis/*"], 
  4.     "c-models/*": ["src/models/*"], 
  5.     "c-stores/*": ["src/stores/*"], 
  6.     "c-utils/*": ["src/shared/*"] 
  7.   } 

 【本文是專欄作者“張梓雄 ”的原創(chuàng)文章,如需轉(zhuǎn)載請通過與作者聯(lián)系】


文章名稱:前端常用開發(fā)工具的路徑解析配置
文章分享:http://www.5511xx.com/article/cdcschh.html