I'd like to extend the default webpack config of ionic. Specifically, I'd like to add an alias for resolving modules so that I can import modules by their absolute path, rather than a relative one:
Instead of importing modules like this:
import { SomeComponent } from '../../components/some.component.ts';
I want to
import { SomeComponent } from '@app/components/some.component.ts';
where @app
is an alias for the root source directory.
In other projects I was able to do this by adding something like this to the webpack config:
module.exports = { ... resolve: { extensions: ['.ts', '.js'], alias: { '@app': path.resolve('./'), } }, ... };
I'm not sure how to do this with Ionic without overriding the default webpack config.
Use webpack-cli's init command to rapidly generate webpack configuration files for your project requirements, it will ask you a couple of questions before creating a configuration file. npx might prompt you to install @webpack-cli/init if it is not yet installed in the project or globally.
The npm module webpack-merge is a confortable way of having multiple configuration files in webpack. It allows to have a common configuration file and several other that "extend" from this one as the following example will show. This would be where the common configurations between your other files are.
To answer your specific question, the webpack configuration is stored wherever your global node_modules are installed; on Windows this is typically %AppData%\Roaming\npm\node_modules\powerbi-visuals-tools\lib\webpack.
You may have noticed that few webpack configurations look exactly alike. This is because webpack's configuration file is a JavaScript file that exports a webpack configuration.
The accepted answer works fine but you will have to update webpack.config.js
each time you update app-script
. So instead of copying code, require
it in webpack.config.js
package.json
"config": { "ionic_webpack": "./config/webpack.config.js" }
webpack.config.js
const webpackConfig = require('../node_modules/@ionic/app-scripts/config/webpack.config'); webpackConfig.resolve = { extensions: ['.ts', '.js'], alias: { '@app': path.resolve('./'), } }
In most cases you can follow this approach and you wont have to update config
each time you update app-script
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With