javascript - webpack 打包文件但 index.js 没有运行

标签 javascript jquery webpack bundle

我有一个导入我的 CSS 和一些包的 index.js 文件,但在 bundle 所有内容并启动服务器后,我注意到 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 the browser 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');

包.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"
  }
}

最佳答案

这里发生的事情是:

1 - webpack 编译并输出到:'./SiteAssets/scripts/RecruitmentTracking.js'

2 - HtmlWebpackPlugin,然后会读取模板文件'./src/index.html',并在正文中注入(inject)RecruitmentTracking.js脚本。

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

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

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

关于javascript - webpack 打包文件但 index.js 没有运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55406965/

相关文章:

javascript - 错误 : Rule can only have one resource source (provided resource and test + include + exclude)

javascript - 网格 flex 端没有按我预期的方式运行

javascript - 从外部 json 添加标记到谷歌地图?

javascript - 提交表单后如何留在同一页面

javascript - 仅当值更改时才启用“提交”按钮

typescript - "Module not found: Error: Can' 解决 Electron 和 typescript 和 webpack 项目中的 'electron-is-dev'"

javascript - "(AppContainer)RangeError: Maximum call stack size exceeded"- 与 react 热加载器 react

javascript - 密码保护目录列表器

Javascript - string.split(regex) 保留分隔符

javascript - 如何将函数名称与 var javascript 连接起来?