@parcel/transformer-stylus 2.0.0-beta.3.1 → 2.0.0-dev.1515

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