Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack.ProvidePlugin angular

I am trying to use angular NG6 starter. in its source code, import angular from angular is written almost every js file. So I try this:

 new webpack.ProvidePlugin({
      // $: "jquery",
      // jQuery: "jquery",
      // "window.jQuery": "jquery",
      'angular': 'angular',
    }),

But it can not work. I dont know why, and how to solve this issue.

like image 735
Leon Avatar asked Aug 17 '16 06:08

Leon


People also ask

What does webpack do in angular?

Webpack is a popular module bundler, a tool for bundling application source code in convenient chunks and for loading that code from a server into a browser. It's an excellent alternative to the SystemJS approach used elsewhere in the documentation.

Does angular still use webpack?

The Angular build process uses webpack behind the scenes to transpile TypeScript to JavaScript, transform Sass files to CSS, and many other tasks.

How do I use webpack ProvidePlugin?

ProvidePlugin({ identifier: path. resolve(path. join(__dirname, 'src/module1')), // ... }); Whenever the identifier is encountered as free variable in a module, the module is loaded automatically and the identifier is filled with the exports of the loaded module (or property in order to support named exports).


1 Answers

Here the solution to the first error message in your screenshot "angular.module is not a function": Angular 1 is not working nicely with webpack without a shim (see https://github.com/webpack/webpack/issues/2049). Try this webpack loader config:

module: {
    loaders: [
        /*
         * Necessary to be able to use angular 1 with webpack as explained in https://github.com/webpack/webpack/issues/2049
         */
        {
            test: require.resolve('angular'),
            loader: 'exports?window.angular'
        },
    ]
},
plugins: [
    new webpack.ProvidePlugin({
        'angular': 'angular',
    }),
],

This should initialize the angular object properly instead of the default action of setting it to an empty object (which does not have a property named module).

like image 61
Motin Avatar answered Oct 19 '22 23:10

Motin