DEV Community

Discussion on: What's your way of publishing Node.js library for the browser? (also, introducing Rollup)

Collapse
 
saurabhdaware profile image
Saurabh Daware 🌻

I'm doing super hacky stuff. I usually write my own build scripts that reads code from different files and writes them into the new files, then I remove the export import keywords from the code string and minify it.

Here's my build script

const fse = require('fs-extra');
const minify = require('@node-minify/core');
const terser = require('@node-minify/terser');


console.log("Build started... \n🌻");

fse.removeSync('dist');
fse.copySync('src', 'dist');

// Read from both the files
const voicesFile = fse.readFileSync('src/voices.mjs');
const indexFile = fse.readFileSync('src/index.mjs');

// Bundle them together
let bolFile = voicesFile + '\n' + indexFile;

// Remove export import keywords
bolFile = bolFile
  .replace(/\b.*(export default).*/g, '')
  .replace(/\b(export)\b/g, '')
  .replace(/\b.*(import).*/g, '')

// Write into new files
fse.writeFileSync('dist/bol.js', bolFile);

// Create a minified version of new file
minify({
  compressor: terser,
  input: 'dist/bol.js',
  output: 'dist/bol.min.js',
  callback: function(err, min) {}
});

console.log(" DONE 🎉");

Though this is not ideal when you have to change the directory structure multiple times. This just works for me since I don't have to add/remove new files for that particular usecase