gpt4 book ai didi

javascript - 由于 MQTT.js 的存在,Webpack UglifyJS 在生产构建中出错

转载 作者:行者123 更新时间:2023-11-30 14:32:48 25 4
gpt4 key购买 nike

我有一个 react web app使用 server side rendering 并且我为 developmentproduction 环境分离了 webpack 配置。

对于每个环境,我设置了两个方面的配置,第一个是client,第二个是server。这些配置非常完整并且工作很棒,但是

我需要 mqtt.js在这个项目中,这个库在它的代码表的第一个有 #!/usr/bin/env node 并且使用这个库导致运行 devbuild 脚本陷入此错误:

ERROR in ./node_modules/mqtt/mqtt.js
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
| #!/usr/bin/env node
| 'use strict'
|
@ ./src/app/App.jsx 23:12-27
@ ./src/server.jsx

所以我使用 shebang-loader 来解决这个问题,并在 webpack 配置中加入 babel-loaderexclude node_modules 文件夹,除了mqtt 文件夹,所以这个问题在开发环境中解决。

然后我运行 build 脚本并看到这个错误:

ERROR in server.js from UglifyJs
Unexpected token: name (zlibLimiter) [server.js:1181,4]

我真的需要使用 mqtt.js 所以我不能省略它或用任何类似的库更改它。

如果分析需要查看所有配置或代码,这是应用程序 Repository

最佳答案

你的问题很简单。如果您查看正在使用的 mqtt.js,它的内容是

$ ls
bin CONTRIBUTING.md dist doc examples lib LICENSE.md mqtt.js node_modules package.json README.md test types
$ cat mqtt.js
#!/usr/bin/env node
'use strict'

/*
* Copyright (c) 2015-2015 MQTT.js contributors.
* Copyright (c) 2011-2014 Adam Rudd.
*
* See LICENSE for more information
*/

var MqttClient = require('./lib/client')
var connect = require('./lib/connect')
var Store = require('./lib/store')

module.exports.connect = connect

// Expose MqttClient
module.exports.MqttClient = MqttClient
module.exports.Client = MqttClient
module.exports.Store = Store

function cli () {
var commist = require('commist')()
var helpMe = require('help-me')()

commist.register('publish', require('./bin/pub'))
commist.register('subscribe', require('./bin/sub'))
commist.register('version', function () {
console.log('MQTT.js version:', require('./package.json').version)
})
commist.register('help', helpMe.toStdout)

if (commist.parse(process.argv.slice(2)) !== null) {
console.log('No such command:', process.argv[2], '\n')
helpMe.toStdout()
}
}

if (require.main === module) {
cli()
}

现在这应该是一个 cli 而不是直接摄取。但是,如果您看到有一个 dist 文件夹,其中包含另一个 mqtt.js。这是你应该使用的。如此变化

import {connect} from 'mqtt';

import {connect} from 'mqtt/dist/mqtt';

再次构建它解决了这个问题

[1] Hash: 23e55c27224c66d940890aee1c472502b9e9a7e0
Version: webpack 3.12.0
Child client:
Hash: 23e55c27224c66d94089
Time: 10477ms
Asset Size Chunks Chunk Names
client.js 311 kB 0 [emitted] [big] main
styles.css 246 bytes 0 [emitted] main
[7] (webpack)/buildin/global.js 509 bytes {0} [built]
[] -> factory:229ms building:43ms = 272ms
[27] ./src/client.jsx 606 bytes {0} [built]
factory:144ms building:145ms = 289ms
[65] ./src/app/App.jsx 7.94 kB {0} [built]
[] -> factory:6ms building:195ms dependencies:5ms = 206ms
[74] ./src/styles/styles.pcss 151 bytes {0} [built]
[] -> factory:2556ms building:2927ms = 5483ms
+ 75 hidden modules
Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js??ref--1-2!node_modules/postcss-loader/lib/index.js??ref--1-3!src/styles/styles.pcss:
[0] ./node_modules/css-loader??ref--1-2!./node_modules/postcss-loader/lib??ref--1-3!./src/styles/styles.pcss 530 bytes {0} [built]
factory:95ms building:838ms = 933ms
+ 1 hidden module
Child server:
Hash: 0aee1c472502b9e9a7e0
Time: 10465ms
Asset Size Chunks Chunk Names
server.js 237 kB 0 [emitted] main
stats.json 0 bytes [emitted]
[54] ./src/server.jsx 1.28 kB {0} [built]
factory:17ms building:254ms = 271ms
[98] ./src/app/template.jsx 622 bytes {0} [built]
[] -> factory:17ms building:16ms = 33ms
[99] ./src/app/App.jsx 7.94 kB {0} [built]
[] -> factory:18ms building:120ms dependencies:79ms = 217ms
[100] ./src/styles/styles.pcss 1.4 kB {0} [built]
[] -> factory:2621ms building:4ms = 2625ms
[101] ./node_modules/css-loader??ref--1-1!./node_modules/postcss-loader/lib??ref--1-2!./src/styles/styles.pcss 530 bytes {0} [built]
[] -> factory:9ms building:918ms = 927ms
+ 132 hidden modules

关于javascript - 由于 MQTT.js 的存在,Webpack UglifyJS 在生产构建中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50905995/

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