postcss-modules 5.0.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -52
- package/README.md +77 -70
- package/build/FileSystemLoader.js +140 -0
- package/build/{css-loader-core/parser.js → Parser.js} +16 -12
- package/build/fs.js +24 -0
- package/build/index.js +8 -152
- package/build/localsConvention.js +47 -0
- package/build/pluginFactory.js +107 -0
- package/build/saveJSON.js +5 -2
- package/build/{behaviours.js → scoping.js} +34 -5
- package/build/{unquote/index.js → unquote.js} +1 -1
- package/index.d.ts +24 -36
- package/package.json +2 -2
- package/build/css-loader-core/loader.js +0 -131
- package/build/generateScopedName.js +0 -17
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _postcss = _interopRequireDefault(require("postcss"));
|
|
9
|
+
|
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
|
11
|
+
|
|
12
|
+
var _Parser = _interopRequireDefault(require("./Parser"));
|
|
13
|
+
|
|
14
|
+
var _fs = require("./fs");
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
// Initially copied from https://github.com/css-modules/css-modules-loader-core
|
|
19
|
+
class Core {
|
|
20
|
+
constructor(plugins) {
|
|
21
|
+
this.plugins = plugins || Core.defaultPlugins;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async load(sourceString, sourcePath, trace, pathFetcher) {
|
|
25
|
+
const parser = new _Parser.default(pathFetcher, trace);
|
|
26
|
+
const plugins = this.plugins.concat([parser.plugin()]);
|
|
27
|
+
const result = await (0, _postcss.default)(plugins).process(sourceString, {
|
|
28
|
+
from: sourcePath
|
|
29
|
+
});
|
|
30
|
+
return {
|
|
31
|
+
injectableSource: result.css,
|
|
32
|
+
exportTokens: parser.exportTokens
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
} // Sorts dependencies in the following way:
|
|
37
|
+
// AAA comes before AA and A
|
|
38
|
+
// AB comes after AA and before A
|
|
39
|
+
// All Bs come after all As
|
|
40
|
+
// This ensures that the files are always returned in the following order:
|
|
41
|
+
// - In the order they were required, except
|
|
42
|
+
// - After all their dependencies
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
const traceKeySorter = (a, b) => {
|
|
46
|
+
if (a.length < b.length) {
|
|
47
|
+
return a < b.substring(0, a.length) ? -1 : 1;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (a.length > b.length) {
|
|
51
|
+
return a.substring(0, b.length) <= b ? -1 : 1;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return a < b ? -1 : 1;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
class FileSystemLoader {
|
|
58
|
+
constructor(root, plugins, fileResolve) {
|
|
59
|
+
if (root === "/" && process.platform === "win32") {
|
|
60
|
+
const cwdDrive = process.cwd().slice(0, 3);
|
|
61
|
+
|
|
62
|
+
if (!/^[A-Za-z]:\\$/.test(cwdDrive)) {
|
|
63
|
+
throw new Error(`Failed to obtain root from "${process.cwd()}".`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
root = cwdDrive;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this.root = root;
|
|
70
|
+
this.fileResolve = fileResolve;
|
|
71
|
+
this.sources = {};
|
|
72
|
+
this.traces = {};
|
|
73
|
+
this.importNr = 0;
|
|
74
|
+
this.core = new Core(plugins);
|
|
75
|
+
this.tokensByFile = {};
|
|
76
|
+
this.fs = (0, _fs.getFileSystem)();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async fetch(_newPath, relativeTo, _trace) {
|
|
80
|
+
const newPath = _newPath.replace(/^["']|["']$/g, "");
|
|
81
|
+
|
|
82
|
+
const trace = _trace || String.fromCharCode(this.importNr++);
|
|
83
|
+
|
|
84
|
+
const useFileResolve = typeof this.fileResolve === "function";
|
|
85
|
+
const fileResolvedPath = useFileResolve ? await this.fileResolve(newPath, relativeTo) : await Promise.resolve();
|
|
86
|
+
|
|
87
|
+
if (fileResolvedPath && !_path.default.isAbsolute(fileResolvedPath)) {
|
|
88
|
+
throw new Error('The returned path from the "fileResolve" option must be absolute.');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const relativeDir = _path.default.dirname(relativeTo);
|
|
92
|
+
|
|
93
|
+
const rootRelativePath = fileResolvedPath || _path.default.resolve(relativeDir, newPath);
|
|
94
|
+
|
|
95
|
+
let fileRelativePath = fileResolvedPath || _path.default.resolve(_path.default.resolve(this.root, relativeDir), newPath); // if the path is not relative or absolute, try to resolve it in node_modules
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
if (!useFileResolve && newPath[0] !== "." && !_path.default.isAbsolute(newPath)) {
|
|
99
|
+
try {
|
|
100
|
+
fileRelativePath = require.resolve(newPath);
|
|
101
|
+
} catch (e) {// noop
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const tokens = this.tokensByFile[fileRelativePath];
|
|
106
|
+
if (tokens) return tokens;
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
this.fs.readFile(fileRelativePath, "utf-8", async (err, source) => {
|
|
109
|
+
if (err) reject(err);
|
|
110
|
+
const {
|
|
111
|
+
injectableSource,
|
|
112
|
+
exportTokens
|
|
113
|
+
} = await this.core.load(source, rootRelativePath, trace, this.fetch.bind(this));
|
|
114
|
+
this.sources[fileRelativePath] = injectableSource;
|
|
115
|
+
this.traces[trace] = fileRelativePath;
|
|
116
|
+
this.tokensByFile[fileRelativePath] = exportTokens;
|
|
117
|
+
resolve(exportTokens);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get finalSource() {
|
|
123
|
+
const traces = this.traces;
|
|
124
|
+
const sources = this.sources;
|
|
125
|
+
let written = new Set();
|
|
126
|
+
return Object.keys(traces).sort(traceKeySorter).map(key => {
|
|
127
|
+
const filename = traces[key];
|
|
128
|
+
|
|
129
|
+
if (written.has(filename)) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
written.add(filename);
|
|
134
|
+
return sources[filename];
|
|
135
|
+
}).join("");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
exports.default = FileSystemLoader;
|
|
@@ -5,11 +5,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _icssUtils = require("icss-utils");
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
// Copied from https://github.com/css-modules/css-modules-loader-core
|
|
10
|
+
// Initially copied from https://github.com/css-modules/css-modules-loader-core
|
|
13
11
|
const importRegexp = /^:import\((.+)\)$/;
|
|
14
12
|
|
|
15
13
|
class Parser {
|
|
@@ -26,8 +24,10 @@ class Parser {
|
|
|
26
24
|
return {
|
|
27
25
|
postcssPlugin: "css-modules-parser",
|
|
28
26
|
|
|
29
|
-
OnceExit(css) {
|
|
30
|
-
|
|
27
|
+
async OnceExit(css) {
|
|
28
|
+
await Promise.all(parser.fetchAllImports(css));
|
|
29
|
+
parser.linkImportedSymbols(css);
|
|
30
|
+
return parser.extractExports(css);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
};
|
|
@@ -44,7 +44,7 @@ class Parser {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
linkImportedSymbols(css) {
|
|
47
|
-
(0,
|
|
47
|
+
(0, _icssUtils.replaceSymbols)(css, this.translations);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
extractExports(css) {
|
|
@@ -65,17 +65,21 @@ class Parser {
|
|
|
65
65
|
exportNode.remove();
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
fetchImport(importNode, relativeTo, depNr) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
async fetchImport(importNode, relativeTo, depNr) {
|
|
69
|
+
const file = importNode.selector.match(importRegexp)[1];
|
|
70
|
+
const depTrace = this.trace + String.fromCharCode(depNr);
|
|
71
|
+
const exports = await this.pathFetcher(file, relativeTo, depTrace);
|
|
72
|
+
|
|
73
|
+
try {
|
|
72
74
|
importNode.each(decl => {
|
|
73
75
|
if (decl.type == "decl") {
|
|
74
76
|
this.translations[decl.prop] = exports[decl.value];
|
|
75
77
|
}
|
|
76
78
|
});
|
|
77
79
|
importNode.remove();
|
|
78
|
-
}
|
|
80
|
+
} catch (err) {
|
|
81
|
+
console.log(err);
|
|
82
|
+
}
|
|
79
83
|
}
|
|
80
84
|
|
|
81
85
|
}
|
package/build/fs.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getFileSystem = getFileSystem;
|
|
7
|
+
exports.setFileSystem = setFileSystem;
|
|
8
|
+
let fileSystem = {
|
|
9
|
+
readFile: () => {
|
|
10
|
+
throw Error("readFile not implemented");
|
|
11
|
+
},
|
|
12
|
+
writeFile: () => {
|
|
13
|
+
throw Error("writeFile not implemented");
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function setFileSystem(fs) {
|
|
18
|
+
fileSystem.readFile = fs.readFile;
|
|
19
|
+
fileSystem.writeFile = fs.writeFile;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getFileSystem() {
|
|
23
|
+
return fileSystem;
|
|
24
|
+
}
|
package/build/index.js
CHANGED
|
@@ -1,160 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _fs = require("fs");
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var _fs2 = require("./fs");
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var _pluginFactory = require("./pluginFactory");
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
(0, _fs2.setFileSystem)({
|
|
10
|
+
readFile: _fs.readFile,
|
|
11
|
+
writeFile: _fs.writeFile
|
|
12
|
+
});
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var _loader = _interopRequireDefault(require("./css-loader-core/loader"));
|
|
14
|
-
|
|
15
|
-
var _generateScopedName = _interopRequireDefault(require("./generateScopedName"));
|
|
16
|
-
|
|
17
|
-
var _saveJSON = _interopRequireDefault(require("./saveJSON"));
|
|
18
|
-
|
|
19
|
-
var _behaviours = require("./behaviours");
|
|
20
|
-
|
|
21
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
-
|
|
23
|
-
const PLUGIN_NAME = "postcss-modules";
|
|
24
|
-
|
|
25
|
-
function getDefaultScopeBehaviour(opts) {
|
|
26
|
-
if (opts.scopeBehaviour && (0, _behaviours.isValidBehaviour)(opts.scopeBehaviour)) {
|
|
27
|
-
return opts.scopeBehaviour;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return _behaviours.behaviours.LOCAL;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function getScopedNameGenerator(opts) {
|
|
34
|
-
const scopedNameGenerator = opts.generateScopedName || _generateScopedName.default;
|
|
35
|
-
if (typeof scopedNameGenerator === "function") return scopedNameGenerator;
|
|
36
|
-
return (0, _genericNames.default)(scopedNameGenerator, {
|
|
37
|
-
context: process.cwd(),
|
|
38
|
-
hashPrefix: opts.hashPrefix
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function getLoader(opts, plugins) {
|
|
43
|
-
const root = typeof opts.root === "undefined" ? "/" : opts.root;
|
|
44
|
-
return typeof opts.Loader === "function" ? new opts.Loader(root, plugins) : new _loader.default(root, plugins);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function isGlobalModule(globalModules, inputFile) {
|
|
48
|
-
return globalModules.some(regex => inputFile.match(regex));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function getDefaultPluginsList(opts, inputFile) {
|
|
52
|
-
const globalModulesList = opts.globalModulePaths || null;
|
|
53
|
-
const exportGlobals = opts.exportGlobals || false;
|
|
54
|
-
const defaultBehaviour = getDefaultScopeBehaviour(opts);
|
|
55
|
-
const generateScopedName = getScopedNameGenerator(opts);
|
|
56
|
-
|
|
57
|
-
if (globalModulesList && isGlobalModule(globalModulesList, inputFile)) {
|
|
58
|
-
return (0, _behaviours.getDefaultPlugins)({
|
|
59
|
-
behaviour: _behaviours.behaviours.GLOBAL,
|
|
60
|
-
generateScopedName,
|
|
61
|
-
exportGlobals
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return (0, _behaviours.getDefaultPlugins)({
|
|
66
|
-
behaviour: defaultBehaviour,
|
|
67
|
-
generateScopedName,
|
|
68
|
-
exportGlobals
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function isOurPlugin(plugin) {
|
|
73
|
-
return plugin.postcssPlugin === PLUGIN_NAME;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function dashesCamelCase(string) {
|
|
77
|
-
return string.replace(/-+(\w)/g, (_, firstLetter) => firstLetter.toUpperCase());
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
module.exports = (opts = {}) => {
|
|
81
|
-
return {
|
|
82
|
-
postcssPlugin: PLUGIN_NAME,
|
|
83
|
-
|
|
84
|
-
async OnceExit(css, {
|
|
85
|
-
result
|
|
86
|
-
}) {
|
|
87
|
-
const getJSON = opts.getJSON || _saveJSON.default;
|
|
88
|
-
const inputFile = css.source.input.file;
|
|
89
|
-
const pluginList = getDefaultPluginsList(opts, inputFile);
|
|
90
|
-
const resultPluginIndex = result.processor.plugins.findIndex(plugin => isOurPlugin(plugin));
|
|
91
|
-
|
|
92
|
-
if (resultPluginIndex === -1) {
|
|
93
|
-
throw new Error('Plugin missing from options.');
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const earlierPlugins = result.processor.plugins.slice(0, resultPluginIndex);
|
|
97
|
-
const loaderPlugins = [...earlierPlugins, ...pluginList];
|
|
98
|
-
const loader = getLoader(opts, loaderPlugins);
|
|
99
|
-
|
|
100
|
-
const fetcher = (file, relativeTo, depTrace) => {
|
|
101
|
-
const unquoteFile = (0, _unquote.default)(file);
|
|
102
|
-
const resolvedResult = typeof opts.resolve === 'function' && opts.resolve(unquoteFile);
|
|
103
|
-
const resolvedFile = resolvedResult instanceof Promise ? resolvedResult : Promise.resolve(resolvedResult);
|
|
104
|
-
return resolvedFile.then(f => {
|
|
105
|
-
return loader.fetch.call(loader, `"${f || unquoteFile}"`, relativeTo, depTrace);
|
|
106
|
-
});
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const parser = new _parser.default(fetcher);
|
|
110
|
-
await (0, _postcss.default)([...pluginList, parser.plugin()]).process(css, {
|
|
111
|
-
from: inputFile
|
|
112
|
-
});
|
|
113
|
-
const out = loader.finalSource;
|
|
114
|
-
if (out) css.prepend(out);
|
|
115
|
-
|
|
116
|
-
if (opts.localsConvention) {
|
|
117
|
-
const isFunc = typeof opts.localsConvention === "function";
|
|
118
|
-
parser.exportTokens = Object.entries(parser.exportTokens).reduce((tokens, [className, value]) => {
|
|
119
|
-
if (isFunc) {
|
|
120
|
-
tokens[opts.localsConvention(className, value, inputFile)] = value;
|
|
121
|
-
return tokens;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
switch (opts.localsConvention) {
|
|
125
|
-
case "camelCase":
|
|
126
|
-
tokens[className] = value;
|
|
127
|
-
tokens[(0, _lodash.default)(className)] = value;
|
|
128
|
-
break;
|
|
129
|
-
|
|
130
|
-
case "camelCaseOnly":
|
|
131
|
-
tokens[(0, _lodash.default)(className)] = value;
|
|
132
|
-
break;
|
|
133
|
-
|
|
134
|
-
case "dashes":
|
|
135
|
-
tokens[className] = value;
|
|
136
|
-
tokens[dashesCamelCase(className)] = value;
|
|
137
|
-
break;
|
|
138
|
-
|
|
139
|
-
case "dashesOnly":
|
|
140
|
-
tokens[dashesCamelCase(className)] = value;
|
|
141
|
-
break;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return tokens;
|
|
145
|
-
}, {});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
result.messages.push({
|
|
149
|
-
type: "export",
|
|
150
|
-
plugin: "postcss-modules",
|
|
151
|
-
exportTokens: parser.exportTokens
|
|
152
|
-
}); // getJSON may return a promise
|
|
153
|
-
|
|
154
|
-
return getJSON(css.source.input.file, parser.exportTokens, result.opts.to);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
};
|
|
158
|
-
};
|
|
14
|
+
module.exports = (opts = {}) => (0, _pluginFactory.makePlugin)(opts);
|
|
159
15
|
|
|
160
16
|
module.exports.postcss = true;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.makeLocalsConventionReducer = makeLocalsConventionReducer;
|
|
7
|
+
|
|
8
|
+
var _lodash = _interopRequireDefault(require("lodash.camelcase"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
function dashesCamelCase(string) {
|
|
13
|
+
return string.replace(/-+(\w)/g, (_, firstLetter) => firstLetter.toUpperCase());
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function makeLocalsConventionReducer(localsConvention, inputFile) {
|
|
17
|
+
const isFunc = typeof localsConvention === "function";
|
|
18
|
+
return (tokens, [className, value]) => {
|
|
19
|
+
if (isFunc) {
|
|
20
|
+
const convention = localsConvention(className, value, inputFile);
|
|
21
|
+
tokens[convention] = value;
|
|
22
|
+
return tokens;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
switch (localsConvention) {
|
|
26
|
+
case "camelCase":
|
|
27
|
+
tokens[className] = value;
|
|
28
|
+
tokens[(0, _lodash.default)(className)] = value;
|
|
29
|
+
break;
|
|
30
|
+
|
|
31
|
+
case "camelCaseOnly":
|
|
32
|
+
tokens[(0, _lodash.default)(className)] = value;
|
|
33
|
+
break;
|
|
34
|
+
|
|
35
|
+
case "dashes":
|
|
36
|
+
tokens[className] = value;
|
|
37
|
+
tokens[dashesCamelCase(className)] = value;
|
|
38
|
+
break;
|
|
39
|
+
|
|
40
|
+
case "dashesOnly":
|
|
41
|
+
tokens[dashesCamelCase(className)] = value;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return tokens;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.makePlugin = makePlugin;
|
|
7
|
+
|
|
8
|
+
var _postcss = _interopRequireDefault(require("postcss"));
|
|
9
|
+
|
|
10
|
+
var _unquote = _interopRequireDefault(require("./unquote"));
|
|
11
|
+
|
|
12
|
+
var _Parser = _interopRequireDefault(require("./Parser"));
|
|
13
|
+
|
|
14
|
+
var _saveJSON = _interopRequireDefault(require("./saveJSON"));
|
|
15
|
+
|
|
16
|
+
var _localsConvention = require("./localsConvention");
|
|
17
|
+
|
|
18
|
+
var _FileSystemLoader = _interopRequireDefault(require("./FileSystemLoader"));
|
|
19
|
+
|
|
20
|
+
var _scoping = require("./scoping");
|
|
21
|
+
|
|
22
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
+
|
|
24
|
+
const PLUGIN_NAME = "postcss-modules";
|
|
25
|
+
|
|
26
|
+
function isGlobalModule(globalModules, inputFile) {
|
|
27
|
+
return globalModules.some(regex => inputFile.match(regex));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function getDefaultPluginsList(opts, inputFile) {
|
|
31
|
+
const globalModulesList = opts.globalModulePaths || null;
|
|
32
|
+
const exportGlobals = opts.exportGlobals || false;
|
|
33
|
+
const defaultBehaviour = (0, _scoping.getDefaultScopeBehaviour)(opts.scopeBehaviour);
|
|
34
|
+
const generateScopedName = (0, _scoping.getScopedNameGenerator)(opts.generateScopedName, opts.hashPrefix);
|
|
35
|
+
|
|
36
|
+
if (globalModulesList && isGlobalModule(globalModulesList, inputFile)) {
|
|
37
|
+
return (0, _scoping.getDefaultPlugins)({
|
|
38
|
+
behaviour: _scoping.behaviours.GLOBAL,
|
|
39
|
+
generateScopedName,
|
|
40
|
+
exportGlobals
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return (0, _scoping.getDefaultPlugins)({
|
|
45
|
+
behaviour: defaultBehaviour,
|
|
46
|
+
generateScopedName,
|
|
47
|
+
exportGlobals
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getLoader(opts, plugins) {
|
|
52
|
+
const root = typeof opts.root === "undefined" ? "/" : opts.root;
|
|
53
|
+
return typeof opts.Loader === "function" ? new opts.Loader(root, plugins, opts.resolve) : new _FileSystemLoader.default(root, plugins, opts.resolve);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function isOurPlugin(plugin) {
|
|
57
|
+
return plugin.postcssPlugin === PLUGIN_NAME;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function makePlugin(opts) {
|
|
61
|
+
return {
|
|
62
|
+
postcssPlugin: PLUGIN_NAME,
|
|
63
|
+
|
|
64
|
+
async OnceExit(css, {
|
|
65
|
+
result
|
|
66
|
+
}) {
|
|
67
|
+
const getJSON = opts.getJSON || _saveJSON.default;
|
|
68
|
+
const inputFile = css.source.input.file;
|
|
69
|
+
const pluginList = getDefaultPluginsList(opts, inputFile);
|
|
70
|
+
const resultPluginIndex = result.processor.plugins.findIndex(plugin => isOurPlugin(plugin));
|
|
71
|
+
|
|
72
|
+
if (resultPluginIndex === -1) {
|
|
73
|
+
throw new Error("Plugin missing from options.");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const earlierPlugins = result.processor.plugins.slice(0, resultPluginIndex);
|
|
77
|
+
const loaderPlugins = [...earlierPlugins, ...pluginList];
|
|
78
|
+
const loader = getLoader(opts, loaderPlugins);
|
|
79
|
+
|
|
80
|
+
const fetcher = async (file, relativeTo, depTrace) => {
|
|
81
|
+
const unquoteFile = (0, _unquote.default)(file);
|
|
82
|
+
return loader.fetch.call(loader, unquoteFile, relativeTo, depTrace);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const parser = new _Parser.default(fetcher);
|
|
86
|
+
await (0, _postcss.default)([...pluginList, parser.plugin()]).process(css, {
|
|
87
|
+
from: inputFile
|
|
88
|
+
});
|
|
89
|
+
const out = loader.finalSource;
|
|
90
|
+
if (out) css.prepend(out);
|
|
91
|
+
|
|
92
|
+
if (opts.localsConvention) {
|
|
93
|
+
const reducer = (0, _localsConvention.makeLocalsConventionReducer)(opts.localsConvention, inputFile);
|
|
94
|
+
parser.exportTokens = Object.entries(parser.exportTokens).reduce(reducer, {});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
result.messages.push({
|
|
98
|
+
type: "export",
|
|
99
|
+
plugin: "postcss-modules",
|
|
100
|
+
exportTokens: parser.exportTokens
|
|
101
|
+
}); // getJSON may return a promise
|
|
102
|
+
|
|
103
|
+
return getJSON(css.source.input.file, parser.exportTokens, result.opts.to);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
};
|
|
107
|
+
}
|
package/build/saveJSON.js
CHANGED
|
@@ -5,10 +5,13 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = saveJSON;
|
|
7
7
|
|
|
8
|
-
var _fs = require("fs");
|
|
8
|
+
var _fs = require("./fs");
|
|
9
9
|
|
|
10
10
|
function saveJSON(cssFile, json) {
|
|
11
11
|
return new Promise((resolve, reject) => {
|
|
12
|
-
|
|
12
|
+
const {
|
|
13
|
+
writeFile
|
|
14
|
+
} = (0, _fs.getFileSystem)();
|
|
15
|
+
writeFile(`${cssFile}.json`, JSON.stringify(json), e => e ? reject(e) : resolve(json));
|
|
13
16
|
});
|
|
14
17
|
}
|
|
@@ -5,14 +5,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.behaviours = void 0;
|
|
7
7
|
exports.getDefaultPlugins = getDefaultPlugins;
|
|
8
|
-
exports.
|
|
9
|
-
|
|
10
|
-
var _postcssModulesLocalByDefault = _interopRequireDefault(require("postcss-modules-local-by-default"));
|
|
8
|
+
exports.getDefaultScopeBehaviour = getDefaultScopeBehaviour;
|
|
9
|
+
exports.getScopedNameGenerator = getScopedNameGenerator;
|
|
11
10
|
|
|
12
11
|
var _postcssModulesExtractImports = _interopRequireDefault(require("postcss-modules-extract-imports"));
|
|
13
12
|
|
|
13
|
+
var _genericNames = _interopRequireDefault(require("generic-names"));
|
|
14
|
+
|
|
15
|
+
var _postcssModulesLocalByDefault = _interopRequireDefault(require("postcss-modules-local-by-default"));
|
|
16
|
+
|
|
14
17
|
var _postcssModulesScope = _interopRequireDefault(require("postcss-modules-scope"));
|
|
15
18
|
|
|
19
|
+
var _stringHash = _interopRequireDefault(require("string-hash"));
|
|
20
|
+
|
|
16
21
|
var _postcssModulesValues = _interopRequireDefault(require("postcss-modules-values"));
|
|
17
22
|
|
|
18
23
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -34,10 +39,10 @@ function getDefaultPlugins({
|
|
|
34
39
|
});
|
|
35
40
|
const plugins = {
|
|
36
41
|
[behaviours.LOCAL]: [_postcssModulesValues.default, (0, _postcssModulesLocalByDefault.default)({
|
|
37
|
-
mode:
|
|
42
|
+
mode: "local"
|
|
38
43
|
}), _postcssModulesExtractImports.default, scope],
|
|
39
44
|
[behaviours.GLOBAL]: [_postcssModulesValues.default, (0, _postcssModulesLocalByDefault.default)({
|
|
40
|
-
mode:
|
|
45
|
+
mode: "global"
|
|
41
46
|
}), _postcssModulesExtractImports.default, scope]
|
|
42
47
|
};
|
|
43
48
|
return plugins[behaviour];
|
|
@@ -45,4 +50,28 @@ function getDefaultPlugins({
|
|
|
45
50
|
|
|
46
51
|
function isValidBehaviour(behaviour) {
|
|
47
52
|
return Object.keys(behaviours).map(key => behaviours[key]).indexOf(behaviour) > -1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getDefaultScopeBehaviour(scopeBehaviour) {
|
|
56
|
+
return scopeBehaviour && isValidBehaviour(scopeBehaviour) ? scopeBehaviour : behaviours.LOCAL;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function generateScopedNameDefault(name, filename, css) {
|
|
60
|
+
const i = css.indexOf(`.${name}`);
|
|
61
|
+
const lineNumber = css.substr(0, i).split(/[\r\n]/).length;
|
|
62
|
+
const hash = (0, _stringHash.default)(css).toString(36).substr(0, 5);
|
|
63
|
+
return `_${name}_${hash}_${lineNumber}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function getScopedNameGenerator(generateScopedName, hashPrefix) {
|
|
67
|
+
const scopedNameGenerator = generateScopedName || generateScopedNameDefault;
|
|
68
|
+
|
|
69
|
+
if (typeof scopedNameGenerator === "function") {
|
|
70
|
+
return scopedNameGenerator;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return (0, _genericNames.default)(scopedNameGenerator, {
|
|
74
|
+
context: process.cwd(),
|
|
75
|
+
hashPrefix: hashPrefix
|
|
76
|
+
});
|
|
48
77
|
}
|