How to use convert-source-map - 10 common examples

To help you get started, we’ve selected a few convert-source-map examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fand / glslify-lite / test / source-map.ts View on Github external
test("Import npm packages", async t => {
    const output = await file(path.resolve(__dirname, "fixtures/test01.frag"));

    // Test sourcemaps
    const lastLine = output.split("\n").pop();
    t.assert(
        /\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,/.test(
            lastLine
        ),
        "contains sourceMaps of the file"
    );

    const sm = convert.fromComment(lastLine).toObject();
    const consumer = await new sourceMap.SourceMapConsumer(sm);
    const hasPos = (
        line: number,
        column: number,
        expLine: number,
        expCol: number
    ) => {
        const op = gOP(output, { line, column }, consumer);
        // console.log(line, column, op.line, op.column);
        t.deepEqual(
            { line: op.line, column: op.column },
            { line: expLine, column: expCol }
        );
    };

    // Line 12
github thlorenz / mold-source-map / test / transform-sources.js View on Github external
.on('end', function () {
      var sm = convert.fromSource(bundle);
      var sources = sm.getProperty('sources')
        .filter(function(source) {
          // exclude browserify's prelude
          return !/_prelude\.js$/.test(source);
        });

      t.equal(sources.length, 3, 'molds 3 sources')
      t.ok(~sources.indexOf('js/main.js'), 'molds main.js relative to root')
      t.ok(~sources.indexOf('js/foo.js'), 'molds foo.js relative to root')
      t.ok(~sources.indexOf('js/wunder/bar.js'), 'molds wunder/bar.js relative to root')
    });
});
github ben-ng / minifyify / lib / minifier.js View on Github external
end = function () {
    if(self.opts.minify === false) {
      this.queue(null)
      return
    }

    var thisStream = this
      , unminCode = buffs.join('')
      , originalCode = false
      , existingMap = convertSM.fromSource(unminCode)
      , finish;

    existingMap = existingMap ? existingMap.toObject() : false;

    if(existingMap && existingMap.sourcesContent && existingMap.sourcesContent.length) {
      originalCode = convertSM.removeComments(existingMap.sourcesContent[0]);
      existingMap = JSON.stringify(existingMap);
    }
    // Only accept existing maps with sourcesContent
    else {
      existingMap = false;
    }

    finish = function (tempExistingMapFile, cleanupCallback) {
      // Don't minify JSON!
      if(file.match(/\.json$/)) {
        try {
          thisStream.queue(JSON.stringify(JSON.parse(unminCode)))
        }
        catch(e) {
          console.error('failed to parse JSON in ' + file)
          thisStream.queue(unminCode)
github istanbuljs / v8-to-istanbul / lib / v8-to-istanbul.js View on Github external
async load () {
    const rawSource = this.sources.source || await readFile(this.path, 'utf8')
    const rawSourceMap = this.sources.sourceMap ||
      // if we find a source-map (either inline, or a .map file) we load
      // both the transpiled and original source, both of which are used during
      // the backflips we perform to remap absolute to relative positions.
      convertSourceMap.fromSource(rawSource) || convertSourceMap.fromMapFileSource(rawSource, dirname(this.path))

    if (rawSourceMap) {
      if (rawSourceMap.sourcemap.sources.length > 1) {
        console.warn('v8-to-istanbul: source-mappings from one to many files not yet supported')
        this.source = new CovSource(rawSource, this.wrapperLength)
      } else {
        this._rewritePath(rawSourceMap)
        this.sourceMap = await new SourceMapConsumer(rawSourceMap.sourcemap)

        let originalRawSource
        if (this.sources.originalSource) {
          originalRawSource = this.sources.originalSource
        } else {
          originalRawSource = await readFile(this.path, 'utf8')
        }
github Jam3 / devtool / lib / require-hook.js View on Github external
script = debugExpr + script;
    }

    // bail early if we don't have a path
    if (!currentWrapFile || !useSourceMaps) return moduleWrap(script);

    var sourceFile = path.relative(basedir, currentWrapFile).replace(/\\/g, '/');
    var sourceFileName = path.basename(sourceFile);
    var sourceFileDir = path.dirname(sourceFile);
    var hasComment = convertSourceMap.commentRegex.test(script);
    var hasMapFile = convertSourceMap.mapFileCommentRegex.test(script);

    // if we have a map pointing to a file, inline it as base64
    if (!hasComment && hasMapFile) {
      try {
        var sm = convertSourceMap.fromMapFileSource(original, sourceFileDir);
        script = [
          combineSourceMap.removeComments(script),
          convertSourceMap.fromObject(sm.sourcemap).toComment()
        ].join('\n');
        hasComment = true; // now we have base64 comment
      } catch (err) {
        // Don't attempt to handle source maps for this file,
        // it is most likely a comment about source maps and not
        // a *real* source map comment!
      }
    }

    var wrapScript = moduleWrap(script);

    // do not make any more alterations to the source maps
    if (hasComment || hasMapFile) return wrapScript;
github jallajs / jalla / lib / pipeline.js View on Github external
emitter.on('asset', function add (id, buff, meta = {}) {
          assert(typeof id === 'string', 'pipeline: id should be type string')
          assert(buff instanceof Buffer, 'pipeline: data should be type buffer')

          if (meta.map) {
            // create an asset for the source map
            const mapAsset = add(id + '.map', meta.map, {
              mime: 'application/json'
            })

            // add map comment to buffer
            const map = sourcemap.generateMapFileComment(mapAsset.url, {
              multiline: /\.css$/.test(id)
            })
            const src = buff.toString()
            buff = Buffer.from(src.replace(/\n?$/, '\n' + map))
          }

          var asset = self.assets.get(id)
          var hash = createHash(buff)
          var base = state.base + '/'

          if (!asset) {
            asset = Object.create({ read, toJSON })
            asset.label = label
            self.assets.set(id, asset)
          }
github javascript-studio / studio-cli / lib / studio.js View on Github external
setSource(source) {
    if (!source) {
      this.fail('No sources received on stdin');
      return;
    }
    this.source = source;
    this.source_map = convert_source_map.fromSource(source);
    if (!this.source_map) {
      try {
        this.source_map = convert_source_map.fromMapFileComment(source,
          this.working_dir);
      } catch (ignore) {
        // It throws if there are no source maps
      }
    }
    if (this.source_map) {
      this.source_map = this.source_map.toObject();
      this.source = convert_source_map.removeComments(source);
    }
    gzip(source, (err, buffer) => {
      if (err) {
        this.fail('gzip failure', err);
      } else {
github fand / glslify-lite / src / glslify-bundle.ts View on Github external
private async preprocess(dep: DepsInfo): Promise {
        // Get sourcemaps created by pretransform
        const rawMap = convert.fromSource(dep.source);
        const consumer = rawMap
            ? await new sourceMap.SourceMapConsumer(rawMap.toObject())
            : null;
        if (consumer) {
            dep.source = convert.removeComments(dep.source); // eslint-disable-line
        }

        const tokens = tokenize(dep.source);
        const imports = [];
        let exports = null;

        depth(tokens);
        scope(tokens);

        // Note: tokens must be sorted by position
        let lastLine = 1;
github gulpjs / vinyl-sourcemap / lib / helpers.js View on Github external
function writeSourceMaps(file, destPath, callback) {
  var sourceMapFile;
  var commentOpts = getCommentOptions(file.extname);

  var comment;
  if (destPath == null) {
    // Encode source map into comment
    comment = convert.fromObject(file.sourceMap).toComment(commentOpts);
  } else {
    var mapFile = path.join(destPath, file.relative) + '.map';
    var sourceMapPath = path.join(file.base, mapFile);

    // Create new sourcemap File
    sourceMapFile = createSourceMapFile({
      cwd: file.cwd,
      base: file.base,
      path: sourceMapPath,
      content: file.sourceMap,
    });

    var sourcemapLocation = path.relative(file.dirname, sourceMapPath);

    sourcemapLocation = normalizePath(sourcemapLocation);
github power-assert-js / gulp-espower / index.js View on Github external
{
            enter: empowerAssert.enter
        },
        espower.createVisitor(jsAst, espowerOptions)
    ]));
    var escodegenOutput = escodegen.generate(modifiedAst, escodegenOptions);
    if (inMap) {
        file.contents = new Buffer(escodegenOutput.code);
        var outMap = convert.fromJSON(escodegenOutput.map.toString());
        outMap.setProperty('sources', inMap.sources);
        outMap.setProperty('sourcesContent', inMap.sourcesContent);

        var reMap;
        if (inMap.mappings === '') {
            applySourceMap(file, outMap.toJSON());
            reMap = convert.fromObject(file.sourceMap);
        } else {
            reMap = convert.fromObject(mergeSourceMap(inMap, outMap.toJSON()));
        }
        reMap.setProperty('sources', inMap.sources);
        reMap.setProperty('sourcesContent', inMap.sourcesContent);
        // do not set sourceMapRoot to keep paths relative until the end of chain
        // reMap.setProperty('sourceRoot', file.base);

        file.sourceMap = reMap.toObject();
    } else {
        file.contents = new Buffer(escodegenOutput);
    }
}