Skip to content

Commit 158cd47

Browse files
feat: fetch remote resources
1 parent 1c259ea commit 158cd47

14 files changed

+267
-17
lines changed

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,45 @@ module.exports = {
168168
};
169169
```
170170

171+
### `unresolveSourceFetcher`
172+
173+
Type: `Function`
174+
Default: `undefined`
175+
176+
The option allows you to fetching the remote content.
177+
178+
**webpack.config.js**
179+
180+
```js
181+
module.exports = {
182+
module: {
183+
rules: [
184+
{
185+
test: /\.js$/,
186+
enforce: 'pre',
187+
use: [
188+
{
189+
loader: 'source-map-loader',
190+
options: {
191+
async unresolveSourceFetcher(url) {
192+
if (/^https?:\/\//i.test(url)) {
193+
const response = await fetch(url);
194+
const result = await response.text();
195+
196+
return result;
197+
}
198+
199+
throw new Error(`${url} is not supported`);
200+
},
201+
},
202+
},
203+
],
204+
},
205+
],
206+
},
207+
};
208+
```
209+
171210
## Contributing
172211

173212
Please take a moment to read our contributing guidelines if you haven't yet done so.

package-lock.json

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

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"jest": "^26.0.1",
6767
"lint-staged": "^10.2.2",
6868
"memfs": "^3.1.2",
69+
"node-fetch": "^2.6.0",
6970
"npm-run-all": "^4.1.5",
7071
"prettier": "^2.0.5",
7172
"standard-version": "^8.0.0",

src/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export default async function loader(input, inputMap) {
3939
({ sourceURL, sourceContent } = await fetchFromURL(
4040
this,
4141
this.context,
42-
sourceMappingURL
42+
sourceMappingURL,
43+
'',
44+
false,
45+
options.unresolveSourceFetcher
4346
));
4447
} catch (error) {
4548
const brokenMapUrlReporter = getErrorReporter(
@@ -101,7 +104,8 @@ export default async function loader(input, inputMap) {
101104
context,
102105
source,
103106
map.sourceRoot,
104-
skipReading
107+
skipReading,
108+
options.unresolveSourceFetcher
105109
));
106110
} catch (error) {
107111
const brokenSourceUrlReporter = getErrorReporter(

src/options.json

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
},
1010
"brokenSourceUrlReportType": {
1111
"enum": ["ignore", "warning", "error"]
12+
},
13+
"unresolveSourceFetcher": {
14+
"instanceof": "Function"
1215
}
1316
},
1417
"additionalProperties": false

src/utils.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ async function fetchFromURL(
137137
context,
138138
url,
139139
sourceRoot,
140-
skipReading = false
140+
skipReading = false,
141+
unresolveSourceFetcher
141142
) {
142143
// 1. It's an absolute url and it is not `windows` path like `C:\dir\file`
143144
if (/^[a-z][a-z0-9+.-]*:/i.test(url) && !path.win32.isAbsolute(url)) {
@@ -163,6 +164,16 @@ async function fetchFromURL(
163164
return { sourceURL, sourceContent };
164165
}
165166

167+
if (skipReading) {
168+
return { sourceURL: url, sourceContent: '' };
169+
}
170+
171+
if (unresolveSourceFetcher) {
172+
const sourceContent = await unresolveSourceFetcher(url);
173+
174+
return { sourceURL: url, sourceContent };
175+
}
176+
166177
throw new Error(`Absolute '${url}' URL is not supported`);
167178
}
168179

test/__snapshots__/loader.test.js.snap

+84-6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ exports[`source-map-loader should leave normal files with fake source-map untouc
8282

8383
exports[`source-map-loader should leave normal files with fake source-map untouched: warnings 1`] = `Array []`;
8484

85+
exports[`source-map-loader should not resolve SourceMap.sources to http: css 1`] = `
86+
"// console.log('with SourceMap')
87+
"
88+
`;
89+
90+
exports[`source-map-loader should not resolve SourceMap.sources to http: errors 1`] = `Array []`;
91+
92+
exports[`source-map-loader should not resolve SourceMap.sources to http: map 1`] = `
93+
Object {
94+
"file": "sources-http.js",
95+
"mappings": "AAAA",
96+
"sources": Array [
97+
"https://unresolved1.com/",
98+
"https://unresolved1.com/",
99+
],
100+
"version": 3,
101+
}
102+
`;
103+
104+
exports[`source-map-loader should not resolve SourceMap.sources to http: warnings 1`] = `
105+
Array [
106+
"ModuleWarning: Module Warning (from \`replaced original path\`):
107+
body used already for: ",
108+
"ModuleWarning: Module Warning (from \`replaced original path\`):
109+
body used already for: ",
110+
]
111+
`;
112+
85113
exports[`source-map-loader should process css sourceMap: css 1`] = `
86114
"* {
87115
box-sizing: border-box; }
@@ -178,12 +206,7 @@ anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\
178206
}
179207
`;
180208

181-
exports[`source-map-loader should process css sourceMap: warnings 1`] = `
182-
Array [
183-
"ModuleWarning: Module Warning (from \`replaced original path\`):
184-
Absolute 'webpack:///./src/app/app.scss' URL is not supported",
185-
]
186-
`;
209+
exports[`source-map-loader should process css sourceMap: warnings 1`] = `Array []`;
187210

188211
exports[`source-map-loader should process css sourceMap: warnings 2`] = `
189212
Array [
@@ -360,6 +383,31 @@ Absolute 'ftp://exampleurl.com' URL is not supported",
360383
]
361384
`;
362385

