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

Commit df81125

Browse files
committedMar 21, 2020
Get rid of the browser version and inline some dependencies
1 parent 34c23e3 commit df81125

16 files changed

+100
-644
lines changed
 

‎bower.json

-30
This file was deleted.

‎component.json

-29
This file was deleted.

‎generate-source-map-resolve.js

-25
This file was deleted.

‎lib/decode-uri-component.js

-8
This file was deleted.

‎lib/resolve-url.js

-9
This file was deleted.

‎lib/source-map-resolve-node.js

+48-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
var sourceMappingURL = require("source-map-url")
1+
var atob = require("atob")
2+
var urlLib = require("url")
3+
var pathLib = require("path")
4+
var decodeUriComponentLib = require("decode-uri-component")
25

3-
var resolveUrl = require("./resolve-url")
4-
var decodeUriComponent = require("./decode-uri-component")
5-
var urix = require("urix")
6-
var atob = require("atob")
76

87

8+
function resolveUrl(/* ...urls */) {
9+
return Array.prototype.reduce.call(arguments, function(resolved, nextUrl) {
10+
return urlLib.resolve(resolved, nextUrl)
11+
})
12+
}
13+
14+
function convertWindowsPath(aPath) {
15+
return pathLib.sep === "\\" ? aPath.replace(/\\/g, "/").replace(/^[a-z]:\/?/i, "/") : aPath
16+
}
17+
18+
function customDecodeUriComponent(string) {
19+
// `decodeUriComponentLib` turns `+` into ` `, but that's not wanted.
20+
return decodeUriComponentLib(string.replace(/\+/g, "%2B"))
21+
}
922

