Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend default webpack config in Ionic v2

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.

like image 828
qzr Avatar asked Jan 06 '17 19:01

qzr


People also ask

How can we generate webpack config file automatically?

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.

Can you have multiple webpack config files?

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.

Where is the webpack config file?

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.

What is the format of webpack config file?

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.


1 Answers

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

like image 181
raj Avatar answered Sep 20 '22 04:09

raj