mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Option to transpile with Babel (#4697)
* Upgrade Babeli (now babel-minify) which fixes the bug that was forcing us to run Babel twice for transpilation * Add --transpile option (WIP) * Node API always compiles a string, so it doesn’t need the option to pass a path to an options file, it can always just pass an object to `transpile`; get `transpile` working with `eval` * Not allowing argument to `--transpile` so don’t need to cover so many cases * Don’t need to worry about `sourceMaps` option to pass to Babel, `inputSourceMap` overrides it * Rewrite Webpack test to use Node API * Make the compiler safe again for browsers and Webpack/Browserify * Node version of CoffeeScript.compile passes reference to Babel if transpile is requested * Test Node API for transpile option * Test for merged source maps * Test for Node API error message * Only stop searching for Babel options if a package.json has a truthy "babel" key * Update docs
This commit is contained in:
parent
970f31c292
commit
f51c1a150b
21 changed files with 656 additions and 240 deletions
|
|
@ -84,10 +84,10 @@
|
|||
// in which case this returns a `{js, v3SourceMap, sourceMap}`
|
||||
// object, where sourceMap is a sourcemap.coffee#SourceMap object, handy for
|
||||
// doing programmatic lookups.
|
||||
exports.compile = compile = withPrettyErrors(function(code, options) {
|
||||
var currentColumn, currentLine, encoded, extend, filename, fragment, fragments, generateSourceMap, header, i, j, js, len, len1, map, merge, newLines, ref, ref1, sourceMapDataURI, sourceURL, token, tokens, v3SourceMap;
|
||||
({merge, extend} = helpers);
|
||||
options = extend({}, options);
|
||||
exports.compile = compile = withPrettyErrors(function(code, options = {}) {
|
||||
var currentColumn, currentLine, encoded, filename, fragment, fragments, generateSourceMap, header, i, j, js, len, len1, map, newLines, ref, ref1, sourceMapDataURI, sourceURL, token, tokens, transpiler, transpilerOutput, v3SourceMap;
|
||||
// Clone `options`, to avoid mutating the `options` object passed in.
|
||||
options = Object.assign({}, options);
|
||||
// Always generate a source map if no filename is passed in, since without a
|
||||
// a filename we have no way to retrieve this source later in the event that
|
||||
// we need to recompile it to get a source map for `prepareStackTrace`.
|
||||
|
|
@ -167,6 +167,28 @@
|
|||
}
|
||||
sourceMaps[filename].push(map);
|
||||
}
|
||||
if (options.transpile) {
|
||||
if (typeof options.transpile !== 'object') {
|
||||
// This only happens if run via the Node API and `transpile` is set to
|
||||
// something other than an object.
|
||||
throw new Error('The transpile option must be given an object with options to pass to Babel');
|
||||
}
|
||||
// Get the reference to Babel that we have been passed if this compiler
|
||||
// is run via the CLI or Node API.
|
||||
transpiler = options.transpile.transpile;
|
||||
delete options.transpile.transpile;
|
||||
// See https://github.com/babel/babel/issues/827#issuecomment-77573107:
|
||||
// Babel can take a v3 source map object as input in `inputSourceMap`
|
||||
// and it will return an *updated* v3 source map object in its output.
|
||||
if (v3SourceMap && (options.transpile.inputSourceMap == null)) {
|
||||
options.transpile.inputSourceMap = v3SourceMap;
|
||||
}
|
||||
transpilerOutput = transpiler(js, options.transpile);
|
||||
js = transpilerOutput.code;
|
||||
if (v3SourceMap && transpilerOutput.map) {
|
||||
v3SourceMap = transpilerOutput.map;
|
||||
}
|
||||
}
|
||||
if (options.inlineMap) {
|
||||
encoded = base64encode(JSON.stringify(v3SourceMap));
|
||||
sourceMapDataURI = `//# sourceMappingURL=data:application/json;base64,${encoded}`;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue