Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack is loading module twice

I am using webpack to build my angular.js application. Given a simple example:

app.js

var angular = require('exports?window.angular!angular'),
    angularMoment = require('angular-moment');

angular.module('myApp', [angularMoment]);

angular-moment.js

define(['angular', 'moment'], function(angular, moment) {
  // some library code
});

Here two ways of import of angular, with loader and just by name. It causes that angular will be injected to page twice and I see following warning in console:

WARNING: Tried to load angular more than once.

Is there a way to make webpack consider both imports of angular as the same module?

My webpack.conf.js

module.exports = {
  entry: {
    'app': './src/app/app.js'
  },
  plugins: [
    new webpack.ResolverPlugin(
      new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
    ),
    new AngularPlugin()
  ]
};
like image 934
just-boris Avatar asked Oct 30 '22 23:10

just-boris


1 Answers

One option is to have angular be a stand-alone script and have it configured as an external dependency:

module.exports = {
  external: {
      'angular' : 'angular'
  },
  entry: {
    'app': './src/app/app.js'
  },
  plugins: [
    new webpack.ResolverPlugin(
      new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
    ),
    new AngularPlugin()
  ]
};
like image 173
Esteban Felix Avatar answered Nov 10 '22 14:11

Esteban Felix