2

I would like to use resolve.alias feature from webpack in my projects using React Starter Kit.

For example:

Instead this:

import Component from '../../components/Component

I would like to use

import Component from 'components/Component

I've added this configuration on config.js file:

resolve: {
  alias: {
    components: './src/components'
  }
}

This resolve.alias enables aliases for projects bundled with webpack but doesn't working with React Starter Kit.

4

2 回答 2

1

您需要使用完整路径配置别名:

resolve: {
  alias: {
    components: path.resolve(__dirname, './src/components')
  }
}

我通常不使用别名,而是使用模块来“挂载”我的整个src文件夹:

resolve: {
  modules: [
    path.resolve(__dirname, "./src"),
    'node_modules',
  ],

其中,假设我有一个像这样的目录结构:

src/
src/components/...
src/util/...
src/routes/...
src/views/...

让我写这些类型的导入:

import C from 'components/C';
import foo from 'util/foo';
import routes from 'routes/blah';
...
于 2017-07-24T22:01:36.963 回答
0

怎么样?

import Component from 'components'
于 2017-07-24T21:07:00.677 回答