@pikacss/integration 0.0.15 → 0.0.17

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
@@ -8,6 +8,7 @@ const localPkg = require('local-pkg');
8
8
  const MagicString = require('magic-string');
9
9
  const micromatch = require('micromatch');
10
10
  const pathe = require('pathe');
11
+ const perfectDebounce = require('perfect-debounce');
11
12
 
12
13
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
13
14
 
@@ -270,8 +271,8 @@ async function createCtx(options) {
270
271
  if (inlineConfig != null)
271
272
  return { config: inlineConfig, file: null };
272
273
  let resolvedConfigPath = configSources.find((path) => {
273
- const stat2 = node_fs.statSync(path, { throwIfNoEntry: false });
274
- return stat2 != null && stat2.isFile();
274
+ const stat = node_fs.statSync(path, { throwIfNoEntry: false });
275
+ return stat != null && stat.isFile();
275
276
  });
276
277
  if (resolvedConfigPath == null) {
277
278
  if (autoCreateConfig === false)
@@ -296,7 +297,7 @@ async function createCtx(options) {
296
297
  const config = await jiti$1.import(resolvedConfigPath, { default: true });
297
298
  return { config, file: resolvedConfigPath };
298
299
  },
299
- init: async () => {
300
+ init: perfectDebounce.debounce(async () => {
300
301
  ctx.isReady = false;
301
302
  ctx.usages.clear();
302
303
  const { config, file } = await ctx.loadConfig().catch((error) => {
@@ -304,34 +305,32 @@ async function createCtx(options) {
304
305
  return { config: null, file: null };
305
306
  });
306
307
  ctx.resolvedConfigPath = file;
307
- try {
308
- ctx.engine = await core.createEngine(config ?? {});
309
- } catch (error) {
310
- core.warn(`Failed to create engine: ${error.message}. Maybe the config file is invalid, falling back to default config.`, error);
311
- ctx.engine = await core.createEngine({});
312
- }
313
- ctx.engine.config.plugins.unshift({
308
+ const devPlugin = core.defineEnginePlugin({
314
309
  name: "@pikacss/integration:dev",
315
310
  preflightUpdated: () => ctx.hooks.styleUpdated.trigger(),
316
311
  atomicStyleAdded: () => ctx.hooks.styleUpdated.trigger(),
317
312
  autocompleteConfigUpdated: () => ctx.hooks.tsCodegenUpdated.trigger()
318
313
  });
314
+ try {
315
+ const _config = config ?? {};
316
+ _config.plugins = _config.plugins ?? [];
317
+ _config.plugins.unshift(devPlugin);
318
+ ctx.engine = await core.createEngine(_config);
319
+ } catch (error) {
320
+ core.warn(`Failed to create engine: ${error.message}. Maybe the config file is invalid, falling back to default config.`, error);
321
+ ctx.engine = await core.createEngine({ plugins: [devPlugin] });
322
+ }
319
323
  await promises.mkdir(pathe.dirname(devCssFilepath), { recursive: true }).catch(() => {
320
324
  });
321
- const isDevCssFileExists = await promises.stat(devCssFilepath).then((stat2) => stat2.isFile()).catch(() => false);
322
- if (isDevCssFileExists === false)
323
- await promises.writeFile(devCssFilepath, "");
325
+ await promises.writeFile(devCssFilepath, "");
324
326
  if (tsCodegenFilepath != null) {
325
327
  await promises.mkdir(pathe.dirname(tsCodegenFilepath), { recursive: true }).catch(() => {
326
328
  });
327
- const isGenTsFileExists = await promises.stat(tsCodegenFilepath).then((stat2) => stat2.isFile()).catch(() => false);
328
- if (isGenTsFileExists === false) {
329
- const content = await generateTsCodegenContent(ctx);
330
- await promises.writeFile(tsCodegenFilepath, content);
331
- }
329
+ const content = generateTsCodegenContent(ctx);
330
+ await promises.writeFile(tsCodegenFilepath, content);
332
331
  }
333
332
  ctx.isReady = true;
334
- },
333
+ }, 300),
335
334
  isReady: false,
336
335
  configSources,
337
336
  resolvedConfigPath: null,
@@ -383,23 +382,35 @@ async function createCtx(options) {
383
382
  return void 0;
384
383
  }
385
384
  },
386
- writeDevCssFile: () => {
385
+ getCssContent: (isDev) => {
387
386
  if (ctx.isReady === false)
388
- return;
387
+ return null;
389
388
  const atomicStyleIds = [...new Set([...ctx.usages.values()].flatMap((i) => [...new Set(i.flatMap((i2) => i2.atomicStyleIds))]))];
390
389
  const css = [
391
390
  `/* Auto-generated by ${ctx.currentPackageName} */`,
392
- ctx.engine.renderPreflights(true),
393
- ctx.engine.renderAtomicStyles(true, { atomicStyleIds })
391
+ ctx.engine.renderPreflights(isDev),
392
+ ctx.engine.renderAtomicStyles(isDev, { atomicStyleIds })
394
393
  ].join("\n").trim();
395
- node_fs.writeFileSync(ctx.devCssFilepath, css);
394
+ return css;
396
395
  },
397
- writeTsCodegenFile: () => {
396
+ getTsCodegenContent: () => {
398
397
  if (ctx.isReady === false || ctx.tsCodegenFilepath == null)
399
- return;
398
+ return null;
400
399
  const content = generateTsCodegenContent(ctx);
401
- node_fs.writeFileSync(ctx.tsCodegenFilepath, content);
402
- }
400
+ return content;
401
+ },
402
+ writeDevCssFile: perfectDebounce.debounce(async () => {
403
+ const content = ctx.getCssContent(true);
404
+ if (content == null)
405
+ return;
406
+ await promises.writeFile(ctx.devCssFilepath, content);
407
+ }, 300),
408
+ writeTsCodegenFile: perfectDebounce.debounce(async () => {
409
+ const content = ctx.getTsCodegenContent();
410
+ if (ctx.tsCodegenFilepath == null || content == null)
411
+ return;
412
+ await promises.writeFile(ctx.tsCodegenFilepath, content);
413
+ }, 300)
403
414
  };
404
415
  await ctx.init();
405
416
  return ctx;
package/dist/index.d.cts CHANGED
@@ -56,8 +56,10 @@ interface IntegrationContext {
56
56
  code: string;
57
57
  map: SourceMap;
58
58
  } | Nullish>;
59
- writeDevCssFile: () => void;
60
- writeTsCodegenFile: () => void;
59
+ getCssContent: (isDev: boolean) => string | Nullish;
60
+ getTsCodegenContent: () => string | Nullish;
61
+ writeDevCssFile: () => Promise<void>;
62
+ writeTsCodegenFile: () => Promise<void>;
61
63
  }
62
64
  interface IntegrationContextOptions {
63
65
  cwd: string;
package/dist/index.d.mts CHANGED
@@ -56,8 +56,10 @@ interface IntegrationContext {
56
56
  code: string;
57
57
  map: SourceMap;
58
58
  } | Nullish>;
59
- writeDevCssFile: () => void;
60
- writeTsCodegenFile: () => void;
59
+ getCssContent: (isDev: boolean) => string | Nullish;
60
+ getTsCodegenContent: () => string | Nullish;
61
+ writeDevCssFile: () => Promise<void>;
62
+ writeTsCodegenFile: () => Promise<void>;
61
63
  }
62
64
  interface IntegrationContextOptions {
63
65
  cwd: string;
package/dist/index.d.ts CHANGED
@@ -56,8 +56,10 @@ interface IntegrationContext {
56
56
  code: string;
57
57
  map: SourceMap;
58
58
  } | Nullish>;
59
- writeDevCssFile: () => void;
60
- writeTsCodegenFile: () => void;
59
+ getCssContent: (isDev: boolean) => string | Nullish;
60
+ getTsCodegenContent: () => string | Nullish;
61
+ writeDevCssFile: () => Promise<void>;
62
+ writeTsCodegenFile: () => Promise<void>;
61
63
  }
62
64
  interface IntegrationContextOptions {
63
65
  cwd: string;
package/dist/index.mjs CHANGED
@@ -1,12 +1,13 @@
1
- import { writeFileSync, statSync } from 'node:fs';
2
- import { mkdir, stat, writeFile } from 'node:fs/promises';
3
- import { setWarnFn, warn, createEngine } from '@pikacss/core';
1
+ import { statSync } from 'node:fs';
2
+ import { writeFile, mkdir } from 'node:fs/promises';
3
+ import { setWarnFn, warn, defineEnginePlugin, createEngine } from '@pikacss/core';
4
4
  export * from '@pikacss/core';
5
5
  import { createJiti } from 'jiti';
6
6
  import { isPackageExists } from 'local-pkg';
7
7
  import MagicString from 'magic-string';
8
8
  import micromatch from 'micromatch';
9
9
  import { isAbsolute, resolve, join, dirname, relative } from 'pathe';
10
+ import { debounce } from 'perfect-debounce';
10
11
 
11
12
  function createEventHook() {
12
13
  const listeners = /* @__PURE__ */ new Set();
@@ -264,8 +265,8 @@ async function createCtx(options) {
264
265
  if (inlineConfig != null)
265
266
  return { config: inlineConfig, file: null };
266
267
  let resolvedConfigPath = configSources.find((path) => {
267
- const stat2 = statSync(path, { throwIfNoEntry: false });
268
- return stat2 != null && stat2.isFile();
268
+ const stat = statSync(path, { throwIfNoEntry: false });
269
+ return stat != null && stat.isFile();
269
270
  });
270
271
  if (resolvedConfigPath == null) {
271
272
  if (autoCreateConfig === false)
@@ -290,7 +291,7 @@ async function createCtx(options) {
290
291
  const config = await jiti.import(resolvedConfigPath, { default: true });
291
292
  return { config, file: resolvedConfigPath };
292
293
  },
293
- init: async () => {
294
+ init: debounce(async () => {
294
295
  ctx.isReady = false;
295
296
  ctx.usages.clear();
296
297
  const { config, file } = await ctx.loadConfig().catch((error) => {
@@ -298,34 +299,32 @@ async function createCtx(options) {
298
299
  return { config: null, file: null };
299
300
  });
300
301
  ctx.resolvedConfigPath = file;
301
- try {
302
- ctx.engine = await createEngine(config ?? {});
303
- } catch (error) {
304
- warn(`Failed to create engine: ${error.message}. Maybe the config file is invalid, falling back to default config.`, error);
305
- ctx.engine = await createEngine({});
306
- }
307
- ctx.engine.config.plugins.unshift({
302
+ const devPlugin = defineEnginePlugin({
308
303
  name: "@pikacss/integration:dev",
309
304
  preflightUpdated: () => ctx.hooks.styleUpdated.trigger(),
310
305
  atomicStyleAdded: () => ctx.hooks.styleUpdated.trigger(),
311
306
  autocompleteConfigUpdated: () => ctx.hooks.tsCodegenUpdated.trigger()
312
307
  });
308
+ try {
309
+ const _config = config ?? {};
310
+ _config.plugins = _config.plugins ?? [];
311
+ _config.plugins.unshift(devPlugin);
312
+ ctx.engine = await createEngine(_config);
313
+ } catch (error) {
314
+ warn(`Failed to create engine: ${error.message}. Maybe the config file is invalid, falling back to default config.`, error);
315
+ ctx.engine = await createEngine({ plugins: [devPlugin] });
316
+ }
313
317
  await mkdir(dirname(devCssFilepath), { recursive: true }).catch(() => {
314
318
  });
315
- const isDevCssFileExists = await stat(devCssFilepath).then((stat2) => stat2.isFile()).catch(() => false);
316
- if (isDevCssFileExists === false)
317
- await writeFile(devCssFilepath, "");
319
+ await writeFile(devCssFilepath, "");
318
320
  if (tsCodegenFilepath != null) {
319
321
  await mkdir(dirname(tsCodegenFilepath), { recursive: true }).catch(() => {
320
322
  });
321
- const isGenTsFileExists = await stat(tsCodegenFilepath).then((stat2) => stat2.isFile()).catch(() => false);
322
- if (isGenTsFileExists === false) {
323
- const content = await generateTsCodegenContent(ctx);
324
- await writeFile(tsCodegenFilepath, content);
325
- }
323
+ const content = generateTsCodegenContent(ctx);
324
+ await writeFile(tsCodegenFilepath, content);
326
325
  }
327
326
  ctx.isReady = true;
328
- },
327
+ }, 300),
329
328
  isReady: false,
330
329
  configSources,
331
330
  resolvedConfigPath: null,
@@ -377,23 +376,35 @@ async function createCtx(options) {
377
376
  return void 0;
378
377
  }
379
378
  },
380
- writeDevCssFile: () => {
379
+ getCssContent: (isDev) => {
381
380
  if (ctx.isReady === false)
382
- return;
381
+ return null;
383
382
  const atomicStyleIds = [...new Set([...ctx.usages.values()].flatMap((i) => [...new Set(i.flatMap((i2) => i2.atomicStyleIds))]))];
384
383
  const css = [
385
384
  `/* Auto-generated by ${ctx.currentPackageName} */`,
386
- ctx.engine.renderPreflights(true),
387
- ctx.engine.renderAtomicStyles(true, { atomicStyleIds })
385
+ ctx.engine.renderPreflights(isDev),
386
+ ctx.engine.renderAtomicStyles(isDev, { atomicStyleIds })
388
387
  ].join("\n").trim();
389
- writeFileSync(ctx.devCssFilepath, css);
388
+ return css;
390
389
  },
391
- writeTsCodegenFile: () => {
390
+ getTsCodegenContent: () => {
392
391
  if (ctx.isReady === false || ctx.tsCodegenFilepath == null)
393
- return;
392
+ return null;
394
393
  const content = generateTsCodegenContent(ctx);
395
- writeFileSync(ctx.tsCodegenFilepath, content);
396
- }
394
+ return content;
395
+ },
396
+ writeDevCssFile: debounce(async () => {
397
+ const content = ctx.getCssContent(true);
398
+ if (content == null)
399
+ return;
400
+ await writeFile(ctx.devCssFilepath, content);
401
+ }, 300),
402
+ writeTsCodegenFile: debounce(async () => {
403
+ const content = ctx.getTsCodegenContent();
404
+ if (ctx.tsCodegenFilepath == null || content == null)
405
+ return;
406
+ await writeFile(ctx.tsCodegenFilepath, content);
407
+ }, 300)
397
408
  };
398
409
  await ctx.init();
399
410
  return ctx;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.0.15",
7
+ "version": "0.0.17",
8
8
  "author": "DevilTea <ch19980814@gmail.com>",
9
9
  "license": "MIT",
10
10
  "repository": {
@@ -40,7 +40,8 @@
40
40
  "magic-string": "^0.30.12",
41
41
  "micromatch": "^4.0.8",
42
42
  "pathe": "^2.0.3",
43
- "@pikacss/core": "0.0.15"
43
+ "perfect-debounce": "^1.0.0",
44
+ "@pikacss/core": "0.0.17"
44
45
  },
45
46
  "devDependencies": {
46
47
  "@types/micromatch": "^4.0.9"