postcss-modules 9.0.0 → 9.1.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 CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## 9.1.0
4
+
5
+ ### Added
6
+
7
+ - Added `scopeBehaviour: "pure"`, matching css-loader's pure CSS Modules mode: every selector branch must contain a local class or ID. Includes TypeScript support, escape comments, dependency validation, and documentation. [#106](https://github.com/madyankin/postcss-modules/issues/106)
8
+
9
+ ### Fixed
10
+
11
+ - Preserved source filenames on CSS prepended from `composes` dependencies, so downstream plugins can resolve relative paths such as `url()` against the correct file. [#173](https://github.com/madyankin/postcss-modules/pull/173) by [@soreavis](https://github.com/soreavis)
12
+ - Reported files read through `composes` and `@value` as PostCSS dependency messages, allowing watch-mode runners to rebuild when imported CSS changes. [#174](https://github.com/madyankin/postcss-modules/pull/174) by [@mkantautas](https://github.com/mkantautas)
13
+
14
+ ## 9.0.1
15
+
16
+ ### Fixed
17
+
18
+ - Added a type declaration for `postcss-modules/loader` so TypeScript users can extend it with `declare module` without hitting a type error. [#172](https://github.com/madyankin/postcss-modules/pull/172) by [@sapphi-red](https://github.com/sapphi-red)
19
+
3
20
  ## 9.0.0
4
21
 
5
22
  ### Breaking
package/README.md CHANGED
@@ -195,13 +195,22 @@ this behaviour using the `scopeBehaviour` option:
195
195
  ```js
196
196
  postcss([
197
197
  require("postcss-modules")({
198
- scopeBehaviour: "global", // can be 'global' or 'local',
198
+ scopeBehaviour: "global", // can be 'global', 'local' or 'pure',
199
199
  }),
200
200
  ]);
201
201
  ```
202
202
 
203
+ Like `local` mode, `pure` mode localizes class and ID names, but it also requires
204
+ every comma-separated selector branch to contain at least one local class or ID.
205
+ For example, `.button :global(.icon)` is allowed, while `:global(.page)` is
206
+ rejected. Add `/* cssmodules-pure-ignore */` immediately before a selector or
207
+ at-rule to skip this check once, or put
208
+ `/* cssmodules-pure-no-check */` at the top of a file to disable the check for
209
+ the whole file.
210
+
203
211
  To define paths for global modules, use the `globalModulePaths` option.
204
- It is an array with regular expressions defining the paths:
212
+ It is an array with regular expressions defining the paths. Matching files are
213
+ treated as global even when `scopeBehaviour` is `"pure"`:
205
214
 
206
215
  ```js
207
216
  postcss([
@@ -93,26 +93,44 @@ class FileSystemLoader {
93
93
  if (tokens) return tokens;
94
94
  return new Promise((resolve, reject)=>{
95
95
  this.fs.readFile(fileRelativePath, "utf-8", async (err, source)=>{
96
- if (err) reject(err);
97
- const { injectableSource, exportTokens } = await this.core.load(source, rootRelativePath, trace, this.fetch.bind(this));
98
- this.sources[fileRelativePath] = injectableSource;
99
- this.traces[trace] = fileRelativePath;
100
- this.tokensByFile[fileRelativePath] = exportTokens;
101
- resolve(exportTokens);
96
+ if (err) {
97
+ reject(err);
98
+ return;
99
+ }
100
+ try {
101
+ const { injectableSource, exportTokens } = await this.core.load(source, rootRelativePath, trace, this.fetch.bind(this));
102
+ this.sources[fileRelativePath] = injectableSource;
103
+ this.traces[trace] = fileRelativePath;
104
+ this.tokensByFile[fileRelativePath] = exportTokens;
105
+ resolve(exportTokens);
106
+ } catch (error) {
107
+ reject(error);
108
+ }
102
109
  });
103
110
  });
104
111
  }
105
- get finalSource() {
112
+ get finalSources() {
106
113
  const traces = this.traces;
107
114
  const sources = this.sources;
108
- let written = new Set();
115
+ const written = new Set();
109
116
  return Object.keys(traces).sort(traceKeySorter).map((key)=>{
110
117
  const filename = traces[key];
111
118
  if (written.has(filename)) {
112
119
  return null;
113
120
  }
114
121
  written.add(filename);
115
- return sources[filename];
116
- }).join("");
122
+ return {
123
+ file: filename,
124
+ source: sources[filename]
125
+ };
126
+ }).filter(Boolean);
127
+ }
128
+ // Files read through `composes` and `@value` imports, including nested ones,
129
+ // in the order their CSS is prepended.
130
+ get dependencies() {
131
+ return this.finalSources.map((entry)=>entry.file);
132
+ }
133
+ get finalSource() {
134
+ return this.finalSources.map((entry)=>entry.source).join("");
117
135
  }
118
136
  }
@@ -82,8 +82,67 @@ function makePlugin(opts) {
82
82
  css.walkRules((rule)=>{
83
83
  if (rule.selector.trim() === "") rule.remove();
84
84
  });
85
- const out = loader.finalSource;
86
- if (out) css.prepend(out);
85
+ // Prepend the traced dependencies exactly as before, then re-parse
86
+ // each dependency with its own `from` and graft those sources onto
87
+ // the prepended nodes, pairing them by position. The pairing holds
88
+ // because each dependency is complete CSS, so parsing the
89
+ // concatenation yields the same node sequence as parsing each part.
90
+ // If malformed CSS merges nodes across a file boundary the counts
91
+ // differ and the graft is skipped, leaving the nodes unattributed
92
+ // as before. Plugins that run after this one (e.g. url rewriters)
93
+ // need `source.input.file` to resolve relative paths against the
94
+ // file the CSS actually came from — see #149.
95
+ const sources = Array.isArray(loader.finalSources) ? loader.finalSources : null;
96
+ if (sources) {
97
+ const joined = sources.map((entry)=>entry.source).join("");
98
+ if (joined) {
99
+ const countBefore = css.nodes.length;
100
+ css.prepend(joined);
101
+ const prepended = css.nodes.slice(0, css.nodes.length - countBefore);
102
+ const sourceSeq = [];
103
+ for (const { file, source } of sources){
104
+ _postcss.default.parse(source, {
105
+ from: file
106
+ }).walk((node)=>{
107
+ sourceSeq.push(node.source);
108
+ });
109
+ }
110
+ let count = 0;
111
+ for (const node of prepended){
112
+ count += 1;
113
+ if (typeof node.walk === "function") {
114
+ node.walk(()=>{
115
+ count += 1;
116
+ });
117
+ }
118
+ }
119
+ if (count === sourceSeq.length) {
120
+ let i = 0;
121
+ for (const node of prepended){
122
+ node.source = sourceSeq[i++];
123
+ if (typeof node.walk === "function") {
124
+ node.walk((child)=>{
125
+ child.source = sourceSeq[i++];
126
+ });
127
+ }
128
+ }
129
+ }
130
+ }
131
+ } else {
132
+ // Custom loaders may not implement `finalSources`.
133
+ const out = loader.finalSource;
134
+ if (out) css.prepend(out);
135
+ }
136
+ // Report the files read through `composes` and `@value` imports, so
137
+ // runners can rebuild this file when one of them changes.
138
+ for (const file of loader.dependencies || []){
139
+ result.messages.push({
140
+ type: "dependency",
141
+ plugin: PLUGIN_NAME,
142
+ file,
143
+ parent: result.opts.from || inputFile
144
+ });
145
+ }
87
146
  if (opts.localsConvention) {
88
147
  const reducer = (0, _localsConvention.makeLocalsConventionReducer)(opts.localsConvention, inputFile);
89
148
  parser.exportTokens = Object.entries(parser.exportTokens).reduce(reducer, {});
package/build/scoping.js CHANGED
@@ -35,7 +35,8 @@ function _interop_require_default(obj) {
35
35
  }
36
36
  const behaviours = {
37
37
  LOCAL: "local",
38
- GLOBAL: "global"
38
+ GLOBAL: "global",
39
+ PURE: "pure"
39
40
  };
40
41
  function getDefaultPlugins({ behaviour, generateScopedName, exportGlobals }) {
41
42
  const scope = (0, _postcssmodulesscope.default)({
@@ -58,6 +59,14 @@ function getDefaultPlugins({ behaviour, generateScopedName, exportGlobals }) {
58
59
  }),
59
60
  _postcssmodulesextractimports.default,
60
61
  scope
62
+ ],
63
+ [behaviours.PURE]: [
64
+ _postcssmodulesvalues.default,
65
+ (0, _postcssmoduleslocalbydefault.default)({
66
+ mode: "pure"
67
+ }),
68
+ _postcssmodulesextractimports.default,
69
+ scope
61
70
  ]
62
71
  };
63
72
  return plugins[behaviour];
package/index.d.ts CHANGED
@@ -28,7 +28,7 @@ declare interface Options {
28
28
  | "none"
29
29
  | LocalsConventionFunction;
30
30
 
31
- scopeBehaviour?: "global" | "local";
31
+ scopeBehaviour?: "global" | "local" | "pure";
32
32
  globalModulePaths?: RegExp[];
33
33
 
34
34
  generateScopedName?: string | GenerateScopedNameFunction;
@@ -50,5 +50,3 @@ declare interface PostcssModulesPlugin {
50
50
  declare const PostcssModulesPlugin: PostcssModulesPlugin;
51
51
 
52
52
  export = PostcssModulesPlugin;
53
-
54
- declare module "postcss-modules/loader" {}
package/loader.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-modules",
3
- "version": "9.0.0",
3
+ "version": "9.1.0",
4
4
  "description": "PostCSS plugin to use CSS Modules everywhere",
5
5
  "main": "build/index.js",
6
6
  "types": "index.d.ts",
@@ -9,7 +9,10 @@
9
9
  "types": "./index.d.ts",
10
10
  "default": "./build/index.js"
11
11
  },
12
- "./loader": "./build/loader.mjs",
12
+ "./loader": {
13
+ "types": "./loader.d.ts",
14
+ "default": "./build/loader.mjs"
15
+ },
13
16
  "./package.json": "./package.json"
14
17
  },
15
18
  "keywords": [
package/.husky/pre-commit DELETED
@@ -1 +0,0 @@
1
- npx lint-staged
package/.swcrc DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "jsc": {
3
- "parser": {
4
- "syntax": "ecmascript"
5
- },
6
- "target": "es2022"
7
- },
8
- "module": {
9
- "type": "commonjs"
10
- }
11
- }
package/eslint.config.mjs DELETED
@@ -1,26 +0,0 @@
1
- import js from "@eslint/js";
2
- import jest from "eslint-plugin-jest";
3
- import globals from "globals";
4
-
5
- export default [
6
- js.configs.recommended,
7
- {
8
- languageOptions: {
9
- ecmaVersion: 2022,
10
- sourceType: "module",
11
- globals: {
12
- ...globals.node,
13
- ...globals.es2021,
14
- },
15
- },
16
- plugins: { jest },
17
- },
18
- {
19
- files: ["test/**/*.js"],
20
- languageOptions: {
21
- globals: {
22
- ...jest.environments.globals.globals,
23
- },
24
- },
25
- },
26
- ];
package/jest.config.js DELETED
@@ -1,5 +0,0 @@
1
- module.exports = {
2
- transform: {
3
- "^.+\\.js$": ["@swc/jest"],
4
- },
5
- };