简体   繁体   English

Webpack ProvidePlugin / vendor bundle:angular.module不是一个函数

[英]Webpack ProvidePlugin/vendor bundle: angular.module is not a function

I have an Angular application that is currently built via Webpack into one large bundle, containing all dependencies and app code in a single file. 我有一个Angular应用程序,目前通过Webpack构建到一个大型包中,包含单个文件中的所有依赖项和应用程序代码。 I am trying to split the code into two bundles, one bundle with all of my dependencies, and another with all of my application code. 我试图将代码分成两个包,一个包含所有依赖项,另一个包含所有应用程序代码。

I have the following webpack.config.js: 我有以下webpack.config.js:

var webpack = require('webpack');
var path = require('path');

var definePlugin = new webpack.DefinePlugin({
    'process.env': {
        NODE_ENV: `"${process.env.NODE_ENV}"`
    }
});

var SRC = path.resolve(__dirname, 'src/main/client');
var APP = path.resolve(SRC, 'app/index.js');
var DEST = path.resolve(__dirname, 'target/webapp/dist');

module.exports = {
    entry: {
        vendor: [
            'angular',
            'angular-animate',
            'moment',
            'angular-moment',
            'angular-translate',
            'angular-ui-bootstrap',
            'angular-ui-router',
            'lodash'
        ],
        app: APP
    },
    plugins: [
        new webpack.ProvidePlugin({
            _: 'lodash',
            angular: 'angular',
            angularMoment: 'angular-moment',
            angularTranslate: 'angular-translate',
            moment: 'moment',
            ngAnimate: 'angular-animate',
            uibootstrap: 'angular-ui-bootstrap',
            uirouter: 'angular-ui-router'
        }),
        new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js'),
        definePlugin
    ],
    output: {
        path: DEST,
        filename: 'bundle.js',
        publicPath: '/',
        hash: true
    },
    module: {
        preLoaders: [],
        loaders: [
            { test: /\.html$/, loaders: ['html'] },
            { test: /\.css$/, loaders: ['style', 'css'] },
            { test: /\.scss$/, loaders: ['style', 'css', 'sass'] },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                loaders: ['ng-annotate', 'babel?presets[]=es2015', 'eslint']
            },
            {
                test: /\.(ttf|eot|svg|woff2?)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'file'
            }
        ]
    },
    sassLoader: {
        includePaths: [path.resolve(__dirname, './node_modules')]
    }
};

This produces two bundles, one with only the dependencies and one with only the application code. 这会生成两个bundle,一个只包含依赖项,另一个只包含应用程序代码。 However, when I attempt to load the application, I get: Uncaught TypeError: angular.module is not a function which is traced back to angular-moment.js within the vendor.bundle.js . 但是,当我尝试加载应用程序时,我得到: Uncaught TypeError: angular.module is not a function追溯到angular-moment.js中的angular-moment.jsvendor.bundle.js In other words, angular isn't accessible to other modules that need to be loaded in the vendor.bundle.js file. 换句话说, angular并不需要在被装载的其它模块可访问的vendor.bundle.js文件。 However, I'm not sure how to make these dependencies visible to each other. 但是,我不确定如何使这些依赖关系彼此可见。

Following should move all the scripts not in src folder to vendor chunk. 以下应将不在src文件夹中的所有脚本移动到供应商块。

const path = require('path');
const projectRoot = path.resolve('./');

      new webpack.optimize.CommonsChunkPlugin({
        name: 'vendor',
        minChunks: (module) => module.resource && module.resource.indexOf(path.join(projectRoot, 'src')) === -1,
      }),

To answer the question in the headline of the original post, Angular 1 is not working nicely with webpack without a shim (see https://github.com/webpack/webpack/issues/2049 ). 为了回答原始帖子标题中的问题,Angular 1在没有垫片的情况下与webpack不能很好地协作(参见https://github.com/webpack/webpack/issues/2049 )。 Try this webpack loader config: 试试这个webpack loader配置:

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). 这应该正确初始化角度对象,而不是将其设置为空对象(没有名为module的属性)的默认操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM