google / closure-compiler-js

Package for the JS version of closure-compiler for use via NPM
https://github.com/google/closure-compiler
Apache License 2.0
1.84k stars 64 forks source link

Generating source map from command line? #84

Closed hultqvist closed 6 years ago

hultqvist commented 6 years ago

Can a source map be generated when calling google-closure-compiler-js from the command line?

I can pass --createSourceMap true but can't find any map files generated.

So far I've been using the pipe output so I guess the map can't be sent there. Using --jsOutputFile test.js gives the error: Unhandled flag: jsOutputFile.

josecanciani commented 6 years ago

Could you resolve it? It seems it's not yet an option. Here's a quick script I wrote to compile it, replace your options as needed:

const closureCompiler = require('google-closure-compiler-js');
const fs = require('fs');

if (process.argv.length !== 4) {
    console.log(process.argv);
    console.log("Usage: node " + __filename + " inputFile outputFile");
    process.exit(1);
}

const inputPath = process.argv[2];
const outputPath = process.argv[3];

if (!fs.existsSync(inputPath)) {
    console.log('Input file does not exists: ' + inputPath);
    process.exit(1);
}

const src = fs.readFileSync(inputPath).toString();
const getErrorMessage = (e) => {
    return [
        'Compile error in file: ' + path,
        '- Type: ' + e.type,
        '- Line: ' + e.lineNo,
        '- Char : ' + e.charNo,
        '- Description: ' + e.description
    ].join('\n');
};

/**
* Use same options as defined in https://github.com/highcharts/highcharts/blob/master/gulpfile.js (search for "closureCompiler.compile")
*/
const out = closureCompiler.compile({
    compilationLevel: 'SIMPLE_OPTIMIZATIONS',
    jsCode: [{
        src: src
    }],
    languageIn: 'ES5',
    languageOut: 'ES5',
    createSourceMap: true
});

const errors = out.errors;
if (errors.length) {
    const msg = errors.map((e) => {
        return getErrorMessage(e);
    }).join('\n');
    console.log("Ooops, there was a compiler error\n");
    console.log(msg);
    process.exit(1);
}

fs.writeFileSync(outputPath, out.compiledCode);
fs.writeFileSync(outputPath + '.map', out.sourceMap);
process.exit(0);
hultqvist commented 6 years ago

I'm afraid not. Using the closure-compiler from a non javascript runtime I don't have access to those tools. I ended up using the java version for now.

martinsik commented 6 years ago

I just stumbled upon the same problem. I added a PR #87 to fix. You can extract the source map with exorcist for example https://github.com/thlorenz/exorcist.

The jsOutputFile is used only by gulp and is then removed: https://github.com/google/closure-compiler-js/blob/master/lib/gulp.js#L95-L96

ChadKillingsworth commented 6 years ago

The JS version does currently support filesystem operations. I'm looking at adding that, but its not currently an option.

The source map is created, but the consumer is responsible for writing out the file.