链接

引言

在组件开发中,业务功能和基础组件一般分开放,比如在我们的项目中,components为基础组件, container为业务组件,但是在container中调用components中的组件时,必须通过相对路径如../../components/XXXX才能找到要用的基础组件,这里 ../../在开发时其实时一种浪费。为了解决这个问题,我们引入了webpack.resolve.alias功能。

目录结构

├── package.json
├── postcss.config.js
├── public
│ ├── favicon.ico
│ ├── index.html
│ └── template.html
├── README.md
├── src
│ ├── components
│ ├── config.ts
│ ├── container
│ ├── index.scss
│ ├── index.tsx
│ ├── README.md
│ ├── routers.tsx
│ ├── stories
│ ├── @types
│ └── utils
├── styleguide.config.js
├── tsconfig.json
├── tslint.json
├── webpack.config.js
├── webpack.doc.js
├── webpack.vendor.js
└── yarn.lock

  

 

webpack 配置

// ...
resolve: {
extensions: ['.ts', '.tsx', '.js', 'config.js', '.json'],
alias: {
'@components': path.resolve(__dirname, './src/components'),
'@container': path.resolve(__dirname, './src/container'),
'@utils': path.resolve(__dirname, './src/utils'),
'@stories': path.resolve(__dirname, './src/stories')
}
},
// ...

通过上述配置,webpack已经没问题,再将代码中所有关于调用这些components、container、utils和stories的代码进行简化,示例如下

之前

// ./src/test/index.tsx
import AjaxTest from '../../components/Ajax'

之后

// ./src/test/index.tsx
import AjaxTest from '@components/Ajax'

问题1 [tslint] Module '@components/Ajax' is not listed as dependency in package.json (no-implicit-dependencies)

因为ts项目用了tslint来规约代码,所以会报错,查看官网关于no-implicit-dependencies的说明,将tslint.json增加如下规则

{
......
"rules": {
......
"no-implicit-dependencies": ["optional", ["src"]]
} ,
......
}

问题2 "no-implicit-dependencies": ["optional", ["src"]]

一看时ts报错,肯定时编译环境有问题,查看官网关于paths的定义,paths主要用来做目录映射,shangma上面webpack解决的时打包过程中的映射,并没有解决ts编译时的映射,所以在tsconfig.json中增加一项映射信息

{
"compilerOptions": {
"baseUrl": ".",
......
"paths": {
"@components/*": ["./src/components/*"],
"@container/*": ["./src/container/*"],
"@utils/*": ["./src/utils"],
"@stories/*": ["./src/stories"]
},
......
},
......
}

一次解决React+TypeScript+Webpack 别名(alias)找不到问题的过程「转载」的更多相关文章

  1. react: typescript import images alias

    1.webpack.config.js resolve: { extensions: ["ts", "tsx", "js", "j ...

  2. React+ES6+Webpack环境配置

    转自http://www.cnblogs.com/chenziyu-blog/p/5675086.html 参考http://www.tuicool.com/articles/BrAVv2y Reac ...

  3. React+ES6+Webpack深入浅出

    React已成为前端当下最热门的前端框架之一 , 其虚拟DOM和组件化开发让前端开发更富灵活性,而Webpack凭借它异步加载和可分离打包等优秀的特性,更为React的开发提供了便利.其优秀的特性不再 ...

  4. webpack配置别名alias出现的错误匹配

    @(webpack) webpack是一款功能强大的前端构建工具,不仅仅是针对js,它也可通过各种loader来构建相关的less,html,image等各种资源,将webpack配合流程制定工具gu ...

  5. webpack配置别名alias

    在webpack.config.js中,通过设置resolve属性可以配置查找“commonJS/AMD模块”的基路径,也可以设置搜索的模块后缀名,还可以设置别名alias.设置别名可以让后续引用的地 ...

  6. IDE vscode识别webpack中alias配置路径

    引言网上看到一篇关于 ctrl+鼠标左键无法识别别名路径的问题,最后有人回复的方法只能在ts项目中可以识别 https://segmentfault.com/q/1010000011911879 最后 ...

  7. Vue之vue中的data为什么是一个函数+vue中路径别名alias设置

    问题描述 为什么在vue组件中,我们的data属性必须是一个函数,new Vue()中的data除外,因为new Vue中只有一个data属性. 原因 因为我们能抽离出来的组件,肯定是具有复用性的,它 ...

  8. 入门React和Webpack

    最近在学习React.js,之前都是直接用最原生的方式去写React代码,发现组织起来特别麻烦,之前听人说用Webpack组织React组件得心应手,就花了点时间学习了一下,收获颇丰 说说React ...

  9. 轻松入门React和Webpack

    最近在学习React.js,之前都是直接用最原生的方式去写React代码,发现组织起来特别麻烦,之前听人说用Webpack组织React组件得心应手,就花了点时间学习了一下,收获颇丰 <!-- ...

随机推荐

  1. 在 Windows 10 x64 上安装及使用 ab 工具的流程

    本文转自:www.shuijingwanwq.com/2017/04/18/1568/ 1.基于AB测试工具进行高并发情形下的模拟测试,打开:http://httpd.apache.org/docs/ ...

  2. hdu 4811 数学 不难

    http://acm.hdu.edu.cn/showproblem.php? pid=4811 由于看到ball[0]>=2 && ball[1]>=2 && ...

  3. [Tools] Fix Only Committed Files with Prettier and lint-staged

    In this lesson we'll use prettier and lint-staged to run prettier only on files that have been chang ...

  4. [Angular2 Form] Model Driven Form Custom Validator

    In this tutorial we are going to learn how simple it is to create custom form field driven validator ...

  5. QAtomicInt支持四种类型的操作,Relaxed、Acquired、Release、Ordered

    Memory Ordering   Background 很久很久很久以前,CPU忠厚老实,一条一条指令的执行我们给它的程序,规规矩矩的进行计算和内存的存取. 很久很久以前, CPU学会了Out-Of ...

  6. Android shape画圆点

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  7. 第一次使用docker for windows 遇到的坑

    原文:第一次使用docker for windows 遇到的坑 1. 目前win10安装docker, 不需要安装其他工具,可直接到官网下载 2. 此版本的docker可同时运行Windows con ...

  8. USB 3.0规范中译本第9章 设备框架

    本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 设备框架可以被分成三层: 最底层是总线接口层,传送和接收包. 中间层处理在总线接口和设备的各种端点之间路由数 ...

  9. HTML代码简写法:Emmet和Haml(转)

    HTML代码写起来很费事,因为它的标签多. 一种解决方法是采用模板, 在别人写好的骨架内,填入自己的内容.还有一种就是我今天想要介绍的方法----简写法. 常用的简写法,目前主要是Emmet和Haml ...

  10. JavaScript 正則表達式

    一.简单介绍 1.什么是正則表達式 正則表達式本身就是一种语言,这在其他语言是通用的. 正則表達式(regular expression)描写叙述了一种字符串匹配的模式,能够用来检查一个串是否含有某种 ...