我正在尝试为ReactJs设置webpack。我无法解释我的Webpack配置文件出了什么问题
找不到Entry模块中的错误:错误:无法解析'D:\ wd \ javascript \ Projects \ reactjs-basics中的'./src'
代码在这里 - 两个文件“Webpack.config.js”和“Package.json”
Webpack.config.js代码是:
var webpack = require('webpack');
var path = require('path');
var DIST_DIR = path.resolve(__dirname,'dist');
var SRC_DIR = path.resolve(__dirname,'src');
var config = {
entry: SRC_DIR+'/app/index.js',
output:{
path:DIST_DIR+'/app',
filename:'bundle.js',
publicPath:'/app/'
},
module:{
rules: [
{
test: /\.js?/,
include: SRC_DIR,
use:{
loader:'babel-loader',
query:{
presets:['react','es2015','stage-2']
}
}
}
]
},
mode: 'development',
watch: true
}
module.export = config;
Package.json文件是
{
"name": "reactjs-basics",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": " npm run build",
"build": "webpack -d && copy src\\app/index.html dist\\index.html && webpack-dev-server --content-base src\\ --inline --hot",
"build:prod": "webpack -p && cp src\\app/index.html dist\\index.html"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"devDependencies": {
"2015": "0.0.1",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.5",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1"
}
}
**
作为参考,文件夹结构与Webpack配置代码我附上一个图像
**
Please Click here for folder structure, code and folder structure is juxtaposed
你要修改一些东西
webpack.config.js
你有module.export
。这是不正确的。它必须是module.exports
[email protected]
和[email protected]
。 babel-loader@8.*
与babel-core@6.*
不兼容。你要使用babel-loader@7
。使用babel-loader
卸载现有的npm uninstall -D babel-loader
并使用babel-loader@7
安装npm install -D babel-loader@7
我注意到的另一件事是,你在mode: 'development'
中指定了webpack.config.js
。最好通过运行时参数设置mode to development or production
更新
从你的mode
删除watch
和webpack.config.js
mode: 'development',
watch: true
用以下详细信息更新您的package.json
。
开发模式你不需要设置watch
和mode
,因为当你运行webpack-dev-server
时npm run dev
会为你做这件事
"scripts": {
"webpack": "webpack",
"dev": "mkdir -p dist && cp ./src/index.html dist/index.html && webpack-dev-server",
"prod": "mkdir -p dist && cp ./src/index.html dist/index.html && npm run webpack -- --mode production"
}
要启动local server
,您需要在webpack.config.js
中添加以下配置。请注意directory name
指向的devserver
。
devServer: {
contentBase: path.join(__dirname, "dist/"),
port: 9000
},
生产模式执行npm run prod
以在生产模式下构建项目
运行npm run dev
时,您可以看到localhost处于工作状态。该服务器由webpack-dev-server
库启动。对于production build
,您必须配置自己的服务器
如果这有帮助,请告诉我