Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

node.js - gulp-4 concate multiple js files into one: Error Identifier has already been defined

I am using Nodejs- v12.9.0 & gulp-4.0.2

I want to concatenate & minify multiple js files from different src directories and bundle then into a single JS file. It does concatenate, minify & generates a single js file. While running this generated js file it gives below error because in almost all the js files we have this module const fs = require('fs').

const fs = require('fs');

Getting below error

SyntaxError: Identifier 'fs' has already been declared

gulp task:

function copyOtherJs(){
    return src(['main.js','app.js', 'src/*.js'])
        .pipe(terser({ parse: { bare_returns: false}, mangle: false }))//{ parse: { bare_returns: true } 
        .pipe(concat('srcJs.js'))
        .pipe(dest('dist/src'));
}

How to overcome this error while running my minified js file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Bundling is generally used for client-side files. There's no reason to bundle and concatenate server-side code, it won't make anything noticeably faster or easier to manage. The only good use case I can think of for trying to bundle your whole server into one file might be obfuscation, but that's not what it looks like you're doing. If on the other hand you have those declarations in client-side code, that also wouldn't work, because those modules don't exist in the browser.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...