【发布时间】:2018-03-27 14:46:34
【问题描述】:
我正在尝试学习 Webpack 配置,但我的控制台中不断出现错误。好像没有找到我的 webpack app.bundle.js。
页面加载并显示我的 html 文件的内容,但不在 app.bundle.js 或我的 dist 目录中的 html 文件中,直到我运行 mpm build。
下面是webpack配置的代码和错误
// import node.js native path module
const path = require('path');
let webpack = require('webpack');
//require HtmlWebPackPlugin
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const port = process.env.PORT || 3000;
//define constant for paths
const paths ={
DIST: path.resolve(__dirname, 'dist'),
SRC: path.resolve(__dirname, 'src'),
JS: path.resolve(__dirname, 'src/js')
};
console.log(paths.DIST);
//webpack configuration
module.exports ={
entry: path.join(paths.JS, 'index.js'),
output: {
path: paths.DIST,
filename: 'app.bundle.js'
},
//set starting point for server
devServer: {
contentBase: paths.SRC,
host:'localhost',
port: port,
historyApiFallback: true,
open: true,
hot:true
},
//set webpack to use plugins
plugins: [
new HtmlWebpackPlugin({
template: path.join(paths.SRC, 'index.html'),
}),
new ExtractTextPlugin('style.bundle.css'),
new webpack.HotModuleReplacementPlugin()
],
//configure loaders
module: {
rules: [
//setup babel loader
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
'babel-loader',
],
},
//setup css loader
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
use: 'css-loader',
}),
},
{
test: /\.(png|jpg|gif)$/,
use: [
'file-loader',
],
},
],
},
//enable JS files without adding their extensions
resolve: {
extensions: ['.js', '.jsx'],
},
};
这是浏览器控制台上的错误
Loading failed for the <script> with source “http://localhost:3000/js/app.bundle.js”
Source map error: request failed with status 404 Resource URL: http://localhost:3000/app.bundle.js Source Map URL: sockjs.js.map
【问题讨论】:
-
你正在将你的 JS 输出到
dist目录 -
尝试在输出中添加
publicPath:output: { path: paths.DIST, filename: 'app.bundle.js', publicPath: '/' } -
@Max Baldwin,我不明白你指的是什么。
-
您的文件路径错误。你失败的路径是
http://localhost:3000/js/app.bundle.js。您没有输出到js目录。您正在输出到dist目录。
标签: javascript reactjs npm webpack