简体   繁体   English

使用Webpack和UglifyJsPlugin的压缩代码不起作用

[英]Minified code using Webpack and UglifyJsPlugin doesn't work

I'm trying do add multiple files into bundle and minify it. 我正在尝试将多个文件添加到包中并将其最小化。 Webpack doesn't give me any error, code is compiled and minified but it doesn't work. Webpack不会给我任何错误,代码会被编译和缩小,但无法正常工作。

在此处输入图片说明

My webpack config: 我的webpack配置:

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

module.exports = {
  entry: {
    app: [
    './js/jquery-2.1.1.min.js',
    './vendor/bootstrap4alpha/js/tether.min.js',
    './vendor/bootstrap4alpha/js/bootstrap.min.js',
    './js/ie10-viewport-bug-workaround.js',
    './vendor/chartjs/Chart.bundle.min.js',
    './vendor/chartjs/utils.js',
    './vendor/spincrement/jquery.spincrement.min.js',
    './js/adminux.js'
  ]
  },
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'bundle.min.js'
  },
  plugins: [
    new webpack.optimize.UglifyJsPlugin({
      minimize: true,
      mangle: true
    })
  ]
};

When I run my app in the browser, it doesn't work and give me an error in console: 当我在浏览器中运行我的应用程序时,它不起作用,并在控制台中给我一个错误:

Uncaught Error: Bootstrap's JavaScript requires jQuery. 未捕获的错误:Bootstrap的JavaScript需要jQuery。 jQuery must be included before Bootstrap's JavaScript. 在Bootstrap的JavaScript之前必须包含jQuery。

So it looks like jquery is not in bundle but it is. 因此,看起来jquery不在捆绑包中,而是捆绑在一起。 When I add jquery in html file, it outputs next error with tether.min.js 当我在html文件中添加jquery时,它会使用tether.min.js输出下一个错误

bootstrap needs to use the $ , it's not a global variable in webpack. bootstrap需要使用$ ,它不是webpack中的全局变量。

you can use bootstrap-webpack instead. 您可以改用bootstrap-webpack

or use webpack ProvidePlugin to define a global $: 或使用webpack ProvidePlugin定义全局$:

new webpack.ProvidePlugin({
    // Automtically detect jQuery and $ as free var in modules
    // and inject the jquery library
    // This is required by many jquery plugins
    jQuery: "jquery",
    $: "jquery"
})

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

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