1023
function callbackAsync(callback, error, result) {
1124
setImmediate(function() { callback(error, result) })
@@ -21,7 +34,7 @@ function parseMapToJSON(string, data) {
2134
}
2235

2336
function readSync(read, url, data) {
24-
var readUrl = decodeUriComponent(url)
37+
var readUrl = customDecodeUriComponent(url)
2538
try {
2639
return String(read(readUrl))
2740
} catch (error) {
@@ -32,6 +45,28 @@ function readSync(read, url, data) {
3245

3346

3447

48+
var innerRegex = /[#@] sourceMappingURL=([^\s'"]*)/
49+
50+
var sourceMappingURLRegex = RegExp(
51+
"(?:" +
52+
"/\\*" +
53+
"(?:\\s*\r?\n(?://)?)?" +
54+
"(?:" + innerRegex.source + ")" +
55+
"\\s*" +
56+
"\\*/" +
57+
"|" +
58+
"//(?:" + innerRegex.source + ")" +
59+
")" +
60+
"\\s*"
61+
)
62+
63+
function getSourceMappingUrl(code) {
64+
var match = code.match(sourceMappingURLRegex)
65+
return match ? match[1] || match[2] || "" : null
66+
}
67+
68+
69+
3570
function resolveSourceMap(code, codeUrl, read, callback) {
3671
var mapData
3772
try {
@@ -42,7 +77,7 @@ function resolveSourceMap(code, codeUrl, read, callback) {
4277
if (!mapData || mapData.map) {
4378
return callbackAsync(callback, null, mapData)
4479
}
45-
var readUrl = decodeUriComponent(mapData.url)
80+
var readUrl = customDecodeUriComponent(mapData.url)
4681
read(readUrl, function(error, result) {
4782
if (error) {
4883
error.sourceMapData = mapData
@@ -109,9 +144,9 @@ function decodeBase64String(b64) {
109144
}
110145

111146
function resolveSourceMapHelper(code, codeUrl) {
112-
codeUrl = urix(codeUrl)
147+
codeUrl = convertWindowsPath(codeUrl)
113148

114-
var url = sourceMappingURL.getFrom(code)
149+
var url = getSourceMappingUrl(code)
115150
if (!url) {
116151
return null
117152
}
@@ -184,7 +219,7 @@ function resolveSources(map, mapUrl, read, options, callback) {
184219
result.sourcesContent[index] = sourceContent
185220
callbackAsync(done, null)
186221
} else {
187-
var readUrl = decodeUriComponent(fullUrl)
222+
var readUrl = customDecodeUriComponent(fullUrl)
188223
read(readUrl, function(error, source) {
189224
result.sourcesContent[index] = error ? error : String(source)
190225
done()
@@ -209,7 +244,7 @@ function resolveSourcesSync(map, mapUrl, read, options) {
209244
if (typeof sourceContent === "string") {
210245
result.sourcesContent[index] = sourceContent
211246
} else {
212-
var readUrl = decodeUriComponent(fullUrl)
247+
var readUrl = customDecodeUriComponent(fullUrl)
213248
try {
214249
result.sourcesContent[index] = String(read(readUrl))
215250
} catch (error) {
@@ -226,7 +261,7 @@ var endingSlash = /\/?$/
226261

227262
function resolveSourcesHelper(map, mapUrl, options, fn) {
228263
options = options || {}
229-
mapUrl = urix(mapUrl)
264+
mapUrl = convertWindowsPath(mapUrl)
230265
var fullUrl
231266
var sourceContent
232267
var sourceRoot
@@ -267,7 +302,7 @@ function resolve(code, codeUrl, read, options, callback) {
267302
sourcesRelativeTo: mapUrl,
268303
map: null
269304
}
270-
var readUrl = decodeUriComponent(mapUrl)
305+
var readUrl = customDecodeUriComponent(mapUrl)
271306
read(readUrl, function(error, result) {
272307
if (error) {
273308
error.sourceMapData = data

‎package-lock.json

-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+3-9
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,17 @@
1919
],
2020
"repository": "lydell/source-map-resolve",
2121
"main": "lib/source-map-resolve-node.js",
22-
"browser": "source-map-resolve.js",
2322
"files": [
24-
"lib",
25-
"source-map-resolve.js"
23+
"lib"
2624
],
2725
"scripts": {
2826
"lint": "jshint lib/ test/",
2927
"unit": "node test/source-map-resolve.js && node test/windows.js",
30-
"test": "npm run lint && npm run unit",
31-
"build": "node generate-source-map-resolve.js"
28+
"test": "npm run lint && npm run unit"
3229
},
3330
"dependencies": {
3431
"atob": "^2.1.2",
35-
"decode-uri-component": "^0.2.0",
36-
"resolve-url": "^0.2.1",
37-
"source-map-url": "^0.4.0",
38-
"urix": "^0.1.0"
32+
"decode-uri-component": "^0.2.0"
3933
},
4034
"devDependencies": {
4135
"Base64": "1.1.0",

‎readme.md

+1-39
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,7 @@ sourceMapResolve.resolve(code, "/js/foo.js", fs.readFile, function(error, result
6060
Installation
6161
============
6262

63-
- `npm install source-map-resolve`
64-
- `bower install source-map-resolve`
65-
- `component install lydell/source-map-resolve`
66-
67-
Works with CommonJS, AMD and browser globals, through UMD.
68-
69-
Note: This module requires `setImmediate` and `atob`.
70-
Use polyfills if needed, such as:
71-
72-
- <https://github.com/NobleJS/setImmediate>
73-
- <https://github.com/davidchambers/Base64.js>
74-
63+
`npm install source-map-resolve`
7564

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

200189

201-
Development
202-
===========
203-
204-
Tests
205-
-----
206-
207-
First off, run `npm install` to install testing modules and browser polyfills.
208-
209-
`npm test` lints the code and runs the test suite in Node.js.
210-
211-
x-package.json5
212-
---------------
213-
214-
package.json, component.json and bower.json are all generated from
215-
x-package.json5 by using [`xpkg`]. Only edit x-package.json5, and remember to
216-
run `xpkg` before commiting!
217-
218-
[`xpkg`]: https://github.com/kof/node-xpkg
219-
220-
Generating the browser version
221-
------------------------------
222-
223-
source-map-resolve.js is generated from source-map-resolve-node.js and
224-
source-map-resolve-template.js. Only edit the two latter files, _not_
225-
source-map-resolve.js! To generate it, run `npm run build`.
226-
227-
228190
License
229191
=======
230192

‎source-map-resolve.js

-348
This file was deleted.

‎source-map-resolve.js.template

-19
This file was deleted.

‎test/common.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
function u(url) {
1+
function u1(url) {
22
return "code\n/*# sourceMappingURL=" + url + " */"
33
}
44

5+
function u2(url) {
6+
return "code\n//# sourceMappingURL=" + url
7+
}
8+
9+
function u3(url) {
10+
return "code\n/*\n# sourceMappingURL=" + url + "\n*/"
11+
}
12+
13+
function u4(url) {
14+
return "code\n/*\n//# sourceMappingURL=" + url + "\n*/"
15+
}
16+
517
function read(x) {
618
return function() {
719
return x
@@ -17,7 +29,10 @@ function identity(x) {
1729
}
1830

1931
module.exports = {
20-
u: u,
32+
u1: u1,
33+
u2: u2,
34+
u3: u3,
35+
u4: u4,
2136
read: read,
2237
Throws: Throws,
2338
identity: identity

‎test/read.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var test = require("tape")
22
var asyncify = require("simple-asyncify")
33
var common = require("./common")
4-
var u = common.u
4+
var u1 = common.u1
55

66
var sourceMapResolve = require("../")
77

@@ -35,7 +35,7 @@ function testResolveSourceMap(method, sync) {
3535
"built files/operators map.json": "{}"
3636
})
3737

38-
method(u(mapUrl), codeUrl, read, function(error) {
38+
method(u1(mapUrl), codeUrl, read, function(error) {
3939
t.error(error)
4040
})
4141

@@ -90,7 +90,7 @@ function testResolve(method, sync) {
9090
"source files/operators:+-<>%.coffee": "source code"
9191
})
9292

93-
method(u(mapUrl), codeUrl, read, function(error) {
93+
method(u1(mapUrl), codeUrl, read, function(error) {
9494
t.error(error)
9595
})
9696

‎test/source-map-resolve.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
var test = require("tape")
22
var asyncify = require("simple-asyncify")
33
var common = require("./common")
4-
var u = common.u
4+
var u1 = common.u1
5+
var u2 = common.u2
6+
var u3 = common.u3
7+
var u4 = common.u4
58
var read = common.read
69
var Throws = common.Throws
710
var identity = common.identity
@@ -70,30 +73,30 @@ map.simpleString = JSON.stringify(map.simple)
7073
map.XSSIsafe = ")]}'" + map.simpleString
7174

7275
var code = {
73-
fileRelative: u("foo.js.map"),
74-
domainRelative: u("/foo.js.map"),
75-
schemeRelative: u("//foo.org/foo.js.map"),
76-
absolute: u("https://foo.org/foo.js.map"),
77-
dataUri: u("data:application/json," +
76+
fileRelative: u1("foo.js.map"),
77+
domainRelative: u2("/foo.js.map"),
78+
schemeRelative: u3("//foo.org/foo.js.map"),
79+
absolute: u4("https://foo.org/foo.js.map"),
80+
dataUri: u1("data:application/json," +
7881
"%7B%22mappings%22%3A%22AAAA%22%2C%22sources%22%3A%5B%22" +
7982
"foo.js%22%5D%2C%22names%22%3A%5B%5D%7D"),
80-
base64: u("data:application/json;base64," +
83+
base64: u2("data:application/json;base64," +
8184
"eyJtYXBwaW5ncyI6IkFBQUEiLCJzb3VyY2VzIjpbImZvby5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyLkuK3mlofwn5iKIl0sIm5hbWVzIjpbXX0="), // jshint ignore:line
82-
base64InvalidUtf8: u("data:application/json;base64,abc"),
83-
dataUriText: u("data:text/json," +
85+
base64InvalidUtf8: u3("data:application/json;base64,abc"),
86+
dataUriText: u4("data:text/json," +
8487
"%7B%22mappings%22%3A%22AAAA%22%2C%22sources%22%3A%5B%22" +
8588
"foo.js%22%5D%2C%22names%22%3A%5B%5D%7D"),
86-
dataUriParameter: u("data:application/json;charset=UTF-8;foo=bar," +
89+
dataUriParameter: u1("data:application/json;charset=UTF-8;foo=bar," +
8790
"%7B%22mappings%22%3A%22AAAA%22%2C%22sources%22%3A%5B%22" +
8891
"foo.js%22%5D%2C%22names%22%3A%5B%5D%7D"),
89-
dataUriNoMime: u("data:,foo"),
90-
dataUriInvalidMime: u("data:text/html,foo"),
91-
dataUriInvalidJSON: u("data:application/json,foo"),
92-
dataUriInvalidCode: u("data:application/json,%"),
93-
dataUriXSSIsafe: u("data:application/json," + ")%5D%7D%27" +
92+
dataUriNoMime: u2("data:,foo"),
93+
dataUriInvalidMime: u3("data:text/html,foo"),
94+
dataUriInvalidJSON: u4("data:application/json,foo"),
95+
dataUriInvalidCode: u1("data:application/json,%"),
96+
dataUriXSSIsafe: u2("data:application/json," + ")%5D%7D%27" +
9497
"%7B%22mappings%22%3A%22AAAA%22%2C%22sources%22%3A%5B%22" +
9598
"foo.js%22%5D%2C%22names%22%3A%5B%5D%7D"),
96-
dataUriEmpty: u("data:"),
99+
dataUriEmpty: u3("data:"),
97100
noMap: ""
98101
}
99102

‎test/windows.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ var path = require("path")
22
var test = require("tape")
33
var asyncify = require("simple-asyncify")
44
var common = require("./common")
5-
var u = common.u
5+
var u1 = common.u1
6+
var u2 = common.u2
7+
var u3 = common.u3
68
var read = common.read
79
var identity = common.identity
810

@@ -26,7 +28,7 @@ function testResolveSourceMap(method, sync) {
2628
var map = {}
2729
var readMap = wrap(read(JSON.stringify(map)))
2830

29-
method(u("foo.js.map"), codeUrl, readMap, function(error, result) {
31+
method(u1("foo.js.map"), codeUrl, readMap, function(error, result) {
3032
t.error(error)
3133
t.deepEqual(result, {
3234
sourceMappingURL: "foo.js.map",
@@ -36,7 +38,7 @@ function testResolveSourceMap(method, sync) {
3638
})
3739
})
3840

39-
method(u("/foo.js.map"), codeUrl, readMap, function(error, result) {
41+
method(u2("/foo.js.map"), codeUrl, readMap, function(error, result) {
4042
t.error(error)
4143
t.deepEqual(result, {
4244
sourceMappingURL: "/foo.js.map",
@@ -46,7 +48,7 @@ function testResolveSourceMap(method, sync) {
4648
})
4749
})
4850

49-
method(u("../foo.js.map"), codeUrl, readMap, function(error, result) {
51+
method(u3("../foo.js.map"), codeUrl, readMap, function(error, result) {
5052
t.error(error)
5153
t.deepEqual(result, {
5254
sourceMappingURL: "../foo.js.map",
@@ -119,7 +121,7 @@ function testResolve(method, sync) {
119121
}
120122
var readMap = wrapMap(read(JSON.stringify(map)), identity)
121123

122-
method(u("foo.js.map"), codeUrl, readMap, function(error, result) {
124+
method(u1("foo.js.map"), codeUrl, readMap, function(error, result) {
123125
t.error(error)
124126
t.deepEqual(result, {
125127
sourceMappingURL: "foo.js.map",
@@ -131,7 +133,7 @@ function testResolve(method, sync) {
131133
})
132134
})
133135

134-
method(u("/foo.js.map"), codeUrl, readMap, function(error, result) {
136+
method(u2("/foo.js.map"), codeUrl, readMap, function(error, result) {
135137
t.error(error)
136138
t.deepEqual(result, {
137139
sourceMappingURL: "/foo.js.map",
@@ -143,7 +145,7 @@ function testResolve(method, sync) {
143145
})
144146
})
145147

146-
method(u("../foo.js.map"), codeUrl, readMap, function(error, result) {
148+
method(u3("../foo.js.map"), codeUrl, readMap, function(error, result) {
147149
t.error(error)
148150
t.deepEqual(result, {
149151
sourceMappingURL: "../foo.js.map",

‎x-package.json5

-72
This file was deleted.

0 commit comments

Comments
 (0)
This repository has been archived.