tailwindcss-injector 0.0.2 → 0.1.1

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/postcss.cjs CHANGED
@@ -28,12 +28,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  ));
29
29
 
30
30
  // ../../node_modules/.pnpm/tsup@8.3.5_jiti@2.4.1_postcss@8.4.49_tsx@4.19.2_typescript@5.7.2_yaml@2.6.1/node_modules/tsup/assets/cjs_shims.js
31
- var getImportMetaUrl, importMetaUrl;
32
31
  var init_cjs_shims = __esm({
33
32
  "../../node_modules/.pnpm/tsup@8.3.5_jiti@2.4.1_postcss@8.4.49_tsx@4.19.2_typescript@5.7.2_yaml@2.6.1/node_modules/tsup/assets/cjs_shims.js"() {
34
33
  "use strict";
35
- getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
36
- importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
37
34
  }
38
35
  });
39
36
 
@@ -222,17 +219,12 @@ var import_set_value = __toESM(require_set_value(), 1);
222
219
  var _process = require('process'); var _process2 = _interopRequireDefault(_process);
223
220
  var _postcss = require('postcss'); var _postcss2 = _interopRequireDefault(_postcss);
224
221
  var _tailwindcss = require('tailwindcss'); var _tailwindcss2 = _interopRequireDefault(_tailwindcss);
225
-
226
- // src/config.ts
227
- init_cjs_shims();
228
- var _path = require('path'); var _path2 = _interopRequireDefault(_path);
229
-
230
- var _jiti = require('jiti');
231
- var _lilconfig = require('lilconfig');
222
+ var _tailwindcssconfig = require('tailwindcss-config');
232
223
 
233
224
  // src/utils.ts
234
225
  init_cjs_shims();
235
-
226
+ var _crypto = require('crypto'); var _crypto2 = _interopRequireDefault(_crypto);
227
+ var _path = require('path'); var _path2 = _interopRequireDefault(_path);
236
228
 
237
229
  // ../../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs
238
230
  init_cjs_shims();
