gpt4 book ai didi

node.js - webpack 与 nodejs 一起使用

转载 作者:搜寻专家 更新时间:2023-10-31 23:00:08 24 4
gpt4 key购买 nike

我是 reactjs 的新手。我刚开始学习 reactjs。我在 nodejs 中使用 webpack 时遇到问题。我想创建将运行 webpack 文件的 Node 服务器。我有 webpack 文件:

const {resolve} = require('path');
const webpack = require('webpack');
const validate = require('webpack-validator');
const {getIfUtils, removeEmpty} = require('webpack-config-utils');

module.exports = env => {
const {ifProd, ifNotProd} = getIfUtils(env)

return validate({
entry: './index.js',
context: __dirname,
output: {
path: resolve(__dirname, './build'),
filename: 'bundle.js',
publicPath: '/build/',
pathinfo: ifNotProd(),
},
devtool: ifProd('source-map', 'eval'),
devServer: {
port: 8080,
historyApiFallback: true
},
module: {
loaders: [
{test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
{test: /\.css$/, loader: 'style-loader!css-loader'},
{test: /(\.eot|\.woff2|\.woff|\.ttf|\.svg)/, loader: 'file-loader'},
],
},
plugins: removeEmpty([
ifProd(new webpack.optimize.DedupePlugin()),
ifProd(new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false,
quiet: true,
})),
ifProd(new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"',
},
})),
ifProd(new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
screw_ie8: true, // eslint-disable-line
warnings: false,
},
})),
])
});
};

我如何将此配置与 nodejs 一起使用。请帮忙

最佳答案

首先,您的 webpack 配置将不会在 webpack 2+ 上运行,因为 webpack-validator 已被弃用,所以我已将其删除。我建议您全局安装 npm install webpack-dev-server -g 并将其用作您的 React 开发中的服务器。这是您可以修改配置以使用它的方式 (webpack.config.js):

const path = require("path");
const webpack = require('webpack');
const {getIfUtils, removeEmpty} = require('webpack-config-utils');

module.exports = env => {
const {ifProd, ifNotProd} = getIfUtils(env)

return {
entry: [
"webpack-dev-server/client?http://localhost:3003",
"webpack/hot/only-dev-server",
"react-hot-loader/patch"
],
context: __dirname,
output: {
path: path.join(__dirname, './build'),
filename: 'bundle.js',
publicPath: '/build/',
pathinfo: ifNotProd(),
},
devtool: ifProd('source-map', 'eval'),
devServer: {
contentBase: path.join(__dirname, "src"),
// enable HMR
hot: true,
// embed the webpack-dev-server runtime into the bundle
inline: true,
// serve index.html in place of 404 responses to allow HTML5 history
historyApiFallback: true,
port: 3003
},
module: {
loaders: [
{test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
{test: /\.css$/, loader: 'style-loader!css-loader'},
{test: /(\.eot|\.woff2|\.woff|\.ttf|\.svg)/, loader: 'file-loader'},
],
},
plugins: removeEmpty([
//...
// same as before
//...
])
};
};

package.json :

{
...
"dependencies": {},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"react-hot-loader": "^3.1.1",
"webpack": "^3.8.1",
"webpack-config-utils": "^2.3.0",
"webpack-dev-server": "^2.9.4",
}
}

在 webpack.config.js 所在的同一个文件夹中创建一个文件 webpack.development.js,它只会设置环境:

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

module.exports = config("development"); // can be "production" or "development"

文件结构:

root
build
bundle.js
src
index.html
index.js
package.json
webpack.config.js
webpack.development.js

最后运行它:webpack-dev-server --config webpack.development.js --progress -p --hot -w

--hot - 将运行服务器,-w - 观看文件

关于node.js - webpack 与 nodejs 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45672066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com