Webpack 2解析别名

我在解决webpack 2中的别名时遇到了一些问题。无论我做什么,我都无法解决这个问题。 这里是相关的代码:

/* webpack.mix.js */

       mix.webpackConfig({
         module: {
               rules: [
                   {
                       test: /.js$/,
                       loader: 'eslint-loader'
                   }
               ]
           },
           resolve: {
             root: path.resolve(__dirname), 
                    // path is reqired at the beggining of file 
             alias: {
               config: 'src/assets/js/config', // this is a config folder
               js: 'src/assets/js'
             }
           }
       });

/* router.js */ 

        import { DashboardRoute } from 'config/route-components'
      // this import is unresolved

resolve.root选项不再的WebPack 2.相反,它被合并到存在resolve.modules (从官方迁移指南)。 Webpack甚至会抛出一个错误,认为它不是一个有效的属性。 如果您希望能够从根目录导入,则可以将解析配置更改为:

resolve: {
  alias: {
    config: 'src/assets/js/config',
    js: 'src/assets/js'
  },
  modules: [
    path.resolve(__dirname),
    'node_modules'
  ]
}

或者,您可以在您的resolve.alias使用绝对路径,如下所示:

resolve: {
  alias: {
    config: path.resolve(__dirname, 'src/assets/js/config'),
    js: path.resolve(__dirname, 'src/assets/js')
  }
}

尝试这个:

resolve: {
  root: [
    'node_modules',
    path.resolve('src') // Resolve on root first
  ], 
  alias: {
    config: 'src/assets/js/config', // this is a config folder
    js: 'src/assets/js'
  }
}

在ionic 3(版本3.13.3)中,为了使别名映射工作,您必须在webpack.config.jstsconfig.json定义路径映射

这里请参考完整的答案这里的问题

链接地址: http://www.djcxy.com/p/95337.html

上一篇: Webpack 2 resolve alias

下一篇: ASP.NET Core: Launch URL vs. App URL