今天做一个node的包时发现运行时,提示我以下内容:
(node:22408) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
我大概知道这个问题出现的原因,就是不能 new Buffer 对象,但我在项目内并没有使用 , 后来发现是 rollup 打包后输入的文件内有以下一段代码导致的:
(function (module) {
// 就是这三句
var MOVE_LEFT = new Buffer('1b5b3130303044', 'hex').toString();
var MOVE_UP = new Buffer('1b5b3141', 'hex').toString();
var CLEAR_LINE = new Buffer('1b5b304b', 'hex').toString();
var stringWidth$1 = stringWidth;
module.exports = function(stream) {
var write = stream.write;
var str;
stream.write = function(data) {
if (str && data !== str) str = null;
return write.apply(this, arguments);
};
if (stream === process.stderr || stream === process.stdout) {
process.on('exit', function() {
if (str !== null) stream.write('');
});
}
var prevLineCount = 0;
var log = function() {
str = '';
var nextStr = Array.prototype.join.call(arguments, ' ');
for (var i=0; i
str += MOVE_LEFT + CLEAR_LINE + (i < prevLineCount-1 ? MOVE_UP : '');
}
str += nextStr;
stream.write(str);
var prevLines = nextStr.split('\n');
prevLineCount = 0;
for (var i=0; i < prevLines.length; i++) {
prevLineCount += Math.ceil(stringWidth$1(prevLines[i]) / stream.columns) || 1;
}
};
log.clear = function() {
stream.write('');
};
return log;
};
module.exports.stdout = module.exports(process.stdout);
module.exports.stderr = module.exports(process.stderr);
} (singleLineLog));
我不知道这段代码有什么作用,所以也不好动它,还有这个是打包后的文件,代码量很多,也不好找,每次都要修改的话,比较容易忘记。
所以我想知道能不能通过修改 rollup 配置文件把这个警告去掉
以下是我的配置文件:
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const commonjs = require('@rollup/plugin-commonjs')
const { terser } = require('rollup-plugin-terser')
const cleanup = require('rollup-plugin-cleanup')
const josn = require('@rollup/plugin-json')
module.exports = {
input: './src/main.js',
output: {
exports: 'auto',
file: './dist/index.js',
format: 'cjs',
name: 'test',
},
plugins: [
commonjs({
ignoreDynamicRequires(target){
return require(target);
}
}),
nodeResolve({
preferBuiltins: true,
}),
cleanup(),
josn(),
],
}