@parcel/transformer-postcss 2.0.0 → 2.2.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.
@@ -103,6 +103,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
103
103
 
104
104
  const COMPOSES_RE = /composes:.+from\s*("|').*("|')\s*;?/;
105
105
  const FROM_IMPORT_RE = /.+from\s*(?:"|')(.*)(?:"|')\s*;?/;
106
+ const LEGACY_MODULE_RE = /@value|(:global|:local)(?!\s*\()/i;
107
+ const MODULE_BY_NAME_RE = /\.module\./;
106
108
 
107
109
  var _default = new (_plugin().Transformer)({
108
110
  loadConfig({
@@ -128,7 +130,9 @@ var _default = new (_plugin().Transformer)({
128
130
  config,
129
131
  options
130
132
  }) {
131
- if (!config) {
133
+ let isLegacy = await isLegacyCssModule(asset);
134
+
135
+ if (!config && !isLegacy) {
132
136
  return;
133
137
  }
134
138
 
@@ -149,52 +153,64 @@ var _default = new (_plugin().Transformer)({
149
153
  resolve
150
154
  }) {
151
155
  asset.type = 'css';
156
+ let isLegacy = await isLegacyCssModule(asset);
157
+
158
+ if (isLegacy && !config) {
159
+ config = {
160
+ hydrated: {
161
+ plugins: [],
162
+ from: asset.filePath,
163
+ to: asset.filePath,
164
+ modules: {}
165
+ }
166
+ }; // TODO: warning?
167
+ }
152
168
 
153
169
  if (!config) {
154
170
  return [asset];
155
171
  }
156
172
 
157
173
  const postcss = await loadPostcss(options, asset.filePath);
174
+ let ast = (0, _nullthrows().default)(await asset.getAST());
175
+ let program = postcss.fromJSON(ast.program);
158
176
  let plugins = [...config.hydrated.plugins];
159
177
  let cssModules = null;
160
178
 
161
179
  if (config.hydrated.modules) {
180
+ asset.meta.cssModulesCompiled = true;
162
181
  plugins.push((0, _postcssModules().default)({
163
182
  getJSON: (filename, json) => cssModules = json,
164
183
  Loader: createLoader(asset, resolve),
165
- generateScopedName: (name, filename) => `_${name}_${(0, _hash().hashString)(_path().default.relative(options.projectRoot, filename)).substr(0, 6)}`,
184
+ generateScopedName: (name, filename) => `${name}_${(0, _hash().hashString)(_path().default.relative(options.projectRoot, filename)).substr(0, 6)}`,
166
185
  ...config.hydrated.modules
167
186
  }));
168
- }
169
-
170
- let ast = (0, _nullthrows().default)(await asset.getAST());
171
- let program = postcss.fromJSON(ast.program);
172
- let code = asset.isASTDirty() ? null : await asset.getCode();
173
-
174
- if (code == null || COMPOSES_RE.test(code)) {
175
- program.walkDecls(decl => {
176
- let [, importPath] = FROM_IMPORT_RE.exec(decl.value) || [];
177
-
178
- if (decl.prop === 'composes' && importPath != null) {
179
- let parsed = (0, _postcssValueParser().default)(decl.value);
180
- parsed.walk(node => {
181
- if (node.type === 'string') {
182
- asset.addDependency({
183
- specifier: importPath,
184
- specifierType: 'url',
185
- loc: {
186
- filePath: asset.filePath,
187
- start: decl.source.start,
188
- end: {
189
- line: decl.source.start.line,
190
- column: decl.source.start.column + importPath.length
187
+ let code = asset.isASTDirty() ? null : await asset.getCode();
188
+
189
+ if (code == null || COMPOSES_RE.test(code)) {
190
+ program.walkDecls(decl => {
191
+ let [, importPath] = FROM_IMPORT_RE.exec(decl.value) || [];
192
+
193
+ if (decl.prop === 'composes' && importPath != null) {
194
+ let parsed = (0, _postcssValueParser().default)(decl.value);
195
+ parsed.walk(node => {
196
+ if (node.type === 'string') {
197
+ asset.addDependency({
198
+ specifier: importPath,
199
+ specifierType: 'url',
200
+ loc: {
201
+ filePath: asset.filePath,
202
+ start: decl.source.start,
203
+ end: {
204
+ line: decl.source.start.line,
205
+ column: decl.source.start.column + importPath.length
206
+ }
191
207
  }
192
- }
193
- });
194
- }
195
- });
196
- }
197
- });
208
+ });
209
+ }
210
+ });
211
+ }
212
+ });
213
+ }
198
214
  } // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381
199
215
 
200
216
 
@@ -318,4 +334,13 @@ function loadPostcss(options, from) {
318
334
  saveDev: true,
319
335
  shouldAutoInstall: options.shouldAutoInstall
320
336
  });
337
+ }
338
+
339
+ async function isLegacyCssModule(asset) {
340
+ if (!MODULE_BY_NAME_RE.test(asset.filePath)) {
341
+ return false;
342
+ }
343
+
344
+ let code = await asset.getCode();
345
+ return LEGACY_MODULE_RE.test(code);
321
346
  }
package/lib/loadConfig.js CHANGED
@@ -51,20 +51,7 @@ var _loadPlugins = _interopRequireDefault(require("./loadPlugins"));
51
51
 
52
52
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
53
53
 
54
- const MODULE_BY_NAME_RE = /\.module\./;
55
-
56
54
  async function configHydrator(configFile, config, resolveFrom, options) {
57
- // Use a basic, modules-only PostCSS config if the file opts in by a name
58
- // like foo.module.css
59
- if (configFile == null && config.searchPath.match(MODULE_BY_NAME_RE)) {
60
- configFile = {
61
- plugins: {
62
- 'postcss-modules': {}
63
- }
64
- };
65
- resolveFrom = __filename;
66
- }
67
-
68
55
  if (configFile == null) {
69
56
  return;
70
57
  } // Load the custom config...
@@ -114,6 +101,10 @@ async function load({
114
101
  options,
115
102
  logger
116
103
  }) {
104
+ if (!config.isSource) {
105
+ return;
106
+ }
107
+
117
108
  let configFile = await config.getConfig(['.postcssrc', '.postcssrc.json', '.postcssrc.js', 'postcss.config.js'], {
118
109
  packageKey: 'postcss'
119
110
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/transformer-postcss",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,21 +17,21 @@
17
17
  "source": "src/PostCSSTransformer.js",
18
18
  "engines": {
19
19
  "node": ">= 12.0.0",
20
- "parcel": "^2.0.0"
20
+ "parcel": "^2.2.0"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/hash": "^2.0.0",
24
- "@parcel/plugin": "^2.0.0",
25
- "@parcel/utils": "^2.0.0",
23
+ "@parcel/hash": "^2.2.0",
24
+ "@parcel/plugin": "^2.2.0",
25
+ "@parcel/utils": "^2.2.0",
26
26
  "clone": "^2.1.1",
27
27
  "css-modules-loader-core": "^1.1.0",
28
28
  "nullthrows": "^1.1.1",
29
29
  "postcss-modules": "^3.2.2",
30
30
  "postcss-value-parser": "^4.1.0",
31
- "semver": "^5.4.1"
31
+ "semver": "^5.7.1"
32
32
  },
33
33
  "devDependencies": {
34
34
  "postcss": "^8.3.0"
35
35
  },
36
- "gitHead": "84da50ae6943bff9741e2fc76d2f4968074cbbd6"
36
+ "gitHead": "4745cd3023f8d5a5adcf9e565d5b82d1418dc262"
37
37
  }
@@ -1,6 +1,6 @@
1
1
  // @flow
2
2
 
3
- import type {FilePath, MutableAsset, PluginOptions} from '@parcel/types';
3
+ import type {FilePath, Asset, MutableAsset, PluginOptions} from '@parcel/types';
4
4
 
5
5
  import {hashString} from '@parcel/hash';
6
6
  import {glob} from '@parcel/utils';
@@ -18,6 +18,8 @@ import {POSTCSS_RANGE} from './constants';
18
18
 
19
19
  const COMPOSES_RE = /composes:.+from\s*("|').*("|')\s*;?/;
20
20
  const FROM_IMPORT_RE = /.+from\s*(?:"|')(.*)(?:"|')\s*;?/;
21
+ const LEGACY_MODULE_RE = /@value|(:global|:local)(?!\s*\()/i;
22
+ const MODULE_BY_NAME_RE = /\.module\./;
21
23
 
22
24
  export default (new Transformer({
23
25
  loadConfig({config, options, logger}) {
@@ -31,7 +33,8 @@ export default (new Transformer({
31
33
  },
32
34
 
33
35
  async parse({asset, config, options}) {
34
- if (!config) {
36
+ let isLegacy = await isLegacyCssModule(asset);
37
+ if (!config && !isLegacy) {
35
38
  return;
36
39
  }
37
40
 
@@ -50,55 +53,70 @@ export default (new Transformer({
50
53
 
51
54
  async transform({asset, config, options, resolve}) {
52
55
  asset.type = 'css';
56
+ let isLegacy = await isLegacyCssModule(asset);
57
+ if (isLegacy && !config) {
58
+ config = {
59
+ hydrated: {
60
+ plugins: [],
61
+ from: asset.filePath,
62
+ to: asset.filePath,
63
+ modules: {},
64
+ },
65
+ };
66
+
67
+ // TODO: warning?
68
+ }
69
+
53
70
  if (!config) {
54
71
  return [asset];
55
72
  }
56
73
 
57
74
  const postcss: Postcss = await loadPostcss(options, asset.filePath);
75
+ let ast = nullthrows(await asset.getAST());
76
+ let program = postcss.fromJSON(ast.program);
58
77
 
59
78
  let plugins = [...config.hydrated.plugins];
60
79
  let cssModules: ?{|[string]: string|} = null;
61
80
  if (config.hydrated.modules) {
81
+ asset.meta.cssModulesCompiled = true;
62
82
  plugins.push(
63
83
  postcssModules({
64
84
  getJSON: (filename, json) => (cssModules = json),
65
85
  Loader: createLoader(asset, resolve),
66
86
  generateScopedName: (name, filename) =>
67
- `_${name}_${hashString(
87
+ `${name}_${hashString(
68
88
  path.relative(options.projectRoot, filename),
69
89
  ).substr(0, 6)}`,
70
90
  ...config.hydrated.modules,
71
91
  }),
72
92
  );
73
- }
74
93
 
75
- let ast = nullthrows(await asset.getAST());
76
- let program = postcss.fromJSON(ast.program);
77
- let code = asset.isASTDirty() ? null : await asset.getCode();
78
- if (code == null || COMPOSES_RE.test(code)) {
79
- program.walkDecls(decl => {
80
- let [, importPath] = FROM_IMPORT_RE.exec(decl.value) || [];
81
- if (decl.prop === 'composes' && importPath != null) {
82
- let parsed = valueParser(decl.value);
83
-
84
- parsed.walk(node => {
85
- if (node.type === 'string') {
86
- asset.addDependency({
87
- specifier: importPath,
88
- specifierType: 'url',
89
- loc: {
90
- filePath: asset.filePath,
91
- start: decl.source.start,
92
- end: {
93
- line: decl.source.start.line,
94
- column: decl.source.start.column + importPath.length,
94
+ let code = asset.isASTDirty() ? null : await asset.getCode();
95
+ if (code == null || COMPOSES_RE.test(code)) {
96
+ program.walkDecls(decl => {
97
+ let [, importPath] = FROM_IMPORT_RE.exec(decl.value) || [];
98
+ if (decl.prop === 'composes' && importPath != null) {
99
+ let parsed = valueParser(decl.value);
100
+
101
+ parsed.walk(node => {
102
+ if (node.type === 'string') {
103
+ asset.addDependency({
104
+ specifier: importPath,
105
+ specifierType: 'url',
106
+ loc: {
107
+ filePath: asset.filePath,
108
+ start: decl.source.start,
109
+ end: {
110
+ line: decl.source.start.line,
111
+ column: decl.source.start.column + importPath.length,
112
+ },
95
113
  },
96
- },
97
- });
98
- }
99
- });
100
- }
101
- });
114
+ });
115
+ }
116
+ });
117
+ }
118
+ });
119
+ }
102
120
  }
103
121
 
104
122
  // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381
@@ -218,3 +236,12 @@ function loadPostcss(options: PluginOptions, from: FilePath): Promise<Postcss> {
218
236
  shouldAutoInstall: options.shouldAutoInstall,
219
237
  });
220
238
  }
239
+
240
+ async function isLegacyCssModule(asset: Asset | MutableAsset) {
241
+ if (!MODULE_BY_NAME_RE.test(asset.filePath)) {
242
+ return false;
243
+ }
244
+
245
+ let code = await asset.getCode();
246
+ return LEGACY_MODULE_RE.test(code);
247
+ }
package/src/loadConfig.js CHANGED
@@ -13,8 +13,6 @@ import {POSTCSS_RANGE} from './constants';
13
13
 
14
14
  import loadExternalPlugins from './loadPlugins';
15
15
 
16
- const MODULE_BY_NAME_RE = /\.module\./;
17
-
18
16
  type ConfigResult = {|
19
17
  raw: any,
20
18
  hydrated: {|
@@ -31,17 +29,6 @@ async function configHydrator(
31
29
  resolveFrom: ?FilePath,
32
30
  options: PluginOptions,
33
31
  ): Promise<?ConfigResult> {
34
- // Use a basic, modules-only PostCSS config if the file opts in by a name
35
- // like foo.module.css
36
- if (configFile == null && config.searchPath.match(MODULE_BY_NAME_RE)) {
37
- configFile = {
38
- plugins: {
39
- 'postcss-modules': {},
40
- },
41
- };
42
- resolveFrom = __filename;
43
- }
44
-
45
32
  if (configFile == null) {
46
33
  return;
47
34
  }
@@ -104,6 +91,10 @@ export async function load({
104
91
  options: PluginOptions,
105
92
  logger: PluginLogger,
106
93
  |}): Promise<?ConfigResult> {
94
+ if (!config.isSource) {
95
+ return;
96
+ }
97
+
107
98
  let configFile: any = await config.getConfig(
108
99
  ['.postcssrc', '.postcssrc.json', '.postcssrc.js', 'postcss.config.js'],
109
100
  {packageKey: 'postcss'},