babel/minify

SourceMap URL not appended when Babili is executed from JS API

Open

#362 opened on Jan 4, 2017

View on GitHub
 (0 comments) (1 reaction) (0 assignees)JavaScript (4,377 stars) (223 forks)batch import
help wanted

Description

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));

Contributor guide