gpt4 book ai didi

node.js - Webpack target=node 和 babel

转载 作者:行者123 更新时间:2023-12-02 10:33:02 25 4
gpt4 key购买 nike

如何使用 Node Express 启动 Webpack 项目。我尝试了 webtack target-node 属性,但是 babel 没有按预期处理我的目标文件。 webpack 配置文件:

import webpack from 'webpack';
import path from 'path';
var fs = require('fs');


var nodeModules = {};
fs.readdirSync('node_modules')
.filter(function(x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = 'commonjs ' + mod;
});

const GLOBALS = {
'process.env.NODE_ENV': JSON.stringify('development'),
__DEV__: true
};

export default {
name:'server',
debug: true,
devtool: 'cheap-module-eval-source-map', // more info:https://webpack.github.io/docs/build-performance.html#sourcemaps and https://webpack.github.io/docs/configuration.html#devtool
noInfo: false, // set to false to see a list of every file being bundled.
entry: './tools/socketServer.js',
target: 'node', // necessary per https://webpack.github.io/docs/testing.html#compile-and-test
output: {
path: `${__dirname}/dist`, // Note: Physical files are only output by the production build task `npm run build`.
publicPath: 'http://localhost:3000/', // Use absolute paths to avoid the way that URLs are resolved by Chrome when they're parsed from a dynamically loaded CSS blob. Note: Only necessary in Dev.
filename: '/bundle.js'
},
externals: nodeModules,
plugins: [
new webpack.DefinePlugin(GLOBALS), // Tells React to build in prod mode. https://facebook.github.io/react/downloads.htmlnew webpack.HotModuleReplacementPlugin());
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
],
module: {
loaders: [
{test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']},
{test: /\.eot(\?v=\d+.\d+.\d+)?$/, loader: 'file'},
{test: /\.(woff|woff2)$/, loader: 'file-loader?prefix=font/&limit=5000'},
{test: /\.ttf(\?v=\d+.\d+.\d+)?$/, loader: 'file-loader?limit=10000&mimetype=application/octet-stream'},
{test: /\.svg(\?v=\d+.\d+.\d+)?$/, loader: 'file-loader?limit=10000&mimetype=image/svg+xml'},
{test: /\.(jpe?g|png|gif)$/i, loaders: ['file']},
{test: /\.ico$/, loader: 'file-loader?name=[name].[ext]'},
{test: /(\.css|\.scss)$/, loaders: ['style', 'css?sourceMap', 'sass?sourceMap']}
]
}
};

最佳答案

我假设您从 here 复制了代码。我认为您的问题在于您使用的是 export default 而不是 module.exports。 Node < v13 不支持导入/导出,因此这可能是问题的症结所在。

关于node.js - Webpack target=node 和 babel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38035327/

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