vite-plugin-qingkuai 1.0.9 → 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,13 +1,15 @@
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
- var sourcemapCodec = require('@jridgewell/sourcemap-codec');
7
6
  var compiler = require('qingkuai/compiler');
7
+ var linesAndColumns = require('lines-and-columns');
8
+ var sourcemapCodec = require('@jridgewell/sourcemap-codec');
8
9
  var postcss = require('postcss');
9
10
  var selectorParser = require('postcss-selector-parser');
10
11
  var vite = require('vite');
12
+ var sourceMapJs = require('source-map-js');
11
13
 
12
14
  function findFilesByName(dir, targetFileName, ignoreList) {
13
15
  const result = [];
@@ -16,7 +18,7 @@ function findFilesByName(dir, targetFileName, ignoreList) {
16
18
  if (ignoreList.has(entry.name)) {
17
19
  continue;
18
20
  }
19
- const fullPath = node_path.join(dir, entry.name);
21
+ const fullPath = path.join(dir, entry.name);
20
22
  if (entry.name === targetFileName) {
21
23
  result.push(fullPath);
22
24
  } else if (entry.isDirectory()) {
@@ -26,23 +28,13 @@ function findFilesByName(dir, targetFileName, ignoreList) {
26
28
  return result;
27
29
  }
28
30
 
29
- function offsetSourceMap(mappings, sourceIndex, preLine, preColumn) {
30
- return sourcemapCodec.decode(mappings).map((line) => {
31
- return line.map((segment) => {
32
- if (segment.length === 1 || segment[1] !== sourceIndex) {
33
- return segment;
34
- }
35
- if (segment[2] === preLine) {
36
- segment[3] += preColumn;
37
- } else {
38
- segment[2] += preLine;
39
- }
40
- return segment;
41
- });
42
- });
43
- }
44
-
45
31
  async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
32
+ let error;
33
+ const hashAttribute = selectorParser.attribute({
34
+ attribute: `qk-${hash}`,
35
+ value: void 0,
36
+ raws: {}
37
+ });
46
38
  const processor = postcss([
47
39
  {
48
40
  postcssPlugin: "postcss-attach-scope-qingkuai",
@@ -52,19 +44,22 @@ async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
52
44
  }
53
45
  rule.selector = selectorParser((selectors) => {
54
46
  selectors.each((selector) => {
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];
52
+ }
53
+ }
54
+ if (usedScopeAttribute) {
55
+ return;
56
+ }
55
57
  const index = selector.nodes.findLastIndex(({ type }) => {
56
- return type === "class" || type === "tag" || type === "id" || type === "universal";
58
+ return type === "id" || type === "tag" || type === "class" || type === "universal" || type === "attribute";
57
59
  });
58
60
  if (index !== -1) {
59
61
  const lastNode = selector.nodes[index];
60
- lastNode.parent?.insertAfter(
61
- lastNode,
62
- selectorParser.attribute({
63
- attribute: `qk-${hash}`,
64
- value: void 0,
65
- raws: {}
66
- })
67
- );
62
+ lastNode.parent?.insertAfter(lastNode, hashAttribute);
68
63
  }
69
64
  });
70
65
  }).processSync(rule.selector);
@@ -78,7 +73,26 @@ async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
78
73
  annotation: false
79
74
  }
80
75
  });
81
- return { code: ret.css, mappings: ret.map.toJSON().mappings };
76
+ return { error, code: ret.css, mappings: ret.map.toJSON().mappings };
77
+ }
78
+
79
+ function offsetSourceMap(mappings, sourceIndex, preLine, preColumn) {
80
+ return sourcemapCodec.decode(mappings).map((line) => {
81
+ return line.map((segment) => {
82
+ if (segment.length === 1 || segment[1] !== sourceIndex) {
83
+ return segment;
84
+ }
85
+ if (segment[2] === preLine) {
86
+ segment[3] += preColumn;
87
+ } else {
88
+ segment[2] += preLine;
89
+ }
90
+ return segment;
91
+ });
92
+ });
93
+ }
94
+ async function getOriginalPosition(map, line, column) {
95
+ return new sourceMapJs.SourceMapConsumer(map).originalPositionFor({ line, column });
82
96
  }
