Coming in from JSWORLD Conference? →

Tuesday, February 22 2022

Compute runtime dependencies for your Remix Express app via @vercel/nft

Rishi Raj Jain
Rishi Raj Jain@rishi_raj_jain_
async function exec() {
  const fs = require('fs')
  const { nodeFileTrace } = require('@vercel/nft')
  // The whole app inside index.js
  const files = ['./build/index.js']
  // Compute file trace
  const { fileList } = await nodeFileTrace(files)
  // Store set of packages
  let packages = {}
  fileList.forEach((i) => {
    if (i.includes('node_modules/')) {
      let temp = i.replace('node_modules/', '')
      temp = temp.substring(0, temp.indexOf('/'))
      packages[`node_modules/${temp}`] = true
    } else {
      packages[i] = true
    }
  })
  // Sort the set of packages by name (for easier difference comparison with git)
  packages = Object.keys(packages)
    .sort()
    .reduce((obj, key) => {
      obj[key] = packages[key]
      return obj
    }, {})
  // Dump the list of the computed packages for further references while deploying the app
  fs.writeFileSync('./toInclude.json', JSON.stringify(packages, null, 2), 'utf-8')
}

exec()