[英]Webpack add JS library
package.json: 的package.json:
{
"name": "**",
"version": "1.0.0",
"description": "**",
"dependencies": {
"lodash": "4.17.4",
"jquery": "3.2.1",
"jplayer": "2.9.2",
"jquery-ui": "1.12.1",
"owl.carousel": "2.2.0",
"wowjs": "1.1.3"
},
"devDependencies": {
"webpack": "2.3.3",
"webpack-dev-server": "2.4.2",
"html-webpack-plugin": "2.28.0",
"extract-text-webpack-plugin": "2.1.0",
"clean-webpack-plugin": "0.1.16",
"babel-core": "6.24.1",
"babel-loader": "6.4.1",
"babel-preset-es2015": "6.24.0",
"babel-plugin-transform-runtime": "6.23.0",
"uglify-js": "2.8.21",
"html-loader": "0.4.5",
"style-loader": "0.16.1",
"css-loader": "0.28.0",
"url-loader": "0.5.8",
"stylefmt": "5.3.2",
"file-loader": "0.11.1",
"purifycss-webpack": "0.6.0"
},
"scripts": {
"build:dist": "webpack --env=prod --config=webpack.config.js",
"build:dev": "webpack-dev-server --env=dev --config=webpack.config.js"
}
}
webpack.config.js: webpack.config.js:
const path = require('path');
const glob = require('glob');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
const CleanWebpackPlugin = require('clean-webpack-plugin');
module.exports = function (env) {
return {
devServer: {
inline: true
},
devtool: 'source-map',
context: __dirname,
entry: {
landing: [
'./node_modules/wowjs',
'./js/landing.js'
]
},
output: {
path: path.resolve(__dirname, './app/'),
filename: 'js/[name].js',
chunkFilename: '[id].js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['es2015'],
plugins: ['transform-runtime']
}
}
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader']
})
},
{
test: /\.html$/,
use: 'html-loader'
},
{
test: /\.(eot|woff|ttf|svg|png|jpg)$/,
use: 'url-loader?limit=50000&name=assets/[name]-[hash].[ext]'
}
]
},
plugins: [
new CleanWebpackPlugin(['app']),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
_: 'lodash'
}),
new ExtractTextPlugin({
filename: (getPath) => {
return getPath('css/[name].css');
},
allChunks: true
}),
new HtmlWebpackPlugin({
filename: 'index.html',
chunks: ['landing', 'bundle'],
favicon: './img/favicon.png',
template: './pages/index.html',
inject: true
}),
new CommonsChunkPlugin('bundle')
]
};
};
landing.js: landing.js:
$(() => {
const wow = new WOW({
boxClass: 'wow',
animateClass: 'animated',
offset: 100,
mobile: false
});
wow.init();
});
I used the command: webpack --env=prod --config=webpack.config.js 我使用了以下命令:webpack --env = prod --config = webpack.config.js
After running the command I open the page /app/index.html in the browser 运行命令后,我在浏览器中打开页面/app/index.html
But the error on the page: Uncaught ReferenceError: WOW is not defined 但是页面上的错误:未捕获ReferenceError:未定义WOW
The problem here is that when your'e bundles are being made. 这里的问题是制作捆绑包时。 Somewhere in there, your JS references
WOW
object. 在那里,您的JS引用了
WOW
对象。 As at the moment there is no DOM present, WOW
is not attached to the DOM and hence you see the error. 由于目前没有DOM,
WOW
未附加到DOM,因此您会看到错误。
The solution is to use the ProvidePlugin which make the reference a valid one whenever it is made by any chunk. 解决方案是使用ProvidePlugin ,只要有任何块创建引用,该引用就使引用成为有效引用。 As described in the documentation :
如文档中所述:
Automatically loads modules.
自动加载模块。 Whenever the
identifier
is encountered as free variable in a module, themodule
is loaded automatically and theidentifier
is filled with the exports of the loadedmodule
.每当
identifier
在模块中作为自由变量遇到时,该module
就会自动加载,并且identifier
将使用已加载module
的导出进行填充。
In your case you can add the following snippet and it'll work 根据您的情况,您可以添加以下代码段,它将起作用
plugins: [
....
new webpack.ProvidePlugin({
WOW: 'wowjs',
})
]
*EDIT: * The value you pass to the ProvidePlugin
is the one who's module is loaded *编辑:*您传递给
ProvidePlugin
的值是加载模块的人
So for 因此对于
import 'jquery';
you'd use 你会用
new webpack.ProvidePlugin({
$: 'jquery',
})
Under the hood, plugin searches for jquery
module imported somewhere and makes the reference available. 在后台,插件搜索导入到某处的
jquery
模块并使参考可用。
To answer your comment. 回答您的评论。 If you've use
import 'wowjs'
you'll have to pass wowjs
to the plugin as the value for WOW
如果您已经使用
import 'wowjs'
你必须通过wowjs
到插件作为价值WOW
new webpack.ProvidePlugin({
WOW: 'wowjs',
})
Sorry for missing out on this part, I had assumed that you would have imported it as wow
instead of wowjs
as that's the correct way to do it. 对不起,我错过了这一部分,我假设您将它导入为
wow
而不是wowjs
因为这是正确的方法。
import * as wow from "wowjs"
In any case, you can use the previous snippet and you should be good to go. 无论如何,您都可以使用前面的代码段,并且应该会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.