babel/minify

SourceMap URL not appended when Babili is executed from JS API

Aperta

#362 aperta il 4 gen 2017

 (0 commenti) (1 reazione) (0 assegnatari)JavaScript (223 fork)batch import
help wanted

Metriche repository

Star
 (4377 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

Hi guys, I'm trying to use babili from JS API but I'm having a problem. If I use it from CLI in this way:

babel ./build/popper.js --inputSourceMap=./build/popper.js.map -s -o ./build/popper.min.js

The resulting popper.min.js file will have at its bottom the comment:

//# sourceMappingURL=popper.min.js.map

If, instead, I use it from JS:

const options = {
    presets: ['babili', 'stage-2'],
    comments: false,
    minified: true,
    compact: true,
    sourceMaps: true,
    inputSourceMap: JSON.parse(fs.readFileSync(`${input}.map`, { encoding: 'utf8' })),
};
const result = babel.transform(inputCode, options);

The resulting code has not the source map comment. Do you know what I'm doing wrong?

For reference, this is the whole script:

// Not working...

const argv = require('yargs').argv;
const babel = require('babel-core');
const fs = require('fs');
const path = require('path');

const input = path.resolve(process.cwd(), argv.i);
const output = path.resolve(process.cwd(), argv.o);
const outputMap = `${output}.map`;

const inputCode = fs.readFileSync(input, { encoding: 'utf8' });
const inputSourceMap = JSON.parse(fs.readFileSync(`${input}.map`, { encoding: 'utf8' }));

// Generate options
//=================
const options = {
    presets: [
        'babili',
        !!argv.es5 && ['es2015', { modules: false }],
        'stage-2',
    ],
    comments: false,
    minified: true,
    compact: true,
    sourceMaps: true,
    inputSourceMap,
};

options.presets = options.presets.filter(a => !!a);

// Transform code
//===============
const result = babel.transform(inputCode, options);

// Write output files
//===================
fs.writeFileSync(output, result.code);
fs.writeFileSync(outputMap, JSON.stringify(result.map));

Guida contributor