Skip to content

Instantly share code, notes, and snippets.

@fallen90
Created March 21, 2019 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fallen90/e701ad39c49144efaffeffe394e4151b to your computer and use it in GitHub Desktop.
Save fallen90/e701ad39c49144efaffeffe394e4151b to your computer and use it in GitHub Desktop.
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var mkdirp = require('mkdirp');
var ncc = require('@zeit/ncc');
var dist = path.join(__dirname, 'dist');
var distRoutes = path.join(dist, 'routes');
var src = path.join(__dirname, 'src');
var srcRoutes = path.join(src, 'routes');
// check if src exists
console.log(`
Building route files
`)
if (fs.existsSync(src)) {
// list all files in directory
glob(path.join(srcRoutes, '/**/*.js'), function (err, files) {
if (!err) {
files.forEach(file => {
const base = file.replace(srcRoutes, '');
const completePath = path.join(dist, base);
const outputDir = path.dirname(completePath);
mkdirp(outputDir, err => {
if (!err) {
console.log('Compiling ==>', base);
ncc(file, {
// provide a custom cache path or disable caching
cache: false,
minify: false, // default
sourceMap: false, // default
sourceMapRegister: false, // default
watch: false // default
}).then(({ code }) => {
fs.writeFileSync(completePath, code);
if (fs.existsSync(completePath)) {
console.log('Output ==>', completePath);
}
})
}
});
})
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment