vite-plugin-qingkuai 1.0.21 → 1.0.22

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.
Files changed (3) hide show
  1. package/dist/index.cjs +108 -107
  2. package/dist/index.js +109 -108
  3. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  var vite = require('vite');
4
+ var nodeFs = require('node:fs');
4
5
  var nodePath = require('node:path');
5
6
  var nodeCrypto = require('node:crypto');
6
7
  var compiler = require('qingkuai/compiler');
7
- var nodeFs = require('node:fs');
8
8
  var linesAndColumns = require('lines-and-columns');
9
9
  var sourcemapCodec = require('@jridgewell/sourcemap-codec');
10
10
  var postcss = require('postcss');
@@ -30,16 +30,6 @@ function _interopNamespaceDefault(e) {
30
30
 
31
31
  var vite__namespace = /*#__PURE__*/_interopNamespaceDefault(vite);
32
32
 
33
- const globalStyle = "\n" + compiler.util.formatSourceCode(
34
- `
35
- /* Injected by vite-plugin-qingkuai */
36
-
37
- *[hidden] {
38
- display: none !important;
39
- }
40
- `
41
- ).replace(/^/gm, " ") + "\n";
42
-
43
33
  function isUndefined(v) {
44
34
  return void 0 === v;
45
35
  }
@@ -122,6 +112,17 @@ async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
122
112
  };
123
113
  }
124
114
 
115
+ const globalStyle = "\n" + compiler.util.formatSourceCode(
116
+ `
117
+ /* Injected by vite-plugin-qingkuai */
118
+
119
+ *[hidden] {
120
+ display: none !important;
121
+ }
122
+ `
123
+ ).replace(/^/gm, " ") + "\n";
124
+ const VIRTUAL_STYLE_ID_RE = /^virtual:\[\d+\].*?\.qk\.(css|s[ac]ss|less|stylus|postcss)\?\d{13}$/;
125
+
125
126
  function offsetSourceMap(mappings, sourceIndex, preLine, preColumn) {
126
127
  return sourcemapCodec.decode(mappings).map((line) => {
127
128
  return line.map((segment) => {
@@ -148,7 +149,6 @@ function qingkuai(options = {}) {
148
149
  let viteConfig;
149
150
  const compileResultCache = /* @__PURE__ */ new Map();
150
151
  const qingkuaiConfigurations = /* @__PURE__ */ new Map();
151
- const styleIdRE = /^virtual:\[\d+\].*?\.qk.(?:css|s[ac]ss|less|stylus|postcss)\?\d{13}$/;
152
152
  if (isUndefined(options.maxScheduleDepth)) {
153
153
  options.maxScheduleDepth = 300;
154
154
  }
@@ -189,7 +189,7 @@ function qingkuai(options = {}) {
189
189
  };
190
190
  },
191
191
  resolveId(id, importer) {
192
- if (styleIdRE.test(id)) {
192
+ if (VIRTUAL_STYLE_ID_RE.test(id)) {
193
193
  return id;
194
194
  }
195
195
  if (importer?.endsWith(".qk") && !nodePath.extname(id)) {
@@ -202,111 +202,112 @@ function qingkuai(options = {}) {
202
202
  return id;
203
203
  },
204
204
  async load(id) {
205
- if (styleIdRE.test(id)) {
206
- const { fileId, index } = parseStyleId(id);
207
- if (index === -1) {
208
- return "";
209
- }
210
- let virtualFileName;
211
- const compileRes = compileResultCache.get(fileId);
212
- const style = compileRes.styleDescriptors[index];
213
- while (true) {
214
- const hash = nodeCrypto.randomBytes(6).toString("hex");
215
- virtualFileName = `${fileId}.${hash}.${style.lang}`;
216
- if (!nodeFs.existsSync(virtualFileName)) {
217
- break;
218
- }
205
+ if (!VIRTUAL_STYLE_ID_RE.test(id)) {
206
+ return;
207
+ }
208
+ const { fileId, index } = parseStyleId(id);
209
+ if (index === -1) {
210
+ return "";
211
+ }
212
+ let virtualFileName;
213
+ const compileRes = compileResultCache.get(fileId);
214
+ const style = compileRes.styleDescriptors[index];
215
+ while (true) {
216
+ const hash = nodeCrypto.randomBytes(6).toString("hex");
217
+ virtualFileName = `${fileId}.${hash}.${style.lang}`;
218
+ if (!nodeFs.existsSync(virtualFileName)) {
219
+ break;
219
220
  }
220
- const preprocessRes = await vite__namespace.preprocessCSS(style.code, virtualFileName, {
221
- ...viteConfig,
222
- css: {
223
- ...viteConfig.css,
224
- postcss: {
225
- from: virtualFileName
226
- }
221
+ }
222
+ const preprocessRes = await vite__namespace.preprocessCSS(style.code, virtualFileName, {
223
+ ...viteConfig,
224
+ css: {
225
+ ...viteConfig.css,
226
+ postcss: {
227
+ from: virtualFileName
227
228
  }
228
- });
229
- if (!cssSourcemap) {
230
- return preprocessRes.code;
231
229
  }
232
- const assertedPreprocessMap = preprocessRes.map;
233
- const attachScopeResult = await attachScopeForStyleSelectors(
234
- preprocessRes.code,
235
- compileRes.hashId,
236
- virtualFileName,
237
- assertedPreprocessMap
238
- );
239
- const attachScopeMap = attachScopeResult.map ?? assertedPreprocessMap;
240
- const samePath = (left, right) => {
241
- return nodePath.normalize(left) === nodePath.normalize(right);
242
- };
243
- const sourceIndex = attachScopeMap?.sources.findIndex((source) => samePath(source, virtualFileName)) ?? -1;
244
- const currentSourceIndex = sourceIndex === -1 ? preprocessRes.deps?.size || 0 : sourceIndex;
245
- const offsetMappings = sourcemapCodec.encode(
246
- offsetSourceMap(
247
- attachScopeResult.mappings,
248
- currentSourceIndex,
249
- style.loc.start.line - 1,
250
- style.loc.start.column - 1
251
- )
252
- );
253
- if (attachScopeResult.error) {
254
- if (!attachScopeResult.error.loc) {
255
- this.error(attachScopeResult.error.message);
256
- } else {
257
- const preprocessedPosition = await getOriginalPosition(
258
- assertedPreprocessMap,
259
- attachScopeResult.error.loc.line,
260
- attachScopeResult.error.loc.column
261
- );
262
- if (preprocessedPosition.source && !samePath(preprocessedPosition.source, virtualFileName)) {
263
- this.error({
264
- message: attachScopeResult.error.message,
265
- loc: {
266
- file: preprocessedPosition.source,
267
- line: preprocessedPosition.line,
268
- column: preprocessedPosition.column
269
- }
270
- });
271
- }
272
- const preprocessedIndex = new linesAndColumns.LinesAndColumns(style.code).indexForLocation({
273
- line: preprocessedPosition.line - 1,
274
- column: preprocessedPosition.column
275
- }) || 0;
276
- const position = compileRes.positions[style.loc.start.index + preprocessedIndex];
230
+ });
231
+ if (!cssSourcemap || style.global) {
232
+ return preprocessRes;
233
+ }
234
+ const assertedPreprocessMap = preprocessRes.map;
235
+ const attachScopeResult = await attachScopeForStyleSelectors(
236
+ preprocessRes.code,
237
+ compileRes.hashId,
238
+ virtualFileName,
239
+ assertedPreprocessMap
240
+ );
241
+ const attachScopeMap = attachScopeResult.map ?? assertedPreprocessMap;
242
+ const samePath = (left, right) => {
243
+ return nodePath.normalize(left) === nodePath.normalize(right);
244
+ };
245
+ const sourceIndex = attachScopeMap?.sources.findIndex((source) => samePath(source, virtualFileName)) ?? -1;
246
+ const currentSourceIndex = sourceIndex === -1 ? preprocessRes.deps?.size || 0 : sourceIndex;
247
+ const offsetMappings = sourcemapCodec.encode(
248
+ offsetSourceMap(
249
+ attachScopeResult.mappings,
250
+ currentSourceIndex,
251
+ style.loc.start.line - 1,
252
+ style.loc.start.column - 1
253
+ )
254
+ );
255
+ if (attachScopeResult.error) {
256
+ if (!attachScopeResult.error.loc) {
257
+ this.error(attachScopeResult.error.message);
258
+ } else {
259
+ const preprocessedPosition = await getOriginalPosition(
260
+ assertedPreprocessMap,
261
+ attachScopeResult.error.loc.line,
262
+ attachScopeResult.error.loc.column
263
+ );
264
+ if (preprocessedPosition.source && !samePath(preprocessedPosition.source, virtualFileName)) {
277
265
  this.error({
278
266
  message: attachScopeResult.error.message,
279
267
  loc: {
280
- file: fileId,
281
- line: position.line,
282
- column: position.column
268
+ file: preprocessedPosition.source,
269
+ line: preprocessedPosition.line,
270
+ column: preprocessedPosition.column
283
271
  }
284
272
  });
285
273
  }
274
+ const preprocessedIndex = new linesAndColumns.LinesAndColumns(style.code).indexForLocation({
275
+ line: preprocessedPosition.line - 1,
276
+ column: preprocessedPosition.column
277
+ }) || 0;
278
+ const position = compileRes.positions[style.loc.start.index + preprocessedIndex];
279
+ this.error({
280
+ message: attachScopeResult.error.message,
281
+ loc: {
282
+ file: fileId,
283
+ line: position.line,
284
+ column: position.column
285
+ }
286
+ });
286
287
  }
287
- return {
288
- code: attachScopeResult.code,
289
- map: {
290
- version: 3,
291
- mappings: offsetMappings,
292
- names: attachScopeMap?.names || assertedPreprocessMap?.names || [],
293
- sources: attachScopeMap?.sources ? (() => {
294
- const sources = [...attachScopeMap.sources];
295
- if (currentSourceIndex !== -1) {
296
- sources[currentSourceIndex] = fileId;
297
- }
298
- return sources;
299
- })() : [...assertedPreprocessMap ? assertedPreprocessMap.sources.slice(0, -1) : [], fileId],
300
- sourcesContent: attachScopeMap?.sourcesContent ? (() => {
301
- const sourcesContent = [...attachScopeMap.sourcesContent];
302
- if (currentSourceIndex !== -1) {
303
- sourcesContent[currentSourceIndex] = nodeFs.readFileSync(fileId, "utf-8");
304
- }
305
- return sourcesContent;
306
- })() : void 0
307
- }
308
- };
309
288
  }
289
+ return {
290
+ code: attachScopeResult.code,
291
+ map: {
292
+ version: 3,
293
+ mappings: offsetMappings,
294
+ names: attachScopeMap?.names || assertedPreprocessMap?.names || [],
295
+ sources: attachScopeMap?.sources ? (() => {
296
+ const sources = [...attachScopeMap.sources];
297
+ if (currentSourceIndex !== -1) {
298
+ sources[currentSourceIndex] = fileId;
299
+ }
300
+ return sources;
301
+ })() : [...assertedPreprocessMap ? assertedPreprocessMap.sources.slice(0, -1) : [], fileId],
302
+ sourcesContent: attachScopeMap?.sourcesContent ? (() => {
303
+ const sourcesContent = [...attachScopeMap.sourcesContent];
304
+ if (currentSourceIndex !== -1) {
305
+ sourcesContent[currentSourceIndex] = nodeFs.readFileSync(fileId, "utf-8");
306
+ }
307
+ return sourcesContent;
308
+ })() : void 0
309
+ }
310
+ };
310
311
  },
311
312
  async transform(src, id) {
312
313
  if (!id.endsWith(".qk")) {
package/dist/index.js CHANGED
@@ -1,24 +1,14 @@
1
1
  import * as vite from 'vite';
2
+ import nodeFs from 'node:fs';
2
3
  import nodePath from 'node:path';
3
4
  import nodeCrypto from 'node:crypto';
4
5
  import { util, compile } from 'qingkuai/compiler';
5
- import nodeFs, { existsSync, readFileSync } from 'node:fs';
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
- const globalStyle = "\n" + util.formatSourceCode(
13
- `
14
- /* Injected by vite-plugin-qingkuai */
15
-
16
- *[hidden] {
17
- display: none !important;
18
- }
19
- `
20
- ).replace(/^/gm, " ") + "\n";
21
-
22
12
  function isUndefined(v) {
23
13
  return void 0 === v;
24
14
  }
@@ -101,6 +91,17 @@ async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
101
91
  };
102
92
  }
103
93
 
94
+ const globalStyle = "\n" + util.formatSourceCode(
95
+ `
96
+ /* Injected by vite-plugin-qingkuai */
97
+
98
+ *[hidden] {
99
+ display: none !important;
100
+ }
101
+ `
102
+ ).replace(/^/gm, " ") + "\n";
103
+ const VIRTUAL_STYLE_ID_RE = /^virtual:\[\d+\].*?\.qk\.(css|s[ac]ss|less|stylus|postcss)\?\d{13}$/;
104
+
104
105
  function offsetSourceMap(mappings, sourceIndex, preLine, preColumn) {
105
106
  return decode(mappings).map((line) => {
106
107
  return line.map((segment) => {
@@ -127,7 +128,6 @@ function qingkuai(options = {}) {
127
128
  let viteConfig;
128
129
  const compileResultCache = /* @__PURE__ */ new Map();
129
130
  const qingkuaiConfigurations = /* @__PURE__ */ new Map();
130
- const styleIdRE = /^virtual:\[\d+\].*?\.qk.(?:css|s[ac]ss|less|stylus|postcss)\?\d{13}$/;
131
131
  if (isUndefined(options.maxScheduleDepth)) {
132
132
  options.maxScheduleDepth = 300;
133
133
  }
@@ -168,7 +168,7 @@ function qingkuai(options = {}) {
168
168
  };
169
169
  },
170
170
  resolveId(id, importer) {
171
- if (styleIdRE.test(id)) {
171
+ if (VIRTUAL_STYLE_ID_RE.test(id)) {
172
172
  return id;
173
173
  }
174
174
  if (importer?.endsWith(".qk") && !nodePath.extname(id)) {
@@ -181,111 +181,112 @@ function qingkuai(options = {}) {
181
181
  return id;
182
182
  },
183
183
  async load(id) {
184
- if (styleIdRE.test(id)) {
185
- const { fileId, index } = parseStyleId(id);
186
- if (index === -1) {
187
- return "";
188
- }
189
- let virtualFileName;
190
- const compileRes = compileResultCache.get(fileId);
191
- const style = compileRes.styleDescriptors[index];
192
- while (true) {
193
- const hash = nodeCrypto.randomBytes(6).toString("hex");
194
- virtualFileName = `${fileId}.${hash}.${style.lang}`;
195
- if (!existsSync(virtualFileName)) {
196
- break;
197
- }
184
+ if (!VIRTUAL_STYLE_ID_RE.test(id)) {
185
+ return;
186
+ }
187
+ const { fileId, index } = parseStyleId(id);
188
+ if (index === -1) {
189
+ return "";
190
+ }
191
+ let virtualFileName;
192
+ const compileRes = compileResultCache.get(fileId);
193
+ const style = compileRes.styleDescriptors[index];
194
+ while (true) {
195
+ const hash = nodeCrypto.randomBytes(6).toString("hex");
196
+ virtualFileName = `${fileId}.${hash}.${style.lang}`;
197
+ if (!nodeFs.existsSync(virtualFileName)) {
198
+ break;
198
199
  }
199
- const preprocessRes = await vite.preprocessCSS(style.code, virtualFileName, {
200
- ...viteConfig,
201
- css: {
202
- ...viteConfig.css,
203
- postcss: {
204
- from: virtualFileName
205
- }
200
+ }
201
+ const preprocessRes = await vite.preprocessCSS(style.code, virtualFileName, {
202
+ ...viteConfig,
203
+ css: {
204
+ ...viteConfig.css,
205
+ postcss: {
206
+ from: virtualFileName
206
207
  }
207
- });
208
- if (!cssSourcemap) {
209
- return preprocessRes.code;
210
208
  }
211
- const assertedPreprocessMap = preprocessRes.map;
212
- const attachScopeResult = await attachScopeForStyleSelectors(
213
- preprocessRes.code,
214
- compileRes.hashId,
215
- virtualFileName,
216
- assertedPreprocessMap
217
- );
218
- const attachScopeMap = attachScopeResult.map ?? assertedPreprocessMap;
219
- const samePath = (left, right) => {
220
- return nodePath.normalize(left) === nodePath.normalize(right);
221
- };
222
- const sourceIndex = attachScopeMap?.sources.findIndex((source) => samePath(source, virtualFileName)) ?? -1;
223
- const currentSourceIndex = sourceIndex === -1 ? preprocessRes.deps?.size || 0 : sourceIndex;
224
- const offsetMappings = encode(
225
- offsetSourceMap(
226
- attachScopeResult.mappings,
227
- currentSourceIndex,
228
- style.loc.start.line - 1,
229
- style.loc.start.column - 1
230
- )
231
- );
232
- if (attachScopeResult.error) {
233
- if (!attachScopeResult.error.loc) {
234
- this.error(attachScopeResult.error.message);
235
- } else {
236
- const preprocessedPosition = await getOriginalPosition(
237
- assertedPreprocessMap,
238
- attachScopeResult.error.loc.line,
239
- attachScopeResult.error.loc.column
240
- );
241
- if (preprocessedPosition.source && !samePath(preprocessedPosition.source, virtualFileName)) {
242
- this.error({
243
- message: attachScopeResult.error.message,
244
- loc: {
245
- file: preprocessedPosition.source,
246
- line: preprocessedPosition.line,
247
- column: preprocessedPosition.column
248
- }
249
- });
250
- }
251
- const preprocessedIndex = new LinesAndColumns(style.code).indexForLocation({
252
- line: preprocessedPosition.line - 1,
253
- column: preprocessedPosition.column
254
- }) || 0;
255
- const position = compileRes.positions[style.loc.start.index + preprocessedIndex];
209
+ });
210
+ if (!cssSourcemap || style.global) {
211
+ return preprocessRes;
212
+ }
213
+ const assertedPreprocessMap = preprocessRes.map;
214
+ const attachScopeResult = await attachScopeForStyleSelectors(
215
+ preprocessRes.code,
216
+ compileRes.hashId,
217
+ virtualFileName,
218
+ assertedPreprocessMap
219
+ );
220
+ const attachScopeMap = attachScopeResult.map ?? assertedPreprocessMap;
221
+ const samePath = (left, right) => {
222
+ return nodePath.normalize(left) === nodePath.normalize(right);
223
+ };
224
+ const sourceIndex = attachScopeMap?.sources.findIndex((source) => samePath(source, virtualFileName)) ?? -1;
225
+ const currentSourceIndex = sourceIndex === -1 ? preprocessRes.deps?.size || 0 : sourceIndex;
226
+ const offsetMappings = encode(
227
+ offsetSourceMap(
228
+ attachScopeResult.mappings,
229
+ currentSourceIndex,
230
+ style.loc.start.line - 1,
231
+ style.loc.start.column - 1
232
+ )
233
+ );
234
+ if (attachScopeResult.error) {
235
+ if (!attachScopeResult.error.loc) {
236
+ this.error(attachScopeResult.error.message);
237
+ } else {
238
+ const preprocessedPosition = await getOriginalPosition(
239
+ assertedPreprocessMap,
240
+ attachScopeResult.error.loc.line,
241
+ attachScopeResult.error.loc.column
242
+ );
243
+ if (preprocessedPosition.source && !samePath(preprocessedPosition.source, virtualFileName)) {
256
244
  this.error({
257
245
  message: attachScopeResult.error.message,
258
246
  loc: {
259
- file: fileId,
260
- line: position.line,
261
- column: position.column
247
+ file: preprocessedPosition.source,
248
+ line: preprocessedPosition.line,
249
+ column: preprocessedPosition.column
262
250
  }
263
251
  });
264
252
  }
253
+ const preprocessedIndex = new LinesAndColumns(style.code).indexForLocation({
254
+ line: preprocessedPosition.line - 1,
255
+ column: preprocessedPosition.column
256
+ }) || 0;
257
+ const position = compileRes.positions[style.loc.start.index + preprocessedIndex];
258
+ this.error({
259
+ message: attachScopeResult.error.message,
260
+ loc: {
261
+ file: fileId,
262
+ line: position.line,
263
+ column: position.column
264
+ }
265
+ });
265
266
  }
266
- return {
267
- code: attachScopeResult.code,
268
- map: {
269
- version: 3,
270
- mappings: offsetMappings,
271
- names: attachScopeMap?.names || assertedPreprocessMap?.names || [],
272
- sources: attachScopeMap?.sources ? (() => {
273
- const sources = [...attachScopeMap.sources];
274
- if (currentSourceIndex !== -1) {
275
- sources[currentSourceIndex] = fileId;
276
- }
277
- return sources;
278
- })() : [...assertedPreprocessMap ? assertedPreprocessMap.sources.slice(0, -1) : [], fileId],
279
- sourcesContent: attachScopeMap?.sourcesContent ? (() => {
280
- const sourcesContent = [...attachScopeMap.sourcesContent];
281
- if (currentSourceIndex !== -1) {
282
- sourcesContent[currentSourceIndex] = readFileSync(fileId, "utf-8");
283
- }
284
- return sourcesContent;
285
- })() : void 0
286
- }
287
- };
288
267
  }
268
+ return {
269
+ code: attachScopeResult.code,
270
+ map: {
271
+ version: 3,
272
+ mappings: offsetMappings,
273
+ names: attachScopeMap?.names || assertedPreprocessMap?.names || [],
274
+ sources: attachScopeMap?.sources ? (() => {
275
+ const sources = [...attachScopeMap.sources];
276
+ if (currentSourceIndex !== -1) {
277
+ sources[currentSourceIndex] = fileId;
278
+ }
279
+ return sources;
280
+ })() : [...assertedPreprocessMap ? assertedPreprocessMap.sources.slice(0, -1) : [], fileId],
281
+ sourcesContent: attachScopeMap?.sourcesContent ? (() => {
282
+ const sourcesContent = [...attachScopeMap.sourcesContent];
283
+ if (currentSourceIndex !== -1) {
284
+ sourcesContent[currentSourceIndex] = nodeFs.readFileSync(fileId, "utf-8");
285
+ }
286
+ return sourcesContent;
287
+ })() : void 0
288
+ }
289
+ };
289
290
  },
290
291
  async transform(src, id) {
291
292
  if (!id.endsWith(".qk")) {
@@ -418,7 +419,7 @@ function qingkuai(options = {}) {
418
419
  }
419
420
  function recordQingkuaiConfiguration(fileName) {
420
421
  try {
421
- qingkuaiConfigurations.set(nodePath.dirname(fileName), JSON.parse(readFileSync(fileName, "utf-8")));
422
+ qingkuaiConfigurations.set(nodePath.dirname(fileName), JSON.parse(nodeFs.readFileSync(fileName, "utf-8")));
422
423
  } catch {
423
424
  }
424
425
  }
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.21",
5
+ "version": "1.0.22",
6
6
  "type": "module",
7
7
  "keywords": [
8
8
  "vite",