vite-plugin-qingkuai 1.0.10 → 1.0.11

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
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var node_path = require('node:path');
3
+ var path = require('node:path');
4
4
  var node_fs = require('node:fs');
5
5
  var node_crypto = require('node:crypto');
6
6
  var compiler = require('qingkuai/compiler');
@@ -18,7 +18,7 @@ function findFilesByName(dir, targetFileName, ignoreList) {
18
18
  if (ignoreList.has(entry.name)) {
19
19
  continue;
20
20
  }
21
- const fullPath = node_path.join(dir, entry.name);
21
+ const fullPath = path.join(dir, entry.name);
22
22
  if (entry.name === targetFileName) {
23
23
  result.push(fullPath);
24
24
  } else if (entry.isDirectory()) {
@@ -30,6 +30,11 @@ function findFilesByName(dir, targetFileName, ignoreList) {
30
30
 
31
31
  async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
32
32
  let error;
33
+ const hashAttribute = selectorParser.attribute({
34
+ attribute: `qk-${hash}`,
35
+ value: void 0,
36
+ raws: {}
37
+ });
33
38
  const processor = postcss([
34
39
  {
35
40
  postcssPlugin: "postcss-attach-scope-qingkuai",
@@ -39,46 +44,22 @@ async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
39
44
  }
40
45
  rule.selector = selectorParser((selectors) => {
41
46
  selectors.each((selector) => {
42
- const scopePseudoSelectors = selector.nodes.filter(
43
- (node) => node.type === "pseudo" && node.value === ":scope"
44
- );
45
- if (scopePseudoSelectors.length) {
46
- if (scopePseudoSelectors.length > 1) {
47
- error = {
48
- loc: scopePseudoSelectors[1].source?.start,
49
- message: "Duplicate scope pseudo class selectors."
50
- };
51
- } else {
52
- const fistScopePseudo = scopePseudoSelectors[0];
53
- if (!fistScopePseudo.nodes[0]?.toString()) {
54
- error = {
55
- loc: fistScopePseudo?.source?.start,
56
- message: "Must pass paramter for the :scope pseudo class selector."
57
- };
58
- }
59
- if (fistScopePseudo.nodes.length > 1) {
60
- error = {
61
- loc: fistScopePseudo?.source?.start,
62
- message: "The :scope pseudo class selector can accept at most one parameter."
63
- };
64
- }
65
- fistScopePseudo.nodes[0] && (selector = fistScopePseudo.nodes[0]);
47
+ let usedScopeAttribute = false;
48
+ for (let i = 0; i < selector.nodes.length; i++) {
49
+ const item = selector.nodes[i];
50
+ if (item.type === "attribute" && item.attribute === "qk-scope") {
51
+ [usedScopeAttribute, selector.nodes[i]] = [true, hashAttribute];
66
52
  }
67
53
  }
54
+ if (usedScopeAttribute) {
55
+ return;
56
+ }
68
57
  const index = selector.nodes.findLastIndex(({ type }) => {
69
58
  return type === "id" || type === "tag" || type === "class" || type === "universal" || type === "attribute";
70
59
  });
71
60
  if (index !== -1) {
72
61
  const lastNode = selector.nodes[index];
73
- lastNode.parent?.insertAfter(
74
- lastNode,
75
- selectorParser.attribute({
76
- attribute: `qk-${hash}`,
77
- value: void 0,
78
- raws: {}
79
- })
80
- );
81
- scopePseudoSelectors[0]?.replaceWith(...scopePseudoSelectors[0].nodes);
62
+ lastNode.parent?.insertAfter(lastNode, hashAttribute);
82
63
  }
83
64
  });
84
65
  }).processSync(rule.selector);
@@ -121,14 +102,19 @@ function qingkuaiPlugin() {
121
102
  let viteConfig;
122
103
  const compileResultCache = /* @__PURE__ */ new Map();
123
104
  const qingkuaiConfigurations = /* @__PURE__ */ new Map();
124
- const qingkuaiPackageServeRE = /node_modules\/\.vite\/deps\/chunk-.*$/;
125
- const confIdentifierRE = /__qk_expose_(?:dependencies|destructions)__/g;
126
105
  const styleIdRE = /^virtual:\[\d+\].*?\.qk.(?:css|s[ac]ss|less|stylus|postcss)\?\d{13}$/;
127
- const qingkuaiPackageBuildRE = /(?:node_modules)?\/qingkuai\/dist\/esm\/(?:chunks|runtime)\/\w+\.js$/;
128
106
  return {
129
107
  name: "qingkuai-compiler",
130
- config(_, env) {
108
+ config(config, env) {
109
+ const rootPath = path.resolve(process.cwd(), config.root ?? "");
110
+ const qingkuaiConfig = getQingkuaiConfiguration(rootPath);
131
111
  isDev = env.command === "serve";
112
+ return {
113
+ define: {
114
+ __qk_expose_dependencies__: JSON.stringify(!!qingkuaiConfig.exposeDependencies),
115
+ __qk_expose_destructions__: JSON.stringify(!!qingkuaiConfig.exposeDestructions)
116
+ }
117
+ };
132
118
  },
133
119
  configureServer(server) {
134
120
  createQingkuaiConfigurationWatcher(server);
@@ -148,9 +134,9 @@ function qingkuaiPlugin() {
148
134
  if (styleIdRE.test(id)) {
149
135
  return id;
150
136
  }
151
- if (importer?.endsWith(".qk") && !node_path.extname(id)) {
137
+ if (importer?.endsWith(".qk") && !path.extname(id)) {
152
138
  const qingkuaiConfig = getQingkuaiConfiguration(id);
153
- return node_path.join(node_path.dirname(importer), id + (qingkuaiConfig.resolveImportExtension ? ".qk" : ""));
139
+ return path.join(path.dirname(importer), id + (qingkuaiConfig.resolveImportExtension ? ".qk" : ""));
154
140
  }
155
141
  return id;
156
142
  },
@@ -233,21 +219,7 @@ function qingkuaiPlugin() {
233
219
  }
234
220
  },
235
221
  async transform(src, id) {
236
- const qingkuaiConfig = getQingkuaiConfiguration(id);
237
222
  if (!id.endsWith(".qk")) {
238
- if (qingkuaiPackageServeRE.test(id) || qingkuaiPackageBuildRE.test(id)) {
239
- const ret = src.replace(confIdentifierRE, (s) => {
240
- switch (s) {
241
- case "__qk_expose_dependencies__":
242
- return JSON.stringify(!!qingkuaiConfig.exposeDependencies);
243
- case "__qk_expose_destructions__":
244
- return JSON.stringify(!!qingkuaiConfig.exposeDestructions);
245
- default:
246
- return s;
247
- }
248
- });
249
- return ret;
250
- }
251
223
  return;
252
224
  }
253
225
  try {
@@ -255,8 +227,8 @@ function qingkuaiPlugin() {
255
227
  sourcemap,
256
228
  debug: isDev,
257
229
  hashId: compileResultCache.get(id)?.hashId || void 0,
258
- reserveTemplateComment: getReserveHtmlComments(qingkuaiConfig),
259
- componentName: compiler.util.kebab2Camel(node_path.basename(id, node_path.extname(id)), true)
230
+ componentName: compiler.util.kebab2Camel(path.basename(id, path.extname(id)), true),
231
+ reserveTemplateComment: getReserveHtmlComments(getQingkuaiConfiguration(id))
260
232
  });
261
233
  compileRes.messages.forEach(({ type, value: warning }) => {
262
234
  if (type === "warning") {
@@ -323,7 +295,7 @@ function qingkuaiPlugin() {
323
295
  reserveHtmlComments: "development"
324
296
  };
325
297
  while (true) {
326
- const dir = node_path.dirname(id);
298
+ const dir = path.dirname(id);
327
299
  if (dir === id) {
328
300
  break;
329
301
  }
@@ -349,7 +321,7 @@ function qingkuaiPlugin() {
349
321
  }
350
322
  function createQingkuaiConfigurationWatcher(server) {
351
323
  const watcher = server.watcher.add([".qingkuairc", "!**/node_modules/**"]);
352
- watcher.on("unlink", (path) => qingkuaiConfigurations.delete(node_path.dirname(path)));
324
+ watcher.on("unlink", (p) => qingkuaiConfigurations.delete(path.dirname(p)));
353
325
  watcher.on("change", recordQingkuaiConfiguration);
354
326
  watcher.on("add", recordQingkuaiConfiguration);
355
327
  }
@@ -360,7 +332,7 @@ function qingkuaiPlugin() {
360
332
  }
361
333
  function recordQingkuaiConfiguration(fileName) {
362
334
  try {
363
- qingkuaiConfigurations.set(node_path.dirname(fileName), JSON.parse(node_fs.readFileSync(fileName, "utf-8")));
335
+ qingkuaiConfigurations.set(path.dirname(fileName), JSON.parse(node_fs.readFileSync(fileName, "utf-8")));
364
336
  } catch {
365
337
  }
366
338
  }
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { join, basename, extname, dirname } from 'node:path';
1
+ import path, { join } from 'node:path';
2
2
  import { readdirSync, existsSync, readFileSync } from 'node:fs';
3
3
  import { randomBytes } from 'node:crypto';
4
4
  import { compile, util } from 'qingkuai/compiler';
@@ -28,6 +28,11 @@ function findFilesByName(dir, targetFileName, ignoreList) {
28
28
 
29
29
  async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
30
30
  let error;
31
+ const hashAttribute = selectorParser.attribute({
32
+ attribute: `qk-${hash}`,
33
+ value: void 0,
34
+ raws: {}
35
+ });
31
36
  const processor = postcss([
32
37
  {
33
38
  postcssPlugin: "postcss-attach-scope-qingkuai",
@@ -37,46 +42,22 @@ async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
37
42
  }
38
43
  rule.selector = selectorParser((selectors) => {
39
44
  selectors.each((selector) => {
40
- const scopePseudoSelectors = selector.nodes.filter(
41
- (node) => node.type === "pseudo" && node.value === ":scope"
42
- );
43
- if (scopePseudoSelectors.length) {
44
- if (scopePseudoSelectors.length > 1) {
45
- error = {
46
- loc: scopePseudoSelectors[1].source?.start,
47
- message: "Duplicate scope pseudo class selectors."
48
- };
49
- } else {
50
- const fistScopePseudo = scopePseudoSelectors[0];
51
- if (!fistScopePseudo.nodes[0]?.toString()) {
52
- error = {
53
- loc: fistScopePseudo?.source?.start,
54
- message: "Must pass paramter for the :scope pseudo class selector."
55
- };
56
- }
57
- if (fistScopePseudo.nodes.length > 1) {
58
- error = {
59
- loc: fistScopePseudo?.source?.start,
60
- message: "The :scope pseudo class selector can accept at most one parameter."
61
- };
62
- }
63
- fistScopePseudo.nodes[0] && (selector = fistScopePseudo.nodes[0]);
45
+ let usedScopeAttribute = false;
46
+ for (let i = 0; i < selector.nodes.length; i++) {
47
+ const item = selector.nodes[i];
48
+ if (item.type === "attribute" && item.attribute === "qk-scope") {
49
+ [usedScopeAttribute, selector.nodes[i]] = [true, hashAttribute];
64
50
  }
65
51
  }
52
+ if (usedScopeAttribute) {
53
+ return;
54
+ }
66
55
  const index = selector.nodes.findLastIndex(({ type }) => {
67
56
  return type === "id" || type === "tag" || type === "class" || type === "universal" || type === "attribute";
68
57
  });
69
58
  if (index !== -1) {
70
59
  const lastNode = selector.nodes[index];
71
- lastNode.parent?.insertAfter(
72
- lastNode,
73
- selectorParser.attribute({
74
- attribute: `qk-${hash}`,
75
- value: void 0,
76
- raws: {}
77
- })
78
- );
79
- scopePseudoSelectors[0]?.replaceWith(...scopePseudoSelectors[0].nodes);
60
+ lastNode.parent?.insertAfter(lastNode, hashAttribute);
80
61
  }
81
62
  });
82
63
  }).processSync(rule.selector);
@@ -119,14 +100,19 @@ function qingkuaiPlugin() {
119
100
  let viteConfig;
120
101
  const compileResultCache = /* @__PURE__ */ new Map();
121
102
  const qingkuaiConfigurations = /* @__PURE__ */ new Map();
122
- const qingkuaiPackageServeRE = /node_modules\/\.vite\/deps\/chunk-.*$/;
123
- const confIdentifierRE = /__qk_expose_(?:dependencies|destructions)__/g;
124
103
  const styleIdRE = /^virtual:\[\d+\].*?\.qk.(?:css|s[ac]ss|less|stylus|postcss)\?\d{13}$/;
125
- const qingkuaiPackageBuildRE = /(?:node_modules)?\/qingkuai\/dist\/esm\/(?:chunks|runtime)\/\w+\.js$/;
126
104
  return {
127
105
  name: "qingkuai-compiler",
128
- config(_, env) {
106
+ config(config, env) {
107
+ const rootPath = path.resolve(process.cwd(), config.root ?? "");
108
+ const qingkuaiConfig = getQingkuaiConfiguration(rootPath);
129
109
  isDev = env.command === "serve";
110
+ return {
111
+ define: {
112
+ __qk_expose_dependencies__: JSON.stringify(!!qingkuaiConfig.exposeDependencies),
113
+ __qk_expose_destructions__: JSON.stringify(!!qingkuaiConfig.exposeDestructions)
114
+ }
115
+ };
130
116
  },
131
117
  configureServer(server) {
132
118
  createQingkuaiConfigurationWatcher(server);
@@ -146,9 +132,9 @@ function qingkuaiPlugin() {
146
132
  if (styleIdRE.test(id)) {
147
133
  return id;
148
134
  }
149
- if (importer?.endsWith(".qk") && !extname(id)) {
135
+ if (importer?.endsWith(".qk") && !path.extname(id)) {
150
136
  const qingkuaiConfig = getQingkuaiConfiguration(id);
151
- return join(dirname(importer), id + (qingkuaiConfig.resolveImportExtension ? ".qk" : ""));
137
+ return path.join(path.dirname(importer), id + (qingkuaiConfig.resolveImportExtension ? ".qk" : ""));
152
138
  }
153
139
  return id;
154
140
  },
@@ -231,21 +217,7 @@ function qingkuaiPlugin() {
231
217
  }
232
218
  },
233
219
  async transform(src, id) {
234
- const qingkuaiConfig = getQingkuaiConfiguration(id);
235
220
  if (!id.endsWith(".qk")) {
236
- if (qingkuaiPackageServeRE.test(id) || qingkuaiPackageBuildRE.test(id)) {
237
- const ret = src.replace(confIdentifierRE, (s) => {
238
- switch (s) {
239
- case "__qk_expose_dependencies__":
240
- return JSON.stringify(!!qingkuaiConfig.exposeDependencies);
241
- case "__qk_expose_destructions__":
242
- return JSON.stringify(!!qingkuaiConfig.exposeDestructions);
243
- default:
244
- return s;
245
- }
246
- });
247
- return ret;
248
- }
249
221
  return;
250
222
  }
251
223
  try {
@@ -253,8 +225,8 @@ function qingkuaiPlugin() {
253
225
  sourcemap,
254
226
  debug: isDev,
255
227
  hashId: compileResultCache.get(id)?.hashId || void 0,
256
- reserveTemplateComment: getReserveHtmlComments(qingkuaiConfig),
257
- componentName: util.kebab2Camel(basename(id, extname(id)), true)
228
+ componentName: util.kebab2Camel(path.basename(id, path.extname(id)), true),
229
+ reserveTemplateComment: getReserveHtmlComments(getQingkuaiConfiguration(id))
258
230
  });
259
231
  compileRes.messages.forEach(({ type, value: warning }) => {
260
232
  if (type === "warning") {
@@ -321,7 +293,7 @@ function qingkuaiPlugin() {
321
293
  reserveHtmlComments: "development"
322
294
  };
323
295
  while (true) {
324
- const dir = dirname(id);
296
+ const dir = path.dirname(id);
325
297
  if (dir === id) {
326
298
  break;
327
299
  }
@@ -347,7 +319,7 @@ function qingkuaiPlugin() {
347
319
  }
348
320
  function createQingkuaiConfigurationWatcher(server) {
349
321
  const watcher = server.watcher.add([".qingkuairc", "!**/node_modules/**"]);
350
- watcher.on("unlink", (path) => qingkuaiConfigurations.delete(dirname(path)));
322
+ watcher.on("unlink", (p) => qingkuaiConfigurations.delete(path.dirname(p)));
351
323
  watcher.on("change", recordQingkuaiConfiguration);
352
324
  watcher.on("add", recordQingkuaiConfiguration);
353
325
  }
@@ -358,7 +330,7 @@ function qingkuaiPlugin() {
358
330
  }
359
331
  function recordQingkuaiConfiguration(fileName) {
360
332
  try {
361
- qingkuaiConfigurations.set(dirname(fileName), JSON.parse(readFileSync(fileName, "utf-8")));
333
+ qingkuaiConfigurations.set(path.dirname(fileName), JSON.parse(readFileSync(fileName, "utf-8")));
362
334
  } catch {
363
335
  }
364
336
  }
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.10",
5
+ "version": "1.0.11",
6
6
  "type": "module",
7
7
  "keywords": [
8
8
  "vite",
@@ -25,7 +25,7 @@
25
25
  "@types/fs-extra": "^11.0.4",
26
26
  "@types/node": "^22.13.14",
27
27
  "fs-extra": "^11.3.0",
28
- "qingkuai": "^1.0.42",
28
+ "qingkuai": "^1.0.43",
29
29
  "rimraf": "^6.0.1",
30
30
  "rollup": "^4.38.0",
31
31
  "rollup-plugin-dts": "^6.2.1",
@@ -33,7 +33,7 @@
33
33
  "vite": "^6.2.4"
34
34
  },
35
35
  "peerDependencies": {
36
- "qingkuai": "^1.0.42",
36
+ "qingkuai": "^1.0.43",
37
37
  "vite": "^6.2.4"
38
38
  },
39
39
  "dependencies": {
@@ -1,2 +0,0 @@
1
- import type { PluginOption } from "vite";
2
- export default function qingkuaiPlugin(): PluginOption;
@@ -1,6 +0,0 @@
1
- import type { PostcssPluginError, SourceMap } from "./types";
2
- export declare function attachScopeForStyleSelectors(code: string, hash: string, sourceFile: string, map: SourceMap | undefined): Promise<{
3
- error: PostcssPluginError | undefined;
4
- code: string;
5
- mappings: string;
6
- }>;
@@ -1,3 +0,0 @@
1
- import type { SourceMapMappings } from "@jridgewell/sourcemap-codec";
2
- export declare function offsetSourceMap(mappings: string, sourceIndex: number, preLine: number, preColumn: number): SourceMapMappings;
3
- export declare function getOriginalPosition(map: any, line: number, column: number): Promise<import("source-map-js").MappedPosition>;
@@ -1,25 +0,0 @@
1
- export interface SourceMap {
2
- version: number;
3
- file?: string;
4
- names: string[];
5
- mappings: string;
6
- sources: string[];
7
- sourceRoot?: string;
8
- sourcesContent?: string[];
9
- x_google_ignoreList?: number[];
10
- }
11
- export type QingkuaiConfiguration = Partial<{
12
- exposeDestructions: boolean;
13
- exposeDependencies: boolean;
14
- insertTipComments: boolean;
15
- resolveImportExtension: boolean;
16
- convenientDerivedDeclaration: boolean;
17
- reserveHtmlComments: "all" | "never" | "development" | "production";
18
- }>;
19
- export interface PostcssPluginError {
20
- message: string;
21
- loc?: {
22
- line: number;
23
- column: number;
24
- };
25
- }
@@ -1 +0,0 @@
1
- export declare function findFilesByName(dir: string, targetFileName: string, ignoreList: Set<string>): string[];