vite-plugin-qingkuai 1.0.22 → 1.0.23

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/dist/index.cjs CHANGED
@@ -30,6 +30,9 @@ function _interopNamespaceDefault(e) {
30
30
 
31
31
  var vite__namespace = /*#__PURE__*/_interopNamespaceDefault(vite);
32
32
 
33
+ function isNumber(v) {
34
+ return typeof v === "number";
35
+ }
33
36
  function isUndefined(v) {
34
37
  return void 0 === v;
35
38
  }
@@ -211,24 +214,66 @@ function qingkuai(options = {}) {
211
214
  }
212
215
  let virtualFileName;
213
216
  const compileRes = compileResultCache.get(fileId);
214
- const style = compileRes.styleDescriptors[index];
217
+ const styleDescriptor = compileRes.styleDescriptors[index];
215
218
  while (true) {
216
219
  const hash = nodeCrypto.randomBytes(6).toString("hex");
217
- virtualFileName = `${fileId}.${hash}.${style.lang}`;
220
+ virtualFileName = `${fileId}.${hash}.${styleDescriptor.lang}`;
218
221
  if (!nodeFs.existsSync(virtualFileName)) {
219
222
  break;
220
223
  }
221
224
  }
222
- const preprocessRes = await vite__namespace.preprocessCSS(style.code, virtualFileName, {
223
- ...viteConfig,
224
- css: {
225
- ...viteConfig.css,
226
- postcss: {
227
- from: virtualFileName
225
+ let preprocessRes;
226
+ try {
227
+ preprocessRes = await vite__namespace.preprocessCSS(styleDescriptor.code, virtualFileName, {
228
+ ...viteConfig,
229
+ css: {
230
+ ...viteConfig.css,
231
+ postcss: {
232
+ from: virtualFileName
233
+ }
234
+ }
235
+ });
236
+ } catch (err) {
237
+ let errorPos = null;
238
+ const lac = new linesAndColumns.LinesAndColumns(styleDescriptor.code);
239
+ switch (styleDescriptor.lang) {
240
+ case "css":
241
+ case "less":
242
+ case "stylus": {
243
+ delete err.loc;
244
+ err.message += `
245
+
246
+ Caused by: "${fileId}".`;
247
+ break;
248
+ }
249
+ case "sass":
250
+ case "scss": {
251
+ if (err.sassMessage) {
252
+ err.message = err.sassMessage;
253
+ }
254
+ if (isNumber(err.line) && isNumber(err.column)) {
255
+ errorPos = lac.indexForLocation({
256
+ line: err.line - 1,
257
+ column: err.column - 1
258
+ });
259
+ }
260
+ break;
228
261
  }
229
262
  }
230
- });
231
- if (!cssSourcemap || style.global) {
263
+ if (isNumber(errorPos) && !isNaN(errorPos)) {
264
+ const loc = lac.locationForIndex(errorPos + styleDescriptor.loc.start.index);
265
+ if (loc) {
266
+ err.loc = {
267
+ file: fileId,
268
+ line: loc.line + 1,
269
+ column: loc.column
270
+ };
271
+ delete err.frame;
272
+ }
273
+ }
274
+ return this.error(err);
275
+ }
276
+ if (!cssSourcemap || styleDescriptor.global) {
232
277
  return preprocessRes;
233
278
  }
234
279
  const assertedPreprocessMap = preprocessRes.map;
@@ -248,8 +293,8 @@ function qingkuai(options = {}) {
248
293
  offsetSourceMap(
249
294
  attachScopeResult.mappings,
250
295
  currentSourceIndex,
251
- style.loc.start.line - 1,
252
- style.loc.start.column - 1
296
+ styleDescriptor.loc.start.line - 1,
297
+ styleDescriptor.loc.start.column - 1
253
298
  )
254
299
  );
255
300
  if (attachScopeResult.error) {
@@ -271,11 +316,11 @@ function qingkuai(options = {}) {
271
316
  }
272
317
  });
273
318
  }
274
- const preprocessedIndex = new linesAndColumns.LinesAndColumns(style.code).indexForLocation({
319
+ const preprocessedIndex = new linesAndColumns.LinesAndColumns(styleDescriptor.code).indexForLocation({
275
320
  line: preprocessedPosition.line - 1,
276
321
  column: preprocessedPosition.column
277
322
  }) || 0;
278
- const position = compileRes.positions[style.loc.start.index + preprocessedIndex];
323
+ const position = compileRes.positions[styleDescriptor.loc.start.index + preprocessedIndex];
279
324
  this.error({
280
325
  message: attachScopeResult.error.message,
281
326
  loc: {
@@ -383,8 +428,10 @@ function qingkuai(options = {}) {
383
428
  })
384
429
  };
385
430
  } catch (err) {
386
- if (err.loc && "start" in err.loc && "index" in err.loc.start) {
387
- err.pos = err.loc.start.index, this.error(err);
431
+ if (compiler.isCompileError(err)) {
432
+ this.error(err.message, err.loc.start.index);
433
+ } else if (err.cause && isNumber(err.cause.pos)) {
434
+ this.error(err, err.cause.pos);
388
435
  } else {
389
436
  this.error(
390
437
  "Qingkuai compile result is invalid. Please report this at https://github.com/qingkuai-js/qingkuai/issues and include your .qk source for reproduction."
package/dist/index.js CHANGED
@@ -2,13 +2,16 @@ import * as vite from 'vite';
2
2
  import nodeFs from 'node:fs';
3
3
  import nodePath from 'node:path';
4
4
  import nodeCrypto from 'node:crypto';
5
- import { util, compile } from 'qingkuai/compiler';
5
+ import { util, compile, isCompileError } from 'qingkuai/compiler';
6
6
  import { LinesAndColumns } from 'lines-and-columns';
7
7
  import { decode, encode } from '@jridgewell/sourcemap-codec';
8
8
  import postcss from 'postcss';
9
9
  import selectorParser from 'postcss-selector-parser';
10
10
  import { SourceMapConsumer } from 'source-map-js';
11
11
 
12
+ function isNumber(v) {
13
+ return typeof v === "number";
14
+ }
12
15
  function isUndefined(v) {
13
16
  return void 0 === v;
14
17
  }
@@ -190,24 +193,66 @@ function qingkuai(options = {}) {
190
193
  }
191
194
  let virtualFileName;
192
195
  const compileRes = compileResultCache.get(fileId);
193
- const style = compileRes.styleDescriptors[index];
196
+ const styleDescriptor = compileRes.styleDescriptors[index];
194
197
  while (true) {
195
198
  const hash = nodeCrypto.randomBytes(6).toString("hex");
196
- virtualFileName = `${fileId}.${hash}.${style.lang}`;
199
+ virtualFileName = `${fileId}.${hash}.${styleDescriptor.lang}`;
197
200
  if (!nodeFs.existsSync(virtualFileName)) {
198
201
  break;
199
202
  }
200
203
  }
201
- const preprocessRes = await vite.preprocessCSS(style.code, virtualFileName, {
202
- ...viteConfig,
203
- css: {
204
- ...viteConfig.css,
205
- postcss: {
206
- from: virtualFileName
204
+ let preprocessRes;
205
+ try {
206
+ preprocessRes = await vite.preprocessCSS(styleDescriptor.code, virtualFileName, {
207
+ ...viteConfig,
208
+ css: {
209
+ ...viteConfig.css,
210
+ postcss: {
211
+ from: virtualFileName
212
+ }
213
+ }
214
+ });
215
+ } catch (err) {
216
+ let errorPos = null;
217
+ const lac = new LinesAndColumns(styleDescriptor.code);
218
+ switch (styleDescriptor.lang) {
219
+ case "css":
220
+ case "less":
221
+ case "stylus": {
222
+ delete err.loc;
223
+ err.message += `
224
+
225
+ Caused by: "${fileId}".`;
226
+ break;
227
+ }
228
+ case "sass":
229
+ case "scss": {
230
+ if (err.sassMessage) {
231
+ err.message = err.sassMessage;
232
+ }
233
+ if (isNumber(err.line) && isNumber(err.column)) {
234
+ errorPos = lac.indexForLocation({
235
+ line: err.line - 1,
236
+ column: err.column - 1
237
+ });
238
+ }
239
+ break;
207
240
  }
208
241
  }
209
- });
210
- if (!cssSourcemap || style.global) {
242
+ if (isNumber(errorPos) && !isNaN(errorPos)) {
243
+ const loc = lac.locationForIndex(errorPos + styleDescriptor.loc.start.index);
244
+ if (loc) {
245
+ err.loc = {
246
+ file: fileId,
247
+ line: loc.line + 1,
248
+ column: loc.column
249
+ };
250
+ delete err.frame;
251
+ }
252
+ }
253
+ return this.error(err);
254
+ }
255
+ if (!cssSourcemap || styleDescriptor.global) {
211
256
  return preprocessRes;
212
257
  }
213
258
  const assertedPreprocessMap = preprocessRes.map;
@@ -227,8 +272,8 @@ function qingkuai(options = {}) {
227
272
  offsetSourceMap(
228
273
  attachScopeResult.mappings,
229
274
  currentSourceIndex,
230
- style.loc.start.line - 1,
231
- style.loc.start.column - 1
275
+ styleDescriptor.loc.start.line - 1,
276
+ styleDescriptor.loc.start.column - 1
232
277
  )
233
278
  );
234
279
  if (attachScopeResult.error) {
@@ -250,11 +295,11 @@ function qingkuai(options = {}) {
250
295
  }
251
296
  });
252
297
  }
253
- const preprocessedIndex = new LinesAndColumns(style.code).indexForLocation({
298
+ const preprocessedIndex = new LinesAndColumns(styleDescriptor.code).indexForLocation({
254
299
  line: preprocessedPosition.line - 1,
255
300
  column: preprocessedPosition.column
256
301
  }) || 0;
257
- const position = compileRes.positions[style.loc.start.index + preprocessedIndex];
302
+ const position = compileRes.positions[styleDescriptor.loc.start.index + preprocessedIndex];
258
303
  this.error({
259
304
  message: attachScopeResult.error.message,
260
305
  loc: {
@@ -362,8 +407,10 @@ function qingkuai(options = {}) {
362
407
  })
363
408
  };
364
409
  } catch (err) {
365
- if (err.loc && "start" in err.loc && "index" in err.loc.start) {
366
- err.pos = err.loc.start.index, this.error(err);
410
+ if (isCompileError(err)) {
411
+ this.error(err.message, err.loc.start.index);
412
+ } else if (err.cause && isNumber(err.cause.pos)) {
413
+ this.error(err, err.cause.pos);
367
414
  } else {
368
415
  this.error(
369
416
  "Qingkuai compile result is invalid. Please report this at https://github.com/qingkuai-js/qingkuai/issues and include your .qk source for reproduction."
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "vite-plugin-qingkuai",
3
3
  "author": "mlgq",
4
4
  "license": "MIT",
5
- "version": "1.0.22",
5
+ "version": "1.0.23",
6
6
  "type": "module",
7
7
  "keywords": [
8
8
  "vite",