Skip to content

Instantly share code, notes, and snippets.

@hdon
Created September 11, 2017 03:35
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 hdon/1b1528cf137d23e69c8868f6b4800ce3 to your computer and use it in GitHub Desktop.
Save hdon/1b1528cf137d23e69c8868f6b4800ce3 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var resolve = require('resolve');
function compileModule(babel, modulePath) {
var visitedFiles = {};
return _compileModule(modulePath);
function _compileModule(modulePath) {
var filePath = resolve.sync(modulePath, { basedir: process.cwd() });
var realPath = fs.realpathSync(filePath);
if (realPath in visitedFiles)
return;
console.log('compiling', realPath);
visitedFiles[realPath] = true;
/* transpile */
var input = fs.readFileSync(filePath, 'utf-8');
var piled = babel.transform(input, {
ast: false
, code: true
, sourceType: 'module'
, presets: ['es2016', 'es2015']
, plugins: ['transform-object-rest-spread']
, sourceFileName: filePath
, sourceRoot: '/home/hdon/src/git/pitchboard-frontend'
});
piled.metadata.modules.imports.forEach(imp => {
_compileModule(path.join(path.dirname(realPath), imp.source))
})
/* cache the result */
var outFilePath = process.env.HOME + '/.redux-shell/transpiled/' + realPath;
mkdirp.sync(path.dirname(outFilePath));
fs.writeFileSync(outFilePath, piled.code);
return outFilePath.substr(0, outFilePath.length - '.js'.length);
}
}
@hdon
Copy link
Author

hdon commented Sep 11, 2017

Line 32 now has a check that imp.source[0]=='.' to prevent from trying to transpile module dependencies from node_modules/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment