This repository was archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (83 loc) · 3.05 KB
/
index.js
File metadata and controls
112 lines (83 loc) · 3.05 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Webpack plugin for processing postcss modules as classname hash objects.
*
* :CAVEAT: if app components use same class names as those of partials, they will end up global
*
* @package: postcss-modules-component-plugin
* @author: pospi <sam@everledger.io>
* @since: 2016-08-25
*/
const loaderUtils = require('loader-utils');
// JSON cache layer
const cssScopes = {};
function setFileJSON(cssFileName, json) {
cssScopes[cssFileName] = json;
}
function getFileJSON(cssFileName) {
if (cssScopes[cssFileName] === undefined) {
throw new Error("Unable to load PostCSS module JSON for " + cssFileName);
}
return cssScopes[cssFileName];
}
// resolve references to integrate with Webpack loaders system
const loaderName = require.resolve("./");
function getLoaderName(exportAs) {
if (exportAs === 'cjs' || exportAs === 'commonjs') {
exportAs = 'module.exports = ';
} else if (exportAs === 'es6') {
exportAs = 'export default ';
}
return loaderName + (exportAs ? `?{"exportFormat":"${exportAs}"}` : '');
}
// configuration to allow overriding list of global module path regexes
let globalModulePaths = [/\/node_modules\//];
function setGlobalModulePaths(paths) {
if (!Array.isArray(paths)) {
paths = [paths];
}
globalModulePaths = paths;
}
let localModuleNameFormat = '[name][emoji]_[localName]_[hash:base64:5]';
function setLocalModuleNameFormat(newFormat) {
localModuleNameFormat = newFormat;
}
// provide a function for handling name scoping...
const SELECTORS_ENCOUNTERED_GLOBALLY = {};
function scopedName(name, filename, css) {
// In component entrypoints:
// where we find selectors encountered in our partials, leave them alone
if (SELECTORS_ENCOUNTERED_GLOBALLY[name]) {
return name;
}
// In partials:
// use global selectors within node_modules folder
let isGlobal = false;
if (globalModulePaths.filter(regex => isGlobal || (isGlobal = filename.match(regex))).length !== 0) {
SELECTORS_ENCOUNTERED_GLOBALLY[name] = true;
return name;
} else if (SELECTORS_ENCOUNTERED_GLOBALLY[name]) {
SELECTORS_ENCOUNTERED_GLOBALLY[name] = false;
}
// In component entrypoints:
// use local selectors for app code by default
return loaderUtils.interpolateName(
{ resourcePath: filename },
localModuleNameFormat.replace(/\[localName\]/g, name),
{ content: css.substring(css.search(new RegExp('.' + name + '\\b'))) }
);
}
// Main loader entrypoint (injects JSON onto `css-loader` output)
function Loader(source) {
const query = this.query ? JSON.parse(this.query.replace(/^\?/, '')) : {
exportFormat: 'exports.locals = ', // use CSS module export format by default
};
this.cacheable();
return source + "\n" + query.exportFormat + JSON.stringify(getFileJSON(this.resourcePath)) + ';';
}
// exports
Loader.loader = getLoaderName;
Loader.writer = setFileJSON;
Loader.scopedName = scopedName;
Loader.setGlobalModulesWhitelist = setGlobalModulePaths;
Loader.setLocalModuleNameFormat = setLocalModuleNameFormat;
module.exports = Loader;