webpack捆绑了文件,但是index.js没有运行

时间:2019-03-28 21:16:28

标签: javascript jquery webpack bundle

我有一个index.js文件,该文件导入了我的CSS和一些软件包,但是在捆绑所有内容并启动服务器后,我注意到index.js没有运行。我在index.js中做了一个简单的console.log,但是没有达到。

我从正常工作的上一个项目复制了webpack.config文件的内容,所以我不确定这是文件结构/路径错误还是什么。有什么想法吗?

目录结构:

enter image description here

webpack.config.js:

const path = require('path')
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin')
var $ = require("jquery");

var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: './src/index.html',
    filename: 'RecruitmentTracking.txt',
    inject: 'body'
});

module.exports = {
  entry: "./src/index.js", // removing the . fails the build
  output: {
    filename: './SiteAssets/scripts/RecruitmentTracking.js',
    path: path.resolve(__dirname, 'dist')
  },

  module: {
    rules: [{
      loader: 'babel-loader',
      test: /\.js$/,
      exclude: /node_modules/
    }, {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
    }, {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          'file-loader'
        ]
      }
    ],
  },
    devServer: {
        disableHostCheck: true
    },
    devtool: 'cheap-module-eval-source-map', // this helps to browser to point to the exact file in the console, helps in debug
    devServer: {
        contentBase: path.join(__dirname, 'src'),
        historyApiFallback: true // this prevents the default browser full page refresh on form submission and link change
    },
    plugins: [
        HtmlWebpackPluginConfig,
        new webpack.ProvidePlugin({
            "$": "jquery",
            "jQuery": "jquery",
            "window.jQuery": "jquery"
    })]
}

index.js:

import "./RecruitmentTracking.css";
import 'jquery';
import 'bootstrap/dist/js/bootstrap.bundle.min.js';
import 'jquery-ui-bundle/jquery-ui.min.js';

console.log('this is index.js');

package.json:

{
  "name": "recruitmenttracking",
  "version": "1.0.0",
  "description": "Recruitment Initiatives Tracking",
  "main": "index.js", // ----- should a more specific file path be here?
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --open --mode development",
    "build": "webpack --config webpack.config.js",
    "dev-server": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.4.0",
    "@babel/preset-env": "^7.4.2",
    "babel-loader": "^8.0.5",
    "css-loader": "^2.1.1",
    "file-loader": "^3.0.1",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0",
    "webpack-dev-server": "^3.2.1"
  },
  "dependencies": {
    "@babel/polyfill": "^7.4.0",
    "axios": "^0.18.0",
    "bootstrap": "^4.3.1",
    "jquery": "^3.3.1",
    "jquery-ui-bundle": "^1.12.1-migrate",
    "pdfmake": "^0.1.54",
    "popper": "^1.0.1"
  }
}

3 个答案:

答案 0 :(得分:3)

这里发生的是:

1-Webpack编译并输出为:'./SiteAssets/scripts/RecruitmentTracking.js'

2-HtmlWebpackPlugin,然后将读取模板文件'./src/index.html',并将RecruitmentTracking.js脚本注入到体内。

3-然后,将结果输出到dist/RecruitmentTracking.txt

除了文件是.txt而不是.html之外,我没有发现任何问题。并且显然不会被浏览器解释。

尝试输出到html文件,它应该可以工作

答案 1 :(得分:1)

1)出于某种原因,您已经配置了以下插件来输出.txt文件。因此,不要指望浏览器将其解释为html文件

var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: './src/index.html',
    filename: 'RecruitmentTracking.txt',
    inject: 'body'
});

2)另外,我相信您在浏览器中打开的文件为/dist/index.html,并且该文件不会加载js文件。尝试将以下行添加到/dist/index.html中:

<script src"./SiteAssets/scripts/RecruitmentTracking.js"></script>

3)如果上述方法可行,请仍然考虑仔细研究(1)

答案 2 :(得分:1)

您已在 HtmlWebpackPluginConfig 中将输出文件命名为RecruitmentTracking.txt。将其更改为 index.html ,它应该可以工作

var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: './src/index.html', // webpack takes ./src/index.html as input file
  filename: 'index.html', // webpack processes the above input template and should output to index.html
  inject: 'body'
});
相关问题