Created
September 11, 2017 03:35
-
-
Save hdon/1b1528cf137d23e69c8868f6b4800ce3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 32 now has a check that
imp.source[0]=='.'
to prevent from trying to transpile module dependencies fromnode_modules/