{"id":23108591,"url":"https://github.com/venryx/webpack-basic-sourcemap","last_synced_at":"2025-04-03T23:14:19.561Z","repository":{"id":57397380,"uuid":"68058067","full_name":"Venryx/webpack-basic-sourcemap","owner":"Venryx","description":"Parses module start-lines in bundle, embeds the info into the JS, then adds an \u003cError\u003e.Stack getter which calculates the original files/lines.","archived":false,"fork":false,"pushed_at":"2017-04-11T12:12:28.000Z","size":14,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T05:35:07.082Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Venryx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-13T00:05:45.000Z","updated_at":"2016-11-21T04:17:33.000Z","dependencies_parsed_at":"2022-09-16T14:43:29.731Z","dependency_job_id":null,"html_url":"https://github.com/Venryx/webpack-basic-sourcemap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Venryx%2Fwebpack-basic-sourcemap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Venryx%2Fwebpack-basic-sourcemap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Venryx%2Fwebpack-basic-sourcemap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Venryx%2Fwebpack-basic-sourcemap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Venryx","download_url":"https://codeload.github.com/Venryx/webpack-basic-sourcemap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247092393,"owners_count":20882218,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-17T01:28:16.319Z","updated_at":"2025-04-03T23:14:19.543Z","avatar_url":"https://github.com/Venryx.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# webpack-basic-sourcemap\n\n**DEPRECATED**\n\nInstead, use the solution here: https://github.com/stacktracejs/stacktrace.js/issues/188\n\nI might make a second version of this that includes line-numbers as comments in addition to file start-lines (based on a project-specific solution),\n\twhich seems to yield better performance than the solution above; however, for now, the above is recommended over this project.\n\n## Description\n\nParses module start-lines in bundle, embeds the info into the JS, then adds an \\\u003cError\\\u003e.Stack getter which calculates the original files/lines.\n\nIt's an alternative to regular source-maps, for when you need the source stack-traces in the JS code itself, and synchronously.\n\n## Notes\n\n##### Transpilers should retain line-breaks\n\nIf you're using a transpiler in Webpack, you need to make sure it's set to retain the line-breaks of the original files.\n\nFor Babel, this means creating a `.babelrc` file with:\n```\n\t\"retainLines\":true\n```\n\n## Usage\n1) Run `npm install --save webpack-basic-sourcemap`.  \n2) Add the following to your `webpack.config` file:  \n```\nvar WebpackBasicSourcemap = require(\"webpack-basic-sourcemap\");\n// ...\n// module.exports = {\n// ...\n\tplugins: [\n\t\tnew WebpackBasicSourcemap(),\n// ...\n//};\n```\n3) Add the following to your JS start file:  \n```\n// general polyfills\nObject.defineProperty(Object.prototype, \"Props\", {enumerable: false, get: function() {\n\tvar result = [];\n\tvar i = 0;\n\tfor (var propName in this)\n\t\tresult.push({index: i++, name: propName, value: this[propName]});\n\treturn result;\n};\nObject.defineProperty(Array.prototype, \"Last\", {enumerable: false, value: function(matchFunc = null) {\n\tif (matchFunc) {\n        for (var i = this.length - 1; i \u003e= 0; i--)\n            if (matchFunc.call(this[i], this[i]))\n                return this[i];\n        return null;\n    }\n    return this[this.length - 1];\n}});\n\nfunction GetSourceStackEntryInfo(bundleName, bundleLine) {\n\tvar bundle_modStartLinesInBundle = window[\"ModuleFileStartLines_\" + bundleName];\n\tif (bundle_modStartLinesInBundle == null)\n\t\treturn {modulePath: `[Can't find bundle with name: ${bundleName}]`, moduleFileName: `[Can't find bundle with name: ${bundleName}]`};\n    var module = bundle_modStartLinesInBundle.Props.Last(a=\u003ea.value \u003c= bundleLine);\n\tif (module == null)\n\t\treturn {modulePath: `[Can't find module for bundle ${bundleName}, line: ${bundleLine}]`,\n\t\t\tmoduleFileName: `[Can't find module for bundle ${bundleName}, line: ${bundleLine}]`};\n\t\n\tvar {name: modulePath, value: moduleStartLine} = module;\n\tvar moduleFileName = modulePath.substr(modulePath.lastIndexOf(\"/\") + 1);\n\tvar moduleLine = bundleLine - moduleStartLine;\n\treturn {modulePath, moduleFileName, moduleLine};\n}\n// gets the source stack-trace of the error (i.e. the stack-trace as it would be without the js-files being bundled into one)\nObject.defineProperty(Error.prototype, \"Stack\", {enumerable: false, get: function() {\n\tvar options = {\n\t\tbundlePath: true, // turn off for just file-name\n\t\tmodulePath: true, // turn off for just file-name\n\t};\n\n\tvar rawStack = this.stack;\n\tvar oldLines = rawStack.split(\"\\n\");\n\tvar newLines = oldLines.map(oldLine=\u003e {\n\t\tlet lineParts = oldLine.match(/^(.+?)\\((.+?\\.js):([0-9]+)(?::([0-9]+))?\\)$/);\n\t\tif (lineParts == null) return oldLine;\n\n\t\tlet [, beforeText, bundlePath, bundleLine, bundleColumn] = lineParts;\n\t\tlet bundleFileName = bundlePath.substring(bundlePath.lastIndexOf(\"/\") + 1);\n\t\tlet bundleName = bundleFileName.substring(0, bundleFileName.lastIndexOf(\".\"));\n\n\t\tlet {modulePath, moduleFileName, moduleLine} = GetSourceStackEntryInfo(bundleName, bundleLine);\n\t\treturn `${beforeText}(${(options.bundlePath ? encodeURI(bundlePath) : bundleFileName)}:${bundleLine}${bundleColumn ? \":\" + bundleColumn : \"\"})${\"\"\n\t\t\t\t} (${(options.modulePath ? \"file:///\" + encodeURI(modulePath) : moduleFileName)}:${moduleLine}${bundleColumn ? \":\" + bundleColumn : \"\"})`;\n\t});\n\treturn newLines.join(\"\\n\");\n}});\n```\n4) Whenever you need the source stack-trace of an error, just call:\n```\nerror.Stack\n```\n\n## Alternatives\nIf you don't need the source stack-trace synchronously, you can use a library that calculates source stack-traces from regular source-maps: (though this takes longer)\n* https://github.com/stacktracejs/stacktrace.js","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvenryx%2Fwebpack-basic-sourcemap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvenryx%2Fwebpack-basic-sourcemap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvenryx%2Fwebpack-basic-sourcemap/lists"}