Last active
October 11, 2017 05:42
-
-
Save sunzhuoshi/86f2d5a7f3330e227762b0909372ca60 to your computer and use it in GitHub Desktop.
google-closure-compiler-js webpack/gulp plugin externs issue quick fix
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
// add following code into webpack.config.js or gulpfile.js or webpack/gulp plugin file | |
// sample options | |
var options = { | |
languageIn: 'ECMASCRIPT6', | |
languageOut: 'ECMASCRIPT5', | |
compilationLevel: 'ADVANCED', | |
warningLevel: 'VERBOSE', | |
externs: ['externs.js'] | |
} | |
function toArray(arg) { | |
if (typeof arg === 'string') { | |
return [arg]; | |
} else if (arg) { | |
return arg; | |
} else { | |
return []; | |
} | |
} | |
function readExternFile(extern) { | |
if ('string' === typeof extern || | |
'object' === typeof extern && undefined === extern.src && 'string' === typeof extern.path) { | |
var newExtern = { | |
src: '', | |
path: 'string' === typeof extern? extern: extern.path | |
}; | |
fs.readFile(newExtern.path, 'utf8', (err, src) => { | |
if (err) { | |
throw new Error(err); | |
} | |
else { | |
newExtern.src = src; | |
} | |
}); | |
return newExtern; | |
} | |
else { | |
return extern; | |
} | |
} | |
function translateOptions(options) { | |
var externs = options.externs; | |
if (externs) { | |
externs = toArray(externs); | |
options.externs = externs.map(extern => readExternFile(extern)); | |
} | |
} | |
// we have to translate 'externs' content, | |
// due to google closure compiler's webpack plugin doesn't support extern path string(OK with 'src' content) | |
translateOptions(options); | |
var compiler = new ClosureCompiler({options: options}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment