postcss-modules 5.0.0 → 6.0.1

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.
@@ -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
- (0, _fs.writeFile)(`${cssFile}.json`, JSON.stringify(json), e => e ? reject(e) : resolve(json));
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.isValidBehaviour = isValidBehaviour;
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: 'local'
42
+ mode: "local"
38
43
  }), _postcssModulesExtractImports.default, scope],
39
44
  [behaviours.GLOBAL]: [_postcssModulesValues.default, (0, _postcssModulesLocalByDefault.default)({
40
- mode: 'global'
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
  }
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = unquote;
7
7
  // copied from https://github.com/lakenen/node-unquote
8
- var reg = /['"]/;
8
+ const reg = /['"]/;
9
9
 
10
10
  function unquote(str) {
11
11
  if (!str) {
package/index.d.ts CHANGED
@@ -1,60 +1,48 @@
1
1
  import type { Plugin } from "postcss";
2
2
 
3
- declare type GenerateScopedNameFunction = (
4
- name: string,
5
- filename: string,
6
- css: string
7
- ) => string;
3
+ declare type GenerateScopedNameFunction = (name: string, filename: string, css: string) => string;
8
4
 
9
5
  declare type LocalsConventionFunction = (
10
- originalClassName: string,
11
- generatedClassName: string,
12
- inputFile: string
6
+ originalClassName: string,
7
+ generatedClassName: string,
8
+ inputFile: string
13
9
  ) => string;
14
10
 
15
11
  declare class Loader {
16
- constructor(root: string, plugins: Plugin[]);
12
+ constructor(root: string, plugins: Plugin[]);
17
13
 
18
- fetch(
19
- file: string,
20
- relativeTo: string,
21
- depTrace: string
22
- ): Promise<{ [key: string]: string }>;
14
+ fetch(file: string, relativeTo: string, depTrace: string): Promise<{ [key: string]: string }>;
23
15
 
24
- finalSource?: string | undefined;
16
+ finalSource?: string | undefined;
25
17
  }
26
18
 
27
19
  declare interface Options {
28
- getJSON?(
29
- cssFilename: string,
30
- json: { [name: string]: string },
31
- outputFilename?: string
32
- ): void;
20
+ getJSON?(cssFilename: string, json: { [name: string]: string }, outputFilename?: string): void;
33
21
 
34
- localsConvention?:
35
- | "camelCase"
36
- | "camelCaseOnly"
37
- | "dashes"
38
- | "dashesOnly"
39
- | LocalsConventionFunction;
22
+ localsConvention?:
23
+ | "camelCase"
24
+ | "camelCaseOnly"
25
+ | "dashes"
26
+ | "dashesOnly"
27
+ | LocalsConventionFunction;
40
28
 
41
- scopeBehaviour?: "global" | "local";
42
- globalModulePaths?: RegExp[];
29
+ scopeBehaviour?: "global" | "local";
30
+ globalModulePaths?: RegExp[];
43
31
 
44
- generateScopedName?: string | GenerateScopedNameFunction;
32
+ generateScopedName?: string | GenerateScopedNameFunction;
45
33
 
46
- hashPrefix?: string;
47
- exportGlobals?: boolean;
48
- root?: string;
34
+ hashPrefix?: string;
35
+ exportGlobals?: boolean;
36
+ root?: string;
49
37
 
50
- Loader?: typeof Loader;
38
+ Loader?: typeof Loader;
51
39
 
52
- resolve?: (file: string) => string | Promise<string>;
40
+ resolve?: (file: string, importer: string) => string | null | Promise<string | null>;
53
41
  }
54
42
 
55
43
  declare interface PostcssModulesPlugin {
56
- (options: Options): Plugin;
57
- postcss: true;
44
+ (options: Options): Plugin;
45
+ postcss: true;
58
46
  }
59
47
 
60
48
  declare const PostcssModulesPlugin: PostcssModulesPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-modules",
3
- "version": "5.0.0",
3
+ "version": "6.0.1",
4
4
  "description": "PostCSS plugin to use CSS Modules everywhere",
5
5
  "main": "build/index.js",
6
6
  "types": "index.d.ts",
@@ -20,13 +20,13 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "generic-names": "^4.0.0",
23
- "icss-replace-symbols": "^1.1.0",
23
+ "icss-utils": "^5.1.0",
24
24
  "lodash.camelcase": "^4.3.0",
25
- "postcss-modules-extract-imports": "^3.0.0",
26
- "postcss-modules-local-by-default": "^4.0.0",
27
- "postcss-modules-scope": "^3.0.0",
25
+ "postcss-modules-extract-imports": "^3.1.0",
26
+ "postcss-modules-local-by-default": "^4.0.5",
27
+ "postcss-modules-scope": "^3.2.0",
28
28
  "postcss-modules-values": "^4.0.0",
29
- "string-hash": "^1.1.1"
29
+ "string-hash": "^1.1.3"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "postcss": "^8.0.0"
@@ -1,131 +0,0 @@
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 _fs = _interopRequireDefault(require("fs"));
11
-
12
- var _path = _interopRequireDefault(require("path"));
13
-
14
- var _parser = _interopRequireDefault(require("./parser"));
15
-
16
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
- // 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
- load(sourceString, sourcePath, trace, pathFetcher) {
25
- let parser = new _parser.default(pathFetcher, trace);
26
- return (0, _postcss.default)(this.plugins.concat([parser.plugin()])).process(sourceString, {
27
- from: sourcePath
28
- }).then(result => {
29
- return {
30
- injectableSource: result.css,
31
- exportTokens: parser.exportTokens
32
- };
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
- } else if (a.length > b.length) {
49
- return a.substring(0, b.length) <= b ? -1 : 1;
50
- } else {
51
- return a < b ? -1 : 1;
52
- }
53
- };
54
-
55
- class FileSystemLoader {
56
- constructor(root, plugins) {
57
- if (root === '/' && process.platform === "win32") {
58
- const cwdDrive = process.cwd().slice(0, 3);
59
-
60
- if (!/^[A-Z]:\\$/.test(cwdDrive)) {
61
- throw new Error(`Failed to obtain root from "${process.cwd()}".`);
62
- }
63
-
64
- root = cwdDrive;
65
- }
66
-
67
- this.root = root;
68
- this.sources = {};
69
- this.traces = {};
70
- this.importNr = 0;
71
- this.core = new Core(plugins);
72
- this.tokensByFile = {};
73
- }
74
-
75
- fetch(_newPath, relativeTo, _trace) {
76
- let newPath = _newPath.replace(/^["']|["']$/g, ""),
77
- trace = _trace || String.fromCharCode(this.importNr++);
78
-
79
- return new Promise((resolve, reject) => {
80
- let relativeDir = _path.default.dirname(relativeTo),
81
- rootRelativePath = _path.default.resolve(relativeDir, newPath),
82
- fileRelativePath = _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
83
-
84
-
85
- if (newPath[0] !== "." && !_path.default.isAbsolute(newPath)) {
86
- try {
87
- fileRelativePath = require.resolve(newPath);
88
- } catch (e) {// noop
89
- }
90
- }
91
-
92
- const tokens = this.tokensByFile[fileRelativePath];
93
-
94
- if (tokens) {
95
- return resolve(tokens);
96
- }
97
-
98
- _fs.default.readFile(fileRelativePath, "utf-8", (err, source) => {
99
- if (err) reject(err);
100
- this.core.load(source, rootRelativePath, trace, this.fetch.bind(this)).then(({
101
- injectableSource,
102
- exportTokens
103
- }) => {
104
- this.sources[fileRelativePath] = injectableSource;
105
- this.traces[trace] = fileRelativePath;
106
- this.tokensByFile[fileRelativePath] = exportTokens;
107
- resolve(exportTokens);
108
- }, reject);
109
- });
110
- });
111
- }
112
-
113
- get finalSource() {
114
- const traces = this.traces;
115
- const sources = this.sources;
116
- let written = new Set();
117
- return Object.keys(traces).sort(traceKeySorter).map(key => {
118
- const filename = traces[key];
119
-
120
- if (written.has(filename)) {
121
- return null;
122
- }
123
-
124
- written.add(filename);
125
- return sources[filename];
126
- }).join("");
127
- }
128
-
129
- }
130
-
131
- exports.default = FileSystemLoader;
@@ -1,17 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = generateScopedName;
7
-
8
- var _stringHash = _interopRequireDefault(require("string-hash"));
9
-
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
-
12
- function generateScopedName(name, filename, css) {
13
- const i = css.indexOf(`.${name}`);
14
- const lineNumber = css.substr(0, i).split(/[\r\n]/).length;
15
- const hash = (0, _stringHash.default)(css).toString(36).substr(0, 5);
16
- return `_${name}_${hash}_${lineNumber}`;
17
- }