386+
exports[`source-map-loader should resolve SourceMap.sources to http: css 1`] = `
387+
"// console.log('with SourceMap')
388+
"
389+
`;
390+
391+
exports[`source-map-loader should resolve SourceMap.sources to http: errors 1`] = `Array []`;
392+
393+
exports[`source-map-loader should resolve SourceMap.sources to http: map 1`] = `
394+
Object {
395+
"file": "sources-http.js",
396+
"mappings": "AAAA",
397+
"sources": Array [
398+
"https://github2.com/",
399+
"https://github.com/",
400+
],
401+
"sourcesContent": Array [
402+
"static content",
403+
"downloaded content",
404+
],
405+
"version": 3,
406+
}
407+
`;
408+
409+
exports[`source-map-loader should resolve SourceMap.sources to http: warnings 1`] = `Array []`;
410+
363411
exports[`source-map-loader should skip invalid base64 SourceMap: css 1`] = `
364412
"without SourceMap
365413
// @sourceMappingURL=data:application/source-map;base64,\\"something invalid\\"
@@ -614,6 +662,36 @@ Object {
614662

615663
exports[`source-map-loader should support relative sourceRoot paths in sourcemaps: warnings 1`] = `Array []`;
616664

665+
exports[`source-map-loader should throw error for SourceMap.sources to http: css 1`] = `
666+
"// console.log('with SourceMap')
667+
"
668+
`;
669+
670+
exports[`source-map-loader should throw error for SourceMap.sources to http: errors 1`] = `Array []`;
671+
672+
exports[`source-map-loader should throw error for SourceMap.sources to http: map 1`] = `
673+
Object {
674+
"file": "sources-http.js",
675+
"mappings": "AAAA",
676+
"sources": Array [
677+
"https://github2.com/",
678+
"https://github.com/",
679+
],
680+
"sourcesContent": Array [
681+
"static content",
682+
"",
683+
],
684+
"version": 3,
685+
}
686+
`;
687+
688+
exports[`source-map-loader should throw error for SourceMap.sources to http: warnings 1`] = `
689+
Array [
690+
"ModuleWarning: Module Warning (from \`replaced original path\`):
691+
https://github.com/ is not supported",
692+
]
693+
`;
694+
617695
exports[`source-map-loader should use last SourceMap directive: css 1`] = `
618696
"with SourceMap
619697
anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\\"+btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))))+\\" */\\";

test/__snapshots__/validate-options.test.js.snap

+33-8
Original file line numberDiff line numberDiff line change
@@ -111,47 +111,72 @@ exports[`validate options should throw an error on the "brokenSourceUrlReportTyp
111111
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
112112
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
113113
- options has an unknown property 'unknown'. These properties are valid:
114-
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType? }"
114+
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType?, unresolveSourceFetcher? }"
115115
`;
116116
117117
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
118118
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
119119
- options has an unknown property 'unknown'. These properties are valid:
120-
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType? }"
120+
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType?, unresolveSourceFetcher? }"
121121
`;
122122
123123
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
124124
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
125125
- options has an unknown property 'unknown'. These properties are valid:
126-
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType? }"
126+
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType?, unresolveSourceFetcher? }"
127127
`;
128128
129129
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
130130
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
131131
- options has an unknown property 'unknown'. These properties are valid:
132-
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType? }"
132+
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType?, unresolveSourceFetcher? }"
133133
`;
134134
135135
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
136136
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
137137
- options has an unknown property 'unknown'. These properties are valid:
138-
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType? }"
138+
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType?, unresolveSourceFetcher? }"
139139
`;
140140
141141
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
142142
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
143143
- options has an unknown property 'unknown'. These properties are valid:
144-
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType? }"
144+
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType?, unresolveSourceFetcher? }"
145145
`;
146146
147147
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
148148
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
149149
- options has an unknown property 'unknown'. These properties are valid:
150-
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType? }"
150+
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType?, unresolveSourceFetcher? }"
151151
`;
152152
153153
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
154154
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
155155
- options has an unknown property 'unknown'. These properties are valid:
156-
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType? }"
156+
object { brokenMapUrlReportType?, brokenMapParseReportType?, brokenSourceUrlReportType?, unresolveSourceFetcher? }"
157+
`;
158+
159+
exports[`validate options should throw an error on the "unresolveSourceFetcher" option with "[]" value 1`] = `
160+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
161+
- options.unresolveSourceFetcher should be an instance of function."
162+
`;
163+
164+
exports[`validate options should throw an error on the "unresolveSourceFetcher" option with "{}" value 1`] = `
165+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
166+
- options.unresolveSourceFetcher should be an instance of function."
167+
`;
168+
169+
exports[`validate options should throw an error on the "unresolveSourceFetcher" option with "1" value 1`] = `
170+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
171+
- options.unresolveSourceFetcher should be an instance of function."
172+
`;
173+
174+
exports[`validate options should throw an error on the "unresolveSourceFetcher" option with "test" value 1`] = `
175+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
176+
- options.unresolveSourceFetcher should be an instance of function."
177+
`;
178+
179+
exports[`validate options should throw an error on the "unresolveSourceFetcher" option with "true" value 1`] = `
180+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
181+
- options.unresolveSourceFetcher should be an instance of function."
157182
`;

test/fixtures/fetch/sources-http.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// console.log('with SourceMap')
2+
//#sourceMappingURL=sources-http.js.map

test/fixtures/fetch/sources-http.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// console.log('with SourceMap')
2+
//#sourceMappingURL=unresolved-sources-http.js.map

test/fixtures/fetch/unresolved-sources-http.js.map

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

0 commit comments

Comments
 (0)