简体   繁体   English

离子3的`src`的路径别名

[英]Path alias for `src` for Ionic 3

I have a custom webpack configuration for Ionic 3 to use src as a path alias (ie to import from src/module/file ): 我有一个针对Ionic 3的自定义Webpack配置,以使用src作为路径别名(即,从src/module/file导入):

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

However, with Ionic app scripts @ 3, when I try to serve or build I get an error Cannot find module src/module/file . 但是,使用Ionic应用程序脚本@ 3时,当我尝试提供服务或构建时出现错误Cannot find module src/module/file

I have seen ways to create path aliases to import from directories under src (such as app , pages , etc.) that involve updating the Webpack resolve alias as I have as well as updating baseUrl for tsconfig.json . 我已经看到了创建路径别名以从src下的目录(例如apppages等)导入的方法,这些方法涉及更新Webpack解析别名以及为tsconfig.json更新tsconfig.json For example: 例如:

https://medium.com/@siddhartha.ng/ionic-3-import-using-aliases-2aa260d6fab3 https://medium.com/@siddhartha.ng/ionic-3-import-using-aliases-2aa260d6fab3

However, I want to be able to import from src rather than a subdirectory of src . 不过,我希望能够从导入src而不是一个子目录src Is there any way to do this by changing my webpack or TypeScript configuration? 有什么方法可以通过更改我的webpack或TypeScript配置来做到这一点?

Per the updates to Ionic 3, webpack configuration now has two top-level properties, dev and prop for corresponding webpack configurations. 根据对Ionic 3的更新,Webpack配置现在具有两个顶级属性,分别是对应的Webpack配置的devprop

This means that you have to update the dev and prod properties and export the entire object from the webpack configuration. 这意味着您必须更新devprod属性,并从webpack配置中导出整个对象。 You can do this any way you like, but for example: 您可以按照自己喜欢的任何方式执行此操作,但是例如:

const config = require('@ionic/app-scripts/config/webpack.config.js');

config[process.env.IONIC_ENV].resolve.alias = { src: path.resolve('./src') };

module.exports = config; // you can also export a function that returns this

Just keep in mind that you need to export the whole object with both configurations. 请记住,您需要使用两种配置导出整个对象。 module.exports = config[env] won't work. module.exports = config[env]不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM