sorrycc / roadhog

🐷 Cli tool for creating react apps, configurable version of create-react-app.
2.72k stars 342 forks source link

.webpackrc 中的alias配置未生效 #623

Open sandercage opened 6 years ago

sandercage commented 6 years ago

alias 字段配置如下:

{
    "components": "./src/components",
    "utils": "./src/utils",
    "services": "./src/services",
    "routes": "./src/routes",
    "models": "./src/models",
}

但是import时无法识别这个路径:import xxx from "routes/xxx.js"

Module not found: Error: Can't resolve 'routes/xxx.js' in '/path/to/project/src'
dva-cli@0.9.2
roadhog@2.2.0
Liuqing650 commented 6 years ago

同求一个实例

qinhuaihe commented 6 years ago

是不是要改成 .webpackrc.js 才可以呢, 我也很想知道

qinhuaihe commented 6 years ago

确实是要改成 .webpackrc.js alias 就可以生效了

wavelynn commented 6 years ago

.webpackrc.js 这个也不行呢?

dva-cli version 0.9.2
    dva version 2.1.0
roadhog version 2.2.0
wavelynn commented 6 years ago

刚试了,直接改为 .webpackrc.js 是不行的,需要修改为path.resolve 才行,应该是路径不对,具体如下:

const path = require('path');

export default {
  "alias": {
    "components": path.resolve(__dirname, "./src/components"),
    "models": path.resolve(__dirname, "./src/models"),
    "routes": path.resolve(__dirname, "./src/routes")
  }
}
chillyistkult commented 6 years ago

If you run roadhog test the alias does not work.

sorrycc commented 6 years ago

Set alias in .babelrc with babel-plugin-module-resolver should be work for roadhog test.

chillyistkult commented 6 years ago

I did that already and specified

{
  "plugins": [
    ["babel-plugin-module-resolver", {
      "alias": {
        "components": "./src/components",
      }
    }]
  ]
}

but it does not work. I also don't see that .babelrc is anywhere mentioned in the code of roadhog. How does it work?

I work with unmodified @ant-design-pro which contains this bug https://github.com/ant-design/ant-design-pro/issues/1133

sorrycc commented 6 years ago

Haven't tried antd-pro yet. The example here is good.

chillyistkult commented 6 years ago

Well thats exactly the example I used and it does not work. It could be an issue specifically with antd-pro (which I doubt), but to be honest I could not find any example on github where roadhog test is used together with an alias.

sorrycc commented 6 years ago

@chillyistkult https://github.com/umijs/umi/tree/master/packages/umi-test/examples/normal

fireairforce commented 5 years ago

Haven't tried antd-pro yet. The example here is good.

I just solved my question on that way,thx