Webpack 2+ how to feed Cordova
problem
So I am building an Angular project in Cordova and I am wondering if anyone knows how to use Cordova in a webpack.
Usually you link to cordova.js
directly in your index.html, but I prefer to avoid that and include it in the bundle to shake the tree and all. Not sure if this is even plausible, but I decided to ask since google fu let me down.
Code
I refer to a Cordova plugin called Splashscreen as follows:
import * as splashscreen from 'cordova-plugin-splashscreen/www/splashscreen';
However, this plugin has a requirement for cordova / exec:
var exec = require('cordova/exec');
This is where the webpack fails:
ERROR in ./~/cordova-plugin-splashscreen/www/splashscreen.js
Module not found: Error: Can't resolve 'cordova/exec' in './node_modules/cordova-plugin-splashscreen/www'
So I tried webpack.config.js
to get webpack to understand where cordova is:
let cordova;
let cordovaExec;
if (context.opts.platforms[0] === 'ios') {
cordova = './platforms/ios/platform_www/cordova';
cordovaExec = 'cordova-ios/cordova-js-src/exec';
}
else {
cordova = './platforms/android/platform_www/cordova';
cordovaExec = 'cordova-android/cordova-js-src/exec';
}
And then Webpack ProvidePlugin
:
new ProvidePlugin({
'cordova': cordova,
'cordova/exec': cordovaExec
})
But the error remains ...
I'm not an expert when it comes to Webpack, so does anyone know a solution if there is one?
source to share