-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpitcher.js
63 lines (50 loc) · 1.98 KB
/
pitcher.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
const loaderUtils = require('loader-utils');
const qs = require('querystring');
const isPitcher = l => l.path === __filename;
const isStyleLoader = l => /(\/|\\|@)style-loader/.test(l.path);
const isExtractPlugin = l => /(\/|\\|@)mini-css-extract-plugin/.test(l.path);
module.exports = code => code;
module.exports.pitch = function (remainingRequest) {
const rawQuery = this.resourceQuery.slice(1);
const incomingQuery = qs.parse(rawQuery);
/**
* Copied from vue-loader
* @param loaders
*/
const genRequest = loaders => {
const seen = new Map();
const loaderStrings = [];
loaders.forEach(loader => {
const identifier = typeof loader === 'string'
? loader
: (loader.path + loader.query);
const request = typeof loader === 'string' ? loader : loader.request;
if (!seen.has(identifier)) {
seen.set(identifier, true);
// loader.request contains both the resolved loader path and its options
// query (e.g. ??ref-0)
loaderStrings.push(request);
}
});
return loaderUtils.stringifyRequest(this, '-!' + [
...loaderStrings,
this.resourcePath + this.resourceQuery
].join('!'))
}
const shouldExtractCss = incomingQuery.extract !== 'false';
/**
* Rearrange extract plugin loader and remove unwanted loaders
*/
let loaders = this.loaders.reduce((accumulator, loader) => {
if (isExtractPlugin(loader)) {
if (shouldExtractCss === true) {
accumulator.unshift(loader);
}
} else if (!isPitcher(loader) && (!isStyleLoader(loader) || (isStyleLoader(loader) && shouldExtractCss === false))) {
accumulator.push(loader);
}
return accumulator;
}, []);
const request = genRequest(loaders);
return `import mod from ${request}; export default mod; export * from ${request}`
};