在 webpack 中从没有 npm 的外部 lib 文件夹加载库
Load libraries from an external lib folder without npm in webpack
我开始将我的 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(){ ... });
感谢您的帮助!
首先,更正输入中的拼写错误 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。
我开始将我的 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(){ ... });
感谢您的帮助!
首先,更正输入中的拼写错误 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。