return concat(function(data) { if(!self.opts.minify) { // Keep browserify's sourcemap return cb(null, data.toString(), null); } else if (!self.opts.map) { // Remove browserify's inline sourcemap return cb(null, convertSM.removeComments(data.toString()), null); } else { var bundle = self.decoupleBundle(data); if(bundle === false) { if(self.opts.minify) throw new Error('Browserify must be in debug mode for minifyify to consume sourcemaps') return cb(null, convertSM.removeComments(data.toString())); } // Re-maps the browserify sourcemap // to the original source using the // uglify sourcemap bundle.map = self.transformMap(bundle.map); bundle.code = bundle.code + '\n//# sourceMappingURL=' + self.opts.map cb(null, bundle.code, bundle.map); } });
thunk = function() { var header = '_hmr['+JSON.stringify(bundleKey)+ '].initModule('+JSON.stringify(fileKey(row.file))+', module);\n(function(){\n'; var footer = '\n}).apply(this, arguments);\n'; var inputMapCV = convert.fromSource(row.source); var inputMap; if (inputMapCV) { inputMap = inputMapCV.toObject(); row.source = convert.removeComments(row.source); } else { inputMap = makeIdentitySourceMap(row.source, path.relative(basedir, row.file)); } var node = new sm.SourceNode(null, null, null, [ new sm.SourceNode(null, null, null, header), sm.SourceNode.fromStringWithSourceMap(row.source, new sm.SourceMapConsumer(inputMap)), new sm.SourceNode(null, null, null, footer) ]); var result = node.toStringWithSourceMap(); row.source = result.code + convert.fromObject(result.map.toJSON()).toComment(); transformCache[row.file] = { hash: hash, transformedSource: row.source }; return row; };
export default function normalizeFile( options: Object, code: string, ast?: {}, ): NormalizedFile { code = `${code || ""}`; let shebang = null; let inputMap = null; if (options.inputSourceMap !== false) { inputMap = convertSourceMap.fromSource(code); if (inputMap) { code = convertSourceMap.removeComments(code); } else if (typeof options.inputSourceMap === "object") { inputMap = convertSourceMap.fromObject(options.inputSourceMap); } } const shebangMatch = shebangRegex.exec(code); if (shebangMatch) { shebang = shebangMatch[0]; code = code.replace(shebangRegex, ""); } if (!ast) ast = parser(options, code); return { code, ast, shebang, inputMap, }; }
// Clear out autoprefixer's data and fix paths to match original function sourceMapRewriter(file, enc, callback) { if(file.isNull()) { return callback(null, file); } var content = file.contents.toString(); var sourceMap = convert.fromSource(content); var sources = sourceMap.getProperty('sources'); var sourcesContent = sourceMap.getProperty('sourcesContent'); var newSources = sources.map(newSource); sourceMap.setProperty('sourceRoot', null); sourceMap.setProperty('sources', newSources); sourceMap.setProperty('sourcesContent', sourcesContent); content = convert.removeComments(content) + '\n/*# sourceMappingURL=data:application/json;base64,' + sourceMap.toBase64() + ' */'; file.contents = new Buffer(content); callback(null, file); function newSource(source, index) { if(/\.css$/.test(source)) { source = sourcesContent[index] = ''; } else { source = path.resolve(sourcePath, source).split(path.sep).join('/'); source = protocol + source; } return source; } }
return duplexer(concat({ encoding: 'buffer' }, function (buf) { try { body = buf.toString('utf8').replace(/^#!/, '//#!'); var matches = false; for (var key in modules) { if (body.indexOf(key) !== -1) { matches = true; break; } } if (!matches) { // just pass it through output.end(buf); return; } if (opts.sourceMap) { inputMap = convertSourceMap.fromSource(body); if (inputMap) inputMap = inputMap.toObject(); body = convertSourceMap.removeComments(body); sourcemapper = new MagicString(body); } falafel(body, parserOpts, walk); } catch (err) { return error(err) } finish(body); }), output);
function sourceMapInit(file, encoding, callback) { /*jshint validthis:true */ if (file.isNull()) { this.push(file); return callback(); } if (file.isStream()) { return callback(new Error(PLUGIN_NAME + ': Streaming not supported')); } var map = { version : 3, file: file.relative, names: [], mappings: '', sources: [file.relative], sourcesContent: [file.contents.toString()] }; var embeddedMap = convert.fromSource(file.contents.toString()); file.sourceMap = embeddedMap ? embeddedMap.toObject() : map; var str = convert.removeComments(file.contents.toString()); file.contents = new Buffer(str, 'utf8'); this.push(file); callback(); }
filenames.forEach(function(filename) { var text = fs.readFileSync(filename, 'utf-8').trim(); var prepend = 'register(' + JSON.stringify(filename) + ', (function(module, exports, require) {\n'; var append = '\n}));\n'; process.stdout.write(prepend); currentLine += prepend.match(/\n/g).length; var sourceMap = convert.fromSource(text) || convert.fromMapFileSource(text, dirname(filename)); if(sourceMap) { var o = sourceMap.toObject(); o.sourcesContent = o.sources.map(function(filename) { return fs.readFileSync(filename, 'utf-8'); }); comment = convert.fromObject(o).toComment(); text = convert.removeMapFileComments(text); text = convert.removeComments(text); text = text.trim(); } else { comment = ''; } map.addFile( { sourceFile: filename, source: text + '\n' + comment }, { line: currentLine } ); process.stdout.write(text); currentLine += text.match(/\n/g).length; process.stdout.write(append); currentLine += append.match(/\n/g).length; });
function evaluate(js, ctx) { var ctx = ctx || { window: {}, document: {} }; js = convert.removeComments(js); vm.runInNewContext('main =' + js + '(1)', ctx, 'main.vm'); vm.runInNewContext('require =' + js + '', ctx, 'require.vm'); return ctx; }
less.render(str, opts, function (err, css) { if (err) { // convert the keys so PluginError can read them err.lineNumber = err.line; err.fileName = err.filename; // add a better error message err.message = err.message + ' in file ' + err.fileName + ' line no. ' + err.lineNumber; self.emit('error', new PluginError('gulp-less', err)); } else { file.contents = new Buffer(css); file.path = gutil.replaceExtension(file.path, '.css'); if (file.sourceMap) { var comment = convert.fromSource(css); if (comment) { file.contents = new Buffer(convert.removeComments(css)); var sourceMap = comment.sourcemap; for (var i = 0; i < sourceMap.sources.length; i++) { sourceMap.sources[i] = path.relative(file.base, sourceMap.sources[i]); } applySourceMap(file, sourceMap); } } self.push(file); } next(); });
end = function () { 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) { if(self.opts.minify) { var opts = { fromString: true , outSourceMap: (self.opts.map ? self.opts.map : undefined) , inSourceMap: (self.opts.map ? tempExistingMapFile : undefined) }; if (typeof self.opts.minify === 'object') { _.defaults(opts, self.opts.minify); } var min = uglify.minify(unminCode, opts); thisStream.queue(convertSM.removeMapFileComments(min.code).trim()); if(self.opts.map) { self.registerMap(file, originalCode || unminCode, new SMConsumer(min.map)); } } // Otherwise we'll never finish thisStream.queue(null); } if(existingMap) { tmp.file(function (err, path) { if(err) { throw err; } fs.writeFile(path, existingMap, function (err) { if(err) { throw err; } finish(path); }); }); } else { finish(); } }
bundle.bundle({ debug: debug_opt }, function(err, src) { bundle.removeListener('file', collect_deps); if (watch) { watchFiles(bundle, dependencies, req_path); } if (err) { return callback(err); } if (otherError) { return callback(otherError); } var srcmap = undefined; var map_path = undefined; if (debug_opt) { // output sourcemap srcmap = convert.fromComment(src); src = convert.removeComments(src); srcmap.setProperty('file', req_path); map_path = req_path.replace(/.js$/, '.map.json'); } if (compress) { var ugly_opt = { fromString: true }; if (srcmap) { ugly_opt.inSourceMap = srcmap.toObject(), ugly_opt.outSourceMap = req_path } var result = uglifyjs.minify(src, ugly_opt); src = result.code; if (srcmap) { // prepare new sourcemap // we need to get the sources from bundled sources // uglify does not carry those through var srcs = srcmap.getProperty('sourcesContent'); srcmap = convert.fromJSON(result.map); srcmap.setProperty('sourcesContent', srcs); } } if (srcmap) { src += '//# sourceMappingURL=' + path.basename(map_path); maps[map_path] = srcmap.toObject(); } cache[req_path] = src; callback(null, src); });
function expectCompiledOutput(t, expected, actual, sourceDir) { // fix CRLFs on Windows; the expected output uses LFs actual = actual.replace(/\r\n/g, '\n'); expectSource(t, convert.removeMapFileComments(expected), convert.removeComments(actual)); expectSourcemap(t, convert.fromMapFileSource(expected, sourceDir).sourcemap, convert.fromSource(actual).sourcemap); }
parseInputSourceMap(code: string) { var opts = this.opts; if (opts.inputSourceMap !== false) { var inputMap = convertSourceMap.fromSource(code); if (inputMap) { opts.inputSourceMap = inputMap.toObject(); code = convertSourceMap.removeComments(code); } } return code; }
function _done(next) { var bundleMap = convertSourceMap.fromSource(buffer); var bundleMapConsumer = new SourceMapConsumer(bundleMap.sourcemap); var bundleMappings = getAllMappings(bundleMapConsumer); var generator = new SourceMapGenerator(); bundleMappings.forEach(function(bundleMap) { var bundleFile = bundleMap.source; var mapData = fileMaps[bundleFile]; if (!mapData) { return; } var sourceFileMappings = mapData.mappings; var bundleLine = bundleMap.generatedLine; var binJsLine = bundleMap.originalLine; var sourceMappings = sourceFileMappings.filter(function(m) { return m.generatedLine == binJsLine; }); sourceMappings.forEach(function(sourceMapping) { var sourceLine = sourceMapping.originalLine, sourceColumn = sourceMapping.originalColumn, source = sourceMapping.source, name = sourceMapping.name; sourceRelPath = mapData.sources[source]; generator.addMapping({ generated: { line: bundleLine, column: sourceColumn }, original: { line: sourceLine, column: sourceColumn }, name: name, source: sourceRelPath }); }); }); var updatedMap = convertSourceMap.fromJSON(generator.toString()); var bundle = convertSourceMap.removeComments(buffer); bundle += "\n" + updatedMap.toComment(); this.push(bundle); this.push(null); next(); }
Minifier.prototype.decoupleBundle = function (src) { if(typeof src != 'string') src = src.toString(); var map = convertSM.fromSource(src); // The source didn't have a sourcemap in it if(!map) { return false; } return { code: convertSM.removeComments(src) , map: new SMConsumer( map.toObject() ) }; };
bundler.bundle(bundle_opts, function(err, src) { if (err) { return cb(err); } var srcmap = convert.fromSource(src); var map = undefined; src = convert.removeComments(src); if (srcmap) { map = srcmap.toObject(); } cb(null, src, map); });
function separate(src, file, root, url) { var inlined = convert.fromSource(src); if (!inlined) return null; var json = inlined .setProperty('sourceRoot', root || '') .toJSON(2); url = url || path.basename(file); var newSrc = convert.removeComments(src); var comment = '//# sourceMappingURL=' + url; return { json: json, src: newSrc + '\n' + comment } }
create_bundle(files).bundle(bundle_opts, function(err, src) { if (err) { return next(err); } var srcmap = convert.fromSource(src); src = convert.removeComments(src); if (srcmap) { srcmap.setProperty('file', '/__zuul/test-bundle.js'); src += '//# sourceMappingURL=' + '/__zuul/test-bundle.map.json'; map = srcmap.toObject(); } res.send(src); });
ExecDevToolsFile.prototype.saveFile = function(filepath, content) { var inline = this.cleanSourceMap(content); process.fs.mkdirpSync(path.dirname(this.plugin.filepath)); if (inline !== ''){ content = convertSourceMap.removeComments(content); process.fs.writeFileSync(this.plugin.filepath, content + '\n' + inline); }else{ process.fs.writeFileSync(this.plugin.filepath, content); } this.devtoolsLive.streamFinished(this.plugin); return content; }
function expectOutput(t, actual, expectedFile) { t.notEqual(actual, null, 'Should have compiled output'); if (actual === null) return; var expected = fs.readFileSync(expectedFile).toString(); var sourceDir = path.dirname(expectedFile); // fix CRLFs on Windows; the expected output uses LFs actual = actual.replace(/\r\n/g, '\n'); expectSource(t, convert.removeMapFileComments(expected), convert.removeComments(actual)); expectSourcemap(t, convert.fromMapFileSource(expected, sourceDir).sourcemap, convert.fromSource(actual).sourcemap); }
b.bundle(function(err, buffer) { if (err) return cb(err); // Extract the source map, which Browserify includes as a comment var source = buffer.toString('utf8'); var map = convertSourceMap.fromSource(source).toJSON(); source = convertSourceMap.removeComments(source); if (!minify) return cb(null, source, map); var result = uglify.minify(source, { sourceMap: { content: map, includeSources: true } }); if (result.error) return cb(result.error); cb(null, result.code, result.map); });
function _getInlineSources(sources) { var debug = makeDebug(PLUGIN_NAME + ':init:internals:loadMaps:_getInlineSources'); sources.preExistingComment = utils.getInlinePreExisting(sources.content); // Try to read inline source map sources.map = convert.fromSource(sources.content, options.largeFile); if (!sources.map) return sources; sources.map = sources.map.toObject(); // sources in map are relative to the source file sources.path = path.dirname(file.path); if (!options.largeFile) { debug('comment REMOVED'); sources.content = convert.removeComments(sources.content); } }
export default function normalizeFile( pluginPasses: PluginPasses, options: Object, code: string, ast: ?(BabelNodeFile | BabelNodeProgram), ): File { code = `${code || ""}`; let shebang = null; let inputMap = null; if (options.inputSourceMap !== false) { inputMap = convertSourceMap.fromSource(code); if (inputMap) { code = convertSourceMap.removeComments(code); } else if (typeof options.inputSourceMap === "object") { inputMap = convertSourceMap.fromObject(options.inputSourceMap); } } const shebangMatch = shebangRegex.exec(code); if (shebangMatch) { shebang = shebangMatch[0]; code = code.replace(shebangRegex, ""); } if (ast) { if (ast.type === "Program") { ast = t.file(ast, [], []); } else if (ast.type !== "File") { throw new Error("AST root must be a Program or File node"); } } else { ast = parser(pluginPasses, options, code); } return new File(options, { code, ast, shebang, inputMap, }); }
b.bundle(options, function(err, source) { if (err) return cb(err); // Extract the source map, which Browserify includes as a comment var map = convertSourceMap.fromSource(source).toJSON(); source = convertSourceMap.removeComments(source); if (!minify) return cb(null, source, map); options.fromString = true; options.outSourceMap = 'map'; // If inSourceMap is a string it is assumed to be a filename, but passing // in as an object avoids the need to make a file options.inSourceMap = JSON.parse(map); var result = uglify.minify(source, options); // Uglify doesn't include the source content in the map, so copy over from // the map that browserify generates var mapObject = JSON.parse(result.map); mapObject.sourcesContent = options.inSourceMap.sourcesContent; cb(null, result.code, JSON.stringify(mapObject)); });
// Clear out autoprefixer's data and fix paths to match original function sourceMapRewriter(file, enc, callback) { if(file.isNull()) { return callback(null, file); } var content = file.contents.toString(); var sourceMap = convert.fromSource(content); var sources = sourceMap.getProperty('sources').slice(1); var sourcesContent = sourceMap.getProperty('sourcesContent').slice(1); sources = sources.map(rewriteUrl); sourceMap.setProperty('sourceRoot', null); sourceMap.setProperty('sources', [''].concat(sources)); sourceMap.setProperty('sourcesContent', [''].concat(sourcesContent)); content = convert.removeComments(content) + '\n/*# sourceMappingURL=data:application/json;base64,' + sourceMap.toBase64() + ' */'; file.contents = new Buffer(content); callback(null, file); }
Renderer.prototype.extractSourceMap = function (file) { var inline = convert_source_map_1.commentRegex.test(file.text); var external = convert_source_map_1.mapFileCommentRegex.test(file.text); if (inline) { var inlineSourceMap = convert_source_map_1.fromSource(file.text); return { source: convert_source_map_1.removeComments(file.text).replace(/\n\n$/, '\n'), map: inlineSourceMap, isInline: true, }; } else if (external) { var externalSourceMap = null; try { externalSourceMap = convert_source_map_1.fromMapFileSource(file.text, canonical_path_1.dirname(file.fileName)); } catch (e) { if (e.code === 'ENOENT') { console.warn("The external map file specified in the source code comment \"" + e.path + "\" was not found on the file system."); var mapPath = file.fileName + '.map'; if (canonical_path_1.basename(e.path) !== canonical_path_1.basename(mapPath) && fs_1.statSync(mapPath).isFile()) { console.warn("Guessing the map file name from the source file name: \"" + canonical_path_1.basename(mapPath) + "\""); try { externalSourceMap = convert_source_map_1.fromObject(JSON.parse(fs_1.readFileSync(mapPath, 'utf8'))); } catch (e) { console.error(e); } } } } return { source: convert_source_map_1.removeMapFileComments(file.text).replace(/\n\n$/, '\n'), map: externalSourceMap, isInline: false, }; } else { return { source: file.text, map: null, isInline: false }; } };
function transform (file, enc, doneTransform) { // pass through if file is null or already has a source map if (file.isNull() || file.sourceMap) { this.push(file); return doneTransform(); } if (file.isStream()) { return doneTransform(new Error('Streaming not supported')); } let fileContent = file.contents.toString() file.contents = new Buffer(convert.removeComments(fileContent), 'utf8') file.sourceMap = convert.fromSource(fileContent).toObject() //file.sourceMap.sources = file.sourceMap.sources.map(x => { //return `${file.sourceMap.sourceRoot}/${x}` //}) this.push(file) doneTransform() }
.bundle((err, contents) => { if (err) { if (err.message.match(/Cannot find module '.*_stream_0.js'/)) { return done(); // Browserify errors when we pass in no files when `newer` filters the input, we should count that as a success, though } return done(err); } const stringContent = contents.toString(); const file = new Vinyl({ contents, path: bundlePath }); console.log(`Fixing sourcemaps for ${file.path}`); // assumes contents is a Buffer, since that's what browserify yields const maps = convertMap.fromSource(stringContent).toObject(); delete maps.sourceRoot; maps.sources = maps.sources.map(s => path.resolve(s === "_stream_0.js" ? "built/local/_stream_0.js" : s)); // Strip browserify's inline comments away (could probably just let sorcery do this, but then we couldn't fix the paths) file.contents = new Buffer(convertMap.removeComments(stringContent)); const chain = sorcery.loadSync(bundlePath, { content: { "built/local/_stream_0.js": prebundledContent, [bundlePath]: stringContent }, sourcemaps: { "built/local/_stream_0.js": originalMap, [bundlePath]: maps, "node_modules/source-map-support/source-map-support.js": undefined, } }); const finalMap = chain.apply(); file.sourceMap = finalMap; const stream = through2.obj((file, enc, callback) => { return callback(/*err*/ undefined, file); }); stream.pipe(sourcemaps.write(".", { includeContent: false })) .pipe(gulp.dest(".")) .on("end", done) .on("error", done); stream.write(file); stream.end(); });
b.bundle(function (err, buf) { if (err) { bygglib.logger.error('browserify', err.message); return; } bundle.watcher.watch(bundle.watched); // Result var outputNode = bygglib.tree.cloneNode(entrypointNode); var outputPrefix = path.dirname(entrypointNode.name) + '/'; outputPrefix = (outputPrefix === './') ? '' : outputPrefix; outputNode.name = outputPrefix + path.basename(entrypointNode.name, path.extname(entrypointNode.name)) + '.js'; outputNode.metadata.mime = 'application/javascript'; var data = buf.toString('utf-8'); var outputBundle = convertSourceMap.removeComments(data); outputNode.data = new Buffer(outputBundle, 'utf-8'); // Source map var sourceMap = convertSourceMap.fromSource(data).toObject(); sourceMap.sources = sourceMap.sources.map(function (source) { return (source[0] === '/') ? path.relative(entrypointNode.base, source) : source; }); outputNode = bygglib.tree.sourceMap.set(outputNode, sourceMap, { sourceBase: outputPrefix }); bygglib.logger.log('browserify', 'Bundled ' + outputNode.name, new Date() - start); // Push upstream if required if (bundle.outputNode === undefined) { processed++; } bundle.outputNode = outputNode; if (processed === tree.nodes.length) { output.push(bygglib.tree(bundles.map(function (bundle) { return bundle.outputNode; }))); } });
.bundle(function(err, src) { t.error(err, 'no error') src = src.toString('utf8') var expected = fs.readFileSync(path.join(dirpath, file.replace('fixture', 'expected')), 'utf8'), expectedMap = JSON.parse(fs.readFileSync(path.join(dirpath, file.replace('fixture', 'map')), 'utf8')), transformed = convert.removeComments(src).trim() .split('\n').slice(1, -2) // remove prelude and eplilogue .join('\n') // verify t.equal(transformed, expected.trim(), 'equals expected') var map = convert.fromSource(src).toJSON() var con = new SourceMapConsumer(map) expectedMap.forEach(function(m) { var origPos = con.originalPositionFor({line: m[1][0], column: m[1][1]}) t.equal(origPos.line, m[0][0]) t.equal(origPos.column, m[0][1]) }) // console.log(fs.readFileSync(path.join(dirpath, file), 'utf8')); // console.log(src); // con.eachMapping(e => console.log(e)) })