【问题标题】:Load libraries from an external lib folder without npm in webpack在 webpack 中从没有 npm 的外部 lib 文件夹加载库
【发布时间】:2017-01-01 03:58:36
【问题描述】:

我开始将我的 Angular 应用程序迁移到 webpack。我的文件结构如下:

- app/
------ app.js
------ index.html
- lib/
----- angular.js
----- jquery.js
----- ...
- webpack.config.js

由于限制,我无法使用 npm 安装库。我所有的库文件都位于lib 和其他文件夹中。我的 webpack 配置如下所示:

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

module.exports = {
    context: __dirname,
    entry: {
        app: [ './app/app.js'],
        vendors: ['angular']
    },
    output: {
        path: __dirname + '/build',
        filename: 'bundle.js'
    },
    resolve: {
        alias: {
            angular: __dirname + "/lib/angular"
        }
    },
    debug: false,
    devtool: 'source-map',
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"},
            {
                test: /\.png$/,
                loader: "url-loader?limit=100000"},
            {
                test: /\.jpg$/,
                loader: "file-loader"
            },
            {
                test: /\.json/,
                loader: 'json'
            }
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({
            angular: "angular"
        }),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js', Infinity)
    ]
}

我得到了错误

angular.js?848f:80Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

app.js 如下所示

angular.module("myApp", [])
.controller("myCtrl", function(){ ... });

感谢您的帮助!

【问题讨论】:

    标签: angularjs webpack lazy-loading


    【解决方案1】:

    首先,在您的条目中修正拼写错误 vendor 而不是 vendors。它应该与 CommonsChunkPlugin

    中的名称匹配
    entry: {
        app: [ './app/app.js'],
        vendor: ['angular']
    },
    

    其次,移除ProvidePlugin

    plugins: [
            new webpack.optimize.OccurenceOrderPlugin(),
            new webpack.optimize.DedupePlugin(),
            new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js', Infinity)
        ]
    

    现在应该可以了。

    但实际上,我不知道使用 webpack 加载外部库是否是正确的方法。 (Webpack 对我来说是超级黑盒,gulp 更容易预测)。所以现在它可以工作了,但没有适当的 DI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2016-06-25
      • 2015-08-21
      • 1970-01-01
      相关资源
      最近更新 更多