postcss-modules 4.3.1 → 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/.eslintrc.js +19 -0
- package/CHANGELOG.md +90 -50
- package/README.md +77 -77
- package/build/FileSystemLoader.js +140 -0
- package/build/{css-loader-core/parser.js → Parser.js} +22 -15
- package/build/fs.js +24 -0
- package/build/index.js +8 -174
- package/build/localsConvention.js +47 -0
- package/build/pluginFactory.js +107 -0
- package/build/saveJSON.js +5 -2
- package/build/scoping.js +77 -0
- package/build/{unquote/index.js → unquote.js} +4 -2
- package/index.d.ts +24 -36
- package/package.json +8 -7
- package/build/behaviours.js +0 -50
- package/build/css-loader-core/loader.js +0 -121
- package/build/generateScopedName.js +0 -20
|
@@ -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;
|
|
@@ -3,16 +3,13 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.default = void 0;
|
|
6
7
|
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
var _icssReplaceSymbols2 = _interopRequireDefault(_icssReplaceSymbols);
|
|
10
|
-
|
|
11
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
-
|
|
13
|
-
// Copied from https://github.com/css-modules/css-modules-loader-core
|
|
8
|
+
var _icssUtils = require("icss-utils");
|
|
14
9
|
|
|
10
|
+
// Initially copied from https://github.com/css-modules/css-modules-loader-core
|
|
15
11
|
const importRegexp = /^:import\((.+)\)$/;
|
|
12
|
+
|
|
16
13
|
class Parser {
|
|
17
14
|
constructor(pathFetcher, trace) {
|
|
18
15
|
this.pathFetcher = pathFetcher;
|
|
@@ -26,9 +23,13 @@ class Parser {
|
|
|
26
23
|
const parser = this;
|
|
27
24
|
return {
|
|
28
25
|
postcssPlugin: "css-modules-parser",
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
|
|
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
34
|
}
|
|
34
35
|
|
|
@@ -43,7 +44,7 @@ class Parser {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
linkImportedSymbols(css) {
|
|
46
|
-
(0,
|
|
47
|
+
(0, _icssUtils.replaceSymbols)(css, this.translations);
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
extractExports(css) {
|
|
@@ -64,17 +65,23 @@ class Parser {
|
|
|
64
65
|
exportNode.remove();
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
fetchImport(importNode, relativeTo, depNr) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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 {
|
|
71
74
|
importNode.each(decl => {
|
|
72
75
|
if (decl.type == "decl") {
|
|
73
76
|
this.translations[decl.prop] = exports[decl.value];
|
|
74
77
|
}
|
|
75
78
|
});
|
|
76
79
|
importNode.remove();
|
|
77
|
-
}
|
|
80
|
+
} catch (err) {
|
|
81
|
+
console.log(err);
|
|
82
|
+
}
|
|
78
83
|
}
|
|
84
|
+
|
|
79
85
|
}
|
|
86
|
+
|
|
80
87
|
exports.default = Parser;
|
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,182 +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 _genericNames2 = _interopRequireDefault(_genericNames);
|
|
14
|
-
|
|
15
|
-
var _unquote = require("./unquote");
|
|
16
|
-
|
|
17
|
-
var _unquote2 = _interopRequireDefault(_unquote);
|
|
18
|
-
|
|
19
|
-
var _parser = require("./css-loader-core/parser");
|
|
20
|
-
|
|
21
|
-
var _parser2 = _interopRequireDefault(_parser);
|
|
22
|
-
|
|
23
|
-
var _loader = require("./css-loader-core/loader");
|
|
24
|
-
|
|
25
|
-
var _loader2 = _interopRequireDefault(_loader);
|
|
26
|
-
|
|
27
|
-
var _generateScopedName = require("./generateScopedName");
|
|
28
|
-
|
|
29
|
-
var _generateScopedName2 = _interopRequireDefault(_generateScopedName);
|
|
30
|
-
|
|
31
|
-
var _saveJSON = require("./saveJSON");
|
|
32
|
-
|
|
33
|
-
var _saveJSON2 = _interopRequireDefault(_saveJSON);
|
|
34
|
-
|
|
35
|
-
var _behaviours = require("./behaviours");
|
|
36
|
-
|
|
37
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
38
|
-
|
|
39
|
-
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
|
|
40
|
-
|
|
41
|
-
const PLUGIN_NAME = "postcss-modules";
|
|
42
|
-
|
|
43
|
-
function getDefaultScopeBehaviour(opts) {
|
|
44
|
-
if (opts.scopeBehaviour && (0, _behaviours.isValidBehaviour)(opts.scopeBehaviour)) {
|
|
45
|
-
return opts.scopeBehaviour;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return _behaviours.behaviours.LOCAL;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function getScopedNameGenerator(opts) {
|
|
52
|
-
const scopedNameGenerator = opts.generateScopedName || _generateScopedName2.default;
|
|
53
|
-
|
|
54
|
-
if (typeof scopedNameGenerator === "function") return scopedNameGenerator;
|
|
55
|
-
return (0, _genericNames2.default)(scopedNameGenerator, {
|
|
56
|
-
context: process.cwd(),
|
|
57
|
-
hashPrefix: opts.hashPrefix
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function getLoader(opts, plugins) {
|
|
62
|
-
const root = typeof opts.root === "undefined" ? "/" : opts.root;
|
|
63
|
-
return typeof opts.Loader === "function" ? new opts.Loader(root, plugins) : new _loader2.default(root, plugins);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function isGlobalModule(globalModules, inputFile) {
|
|
67
|
-
return globalModules.some(regex => inputFile.match(regex));
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function getDefaultPluginsList(opts, inputFile) {
|
|
71
|
-
const globalModulesList = opts.globalModulePaths || null;
|
|
72
|
-
const exportGlobals = opts.exportGlobals || false;
|
|
73
|
-
const defaultBehaviour = getDefaultScopeBehaviour(opts);
|
|
74
|
-
const generateScopedName = getScopedNameGenerator(opts);
|
|
75
|
-
|
|
76
|
-
if (globalModulesList && isGlobalModule(globalModulesList, inputFile)) {
|
|
77
|
-
return (0, _behaviours.getDefaultPlugins)({
|
|
78
|
-
behaviour: _behaviours.behaviours.GLOBAL,
|
|
79
|
-
generateScopedName,
|
|
80
|
-
exportGlobals
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return (0, _behaviours.getDefaultPlugins)({
|
|
85
|
-
behaviour: defaultBehaviour,
|
|
86
|
-
generateScopedName,
|
|
87
|
-
exportGlobals
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function isOurPlugin(plugin) {
|
|
92
|
-
return plugin.postcssPlugin === PLUGIN_NAME;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function dashesCamelCase(string) {
|
|
96
|
-
return string.replace(/-+(\w)/g, (_, firstLetter) => firstLetter.toUpperCase());
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
module.exports = (opts = {}) => {
|
|
100
|
-
return {
|
|
101
|
-
postcssPlugin: PLUGIN_NAME,
|
|
102
|
-
OnceExit(css, { result }) {
|
|
103
|
-
return _asyncToGenerator(function* () {
|
|
104
|
-
const getJSON = opts.getJSON || _saveJSON2.default;
|
|
105
|
-
const inputFile = css.source.input.file;
|
|
106
|
-
const pluginList = getDefaultPluginsList(opts, inputFile);
|
|
107
|
-
const resultPluginIndex = result.processor.plugins.findIndex(function (plugin) {
|
|
108
|
-
return isOurPlugin(plugin);
|
|
109
|
-
});
|
|
110
|
-
if (resultPluginIndex === -1) {
|
|
111
|
-
throw new Error('Plugin missing from options.');
|
|
112
|
-
}
|
|
113
|
-
const earlierPlugins = result.processor.plugins.slice(0, resultPluginIndex);
|
|
114
|
-
const loaderPlugins = [...earlierPlugins, ...pluginList];
|
|
115
|
-
const loader = getLoader(opts, loaderPlugins);
|
|
116
|
-
const fetcher = function fetcher(file, relativeTo, depTrace) {
|
|
117
|
-
const unquoteFile = (0, _unquote2.default)(file);
|
|
118
|
-
const resolvedResult = typeof opts.resolve === 'function' && opts.resolve(unquoteFile);
|
|
119
|
-
const resolvedFile = resolvedResult instanceof Promise ? resolvedResult : Promise.resolve(resolvedResult);
|
|
120
|
-
|
|
121
|
-
return resolvedFile.then(function (f) {
|
|
122
|
-
return loader.fetch.call(loader, `"${f || unquoteFile}"`, relativeTo, depTrace);
|
|
123
|
-
});
|
|
124
|
-
};
|
|
125
|
-
const parser = new _parser2.default(fetcher);
|
|
126
|
-
|
|
127
|
-
yield (0, _postcss2.default)([...pluginList, parser.plugin()]).process(css, {
|
|
128
|
-
from: inputFile
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
const out = loader.finalSource;
|
|
132
|
-
if (out) css.prepend(out);
|
|
133
|
-
|
|
134
|
-
if (opts.localsConvention) {
|
|
135
|
-
const isFunc = typeof opts.localsConvention === "function";
|
|
136
|
-
|
|
137
|
-
parser.exportTokens = Object.entries(parser.exportTokens).reduce(function (tokens, [className, value]) {
|
|
138
|
-
if (isFunc) {
|
|
139
|
-
tokens[opts.localsConvention(className, value, inputFile)] = value;
|
|
140
|
-
|
|
141
|
-
return tokens;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
switch (opts.localsConvention) {
|
|
145
|
-
case "camelCase":
|
|
146
|
-
tokens[className] = value;
|
|
147
|
-
tokens[(0, _lodash2.default)(className)] = value;
|
|
148
|
-
|
|
149
|
-
break;
|
|
150
|
-
case "camelCaseOnly":
|
|
151
|
-
tokens[(0, _lodash2.default)(className)] = value;
|
|
152
|
-
|
|
153
|
-
break;
|
|
154
|
-
case "dashes":
|
|
155
|
-
tokens[className] = value;
|
|
156
|
-
tokens[dashesCamelCase(className)] = value;
|
|
157
|
-
|
|
158
|
-
break;
|
|
159
|
-
case "dashesOnly":
|
|
160
|
-
tokens[dashesCamelCase(className)] = value;
|
|
161
|
-
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return tokens;
|
|
166
|
-
}, {});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
result.messages.push({
|
|
170
|
-
type: "export",
|
|
171
|
-
plugin: "postcss-modules",
|
|
172
|
-
exportTokens: parser.exportTokens
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
// getJSON may return a promise
|
|
176
|
-
return getJSON(css.source.input.file, parser.exportTokens, result.opts.to);
|
|
177
|
-
})();
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
};
|
|
14
|
+
module.exports = (opts = {}) => (0, _pluginFactory.makePlugin)(opts);
|
|
181
15
|
|
|
182
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
|
}
|
package/build/scoping.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.behaviours = void 0;
|
|
7
|
+
exports.getDefaultPlugins = getDefaultPlugins;
|
|
8
|
+
exports.getDefaultScopeBehaviour = getDefaultScopeBehaviour;
|
|
9
|
+
exports.getScopedNameGenerator = getScopedNameGenerator;
|
|
10
|
+
|
|
11
|
+
var _postcssModulesExtractImports = _interopRequireDefault(require("postcss-modules-extract-imports"));
|
|
12
|
+
|
|
13
|
+
var _genericNames = _interopRequireDefault(require("generic-names"));
|
|
14
|
+
|
|
15
|
+
var _postcssModulesLocalByDefault = _interopRequireDefault(require("postcss-modules-local-by-default"));
|
|
16
|
+
|
|
17
|
+
var _postcssModulesScope = _interopRequireDefault(require("postcss-modules-scope"));
|
|
18
|
+
|
|
19
|
+
var _stringHash = _interopRequireDefault(require("string-hash"));
|
|
20
|
+
|
|
21
|
+
var _postcssModulesValues = _interopRequireDefault(require("postcss-modules-values"));
|
|
22
|
+
|
|
23
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
24
|
+
|
|
25
|
+
const behaviours = {
|
|
26
|
+
LOCAL: "local",
|
|
27
|
+
GLOBAL: "global"
|
|
28
|
+
};
|
|
29
|
+
exports.behaviours = behaviours;
|
|
30
|
+
|
|
31
|
+
function getDefaultPlugins({
|
|
32
|
+
behaviour,
|
|
33
|
+
generateScopedName,
|
|
34
|
+
exportGlobals
|
|
35
|
+
}) {
|
|
36
|
+
const scope = (0, _postcssModulesScope.default)({
|
|
37
|
+
generateScopedName,
|
|
38
|
+
exportGlobals
|
|
39
|
+
});
|
|
40
|
+
const plugins = {
|
|
41
|
+
[behaviours.LOCAL]: [_postcssModulesValues.default, (0, _postcssModulesLocalByDefault.default)({
|
|
42
|
+
mode: "local"
|
|
43
|
+
}), _postcssModulesExtractImports.default, scope],
|
|
44
|
+
[behaviours.GLOBAL]: [_postcssModulesValues.default, (0, _postcssModulesLocalByDefault.default)({
|
|
45
|
+
mode: "global"
|
|
46
|
+
}), _postcssModulesExtractImports.default, scope]
|
|
47
|
+
};
|
|
48
|
+
return plugins[behaviour];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isValidBehaviour(behaviour) {
|
|
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
|
+
});
|
|
77
|
+
}
|