83
97
 
84
98
  function qingkuaiPlugin() {
@@ -88,14 +102,19 @@ function qingkuaiPlugin() {
88
102
  let viteConfig;
89
103
  const compileResultCache = /* @__PURE__ */ new Map();
90
104
  const qingkuaiConfigurations = /* @__PURE__ */ new Map();
91
- const qingkuaiPackageServeRE = /node_modules\/\.vite\/deps\/chunk-.*$/;
92
- const confIdentifierRE = /__qk_expose_(?:dependencies|destructions)__/g;
93
105
  const styleIdRE = /^virtual:\[\d+\].*?\.qk.(?:css|s[ac]ss|less|stylus|postcss)\?\d{13}$/;
94
- const qingkuaiPackageBuildRE = /(?:node_modules)?\/qingkuai\/dist\/esm\/(?:chunks|runtime)\/\w+\.js$/;
95
106
  return {
96
107
  name: "qingkuai-compiler",
97
- config(_, env) {
108
+ config(config, env) {
109
+ const rootPath = path.resolve(process.cwd(), config.root ?? "");
110
+ const qingkuaiConfig = getQingkuaiConfiguration(rootPath);
98
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
+ };
99
118
  },
100
119
  configureServer(server) {
101
120
  createQingkuaiConfigurationWatcher(server);
@@ -115,10 +134,11 @@ function qingkuaiPlugin() {
115
134
  if (styleIdRE.test(id)) {
116
135
  return id;
117
136
  }
118
- if (importer?.endsWith(".qk") && !node_path.extname(id)) {
137
+ if (importer?.endsWith(".qk") && !path.extname(id)) {
119
138
  const qingkuaiConfig = getQingkuaiConfiguration(id);
120
- return node_path.join(node_path.dirname(importer), id + (qingkuaiConfig.resolveImportExtension ? ".qk" : ""));
139
+ return path.join(path.dirname(importer), id + (qingkuaiConfig.resolveImportExtension ? ".qk" : ""));
121
140
  }
141
+ return id;
122
142
  },
123
143
  async load(id) {
124
144
  if (styleIdRE.test(id)) {
@@ -163,6 +183,30 @@ function qingkuaiPlugin() {
163
183
  style.loc.start.column - 1
164
184
  )
165
185
  );
186
+ if (attachScopeResult.error) {
187
+ if (!attachScopeResult.error.loc) {
188
+ this.error(attachScopeResult.error.message);
189
+ } else {
190
+ const preprocessedPosition = await getOriginalPosition(
191
+ assertedPreprocessMap,
192
+ attachScopeResult.error.loc.line,
193
+ attachScopeResult.error.loc.column
194
+ );
195
+ const preprocessedIndex = new linesAndColumns.LinesAndColumns(style.code).indexForLocation({
196
+ line: preprocessedPosition.line - 1,
197
+ column: preprocessedPosition.column
198
+ }) || 0;
199
+ const position = compileRes.inputDescriptor.positions[style.loc.start.index + preprocessedIndex];
200
+ this.error({
201
+ message: attachScopeResult.error.message,
202
+ loc: {
203
+ file: fileId,
204
+ line: position.line,
205
+ column: position.column
206
+ }
207
+ });
208
+ }
209
+ }
166
210
  return {
167
211
  code: attachScopeResult.code,
168
212
  map: {
@@ -175,81 +219,71 @@ function qingkuaiPlugin() {
175
219
  }
176
220
  },
177
221
  async transform(src, id) {
178
- const qingkuaiConfig = getQingkuaiConfiguration(id);
179
222
  if (!id.endsWith(".qk")) {
180
- if (qingkuaiPackageServeRE.test(id) || qingkuaiPackageBuildRE.test(id)) {
181
- const ret = src.replace(confIdentifierRE, (s) => {
182
- switch (s) {
183
- case "__qk_expose_dependencies__":
184
- return JSON.stringify(!!qingkuaiConfig.exposeDependencies);
185
- case "__qk_expose_destructions__":
186
- return JSON.stringify(!!qingkuaiConfig.exposeDestructions);
187
- default:
188
- return s;
189
- }
190
- });
191
- return ret;
192
- }
193
223
  return;
194
224
  }
195
- const compileRes = compiler.compile(src, {
196
- sourcemap,
197
- debug: isDev,
198
- hashId: compileResultCache.get(id)?.hashId || void 0,
199
- reserveTemplateComment: getReserveHtmlComments(qingkuaiConfig),
200
- componentName: compiler.util.kebab2Camel(node_path.basename(id, node_path.extname(id)), true)
201
- });
202
- compileRes.messages.forEach(({ type, value: warning }) => {
203
- if (type === "warning") {
204
- this.warn(warning.message);
225
+ try {
226
+ const compileRes = compiler.compile(src, {
227
+ sourcemap,
228
+ debug: isDev,
229
+ hashId: compileResultCache.get(id)?.hashId || void 0,
230
+ componentName: compiler.util.kebab2Camel(path.basename(id, path.extname(id)), true),
231
+ reserveTemplateComment: getReserveHtmlComments(getQingkuaiConfiguration(id))
232
+ });
233
+ compileRes.messages.forEach(({ type, value: warning }) => {
234
+ if (type === "warning") {
235
+ this.warn(warning.message);
236
+ }
237
+ });
238
+ compileResultCache.set(id, compileRes);
239
+ const compiledCodeArr = [compileRes.code];
240
+ compileRes.inputDescriptor.styles.forEach((_, index) => {
241
+ compiledCodeArr.push(`import "virtual:[${index}]${id}.css?${Date.now()}"`);
242
+ });
243
+ const baseMap = {
244
+ version: 3,
245
+ sources: [id],
246
+ sourcesContent: [src]
247
+ };
248
+ const compiledCode = compiledCodeArr.join("\n");
249
+ if (!compileRes.inputDescriptor.script.isTS) {
250
+ if (!sourcemap) {
251
+ return compiledCode;
252
+ }
253
+ return {
254
+ code: compiledCode,
255
+ map: Object.assign(baseMap, {
256
+ mappings: compileRes.mappings
257
+ })
258
+ };
205
259
  }
206
- });
207
- compileResultCache.set(id, compileRes);
208
- const compiledCodeArr = [compileRes.code];
209
- compileRes.inputDescriptor.styles.forEach((_, index) => {
210
- compiledCodeArr.push(`import "virtual:[${index}]${id}.css?${Date.now()}"`);
211
- });
212
- const baseMap = {
213
- version: 3,
214
- sources: [id],
215
- sourcesContent: [src]
216
- };
217
- const compiledCode = compiledCodeArr.join("\n");
218
- if (!compileRes.inputDescriptor.script.isTS) {
260
+ const esBuildCompileRes = await vite.transformWithEsbuild(
261
+ compiledCode,
262
+ id,
263
+ {
264
+ sourcemap,
265
+ loader: "ts",
266
+ target: "esnext"
267
+ },
268
+ sourcemap ? {
269
+ version: 3,
270
+ sources: [id],
271
+ sourcesContent: [src],
272
+ mappings: compileRes.mappings
273
+ } : void 0
274
+ );
219
275
  if (!sourcemap) {
220
- return compiledCode;
276
+ return esBuildCompileRes.code;
221
277
  }
222
278
  return {
223
- code: compiledCode,
279
+ code: esBuildCompileRes.code,
224
280
  map: Object.assign(baseMap, {
225
- mappings: compileRes.mappings
281
+ mappings: esBuildCompileRes.map.mappings
226
282
  })
227
283
  };
284
+ } catch (err) {
285
+ err.pos = err.loc.start.index, this.error(err);
228
286
  }
229
- const esBuildCompileRes = await vite.transformWithEsbuild(
230
- compiledCode,
231
- id,
232
- {
233
- sourcemap,
234
- loader: "ts",
235
- target: "esnext"
236
- },
237
- sourcemap ? {
238
- version: 3,
239
- sources: [id],
240
- sourcesContent: [src],
241
- mappings: compileRes.mappings
242
- } : void 0
243
- );
244
- if (!sourcemap) {
245
- return esBuildCompileRes.code;
246
- }
247
- return {
248
- code: esBuildCompileRes.code,
249
- map: Object.assign(baseMap, {
250
- mappings: esBuildCompileRes.map.mappings
251
- })
252
- };
253
287
  }
254
288
  };
255
289
  function getQingkuaiConfiguration(id) {
@@ -261,7 +295,7 @@ function qingkuaiPlugin() {
261
295
  reserveHtmlComments: "development"
262
296
  };
263
297
  while (true) {
264
- const dir = node_path.dirname(id);
298
+ const dir = path.dirname(id);
265
299
  if (dir === id) {
266
300
  break;
267
301
  }
@@ -287,7 +321,7 @@ function qingkuaiPlugin() {
287
321
  }
288
322
  function createQingkuaiConfigurationWatcher(server) {
289
323
  const watcher = server.watcher.add([".qingkuairc", "!**/node_modules/**"]);
290
- watcher.on("unlink", (path) => qingkuaiConfigurations.delete(node_path.dirname(path)));
324
+ watcher.on("unlink", (p) => qingkuaiConfigurations.delete(path.dirname(p)));
291
325
  watcher.on("change", recordQingkuaiConfiguration);
292
326
  watcher.on("add", recordQingkuaiConfiguration);
293
327
  }
@@ -298,7 +332,7 @@ function qingkuaiPlugin() {
298
332
  }
299
333
  function recordQingkuaiConfiguration(fileName) {
300
334
  try {
301
- 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")));
302
336
  } catch {
303
337
  }
304
338
  }
package/dist/index.js CHANGED
@@ -1,11 +1,13 @@
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
- import { decode, encode } from '@jridgewell/sourcemap-codec';
5
4
  import { compile, util } from 'qingkuai/compiler';
5
+ import { LinesAndColumns } from 'lines-and-columns';
6
+ import { decode, encode } from '@jridgewell/sourcemap-codec';
6
7
  import postcss from 'postcss';
7
8
  import selectorParser from 'postcss-selector-parser';
8
9
  import { transformWithEsbuild, preprocessCSS } from 'vite';
10
+ import { SourceMapConsumer } from 'source-map-js';
9
11
 
10
12
  function findFilesByName(dir, targetFileName, ignoreList) {
11
13
  const result = [];
@@ -24,23 +26,13 @@ function findFilesByName(dir, targetFileName, ignoreList) {
24
26
  return result;
25
27
  }
26
28
 
27
- function offsetSourceMap(mappings, sourceIndex, preLine, preColumn) {
28
- return decode(mappings).map((line) => {
29
- return line.map((segment) => {
30
- if (segment.length === 1 || segment[1] !== sourceIndex) {
31
- return segment;
32
- }
33
- if (segment[2] === preLine) {
34
- segment[3] += preColumn;
35
- } else {
36
- segment[2] += preLine;
37
- }
38
- return segment;
39
- });
40
- });
41
- }
42
-
43
29
  async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
30
+ let error;
31
+ const hashAttribute = selectorParser.attribute({
32
+ attribute: `qk-${hash}`,
33
+ value: void 0,
34
+ raws: {}
35
+ });
44
36
  const processor = postcss([
45
37
  {
46
38
  postcssPlugin: "postcss-attach-scope-qingkuai",
@@ -50,19 +42,22 @@ async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
50
42
  }
51
43
  rule.selector = selectorParser((selectors) => {
52
44
  selectors.each((selector) => {
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];
50
+ }
51
+ }
52
+ if (usedScopeAttribute) {
53
+ return;
54
+ }
53
55
  const index = selector.nodes.findLastIndex(({ type }) => {
54
- return type === "class" || type === "tag" || type === "id" || type === "universal";
56
+ return type === "id" || type === "tag" || type === "class" || type === "universal" || type === "attribute";
55
57
  });
56
58
  if (index !== -1) {
57
59
  const lastNode = selector.nodes[index];
58
- lastNode.parent?.insertAfter(
59
- lastNode,
60
- selectorParser.attribute({
61
- attribute: `qk-${hash}`,
62
- value: void 0,
63
- raws: {}
64
- })
65
- );
60
+ lastNode.parent?.insertAfter(lastNode, hashAttribute);
66
61
  }
67
62
  });
68
63
  }).processSync(rule.selector);
@@ -76,7 +71,26 @@ async function attachScopeForStyleSelectors(code, hash, sourceFile, map) {
76
71
  annotation: false
77
72
  }
78
73
  });
79
- return { code: ret.css, mappings: ret.map.toJSON().mappings };
74
+ return { error, code: ret.css, mappings: ret.map.toJSON().mappings };
75
+ }
76
+
77
+ function offsetSourceMap(mappings, sourceIndex, preLine, preColumn) {
78
+ return decode(mappings).map((line) => {
79
+ return line.map((segment) => {
80
+ if (segment.length === 1 || segment[1] !== sourceIndex) {
81
+ return segment;
82
+ }
83
+ if (segment[2] === preLine) {
84
+ segment[3] += preColumn;
85
+ } else {
86
+ segment[2] += preLine;
87
+ }
88
+ return segment;
89
+ });
90
+ });
91
+ }
92
+ async function getOriginalPosition(map, line, column) {
93
+ return new SourceMapConsumer(map).originalPositionFor({ line, column });
80
94
  }
81
95
 
82
96
  function qingkuaiPlugin() {
@@ -86,14 +100,19 @@ function qingkuaiPlugin() {
86
100
  let viteConfig;
87
101
  const compileResultCache = /* @__PURE__ */ new Map();
88
102
  const qingkuaiConfigurations = /* @__PURE__ */ new Map();
89
- const qingkuaiPackageServeRE = /node_modules\/\.vite\/deps\/chunk-.*$/;
90
- const confIdentifierRE = /__qk_expose_(?:dependencies|destructions)__/g;
91
103
  const styleIdRE = /^virtual:\[\d+\].*?\.qk.(?:css|s[ac]ss|less|stylus|postcss)\?\d{13}$/;
92
- const qingkuaiPackageBuildRE = /(?:node_modules)?\/qingkuai\/dist\/esm\/(?:chunks|runtime)\/\w+\.js$/;
93
104
  return {
94
105
  name: "qingkuai-compiler",
95
- config(_, env) {
106
+ config(config, env) {
107
+ const rootPath = path.resolve(process.cwd(), config.root ?? "");
108
+ const qingkuaiConfig = getQingkuaiConfiguration(rootPath);
96
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
+ };
97
116
  },
98
117
  configureServer(server) {
99
118
  createQingkuaiConfigurationWatcher(server);
@@ -113,10 +132,11 @@ function qingkuaiPlugin() {
113
132
  if (styleIdRE.test(id)) {
114
133
  return id;
115
134
  }
116
- if (importer?.endsWith(".qk") && !extname(id)) {
135
+ if (importer?.endsWith(".qk") && !path.extname(id)) {
117
136
  const qingkuaiConfig = getQingkuaiConfiguration(id);
118
- return join(dirname(importer), id + (qingkuaiConfig.resolveImportExtension ? ".qk" : ""));
137
+ return path.join(path.dirname(importer), id + (qingkuaiConfig.resolveImportExtension ? ".qk" : ""));
119
138
  }
139
+ return id;
120
140
  },
121
141
  async load(id) {
122
142
  if (styleIdRE.test(id)) {
@@ -161,6 +181,30 @@ function qingkuaiPlugin() {
161
181
  style.loc.start.column - 1
162
182
  )
163
183
  );
184
+ if (attachScopeResult.error) {
185
+ if (!attachScopeResult.error.loc) {
186
+ this.error(attachScopeResult.error.message);
187
+ } else {
188
+ const preprocessedPosition = await getOriginalPosition(
189
+ assertedPreprocessMap,
190
+ attachScopeResult.error.loc.line,
191
+ attachScopeResult.error.loc.column
192
+ );
193
+ const preprocessedIndex = new LinesAndColumns(style.code).indexForLocation({
194
+ line: preprocessedPosition.line - 1,
195
+ column: preprocessedPosition.column
196
+ }) || 0;
197
+ const position = compileRes.inputDescriptor.positions[style.loc.start.index + preprocessedIndex];
198
+ this.error({
199
+ message: attachScopeResult.error.message,
200
+ loc: {
201
+ file: fileId,
202
+ line: position.line,
203
+ column: position.column
204
+ }
205
+ });
206
+ }
207
+ }
164
208
  return {
165
209
  code: attachScopeResult.code,
166
210
  map: {
@@ -173,81 +217,71 @@ function qingkuaiPlugin() {
173
217
  }
174
218
  },
175
219
  async transform(src, id) {
176
- const qingkuaiConfig = getQingkuaiConfiguration(id);
177
220
  if (!id.endsWith(".qk")) {
178
- if (qingkuaiPackageServeRE.test(id) || qingkuaiPackageBuildRE.test(id)) {
179
- const ret = src.replace(confIdentifierRE, (s) => {
180
- switch (s) {
181
- case "__qk_expose_dependencies__":
182
- return JSON.stringify(!!qingkuaiConfig.exposeDependencies);
183
- case "__qk_expose_destructions__":
184
- return JSON.stringify(!!qingkuaiConfig.exposeDestructions);
185
- default:
186
- return s;
187
- }
188
- });
189
- return ret;
190
- }
191
221
  return;
192
222
  }
193
- const compileRes = compile(src, {
194
- sourcemap,
195
- debug: isDev,
196
- hashId: compileResultCache.get(id)?.hashId || void 0,
197
- reserveTemplateComment: getReserveHtmlComments(qingkuaiConfig),
198
- componentName: util.kebab2Camel(basename(id, extname(id)), true)
199
- });
200
- compileRes.messages.forEach(({ type, value: warning }) => {
201
- if (type === "warning") {
202
- this.warn(warning.message);
223
+ try {
224
+ const compileRes = compile(src, {
225
+ sourcemap,
226
+ debug: isDev,
227
+ hashId: compileResultCache.get(id)?.hashId || void 0,
228
+ componentName: util.kebab2Camel(path.basename(id, path.extname(id)), true),
229
+ reserveTemplateComment: getReserveHtmlComments(getQingkuaiConfiguration(id))
230
+ });
231
+ compileRes.messages.forEach(({ type, value: warning }) => {
232
+ if (type === "warning") {
233
+ this.warn(warning.message);
234
+ }
235
+ });
236
+ compileResultCache.set(id, compileRes);
237
+ const compiledCodeArr = [compileRes.code];
238
+ compileRes.inputDescriptor.styles.forEach((_, index) => {
239
+ compiledCodeArr.push(`import "virtual:[${index}]${id}.css?${Date.now()}"`);
240
+ });
241
+ const baseMap = {
242
+ version: 3,
243
+ sources: [id],
244
+ sourcesContent: [src]
245
+ };
246
+ const compiledCode = compiledCodeArr.join("\n");
247
+ if (!compileRes.inputDescriptor.script.isTS) {
248
+ if (!sourcemap) {
249
+ return compiledCode;
250
+ }
251
+ return {
252
+ code: compiledCode,
253
+ map: Object.assign(baseMap, {
254
+ mappings: compileRes.mappings
255
+ })
256
+ };
203
257
  }
204
- });
205
- compileResultCache.set(id, compileRes);
206
- const compiledCodeArr = [compileRes.code];
207
- compileRes.inputDescriptor.styles.forEach((_, index) => {
208
- compiledCodeArr.push(`import "virtual:[${index}]${id}.css?${Date.now()}"`);
209
- });
210
- const baseMap = {
211
- version: 3,
212
- sources: [id],
213
- sourcesContent: [src]
214
- };
215
- const compiledCode = compiledCodeArr.join("\n");
216
- if (!compileRes.inputDescriptor.script.isTS) {
258
+ const esBuildCompileRes = await transformWithEsbuild(
259
+ compiledCode,
260
+ id,
261
+ {
262
+ sourcemap,
263
+ loader: "ts",
264
+ target: "esnext"
265
+ },
266
+ sourcemap ? {
267
+ version: 3,
268
+ sources: [id],
269
+ sourcesContent: [src],
270
+ mappings: compileRes.mappings
271
+ } : void 0
272
+ );
217
273
  if (!sourcemap) {
218
- return compiledCode;
274
+ return esBuildCompileRes.code;
219
275
  }
220
276
  return {
221
- code: compiledCode,
277
+ code: esBuildCompileRes.code,
222
278
  map: Object.assign(baseMap, {
223
- mappings: compileRes.mappings
279
+ mappings: esBuildCompileRes.map.mappings
224
280
  })
225
281
  };
282
+ } catch (err) {
283
+ err.pos = err.loc.start.index, this.error(err);
226
284
  }
227
- const esBuildCompileRes = await transformWithEsbuild(
228
- compiledCode,
229
- id,
230
- {
231
- sourcemap,
232
- loader: "ts",
233
- target: "esnext"
234
- },
235
- sourcemap ? {
236
- version: 3,
237
- sources: [id],
238
- sourcesContent: [src],
239
- mappings: compileRes.mappings
240
- } : void 0
241
- );
242
- if (!sourcemap) {
243
- return esBuildCompileRes.code;
244
- }
245
- return {
246
- code: esBuildCompileRes.code,
247
- map: Object.assign(baseMap, {
248
- mappings: esBuildCompileRes.map.mappings
249
- })
250
- };
251
285
  }
252
286
  };
253
287
  function getQingkuaiConfiguration(id) {
@@ -259,7 +293,7 @@ function qingkuaiPlugin() {
259
293
  reserveHtmlComments: "development"
260
294
  };
261
295
  while (true) {
262
- const dir = dirname(id);
296
+ const dir = path.dirname(id);
263
297
  if (dir === id) {
264
298
  break;
265
299
  }
@@ -285,7 +319,7 @@ function qingkuaiPlugin() {
285
319
  }
286
320
  function createQingkuaiConfigurationWatcher(server) {
287
321
  const watcher = server.watcher.add([".qingkuairc", "!**/node_modules/**"]);
288
- watcher.on("unlink", (path) => qingkuaiConfigurations.delete(dirname(path)));
322
+ watcher.on("unlink", (p) => qingkuaiConfigurations.delete(path.dirname(p)));
289
323
  watcher.on("change", recordQingkuaiConfiguration);
290
324
  watcher.on("add", recordQingkuaiConfiguration);
291
325
  }
@@ -296,7 +330,7 @@ function qingkuaiPlugin() {
296
330
  }
297
331
  function recordQingkuaiConfiguration(fileName) {
298
332
  try {
299
- qingkuaiConfigurations.set(dirname(fileName), JSON.parse(readFileSync(fileName, "utf-8")));
333
+ qingkuaiConfigurations.set(path.dirname(fileName), JSON.parse(readFileSync(fileName, "utf-8")));
300
334
  } catch {
301
335
  }
302
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.9",
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.41",
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,12 +33,14 @@
33
33
  "vite": "^6.2.4"
34
34
  },
35
35
  "peerDependencies": {
36
- "qingkuai": "^1.0.41",
36
+ "qingkuai": "^1.0.43",
37
37
  "vite": "^6.2.4"
38
38
  },
39
39
  "dependencies": {
40
40
  "@jridgewell/sourcemap-codec": "^1.5.0",
41
+ "lines-and-columns": "^2.0.4",
41
42
  "postcss": "^8.5.3",
42
- "postcss-selector-parser": "^7.1.0"
43
+ "postcss-selector-parser": "^7.1.0",
44
+ "source-map-js": "^1.2.1"
43
45
  }
44
46
  }
@@ -1,2 +0,0 @@
1
- import type { PluginOption } from "vite";
2
- export default function qingkuaiPlugin(): PluginOption;
@@ -1,5 +0,0 @@
1
- import type { SourceMap } from "./types";
2
- export declare function attachScopeForStyleSelectors(code: string, hash: string, sourceFile: string, map: SourceMap | undefined): Promise<{
3
- code: string;
4
- mappings: string;
5
- }>;
@@ -1,2 +0,0 @@
1
- import type { SourceMapMappings } from "@jridgewell/sourcemap-codec";
2
- export declare function offsetSourceMap(mappings: string, sourceIndex: number, preLine: number, preColumn: number): SourceMapMappings;
@@ -1,18 +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
- }>;
@@ -1 +0,0 @@
1
- export declare function findFilesByName(dir: string, targetFileName: string, ignoreList: Set<string>): string[];