@@ -305,9 +297,14 @@ var defuArrayFn = createDefu((object, key, currentValue) => {
305
297
 
306
298
  // src/utils.ts
307
299
  function removeFileExtension(filePath) {
308
- let baseName = _path2.default.basename(filePath, _path2.default.extname(filePath));
309
- while (baseName.includes(".")) {
310
- baseName = _path2.default.basename(baseName, _path2.default.extname(baseName));
300
+ if (!filePath) {
301
+ return "";
302
+ }
303
+ let baseName = _path2.default.basename(filePath);
304
+ let ext = _path2.default.extname(baseName);
305
+ while (ext) {
306
+ baseName = baseName.slice(0, -ext.length);
307
+ ext = _path2.default.extname(baseName);
311
308
  }
312
309
  const dir = _path2.default.dirname(filePath);
313
310
  return _path2.default.join(dir, baseName);
@@ -318,41 +315,107 @@ var defuOverrideArray = createDefu((obj, key, value) => {
318
315
  return true;
319
316
  }
320
317
  });
318
+ function md5(input) {
319
+ const hash = _crypto2.default.createHash("md5");
320
+ hash.update(input);
321
+ return hash.digest("hex");
322
+ }
321
323
 
322
- // src/config.ts
323
- var jiti = _jiti.createJiti.call(void 0, importMetaUrl);
324
- var moduleName = "tailwind";
325
- async function loadConfig(options) {
326
- const { config, cwd } = defuOverrideArray(options, {
327
- cwd: _process2.default.cwd()
328
- });
329
- const searcher = _lilconfig.lilconfig.call(void 0, "tailwindcss", {
330
- searchPlaces: [
331
- `${moduleName}.config.js`,
332
- `${moduleName}.config.cjs`,
333
- `${moduleName}.config.mjs`,
334
- `${moduleName}.config.ts`,
335
- `${moduleName}.config.cts`,
336
- `${moduleName}.config.mts`
337
- ],
338
- loaders: {
339
- ".js": jiti,
340
- ".cjs": jiti,
341
- ".mjs": jiti,
342
- ".ts": jiti,
343
- ".cts": jiti,
344
- ".mts": jiti
324
+ // src/wxml.ts
325
+ init_cjs_shims();
326
+
327
+ var _fsextra = require('fs-extra'); var _fsextra2 = _interopRequireDefault(_fsextra);
328
+ var _htmlparser2 = require('htmlparser2');
329
+ var _magicstring = require('magic-string'); var _magicstring2 = _interopRequireDefault(_magicstring);
330
+ var hashMap = /* @__PURE__ */ new Map();
331
+ var depsMap = /* @__PURE__ */ new Map();
332
+ var srcImportTagsMap = {
333
+ // audio: ['src', 'poster'],
334
+ // video: ['src', 'poster'],
335
+ // image: ['src'],
336
+ // https://developers.weixin.qq.com/miniprogram/dev/reference/wxs/01wxs-module.html
337
+ // wxs: ['src'],
338
+ // https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/import.html
339
+ import: ["src"],
340
+ include: ["src"]
341
+ };
342
+ function processWxml(wxml) {
343
+ const ms = new (0, _magicstring2.default)(wxml.toString());
344
+ const deps = [];
345
+ let currentTagName = "";
346
+ let importAttrs;
347
+ let attrs = {};
348
+ const parser = new (0, _htmlparser2.Parser)(
349
+ {
350
+ onopentagname(name) {
351
+ currentTagName = name;
352
+ importAttrs = srcImportTagsMap[currentTagName];
353
+ },
354
+ onattribute(name, value, quote) {
355
+ attrs[name] = value;
356
+ if (importAttrs) {
357
+ for (const attrName of importAttrs) {
358
+ if (attrName === name) {
359
+ deps.push({
360
+ name,
361
+ value,
362
+ quote,
363
+ tagName: currentTagName,
364
+ start: parser.startIndex,
365
+ end: parser.endIndex,
366
+ attrs
367
+ });
368
+ }
369
+ }
370
+ }
371
+ },
372
+ onclosetag() {
373
+ currentTagName = "";
374
+ attrs = {};
375
+ importAttrs = void 0;
376
+ }
377
+ },
378
+ {
379
+ lowerCaseTags: false,
380
+ xmlMode: true
381
+ }
382
+ );
383
+ parser.write(
384
+ ms.original
385
+ );
386
+ parser.end();
387
+ return deps;
388
+ }
389
+ async function getDep(filepath, set2) {
390
+ if (await _fsextra2.default.exists(filepath)) {
391
+ const dirname = _path2.default.dirname(filepath);
392
+ set2.add(filepath);
393
+ const wxml = await _fsextra2.default.readFile(filepath, "utf8");
394
+ const hash = md5(wxml);
395
+ const oldHash = hashMap.get(filepath);
396
+ const isChanged = hash !== oldHash;
397
+ let deps = [];
398
+ const cacheDeps = depsMap.get(filepath);
399
+ if (isChanged || !cacheDeps) {
400
+ hashMap.set(filepath, hash);
401
+ deps = await processWxml(wxml);
402
+ depsMap.set(filepath, deps);
403
+ } else {
404
+ deps = cacheDeps;
405
+ }
406
+ for (const { value } of deps) {
407
+ const p = _path2.default.resolve(dirname, value);
408
+ if (!set2.has(p)) {
409
+ await getDep(p, set2);
410
+ }
345
411
  }
346
- });
347
- if (typeof config === "string" && config) {
348
- const configPath = _path2.default.isAbsolute(config) ? config : _path2.default.resolve(cwd, config);
349
- const result = await searcher.load(configPath);
350
- return _optionalChain([result, 'optionalAccess', _ => _.config]);
351
- } else {
352
- const result = await searcher.search(cwd);
353
- return _optionalChain([result, 'optionalAccess', _2 => _2.config]);
354
412
  }
355
413
  }
414
+ async function getDepFiles(file) {
415
+ const set2 = /* @__PURE__ */ new Set();
416
+ await getDep(file, set2);
417
+ return set2;
418
+ }
356
419
 
357
420
  // src/postcss.ts
358
421
  var creator = (options) => {
@@ -370,7 +433,7 @@ var creator = (options) => {
370
433
  {
371
434
  postcssPlugin: "post",
372
435
  async Once(root, helpers) {
373
- if (filter(_optionalChain([root, 'access', _3 => _3.source, 'optionalAccess', _4 => _4.input]))) {
436
+ if (filter(_optionalChain([root, 'access', _ => _.source, 'optionalAccess', _2 => _2.input]))) {
374
437
  let _root = root;
375
438
  for (const params of directiveParams) {
376
439
  const node = root.nodes.find((x) => x.type === "atrule" && x.params === params);
@@ -378,10 +441,10 @@ var creator = (options) => {
378
441
  _root = _root.prepend(helpers.atRule({ name: "tailwind", params }));
379
442
  }
380
443
  }
381
- const cfg = typeof config === "function" ? config(_optionalChain([root, 'access', _5 => _5.source, 'optionalAccess', _6 => _6.input])) : config;
444
+ const cfg = typeof config === "function" ? config(_optionalChain([root, 'access', _3 => _3.source, 'optionalAccess', _4 => _4.input])) : config;
382
445
  let tailwindcssConfig;
383
446
  if (typeof cfg === "string") {
384
- tailwindcssConfig = await loadConfig({
447
+ tailwindcssConfig = await _tailwindcssconfig.loadConfig.call(void 0, {
385
448
  cwd,
386
449
  config: cfg
387
450
  });
@@ -390,10 +453,17 @@ var creator = (options) => {
390
453
  content: []
391
454
  }));
392
455
  }
393
- if (tailwindcssConfig && _optionalChain([root, 'access', _7 => _7.source, 'optionalAccess', _8 => _8.input]) && root.source.input.file) {
456
+ if (tailwindcssConfig && _optionalChain([root, 'access', _5 => _5.source, 'optionalAccess', _6 => _6.input]) && root.source.input.file) {
457
+ const basename = removeFileExtension(root.source.input.file);
394
458
  (0, import_set_value.default)(tailwindcssConfig, "content", [
395
- `${removeFileExtension(root.source.input.file)}.${extensionsGlob}`
459
+ `${basename}.${extensionsGlob}`
396
460
  ]);
461
+ const deps = await getDepFiles(`${basename}.wxml`);
462
+ for (const dep of deps) {
463
+ if (Array.isArray(tailwindcssConfig.content)) {
464
+ tailwindcssConfig.content.push(`${removeFileExtension(dep)}.${extensionsGlob}`);
465
+ }
466
+ }
397
467
  }
398
468
  const { root: newRoot } = await _postcss2.default.call(void 0, [
399
469
  _tailwindcss2.default.call(void 0, tailwindcssConfig)
package/dist/postcss.js CHANGED
@@ -216,19 +216,14 @@ var require_set_value = __commonJS({
216
216
  // src/postcss.ts
217
217
  init_esm_shims();
218
218
  var import_set_value = __toESM(require_set_value(), 1);
219
- import process2 from "node:process";
219
+ import process from "node:process";
220
220
  import postcss from "postcss";
221
221
  import tailwindcss from "tailwindcss";
222
-
223
- // src/config.ts
224
- init_esm_shims();
225
- import path2 from "node:path";
226
- import process from "node:process";
227
- import { createJiti } from "jiti";
228
- import { lilconfig } from "lilconfig";
222
+ import { loadConfig } from "tailwindcss-config";
229
223
 
230
224
  // src/utils.ts
231
225
  init_esm_shims();
226
+ import crypto from "node:crypto";
232
227
  import path from "node:path";
233
228
 
234
229
  // ../../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs
@@ -302,9 +297,14 @@ var defuArrayFn = createDefu((object, key, currentValue) => {
302
297
 
303
298
  // src/utils.ts
304
299
  function removeFileExtension(filePath) {
305
- let baseName = path.basename(filePath, path.extname(filePath));
306
- while (baseName.includes(".")) {
307
- baseName = path.basename(baseName, path.extname(baseName));
300
+ if (!filePath) {
301
+ return "";
302
+ }
303
+ let baseName = path.basename(filePath);
304
+ let ext = path.extname(baseName);
305
+ while (ext) {
306
+ baseName = baseName.slice(0, -ext.length);
307
+ ext = path.extname(baseName);
308
308
  }
309
309
  const dir = path.dirname(filePath);
310
310
  return path.join(dir, baseName);
@@ -315,41 +315,107 @@ var defuOverrideArray = createDefu((obj, key, value) => {
315
315
  return true;
316
316
  }
317
317
  });
318
+ function md5(input) {
319
+ const hash = crypto.createHash("md5");
320
+ hash.update(input);
321
+ return hash.digest("hex");
322
+ }
318
323
 
319
- // src/config.ts
320
- var jiti = createJiti(import.meta.url);
321
- var moduleName = "tailwind";
322
- async function loadConfig(options) {
323
- const { config, cwd } = defuOverrideArray(options, {
324
- cwd: process.cwd()
325
- });
326
- const searcher = lilconfig("tailwindcss", {
327
- searchPlaces: [
328
- `${moduleName}.config.js`,
329
- `${moduleName}.config.cjs`,
330
- `${moduleName}.config.mjs`,
331
- `${moduleName}.config.ts`,
332
- `${moduleName}.config.cts`,
333
- `${moduleName}.config.mts`
334
- ],
335
- loaders: {
336
- ".js": jiti,
337
- ".cjs": jiti,
338
- ".mjs": jiti,
339
- ".ts": jiti,
340
- ".cts": jiti,
341
- ".mts": jiti
324
+ // src/wxml.ts
325
+ init_esm_shims();
326
+ import path2 from "node:path";
327
+ import fs from "fs-extra";
328
+ import { Parser } from "htmlparser2";
329
+ import MagicString from "magic-string";
330
+ var hashMap = /* @__PURE__ */ new Map();
331
+ var depsMap = /* @__PURE__ */ new Map();
332
+ var srcImportTagsMap = {
333
+ // audio: ['src', 'poster'],
334
+ // video: ['src', 'poster'],
335
+ // image: ['src'],
336
+ // https://developers.weixin.qq.com/miniprogram/dev/reference/wxs/01wxs-module.html
337
+ // wxs: ['src'],
338
+ // https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/import.html
339
+ import: ["src"],
340
+ include: ["src"]
341
+ };
342
+ function processWxml(wxml) {
343
+ const ms = new MagicString(wxml.toString());
344
+ const deps = [];
345
+ let currentTagName = "";
346
+ let importAttrs;
347
+ let attrs = {};
348
+ const parser = new Parser(
349
+ {
350
+ onopentagname(name) {
351
+ currentTagName = name;
352
+ importAttrs = srcImportTagsMap[currentTagName];
353
+ },
354
+ onattribute(name, value, quote) {
355
+ attrs[name] = value;
356
+ if (importAttrs) {
357
+ for (const attrName of importAttrs) {
358
+ if (attrName === name) {
359
+ deps.push({
360
+ name,
361
+ value,
362
+ quote,
363
+ tagName: currentTagName,
364
+ start: parser.startIndex,
365
+ end: parser.endIndex,
366
+ attrs
367
+ });
368
+ }
369
+ }
370
+ }
371
+ },
372
+ onclosetag() {
373
+ currentTagName = "";
374
+ attrs = {};
375
+ importAttrs = void 0;
376
+ }
377
+ },
378
+ {
379
+ lowerCaseTags: false,
380
+ xmlMode: true
381
+ }
382
+ );
383
+ parser.write(
384
+ ms.original
385
+ );
386
+ parser.end();
387
+ return deps;
388
+ }
389
+ async function getDep(filepath, set2) {
390
+ if (await fs.exists(filepath)) {
391
+ const dirname = path2.dirname(filepath);
392
+ set2.add(filepath);
393
+ const wxml = await fs.readFile(filepath, "utf8");
394
+ const hash = md5(wxml);
395
+ const oldHash = hashMap.get(filepath);
396
+ const isChanged = hash !== oldHash;
397
+ let deps = [];
398
+ const cacheDeps = depsMap.get(filepath);
399
+ if (isChanged || !cacheDeps) {
400
+ hashMap.set(filepath, hash);
401
+ deps = await processWxml(wxml);
402
+ depsMap.set(filepath, deps);
403
+ } else {
404
+ deps = cacheDeps;
405
+ }
406
+ for (const { value } of deps) {
407
+ const p = path2.resolve(dirname, value);
408
+ if (!set2.has(p)) {
409
+ await getDep(p, set2);
410
+ }
342
411
  }
343
- });
344
- if (typeof config === "string" && config) {
345
- const configPath = path2.isAbsolute(config) ? config : path2.resolve(cwd, config);
346
- const result = await searcher.load(configPath);
347
- return result?.config;
348
- } else {
349
- const result = await searcher.search(cwd);
350
- return result?.config;
351
412
  }
352
413
  }
414
+ async function getDepFiles(file) {
415
+ const set2 = /* @__PURE__ */ new Set();
416
+ await getDep(file, set2);
417
+ return set2;
418
+ }
353
419
 
354
420
  // src/postcss.ts
355
421
  var creator = (options) => {
@@ -357,7 +423,7 @@ var creator = (options) => {
357
423
  filter: () => true,
358
424
  config: void 0,
359
425
  directiveParams: ["utilities", "components"],
360
- cwd: process2.cwd(),
426
+ cwd: process.cwd(),
361
427
  extensions: ["wxml", "js", "ts"]
362
428
  });
363
429
  const extensionsGlob = `{${extensions.join(",")}}`;
@@ -388,9 +454,16 @@ var creator = (options) => {
388
454
  };
389
455
  }
390
456
  if (tailwindcssConfig && root.source?.input && root.source.input.file) {
457
+ const basename = removeFileExtension(root.source.input.file);
391
458
  (0, import_set_value.default)(tailwindcssConfig, "content", [
392
- `${removeFileExtension(root.source.input.file)}.${extensionsGlob}`
459
+ `${basename}.${extensionsGlob}`
393
460
  ]);
461
+ const deps = await getDepFiles(`${basename}.wxml`);
462
+ for (const dep of deps) {
463
+ if (Array.isArray(tailwindcssConfig.content)) {
464
+ tailwindcssConfig.content.push(`${removeFileExtension(dep)}.${extensionsGlob}`);
465
+ }
466
+ }
394
467
  }
395
468
  const { root: newRoot } = await postcss([
396
469
  tailwindcss(tailwindcssConfig)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tailwindcss-injector",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.1.1",
5
5
  "description": "tsup(esbuild) build package template",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -31,10 +31,12 @@
31
31
  "dist"
32
32
  ],
33
33
  "dependencies": {
34
- "jiti": "^2.4.1",
35
- "lilconfig": "^3.1.3",
34
+ "fs-extra": "^11.2.0",
35
+ "htmlparser2": "^9.1.0",
36
+ "magic-string": "^0.30.14",
36
37
  "postcss": "^8.4.49",
37
- "tailwindcss": "^3.4.15"
38
+ "tailwindcss": "^3.4.16",
39
+ "tailwindcss-config": "0.0.0"
38
40
  },
39
41
  "scripts": {
40
42
  "dev": "tsup --watch --sourcemap",