Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
Get rid of the browser version and inline some dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Mar 21, 2020
1 parent 34c23e3 commit df81125
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 644 deletions.
30 changes: 0 additions & 30 deletions bower.json

This file was deleted.

29 changes: 0 additions & 29 deletions component.json

This file was deleted.

25 changes: 0 additions & 25 deletions generate-source-map-resolve.js

This file was deleted.

8 changes: 0 additions & 8 deletions lib/decode-uri-component.js

This file was deleted.

9 changes: 0 additions & 9 deletions lib/resolve-url.js

This file was deleted.

61 changes: 48 additions & 13 deletions lib/source-map-resolve-node.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
var sourceMappingURL = require("source-map-url")
var atob = require("atob")
var urlLib = require("url")
var pathLib = require("path")
var decodeUriComponentLib = require("decode-uri-component")

var resolveUrl = require("./resolve-url")
var decodeUriComponent = require("./decode-uri-component")
var urix = require("urix")
var atob = require("atob")


function resolveUrl(/* ...urls */) {
return Array.prototype.reduce.call(arguments, function(resolved, nextUrl) {
return urlLib.resolve(resolved, nextUrl)
})
}

function convertWindowsPath(aPath) {
return pathLib.sep === "\\" ? aPath.replace(/\\/g, "/").replace(/^[a-z]:\/?/i, "/") : aPath
}

function customDecodeUriComponent(string) {
// `decodeUriComponentLib` turns `+` into ` `, but that's not wanted.
return decodeUriComponentLib(string.replace(/\+/g, "%2B"))
}

function callbackAsync(callback, error, result) {
setImmediate(function() { callback(error, result) })
Expand All @@ -21,7 +34,7 @@ function parseMapToJSON(string, data) {
}

function readSync(read, url, data) {
var readUrl = decodeUriComponent(url)
var readUrl = customDecodeUriComponent(url)
try {
return String(read(readUrl))
} catch (error) {
Expand All @@ -32,6 +45,28 @@ function readSync(read, url, data) {



var innerRegex = /[#@] sourceMappingURL=([^\s'"]*)/

var sourceMappingURLRegex = RegExp(
"(?:" +
"/\\*" +
"(?:\\s*\r?\n(?://)?)?" +
"(?:" + innerRegex.source + ")" +
"\\s*" +
"\\*/" +
"|" +
"//(?:" + innerRegex.source + ")" +
")" +
"\\s*"
)

function getSourceMappingUrl(code) {
var match = code.match(sourceMappingURLRegex)
return match ? match[1] || match[2] || "" : null
}



function resolveSourceMap(code, codeUrl, read, callback) {
var mapData
try {
Expand All @@ -42,7 +77,7 @@ function resolveSourceMap(code, codeUrl, read, callback) {
if (!mapData || mapData.map) {
return callbackAsync(callback, null, mapData)
}
var readUrl = decodeUriComponent(mapData.url)
var readUrl = customDecodeUriComponent(mapData.url)
read(readUrl, function(error, result) {
if (error) {
error.sourceMapData = mapData
Expand Down Expand Up @@ -109,9 +144,9 @@ function decodeBase64String(b64) {
}

function resolveSourceMapHelper(code, codeUrl) {
codeUrl = urix(codeUrl)
codeUrl = convertWindowsPath(codeUrl)

var url = sourceMappingURL.getFrom(code)
var url = getSourceMappingUrl(code)
if (!url) {
return null
}
Expand Down Expand Up @@ -184,7 +219,7 @@ function resolveSources(map, mapUrl, read, options, callback) {
result.sourcesContent[index] = sourceContent
callbackAsync(done, null)
} else {
var readUrl = decodeUriComponent(fullUrl)
var readUrl = customDecodeUriComponent(fullUrl)
read(readUrl, function(error, source) {
result.sourcesContent[index] = error ? error : String(source)
done()
Expand All @@ -209,7 +244,7 @@ function resolveSourcesSync(map, mapUrl, read, options) {
if (typeof sourceContent === "string") {
result.sourcesContent[index] = sourceContent
} else {
var readUrl = decodeUriComponent(fullUrl)
var readUrl = customDecodeUriComponent(fullUrl)
try {
result.sourcesContent[index] = String(read(readUrl))
} catch (error) {
Expand All @@ -226,7 +261,7 @@ var endingSlash = /\/?$/

function resolveSourcesHelper(map, mapUrl, options, fn) {
options = options || {}
mapUrl = urix(mapUrl)
mapUrl = convertWindowsPath(mapUrl)
var fullUrl
var sourceContent
var sourceRoot
Expand Down Expand Up @@ -267,7 +302,7 @@ function resolve(code, codeUrl, read, options, callback) {
sourcesRelativeTo: mapUrl,
map: null
}
var readUrl = decodeUriComponent(mapUrl)
var readUrl = customDecodeUriComponent(mapUrl)
read(readUrl, function(error, result) {
if (error) {
error.sourceMapData = data
Expand Down
15 changes: 0 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,17 @@
],
"repository": "lydell/source-map-resolve",
"main": "lib/source-map-resolve-node.js",
"browser": "source-map-resolve.js",
"files": [
"lib",
"source-map-resolve.js"
"lib"
],
"scripts": {
"lint": "jshint lib/ test/",
"unit": "node test/source-map-resolve.js && node test/windows.js",
"test": "npm run lint && npm run unit",
"build": "node generate-source-map-resolve.js"
"test": "npm run lint && npm run unit"
},
"dependencies": {
"atob": "^2.1.2",
"decode-uri-component": "^0.2.0",
"resolve-url": "^0.2.1",
"source-map-url": "^0.4.0",
"urix": "^0.1.0"
"decode-uri-component": "^0.2.0"
},
"devDependencies": {
"Base64": "1.1.0",
Expand Down
40 changes: 1 addition & 39 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,7 @@ sourceMapResolve.resolve(code, "/js/foo.js", fs.readFile, function(error, result
Installation
============

- `npm install source-map-resolve`
- `bower install source-map-resolve`
- `component install lydell/source-map-resolve`

Works with CommonJS, AMD and browser globals, through UMD.

Note: This module requires `setImmediate` and `atob`.
Use polyfills if needed, such as:

- <https://github.com/NobleJS/setImmediate>
- <https://github.com/davidchambers/Base64.js>

`npm install source-map-resolve`

Usage
=====
Expand Down Expand Up @@ -198,33 +187,6 @@ _you_ give the generated code to the module), it’s up to you to look for such
header when you retrieve the file (should the need arise).


Development
===========

Tests
-----

First off, run `npm install` to install testing modules and browser polyfills.

`npm test` lints the code and runs the test suite in Node.js.

x-package.json5
---------------

package.json, component.json and bower.json are all generated from
x-package.json5 by using [`xpkg`]. Only edit x-package.json5, and remember to
run `xpkg` before commiting!

[`xpkg`]: https://github.com/kof/node-xpkg

Generating the browser version
------------------------------

source-map-resolve.js is generated from source-map-resolve-node.js and
source-map-resolve-template.js. Only edit the two latter files, _not_
source-map-resolve.js! To generate it, run `npm run build`.


License
=======

Expand Down
Loading

0 comments on commit df81125

Please sign in to comment.