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