-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource-interface.js
95 lines (80 loc) · 2.87 KB
/
resource-interface.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const fs = require('fs');
const path = require('path');
class ResourceInterface {
constructor(compilation, options = {}) {
this.compilation = compilation;
this.options = options;
this.extensions = [];
this.contentType = '';
}
// get rid of things like query string parameters
// that will break when trying to use with fs
getBareUrlPath(url) {
return url.replace(/\?(.*)/, '');
}
// turn relative paths into relatively absolute based on a known root directory
// e.g. "../styles/theme.css" -> `${userWorkspace}/styles/theme.css`
resolveRelativeUrl(root, url) {
let reducedUrl;
url.split('/')
.filter((segment) => segment !== '')
.reduce((acc, segment) => {
const reducedPath = url.replace(`${acc}/${segment}`, '');
if (path.extname(reducedPath) !== '' && fs.existsSync(path.join(root, reducedPath))) {
reducedUrl = reducedPath;
}
return `${acc}/${segment}`;
}, '');
return reducedUrl;
}
// test if this plugin should change a relative URL from the browser to an absolute path on disk
// like for node_modules/ resolution. not commonly needed by most resource plugins
// return true | false
// eslint-disable-next-line no-unused-vars
async shouldResolve(url) {
return Promise.resolve(false);
}
// return an absolute path
async resolve(url) {
return Promise.resolve(url);
}
// test if this plugin should be used to process a given url / header combo the browser and retu
// ex: `<script type="module" src="index.ts">`
// return true | false
// eslint-disable-next-line no-unused-vars
async shouldServe(url, headers) {
return Promise.resolve(this.extensions.indexOf(path.extname(url)) >= 0);
}
// return the new body and / or contentType, e.g. convert file.foo -> file.js
// eslint-disable-next-line no-unused-vars
async serve(url, headers) {
return Promise.resolve({});
}
// test if this plugin should return a new body for an already resolved resource
// useful for modifying code on the fly without needing to read the file from disk
// return true | false
// eslint-disable-next-line no-unused-vars
async shouldIntercept(url, body, headers) {
return Promise.resolve(false);
}
// return the new body
// eslint-disable-next-line no-unused-vars
async intercept(url, body, headers) {
return Promise.resolve({ body });
}
// test if this plugin should manipulate any files prior to any final optmizations happening
// ex: add a "banner" to all .js files with a timestamp of the build, or minifying files
// return true | false
// eslint-disable-next-line no-unused-vars
async shouldOptimize(url, body) {
return Promise.resolve(false);
}
// return the new body
// eslint-disable-next-line no-unused-vars
async optimize (url, body) {
return Promise.resolve(body);
}
}
module.exports = {
ResourceInterface
};