简体   繁体   English

捆绑静态网站的javascript和CSS? (的WebPack?)

[英]Bundle javascript & css for static website? (webpack?)

I am trying to bundle all of my javascript files together, which I am using in a static HTML website. 我正在尝试将所有的javascript文件捆绑在一起,以在静态HTML网站中使用。 As of now, they are all unconnected, included in different HTML files via the script tag. 截至目前,它们都未连接,通过script标记包含在不同的HTML文件中。

I have now created a main.js file, which just requires all the other files, so that I can use this as my webpack entry point. 现在,我已经创建了一个main.js文件,该文件仅需要所有其他文件,因此可以将其用作我的webpack入口点。

This seems to work with the files I wrote myself, but the bootstrap, tether & jquery files are throwing errors. 这似乎可以与我自己编写的文件一起使用,但是引导程序,系链和jquery文件抛出错误。

Note: I have not installed jquery or bootstrap via npm, I have just downloaded the javascript files, put them in the same folder and included them in the main.js file via require . 注意:我尚未通过npm安装jquery或bootstrap,我刚刚下载了javascript文件,将它们放在同一文件夹中,并通过require将它们包含在main.js文件中。 Is there anything wrong with the way I am going about this? 我的处理方式有什么问题吗?

// webpack.config.js
var path = require('path')

module.exports = {
  entry: ['./js/main'], // file extension after index is optional for .js files
  output: {
    path: path.join(__dirname, 'js'),
    filename: 'bundle.js'
  }
}

main.js: main.js:

require('./jquery-3.1.1.slim.min.js');
require('./tether.min.js');
require('./bootstrap.min.js');
require('./navigation.js');
require('./geschichte.js');
require('./iziToast.min.js');
require('./kontakt.js');

edit: 编辑:

My error is: 我的错误是:

Uncaught Error: Bootstrap's JavaScript requires jQuery. jQuery must be included before Bootstrap's JavaScript.
    at Object.<anonymous> (bundle.js:119)
    at __webpack_require__ (bundle.js:20)
    at Object.<anonymous> (bundle.js:79)
    at __webpack_require__ (bundle.js:20)
    at Object.<anonymous> (bundle.js:70)
    at __webpack_require__ (bundle.js:20)
    at bundle.js:63
    at bundle.js:66
  • although it is included, or should be. 尽管已包含或应该包含。

In your webpack.config.js: 在您的webpack.config.js中:

// webpack.config.js
var path = require('path');
var webpack = require('webpack');

module.exports = {
    entry: ['./js/main'], // file extension after index is optional for .js files
    output: {
        path: path.join(__dirname, 'js'),
        filename: 'bundle.js'
    },
    plugins: [
        new webpack.ProvidePlugin({
           $: "jquery",
           jQuery: "jquery"
       })
    ],
    resolve: {
       alias: {
            jquery: "../jquery-3.1.1.slim.min.js"
        }
    }
}

In your main.js: require('jquery'); 在您的main.js中: require('jquery');

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

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