babel/minify

SourceMap URL not appended when Babili is executed from JS API

Open

#362 建立於 2017年1月4日

在 GitHub 查看
 (0 留言) (1 反應) (0 負責人)JavaScript (223 fork)batch import
help wanted

倉庫指標

Star
 (4,377 star)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

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

貢獻者指南