@parcel/transformer-stylus 2.8.3 → 2.9.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.
@@ -4,135 +4,93 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  function _plugin() {
9
8
  const data = require("@parcel/plugin");
10
-
11
9
  _plugin = function () {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _utils() {
19
15
  const data = require("@parcel/utils");
20
-
21
16
  _utils = function () {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  function _path() {
29
22
  const data = _interopRequireDefault(require("path"));
30
-
31
23
  _path = function () {
32
24
  return data;
33
25
  };
34
-
35
26
  return data;
36
27
  }
37
-
38
28
  function _fs() {
39
29
  const data = _interopRequireDefault(require("fs"));
40
-
41
30
  _fs = function () {
42
31
  return data;
43
32
  };
44
-
45
33
  return data;
46
34
  }
47
-
48
35
  function _stylus() {
49
36
  const data = _interopRequireDefault(require("stylus"));
50
-
51
37
  _stylus = function () {
52
38
  return data;
53
39
  };
54
-
55
40
  return data;
56
41
  }
57
-
58
42
  function _parser() {
59
43
  const data = _interopRequireDefault(require("stylus/lib/parser"));
60
-
61
44
  _parser = function () {
62
45
  return data;
63
46
  };
64
-
65
47
  return data;
66
48
  }
67
-
68
49
  function _depsResolver() {
69
50
  const data = _interopRequireDefault(require("stylus/lib/visitor/deps-resolver"));
70
-
71
51
  _depsResolver = function () {
72
52
  return data;
73
53
  };
74
-
75
54
  return data;
76
55
  }
77
-
78
56
  function _nodes() {
79
57
  const data = _interopRequireDefault(require("stylus/lib/nodes"));
80
-
81
58
  _nodes = function () {
82
59
  return data;
83
60
  };
84
-
85
61
  return data;
86
62
  }
87
-
88
63
  function _utils2() {
89
64
  const data = _interopRequireDefault(require("stylus/lib/utils"));
90
-
91
65
  _utils2 = function () {
92
66
  return data;
93
67
  };
94
-
95
68
  return data;
96
69
  }
97
-
98
70
  function _evaluator() {
99
71
  const data = _interopRequireDefault(require("stylus/lib/visitor/evaluator"));
100
-
101
72
  _evaluator = function () {
102
73
  return data;
103
74
  };
104
-
105
75
  return data;
106
76
  }
107
-
108
77
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
109
-
110
78
  const URL_RE = /^(?:url\s*\(\s*)?['"]?(?:[#/]|(?:https?:)?\/\/)/i;
111
-
112
79
  var _default = new (_plugin().Transformer)({
113
80
  async loadConfig({
114
81
  config
115
82
  }) {
116
- let configFile = await config.getConfig(['.stylusrc', '.stylusrc.js', '.stylusrc.cjs'], {
83
+ let configFile = await config.getConfig(['.stylusrc', '.stylusrc.js', '.stylusrc.cjs', '.stylusrc.mjs'], {
117
84
  packageKey: 'stylus'
118
85
  });
119
-
120
86
  if (configFile) {
121
- let isJavascript = _path().default.extname(configFile.filePath).endsWith('js');
122
-
123
- if (isJavascript) {
124
- config.invalidateOnStartup();
125
- } // Resolve relative paths from config file
126
-
127
-
87
+ // Resolve relative paths from config file
128
88
  if (configFile.contents.paths) {
129
89
  configFile.contents.paths = configFile.contents.paths.map(p => _path().default.resolve(_path().default.dirname(configFile.filePath), p));
130
90
  }
131
-
132
91
  return configFile.contents;
133
92
  }
134
93
  },
135
-
136
94
  async transform({
137
95
  asset,
138
96
  resolve,
@@ -141,11 +99,12 @@ var _default = new (_plugin().Transformer)({
141
99
  }) {
142
100
  let stylusConfig = config !== null && config !== void 0 ? config : {};
143
101
  let code = await asset.getCode();
144
- let style = (0, _stylus().default)(code, { ...stylusConfig
102
+ let style = (0, _stylus().default)(code, {
103
+ ...stylusConfig
145
104
  });
146
105
  style.set('filename', asset.filePath);
147
- style.set('include css', true); // Setup a handler for the URL function so we add dependencies for linked assets.
148
-
106
+ style.set('include css', true);
107
+ // Setup a handler for the URL function so we add dependencies for linked assets.
149
108
  style.define('url', node => {
150
109
  let filename = asset.addURLDependency(node.val, {
151
110
  loc: (0, _utils().createDependencyLocation)({
@@ -165,54 +124,49 @@ var _default = new (_plugin().Transformer)({
165
124
  asset.meta.hasDependencies = false;
166
125
  return [asset];
167
126
  }
168
-
169
127
  });
170
-
171
128
  exports.default = _default;
172
-
173
129
  function attemptResolve(importedPath, filepath, asset, resolve, deps) {
174
130
  if (deps.has(importedPath)) {
175
131
  return;
176
132
  }
177
-
178
133
  if ((0, _utils().isGlob)(importedPath)) {
179
134
  // Invalidate when new files are created that match the glob pattern.
180
135
  let absoluteGlob = _path().default.resolve(_path().default.dirname(filepath), importedPath);
181
-
182
136
  asset.invalidateOnFileCreate({
183
137
  glob: absoluteGlob
184
138
  });
185
139
  deps.set(importedPath, (0, _utils().glob)(absoluteGlob, asset.fs, {
186
140
  onlyFiles: true
187
- }).then(entries => Promise.all(entries.map(entry => resolve(filepath, './' + _path().default.relative(_path().default.dirname(filepath), entry))))));
141
+ }).then(entries => Promise.all(entries.map(entry => resolve(filepath, './' + _path().default.relative(_path().default.dirname(filepath), entry), {
142
+ packageConditions: ['stylus', 'style']
143
+ })))));
188
144
  } else {
189
- deps.set(importedPath, resolve(filepath, importedPath));
145
+ deps.set(importedPath, resolve(filepath, importedPath, {
146
+ packageConditions: ['stylus', 'style']
147
+ }));
190
148
  }
191
149
  }
192
-
193
150
  async function getDependencies(code, filepath, asset, resolve, options, parcelOptions, nativeGlob, seen = new Set(), includeImports = true) {
194
151
  seen.add(filepath);
195
152
  _nodes().default.filename = asset.filePath;
196
153
  let parser = new (_parser().default)(code, options);
197
154
  let ast = parser.parse();
198
155
  let deps = new Map();
199
-
200
156
  if (includeImports && options.imports) {
201
157
  for (let importedPath of options.imports) {
202
158
  attemptResolve(importedPath, filepath, asset, resolve, deps);
203
159
  }
204
160
  }
205
-
206
161
  class ImportVisitor extends _depsResolver().default {
207
162
  visitImport(imported) {
208
163
  let importedPath = imported.path.first.string;
209
164
  attemptResolve(importedPath, filepath, asset, resolve, deps);
210
165
  }
211
-
212
166
  }
167
+ new ImportVisitor(ast, options).visit(ast);
213
168
 
214
- new ImportVisitor(ast, options).visit(ast); // Recursively process depdendencies, and return a map with all resolved paths.
215
-
169
+ // Recursively process depdendencies, and return a map with all resolved paths.
216
170
  let res = new Map();
217
171
  await Promise.all(Array.from(deps.entries()).map(async ([importedPath, resolved]) => {
218
172
  try {
@@ -220,47 +174,41 @@ async function getDependencies(code, filepath, asset, resolve, options, parcelOp
220
174
  } catch (err) {
221
175
  resolved = null;
222
176
  }
223
-
224
177
  let found;
225
-
226
178
  if (resolved && (!Array.isArray(resolved) || resolved.length > 0)) {
227
179
  found = Array.isArray(resolved) ? resolved : [resolved];
228
180
  res.set(importedPath, resolved);
229
181
  } else {
230
182
  // If we couldn't resolve, try the normal stylus resolver.
231
183
  // We just need to do this to keep track of the dependencies - stylus does the real work.
184
+
232
185
  // support optional .styl
233
186
  let originalPath = importedPath;
234
-
235
187
  if (!/\.styl$/i.test(importedPath)) {
236
188
  importedPath += '.styl';
237
- } // Patch the native FS so we use Parcel's FS, and track files that are
238
- // checked so we invalidate the cache when they are created.
239
-
189
+ }
240
190
 
191
+ // Patch the native FS so we use Parcel's FS, and track files that are
192
+ // checked so we invalidate the cache when they are created.
241
193
  let restore = patchNativeFS(asset.fs, nativeGlob);
242
194
  let paths = [...new Set((options.paths || []).concat(_path().default.dirname(filepath || '.')))];
243
195
  found = _utils2().default.find(importedPath, paths, filepath);
244
-
245
196
  if (!found) {
246
197
  found = _utils2().default.lookupIndex(originalPath, paths, filepath);
247
198
  }
248
-
249
199
  for (let invalidation of restore()) {
250
200
  asset.invalidateOnFileCreate(invalidation);
251
201
  }
252
-
253
202
  if (!found) {
254
203
  throw new Error('failed to locate file ' + originalPath);
255
204
  }
256
- } // Recursively process resolved files as well to get nested deps
257
-
205
+ }
258
206
 
207
+ // Recursively process resolved files as well to get nested deps
259
208
  for (let resolved of found) {
260
209
  if (!seen.has(resolved)) {
261
210
  await asset.invalidateOnFileChange(resolved);
262
211
  let code = await asset.fs.readFile(resolved, 'utf8');
263
-
264
212
  for (let [path, resolvedPath] of await getDependencies(code, resolved, asset, resolve, options, parcelOptions, nativeGlob, seen, false)) {
265
213
  res.set(path, resolvedPath);
266
214
  }
@@ -269,22 +217,22 @@ async function getDependencies(code, filepath, asset, resolve, options, parcelOp
269
217
  }));
270
218
  return res;
271
219
  }
272
-
273
220
  async function createEvaluator(code, asset, resolve, options, parcelOptions, nativeGlob) {
274
- const deps = await getDependencies(code, asset.filePath, asset, resolve, options, parcelOptions, nativeGlob); // This is a custom stylus evaluator that extends stylus with support for the node
221
+ const deps = await getDependencies(code, asset.filePath, asset, resolve, options, parcelOptions, nativeGlob);
222
+
223
+ // This is a custom stylus evaluator that extends stylus with support for the node
275
224
  // require resolution algorithm. It also adds all dependencies to the parcel asset
276
225
  // tree so the file watcher works correctly, etc.
277
-
278
226
  class CustomEvaluator extends _evaluator().default {
279
227
  visitImport(imported) {
280
228
  let node = this.visit(imported.path).first;
281
229
  let path = node.string;
282
-
283
230
  if (node.name !== 'url' && path && !URL_RE.test(path)) {
284
- let resolved = deps.get(path); // First try resolving using the node require resolution algorithm.
231
+ let resolved = deps.get(path);
232
+
233
+ // First try resolving using the node require resolution algorithm.
285
234
  // This allows stylus files in node_modules to be resolved properly.
286
235
  // If we find something, update the AST so stylus gets the absolute path to load later.
287
-
288
236
  if (resolved) {
289
237
  if (!Array.isArray(resolved)) {
290
238
  node.string = resolved;
@@ -300,34 +248,30 @@ async function createEvaluator(code, asset, resolve, options, parcelOptions, nat
300
248
  }));
301
249
  }
302
250
  }
303
- } // Patch the native FS so stylus uses Parcel's FS to read the file.
304
-
251
+ }
305
252
 
306
- let restore = patchNativeFS(asset.fs, nativeGlob); // Done. Let stylus do its thing.
253
+ // Patch the native FS so stylus uses Parcel's FS to read the file.
254
+ let restore = patchNativeFS(asset.fs, nativeGlob);
307
255
 
256
+ // Done. Let stylus do its thing.
308
257
  let res = super.visitImport(imported);
309
258
  restore();
310
259
  return res;
311
260
  }
312
-
313
261
  }
314
-
315
262
  return CustomEvaluator;
316
263
  }
317
-
318
264
  function patchNativeFS(fs, nativeGlob) {
319
265
  let invalidations = [];
320
-
321
266
  let readFileSync = _fs().default.readFileSync;
267
+ let statSync = _fs().default.statSync;
322
268
 
323
- let statSync = _fs().default.statSync; // $FlowFixMe
324
-
325
-
269
+ // $FlowFixMe
326
270
  _fs().default.readFileSync = (filename, encoding) => {
327
271
  return fs.readFileSync(filename, encoding);
328
- }; // $FlowFixMe
329
-
272
+ };
330
273
 
274
+ // $FlowFixMe
331
275
  _fs().default.statSync = p => {
332
276
  try {
333
277
  return fs.statSync(p);
@@ -338,17 +282,14 @@ function patchNativeFS(fs, nativeGlob) {
338
282
  filePath: p
339
283
  });
340
284
  }
341
-
342
285
  throw err;
343
286
  }
344
- }; // Patch the `glob` module as well so we use the Parcel FS and track invalidations.
345
-
287
+ };
346
288
 
289
+ // Patch the `glob` module as well so we use the Parcel FS and track invalidations.
347
290
  let glob = nativeGlob.sync;
348
-
349
291
  nativeGlob.sync = p => {
350
292
  let res = (0, _utils().globSync)(p, fs);
351
-
352
293
  if (!p.includes(`node_modules${_path().default.sep}stylus`)) {
353
294
  // Sometimes stylus passes file paths with no glob parts to the `glob` module.
354
295
  // We want to avoid treating these as globs for performance.
@@ -362,27 +303,23 @@ function patchNativeFS(fs, nativeGlob) {
362
303
  });
363
304
  }
364
305
  }
365
-
366
306
  return res;
367
307
  };
368
-
369
308
  return () => {
370
309
  // $FlowFixMe
371
- _fs().default.readFileSync = readFileSync; // $FlowFixMe
372
-
310
+ _fs().default.readFileSync = readFileSync;
311
+ // $FlowFixMe
373
312
  _fs().default.statSync = statSync;
374
313
  nativeGlob.sync = glob;
375
314
  return invalidations;
376
315
  };
377
316
  }
317
+
378
318
  /**
379
319
  * Puts the content of all given node blocks into the first one, essentially merging them.
380
320
  */
381
-
382
-
383
321
  function mergeBlocks(blocks) {
384
322
  let finalBlock;
385
-
386
323
  for (const block of blocks) {
387
324
  if (finalBlock) {
388
325
  // $FlowFixMe - finalBlock is definitely defined
@@ -391,6 +328,5 @@ function mergeBlocks(blocks) {
391
328
  finalBlock = block;
392
329
  }
393
330
  }
394
-
395
331
  return finalBlock;
396
332
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/transformer-stylus",
3
- "version": "2.8.3",
3
+ "version": "2.9.0",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,12 +17,12 @@
17
17
  "source": "src/StylusTransformer.js",
18
18
  "engines": {
19
19
  "node": ">= 12.0.0",
20
- "parcel": "^2.8.3"
20
+ "parcel": "^2.9.0"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/plugin": "2.8.3",
24
- "@parcel/utils": "2.8.3",
23
+ "@parcel/plugin": "2.9.0",
24
+ "@parcel/utils": "2.9.0",
25
25
  "stylus": "^0.55.0"
26
26
  },
27
- "gitHead": "349a6caf40ec8abb6a49fcae0765f8f8deb2073d"
27
+ "gitHead": "dd9435be8afed35c5ffc161cf4b586fd6c78fc1f"
28
28
  }
@@ -16,18 +16,13 @@ const URL_RE = /^(?:url\s*\(\s*)?['"]?(?:[#/]|(?:https?:)?\/\/)/i;
16
16
  export default (new Transformer({
17
17
  async loadConfig({config}) {
18
18
  let configFile = await config.getConfig(
19
- ['.stylusrc', '.stylusrc.js', '.stylusrc.cjs'],
19
+ ['.stylusrc', '.stylusrc.js', '.stylusrc.cjs', '.stylusrc.mjs'],
20
20
  {
21
21
  packageKey: 'stylus',
22
22
  },
23
23
  );
24
24
 
25
25
  if (configFile) {
26
- let isJavascript = path.extname(configFile.filePath).endsWith('js');
27
- if (isJavascript) {
28
- config.invalidateOnStartup();
29
- }
30
-
31
26
  // Resolve relative paths from config file
32
27
  if (configFile.contents.paths) {
33
28
  configFile.contents.paths = configFile.contents.paths.map(p =>
@@ -101,13 +96,21 @@ function attemptResolve(importedPath, filepath, asset, resolve, deps) {
101
96
  resolve(
102
97
  filepath,
103
98
  './' + path.relative(path.dirname(filepath), entry),
99
+ {
100
+ packageConditions: ['stylus', 'style'],
101
+ },
104
102
  ),
105
103
  ),
106
104
  ),
107
105
  ),
108
106
  );
109
107
  } else {
110
- deps.set(importedPath, resolve(filepath, importedPath));
108
+ deps.set(
109
+ importedPath,
110
+ resolve(filepath, importedPath, {
111
+ packageConditions: ['stylus', 'style'],
112
+ }),
113
+ );
111
114
  }
112
115
  }
113
116