Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
How to insert a javascript file in another javascript at build time using comments

Is there something on npm or in VS Code or anywhere that can bundle or concatenate Javascript files based on comments, like:

function myBigLibrary(){
    //@include util.js
    //@include init.js

    function privateFunc(){...}

    function publicFunc(){
      //@include somethingElse.js
      ...
    }

    
    
    return {
        init, publicFunc, etc
    }
}

Or something like that? I would think that such a thing is common when your javascript files get very large. All I can find are complicated things like webpack.

I'm looking for any equivalent solution that allows you to include arbitrary code in arbitrary positions in other code. I suppose that would cause trouble for intellisense, but an extension could handle that.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I'm not sure what you really means but if you means linking variables from other javascript files. probably this will help you
const { variable name } = require(the javascript file path)
example:
in index.js

const { blue } = require('./../js/blue.js)
console.log(blue)


Meanwhile in blue.js

const blue = "dumbass"

if this doesnt help you just ignore this

about 4 years ago · Juan Pablo Isaza Report

0

So here is a bare bones way to to what I wanted. I have been learning more about how you can do things with esbuild or other bundlers, but I didn't quite figure out something that fit my needs. And this is simpler and more flexible. It can work for any file type. You can do automatic updates when files change using nodemon instead of node to run this code.

const fs = require('fs')
/////////////////////////

const input  = 'example.js'
const output = 'output.js'

const ext    = '.js'

// looks for file with optional directory as: //== dir/file
const regex  = /\/\/== *([\w-\/]+)/g


const fileContent = fs.readFileSync(input).toString()


// replace the comment references with the corresponding file content
const replacement = fileContent.replace(regex, (match, group)=>{

    const comment = '//////// '+group+ext+' ////////\n\n'
    const replace = fs.readFileSync(group+ext).toString()
    return comment + replace

})

// write replacement to a file
fs.writeFileSync(output, replacement)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!