作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想扩展 ionic 的默认 webpack 配置。具体来说,我想为解析模块添加一个别名,以便我可以通过它们的绝对路径而不是相对路径导入模块:
而不是像这样导入模块:
import { SomeComponent } from '../../components/some.component.ts';
import { SomeComponent } from '@app/components/some.component.ts';
@app
是根源目录的别名。
module.exports = {
...
resolve: {
extensions: ['.ts', '.js'],
alias: {
'@app': path.resolve('./'),
}
},
...
};
最佳答案
Module path mapping in @Ionic version - 3.14.0
webpack.config.js
&
tsconfig.json
- package.json
package.json
加载您的自定义
webpack.config.js
.
"config": {
"ionic_webpack": "./config/webpack.config.js"
}
- config/webpack.config.js
const path = require('path');
const { dev, prod } = require('@ionic/app-scripts/config/webpack.config');
const webpackMerge = require('webpack-merge');
const customConfig = {
resolve: {
alias: {
'@app': path.resolve('src/app'),
'@pages': path.resolve('src/app/pages'),
'@constants': path.resolve('src/app/constants'),
'@components': path.resolve('src/app/components'),
"@shared": path.resolve('src/app/shared')
}
}
};
module.exports = {
dev: webpackMerge(dev, customConfig),
prod: webpackMerge(prod, customConfig)
};
- config/test/webpack.config.js
const path = require('path');
const webpackDefault = require('@ionic/app-scripts/config/webpack.config');
const webpackMerge = require('webpack-merge');
const customConfig = {
resolve: {
alias: {
'@app': path.resolve('src/app'),
'@pages': path.resolve('src/app/pages'),
'@constants': path.resolve('src/app/constants'),
'@components': path.resolve('src/app/components'),
"@shared": path.resolve('src/app/shared')
}
}
};
module.exports = webpackMerge(webpackDefault, customConfig);
- tsconfig.json
baseUrl
& paths
在 tsconfig.json
如下所示: {
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@app/*": ["app/*"],
"@pages/*": ["app/pages/*"],
"@constants/*": ["app/constants/*"],
"@components/*": ["app/components/*"],
"@shared/*": ["app/shared/*"]
}
},
}
Ref: My ionic env info:
@ionic/cli-plugin-proxy : 1.4.13
@ionic/cli-utils : 1.14.0
ionic (Ionic CLI) : 3.14.0
cordova (Cordova CLI) : 7.1.0
@ionic/app-scripts : 2.1.4
Cordova Platforms : android 6.2.3 ios 4.5.1
Ionic Framework : ionic-angular 3.6.0
关于ionic-framework - 如何在 Ionic v2 中扩展默认 webpack 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41512923/
我是一名优秀的程序员,十分优秀!