vite-plugin-qingkuai 1.0.25 → 1.0.26

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
@@ -2,13 +2,14 @@
2
2
 
3
3
  var vite = require('vite');
4
4
  var nodeFs = require('node:fs');
5
+ var nodeUrl = require('node:url');
5
6
  var nodePath = require('node:path');
6
7
  var nodeCrypto = require('node:crypto');
7
- var compiler = require('qingkuai/compiler');
8
8
  var linesAndColumns = require('lines-and-columns');
9
9
  var sourcemapCodec = require('@jridgewell/sourcemap-codec');
10
10
  var postcss = require('postcss');
11
11
  var selectorParser = require('postcss-selector-parser');
12
+ var compiler = require('qingkuai/compiler');
12
13
  var sourceMapJs = require('source-map-js');
13
14
 
14
15
  function _interopNamespaceDefault(e) {
@@ -30,29 +31,6 @@ function _interopNamespaceDefault(e) {
30
31
 
31
32
  var vite__namespace = /*#__PURE__*/_interopNamespaceDefault(vite);
32
33
 
33
- function isNumber(v) {
34
- return typeof v === "number";
35
- }
36
- function isUndefined(v) {
37
- return void 0 === v;
38
- }
39
- function findFilesByName(dir, targetFileName, ignoreList) {
40
- const result = [];
41
- const entries = nodeFs.readdirSync(dir, { withFileTypes: true });
42
- for (const entry of entries) {
43
- if (ignoreList.has(entry.name)) {
44
- continue;
45
- }
46
- const fullPath = nodePath.join(dir, entry.name);
47
- if (entry.name === targetFileName) {
48
- result.push(fullPath);
49
- } else if (entry.isDirectory()) {
50
- result.push(...findFilesByName(fullPath, targetFileName, ignoreList));
51
- }
52
- }
53
- return result;
54
- }
55
-
56
34
  async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
57
35
  let error;
58
36
  const processedRules = /* @__PURE__ */ new WeakSet();
@@ -126,10 +104,34 @@ const globalStyle = "\n" + compiler.util.formatSourceCode(
126
104
  ).replace(/^/gm, " ") + "\n";
127
105
  const VIRTUAL_STYLE_ID_RE = /^virtual:\[\d+\].*?\.qk\.(css|s[ac]ss|less|stylus|postcss)\?\d{13}$/;
128
106
 
129
- function offsetSourceMap(mappings, sourceIndex, preLine, preColumn) {
107
+ function isNumber(v) {
108
+ return typeof v === "number";
109
+ }
110
+ function isUndefined(v) {
111
+ return void 0 === v;
112
+ }
113
+ function findFilesByName(dir, targetFileName, ignoreList) {
114
+ const result = [];
115
+ const entries = nodeFs.readdirSync(dir, { withFileTypes: true });
116
+ for (const entry of entries) {
117
+ if (ignoreList.has(entry.name)) {
118
+ continue;
119
+ }
120
+ const fullPath = nodePath.join(dir, entry.name);
121
+ if (entry.name === targetFileName) {
122
+ result.push(fullPath);
123
+ } else if (entry.isDirectory()) {
124
+ result.push(...findFilesByName(fullPath, targetFileName, ignoreList));
125
+ }
126
+ }
127
+ return result;
128
+ }
129
+
130
+ function offsetSourceMap(sourceIndices, mappings, preLine, preColumn) {
131
+ const indices = new Set(sourceIndices);
130
132
  return sourcemapCodec.decode(mappings).map((line) => {
131
133
  return line.map((segment) => {
132
- if (segment.length === 1 || segment[1] !== sourceIndex) {
134
+ if (segment.length === 1 || !indices.has(segment[1])) {
133
135
  return segment;
134
136
  }
135
137
  if (segment[2] === preLine) {
@@ -273,9 +275,11 @@ Caused by: "${fileId}".`;
273
275
  }
274
276
  return this.error(err);
275
277
  }
278
+ preprocessRes.deps?.forEach((dep) => this.addWatchFile(dep));
276
279
  if (!cssSourcemap || styleDescriptor.global) {
277
280
  return preprocessRes;
278
281
  }
282
+ const phantomIndices = [];
279
283
  const assertedPreprocessMap = preprocessRes.map;
280
284
  const attachScopeResult = await attachScopeForStyleSelectors(
281
285
  preprocessRes.code,
@@ -284,15 +288,28 @@ Caused by: "${fileId}".`;
284
288
  assertedPreprocessMap
285
289
  );
286
290
  const attachScopeMap = attachScopeResult.map ?? assertedPreprocessMap;
287
- const samePath = (left, right) => {
288
- return nodePath.normalize(left) === nodePath.normalize(right);
291
+ const sameVirtualSource = (source) => {
292
+ let resolved = source.startsWith("file://") ? nodeUrl.fileURLToPath(source) : source;
293
+ if (!nodePath.isAbsolute(resolved)) {
294
+ const candidates = [
295
+ nodePath.resolve(nodePath.dirname(virtualFileName), resolved),
296
+ nodePath.resolve(resolved)
297
+ ];
298
+ return candidates.some(
299
+ (candidate) => nodePath.normalize(candidate) === nodePath.normalize(virtualFileName)
300
+ );
301
+ }
302
+ return nodePath.normalize(resolved) === nodePath.normalize(virtualFileName);
289
303
  };
290
- const sourceIndex = attachScopeMap?.sources.findIndex((source) => samePath(source, virtualFileName)) ?? -1;
291
- const currentSourceIndex = sourceIndex === -1 ? preprocessRes.deps?.size || 0 : sourceIndex;
304
+ attachScopeMap?.sources.forEach((source, index2) => {
305
+ if (sameVirtualSource(source)) {
306
+ phantomIndices.push(index2);
307
+ }
308
+ });
292
309
  const offsetMappings = sourcemapCodec.encode(
293
310
  offsetSourceMap(
311
+ phantomIndices,
294
312
  attachScopeResult.mappings,
295
- currentSourceIndex,
296
313
  styleDescriptor.loc.start.line - 1,
297
314
  styleDescriptor.loc.start.column - 1
298
315
  )
@@ -306,7 +323,7 @@ Caused by: "${fileId}".`;
306
323
  attachScopeResult.error.loc.line,
307
324
  attachScopeResult.error.loc.column
308
325
  );
309
- if (preprocessedPosition.source && !samePath(preprocessedPosition.source, virtualFileName)) {
326
+ if (preprocessedPosition.source && !sameVirtualSource(preprocessedPosition.source)) {
310
327
  this.error({
311
328
  message: attachScopeResult.error.message,
312
329
  loc: {
@@ -337,20 +354,17 @@ Caused by: "${fileId}".`;
337
354
  version: 3,
338
355
  mappings: offsetMappings,
339
356
  names: attachScopeMap?.names || assertedPreprocessMap?.names || [],
340
- sources: attachScopeMap?.sources ? (() => {
341
- const sources = [...attachScopeMap.sources];
342
- if (currentSourceIndex !== -1) {
343
- sources[currentSourceIndex] = fileId;
344
- }
345
- return sources;
346
- })() : [...assertedPreprocessMap ? assertedPreprocessMap.sources.slice(0, -1) : [], fileId],
347
- sourcesContent: attachScopeMap?.sourcesContent ? (() => {
348
- const sourcesContent = [...attachScopeMap.sourcesContent];
349
- if (currentSourceIndex !== -1) {
350
- sourcesContent[currentSourceIndex] = nodeFs.readFileSync(fileId, "utf-8");
351
- }
352
- return sourcesContent;
353
- })() : void 0
357
+ sources: attachScopeMap?.sources.map((source, index2) => {
358
+ return phantomIndices.includes(index2) ? fileId : source;
359
+ }) ?? [
360
+ ...assertedPreprocessMap?.sources.filter((source) => {
361
+ return !sameVirtualSource(source);
362
+ }) ?? [],
363
+ fileId
364
+ ],
365
+ sourcesContent: attachScopeMap?.sourcesContent?.map((content, index2) => {
366
+ return phantomIndices.includes(index2) ? nodeFs.readFileSync(fileId, "utf-8") : content;
367
+ })
354
368
  }
355
369
  };
356
370
  },
package/dist/index.js CHANGED
@@ -1,37 +1,15 @@
1
1
  import * as vite from 'vite';
2
2
  import nodeFs from 'node:fs';
3
+ import nodeUrl from 'node:url';
3
4
  import nodePath from 'node:path';
4
5
  import nodeCrypto from 'node:crypto';
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
+ import { util, compile, isCompileError } from 'qingkuai/compiler';
10
11
  import { SourceMapConsumer } from 'source-map-js';
11
12
 
12
- function isNumber(v) {
13
- return typeof v === "number";
14
- }
15
- function isUndefined(v) {
16
- return void 0 === v;
17
- }
18
- function findFilesByName(dir, targetFileName, ignoreList) {
19
- const result = [];
20
- const entries = nodeFs.readdirSync(dir, { withFileTypes: true });
21
- for (const entry of entries) {
22
- if (ignoreList.has(entry.name)) {
23
- continue;
24
- }
25
- const fullPath = nodePath.join(dir, entry.name);
26
- if (entry.name === targetFileName) {
27
- result.push(fullPath);
28
- } else if (entry.isDirectory()) {
29
- result.push(...findFilesByName(fullPath, targetFileName, ignoreList));
30
- }
31
- }
32
- return result;
33
- }
34
-
35
13
  async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
36
14
  let error;
37
15
  const processedRules = /* @__PURE__ */ new WeakSet();
@@ -105,10 +83,34 @@ const globalStyle = "\n" + util.formatSourceCode(
105
83
  ).replace(/^/gm, " ") + "\n";
106
84
  const VIRTUAL_STYLE_ID_RE = /^virtual:\[\d+\].*?\.qk\.(css|s[ac]ss|less|stylus|postcss)\?\d{13}$/;
107
85
 
108
- function offsetSourceMap(mappings, sourceIndex, preLine, preColumn) {
86
+ function isNumber(v) {
87
+ return typeof v === "number";
88
+ }
89
+ function isUndefined(v) {
90
+ return void 0 === v;
91
+ }
92
+ function findFilesByName(dir, targetFileName, ignoreList) {
93
+ const result = [];
94
+ const entries = nodeFs.readdirSync(dir, { withFileTypes: true });
95
+ for (const entry of entries) {
96
+ if (ignoreList.has(entry.name)) {
97
+ continue;
98
+ }
99
+ const fullPath = nodePath.join(dir, entry.name);
100
+ if (entry.name === targetFileName) {
101
+ result.push(fullPath);
102
+ } else if (entry.isDirectory()) {
103
+ result.push(...findFilesByName(fullPath, targetFileName, ignoreList));
104
+ }
105
+ }
106
+ return result;
107
+ }
108
+
109
+ function offsetSourceMap(sourceIndices, mappings, preLine, preColumn) {
110
+ const indices = new Set(sourceIndices);
109
111
  return decode(mappings).map((line) => {
110
112
  return line.map((segment) => {
111
- if (segment.length === 1 || segment[1] !== sourceIndex) {
113
+ if (segment.length === 1 || !indices.has(segment[1])) {
112
114
  return segment;
113
115
  }
114
116
  if (segment[2] === preLine) {
@@ -252,9 +254,11 @@ Caused by: "${fileId}".`;
252
254
  }
253
255
  return this.error(err);
254
256
  }
257
+ preprocessRes.deps?.forEach((dep) => this.addWatchFile(dep));
255
258
  if (!cssSourcemap || styleDescriptor.global) {
256
259
  return preprocessRes;
257
260
  }
261
+ const phantomIndices = [];
258
262
  const assertedPreprocessMap = preprocessRes.map;
259
263
  const attachScopeResult = await attachScopeForStyleSelectors(
260
264
  preprocessRes.code,
@@ -263,15 +267,28 @@ Caused by: "${fileId}".`;
263
267
  assertedPreprocessMap
264
268
  );
265
269
  const attachScopeMap = attachScopeResult.map ?? assertedPreprocessMap;
266
- const samePath = (left, right) => {
267
- return nodePath.normalize(left) === nodePath.normalize(right);
270
+ const sameVirtualSource = (source) => {
271
+ let resolved = source.startsWith("file://") ? nodeUrl.fileURLToPath(source) : source;
272
+ if (!nodePath.isAbsolute(resolved)) {
273
+ const candidates = [
274
+ nodePath.resolve(nodePath.dirname(virtualFileName), resolved),
275
+ nodePath.resolve(resolved)
276
+ ];
277
+ return candidates.some(
278
+ (candidate) => nodePath.normalize(candidate) === nodePath.normalize(virtualFileName)
279
+ );
280
+ }
281
+ return nodePath.normalize(resolved) === nodePath.normalize(virtualFileName);
268
282
  };
269
- const sourceIndex = attachScopeMap?.sources.findIndex((source) => samePath(source, virtualFileName)) ?? -1;
270
- const currentSourceIndex = sourceIndex === -1 ? preprocessRes.deps?.size || 0 : sourceIndex;
283
+ attachScopeMap?.sources.forEach((source, index2) => {
284
+ if (sameVirtualSource(source)) {
285
+ phantomIndices.push(index2);
286
+ }
287
+ });
271
288
  const offsetMappings = encode(
272
289
  offsetSourceMap(
290
+ phantomIndices,
273
291
  attachScopeResult.mappings,
274
- currentSourceIndex,
275
292
  styleDescriptor.loc.start.line - 1,
276
293
  styleDescriptor.loc.start.column - 1
277
294
  )
@@ -285,7 +302,7 @@ Caused by: "${fileId}".`;
285
302
  attachScopeResult.error.loc.line,
286
303
  attachScopeResult.error.loc.column
287
304
  );
288
- if (preprocessedPosition.source && !samePath(preprocessedPosition.source, virtualFileName)) {
305
+ if (preprocessedPosition.source && !sameVirtualSource(preprocessedPosition.source)) {
289
306
  this.error({
290
307
  message: attachScopeResult.error.message,
291
308
  loc: {
@@ -316,20 +333,17 @@ Caused by: "${fileId}".`;
316
333
  version: 3,
317
334
  mappings: offsetMappings,
318
335
  names: attachScopeMap?.names || assertedPreprocessMap?.names || [],
319
- sources: attachScopeMap?.sources ? (() => {
320
- const sources = [...attachScopeMap.sources];
321
- if (currentSourceIndex !== -1) {
322
- sources[currentSourceIndex] = fileId;
323
- }
324
- return sources;
325
- })() : [...assertedPreprocessMap ? assertedPreprocessMap.sources.slice(0, -1) : [], fileId],
326
- sourcesContent: attachScopeMap?.sourcesContent ? (() => {
327
- const sourcesContent = [...attachScopeMap.sourcesContent];
328
- if (currentSourceIndex !== -1) {
329
- sourcesContent[currentSourceIndex] = nodeFs.readFileSync(fileId, "utf-8");
330
- }
331
- return sourcesContent;
332
- })() : void 0
336
+ sources: attachScopeMap?.sources.map((source, index2) => {
337
+ return phantomIndices.includes(index2) ? fileId : source;
338
+ }) ?? [
339
+ ...assertedPreprocessMap?.sources.filter((source) => {
340
+ return !sameVirtualSource(source);
341
+ }) ?? [],
342
+ fileId
343
+ ],
344
+ sourcesContent: attachScopeMap?.sourcesContent?.map((content, index2) => {
345
+ return phantomIndices.includes(index2) ? nodeFs.readFileSync(fileId, "utf-8") : content;
346
+ })
333
347
  }
334
348
  };
335
349
  },
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "mlgq",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
- "version": "1.0.25",
6
+ "version": "1.0.26",
7
7
  "packageManager": "pnpm@9.9.0",
8
8
  "exports": {
9
9
  ".": {