Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack externals module name with hyphen

I'm trying to load angular-ui-router in Webpack as an external dependency. The module name is "angular-ui-router". Here's an example:

module.exports = webpackMerge(commonConfig, {
    ...
    externals: {
        'angular': true,
        'angular-ui-router': true
    },
    ...
});

The problem with this is that Webpack creates a module in my app.bundle.js that looks like the following:

/***/ },
/* 1 */
/***/ function(module, exports) {

    module.exports = angular;

/***/ },
/* 2 */
/***/ function(module, exports) {

    module.exports = angular-ui-router;

/***/ }
/******/ ]);

When the browser tries to load the module, it evaluates module.exports = angular-ui-router as an expression, throwing the following error:

Uncaught ReferenceError: ui is not defined

The only fix I have found for this issue is:

module.exports = webpackMerge(commonConfig, {
    ...
    externals: {
        'angular': true,
        'angular-ui-router': 'window["angular-ui-router"]'
    },
    ...
});

This yields the correct result.

Is there a better way?

like image 976
Joseph238 Avatar asked Oct 17 '16 22:10

Joseph238


Video Answer


1 Answers

I was having the same problem, so I got it solved using something like this

module.exports = webpackMerge(commonConfig, {
    ...
    externals: [
      ....
      'angular-ui-router': {
                   commonjs: 'angular-ui-router' 
          }
        }
        ],
    ...
});
like image 96
Gaurav Pandvia Avatar answered Sep 28 '22 06:09

Gaurav Pandvia