Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

75
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.

7 months 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

7 months 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)
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs