tailwindcss-injector 0.0.2 → 0.1.0

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,11 @@ 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 _path = require('path'); var _path2 = _interopRequireDefault(_path);
236
227
 
237
228
  // ../../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs
238
229
  init_cjs_shims();
@@ -305,9 +296,14 @@ var defuArrayFn = createDefu((object, key, currentValue) => {
305
296
 
306
297
  // src/utils.ts
307
298
  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));
299
+ if (!filePath) {
300
+ return "";
301
+ }
302
+ let baseName = _path2.default.basename(filePath);
303
+ let ext = _path2.default.extname(baseName);
304
+ while (ext) {
305
+ baseName = baseName.slice(0, -ext.length);
306
+ ext = _path2.default.extname(baseName);
311
307
  }
312
308
  const dir = _path2.default.dirname(filePath);
313
309
  return _path2.default.join(dir, baseName);
@@ -319,39 +315,87 @@ var defuOverrideArray = createDefu((obj, key, value) => {
319
315
  }
320
316
  });
321
317
 
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
318
+ // src/wxml.ts
319
+ init_cjs_shims();
320
+
321
+ var _fsextra = require('fs-extra'); var _fsextra2 = _interopRequireDefault(_fsextra);
322
+ var _htmlparser2 = require('htmlparser2');
323
+ var _magicstring = require('magic-string'); var _magicstring2 = _interopRequireDefault(_magicstring);
324
+ var srcImportTagsMap = {
325
+ // audio: ['src', 'poster'],
326
+ // video: ['src', 'poster'],
327
+ // image: ['src'],
328
+ // https://developers.weixin.qq.com/miniprogram/dev/reference/wxs/01wxs-module.html
329
+ // wxs: ['src'],
330
+ // https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/import.html
331
+ import: ["src"],
332
+ include: ["src"]
333
+ };
334
+ function processWxml(wxml) {
335
+ const ms = new (0, _magicstring2.default)(wxml.toString());
336
+ const deps = [];
337
+ let currentTagName = "";
338
+ let importAttrs;
339
+ let attrs = {};
340
+ const parser = new (0, _htmlparser2.Parser)(
341
+ {
342
+ onopentagname(name) {
343
+ currentTagName = name;
344
+ importAttrs = srcImportTagsMap[currentTagName];
345
+ },
346
+ onattribute(name, value, quote) {
347
+ attrs[name] = value;
348
+ if (importAttrs) {
349
+ for (const attrName of importAttrs) {
350
+ if (attrName === name) {
351
+ deps.push({
352
+ name,
353
+ value,
354
+ quote,
355
+ tagName: currentTagName,
356
+ start: parser.startIndex,
357
+ end: parser.endIndex,
358
+ attrs
359
+ });
360
+ }
361
+ }
362
+ }
363
+ },
364
+ onclosetag() {
365
+ currentTagName = "";
366
+ attrs = {};
367
+ importAttrs = void 0;
368
+ }
369
+ },
370
+ {
371
+ lowerCaseTags: false,
372
+ xmlMode: true
373
+ }
374
+ );
375
+ parser.write(
376
+ ms.original
377
+ );
378
+ parser.end();
379
+ return deps;
380
+ }
381
+ async function getDepFiles(file) {
382
+ const set2 = /* @__PURE__ */ new Set();
383
+ async function getDep(filepath) {
384
+ if (await _fsextra2.default.exists(filepath)) {
385
+ const dirname = _path2.default.dirname(filepath);
386
+ set2.add(filepath);
387
+ const wxml = await _fsextra2.default.readFile(filepath, "utf8");
388
+ const deps = await processWxml(wxml);
389
+ for (const { value } of deps) {
390
+ const p = _path2.default.resolve(dirname, value);
391
+ if (!set2.has(p)) {
392
+ await getDep(p);
393
+ }
394
+ }
345
395
  }
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
396
  }
397
+ await getDep(file);
398
+ return set2;
355
399
  }
356
400
 
357
401
  // src/postcss.ts
@@ -370,7 +414,7 @@ var creator = (options) => {
370
414
  {
371
415
  postcssPlugin: "post",
372
416
  async Once(root, helpers) {
373
- if (filter(_optionalChain([root, 'access', _3 => _3.source, 'optionalAccess', _4 => _4.input]))) {
417
+ if (filter(_optionalChain([root, 'access', _ => _.source, 'optionalAccess', _2 => _2.input]))) {
374
418
  let _root = root;
375
419
  for (const params of directiveParams) {
376
420
  const node = root.nodes.find((x) => x.type === "atrule" && x.params === params);
@@ -378,10 +422,10 @@ var creator = (options) => {
378
422
  _root = _root.prepend(helpers.atRule({ name: "tailwind", params }));
379
423
  }
380
424
  }
381
- const cfg = typeof config === "function" ? config(_optionalChain([root, 'access', _5 => _5.source, 'optionalAccess', _6 => _6.input])) : config;
425
+ const cfg = typeof config === "function" ? config(_optionalChain([root, 'access', _3 => _3.source, 'optionalAccess', _4 => _4.input])) : config;
382
426
  let tailwindcssConfig;
383
427
  if (typeof cfg === "string") {
384
- tailwindcssConfig = await loadConfig({
428
+ tailwindcssConfig = await _tailwindcssconfig.loadConfig.call(void 0, {
385
429
  cwd,
386
430
  config: cfg
387
431
  });
@@ -390,10 +434,17 @@ var creator = (options) => {
390
434
  content: []
391
435
  }));
392
436
  }
393
- if (tailwindcssConfig && _optionalChain([root, 'access', _7 => _7.source, 'optionalAccess', _8 => _8.input]) && root.source.input.file) {
437
+ if (tailwindcssConfig && _optionalChain([root, 'access', _5 => _5.source, 'optionalAccess', _6 => _6.input]) && root.source.input.file) {
438
+ const basename = removeFileExtension(root.source.input.file);
394
439
  (0, import_set_value.default)(tailwindcssConfig, "content", [
395
- `${removeFileExtension(root.source.input.file)}.${extensionsGlob}`
440
+ `${basename}.${extensionsGlob}`
396
441
  ]);
442
+ const deps = await getDepFiles(`${basename}.wxml`);
443
+ for (const dep of deps) {
444
+ if (Array.isArray(tailwindcssConfig.content)) {
445
+ tailwindcssConfig.content.push(`${removeFileExtension(dep)}.${extensionsGlob}`);
446
+ }
447
+ }
397
448
  }
398
449
  const { root: newRoot } = await _postcss2.default.call(void 0, [
399
450
  _tailwindcss2.default.call(void 0, tailwindcssConfig)
package/dist/postcss.js CHANGED
@@ -216,16 +216,10 @@ 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();
@@ -302,9 +296,14 @@ var defuArrayFn = createDefu((object, key, currentValue) => {
302
296
 
303
297
  // src/utils.ts
304
298
  function removeFileExtension(filePath) {
305
- let baseName = path.basename(filePath, path.extname(filePath));
306
- while (baseName.includes(".")) {
307
- baseName = path.basename(baseName, path.extname(baseName));
299
+ if (!filePath) {
300
+ return "";
301
+ }
302
+ let baseName = path.basename(filePath);
303
+ let ext = path.extname(baseName);
304
+ while (ext) {
305
+ baseName = baseName.slice(0, -ext.length);
306
+ ext = path.extname(baseName);
308
307
  }
309
308
  const dir = path.dirname(filePath);
310
309
  return path.join(dir, baseName);
@@ -316,39 +315,87 @@ var defuOverrideArray = createDefu((obj, key, value) => {
316
315
  }
317
316
  });
318
317
 
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
318
+ // src/wxml.ts
319
+ init_esm_shims();
320
+ import path2 from "node:path";
321
+ import fs from "fs-extra";
322
+ import { Parser } from "htmlparser2";
323
+ import MagicString from "magic-string";
324
+ var srcImportTagsMap = {
325
+ // audio: ['src', 'poster'],
326
+ // video: ['src', 'poster'],
327
+ // image: ['src'],
328
+ // https://developers.weixin.qq.com/miniprogram/dev/reference/wxs/01wxs-module.html
329
+ // wxs: ['src'],
330
+ // https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/import.html
331
+ import: ["src"],
332
+ include: ["src"]
333
+ };
334
+ function processWxml(wxml) {
335
+ const ms = new MagicString(wxml.toString());
336
+ const deps = [];
337
+ let currentTagName = "";
338
+ let importAttrs;
339
+ let attrs = {};
340
+ const parser = new Parser(
341
+ {
342
+ onopentagname(name) {
343
+ currentTagName = name;
344
+ importAttrs = srcImportTagsMap[currentTagName];
345
+ },
346
+ onattribute(name, value, quote) {
347
+ attrs[name] = value;
348
+ if (importAttrs) {
349
+ for (const attrName of importAttrs) {
350
+ if (attrName === name) {
351
+ deps.push({
352
+ name,
353
+ value,
354
+ quote,
355
+ tagName: currentTagName,
356
+ start: parser.startIndex,
357
+ end: parser.endIndex,
358
+ attrs
359
+ });
360
+ }
361
+ }
362
+ }
363
+ },
364
+ onclosetag() {
365
+ currentTagName = "";
366
+ attrs = {};
367
+ importAttrs = void 0;
368
+ }
369
+ },
370
+ {
371
+ lowerCaseTags: false,
372
+ xmlMode: true
373
+ }
374
+ );
375
+ parser.write(
376
+ ms.original
377
+ );
378
+ parser.end();
379
+ return deps;
380
+ }
381
+ async function getDepFiles(file) {
382
+ const set2 = /* @__PURE__ */ new Set();
383
+ async function getDep(filepath) {
384
+ if (await fs.exists(filepath)) {
385
+ const dirname = path2.dirname(filepath);
386
+ set2.add(filepath);
387
+ const wxml = await fs.readFile(filepath, "utf8");
388
+ const deps = await processWxml(wxml);
389
+ for (const { value } of deps) {
390
+ const p = path2.resolve(dirname, value);
391
+ if (!set2.has(p)) {
392
+ await getDep(p);
393
+ }
394
+ }
342
395
  }
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
396
  }
397
+ await getDep(file);
398
+ return set2;
352
399
  }
353
400
 
354
401
  // src/postcss.ts
@@ -357,7 +404,7 @@ var creator = (options) => {
357
404
  filter: () => true,
358
405
  config: void 0,
359
406
  directiveParams: ["utilities", "components"],
360
- cwd: process2.cwd(),
407
+ cwd: process.cwd(),
361
408
  extensions: ["wxml", "js", "ts"]
362
409
  });
363
410
  const extensionsGlob = `{${extensions.join(",")}}`;
@@ -388,9 +435,16 @@ var creator = (options) => {
388
435
  };
389
436
  }
390
437
  if (tailwindcssConfig && root.source?.input && root.source.input.file) {
438
+ const basename = removeFileExtension(root.source.input.file);
391
439
  (0, import_set_value.default)(tailwindcssConfig, "content", [
392
- `${removeFileExtension(root.source.input.file)}.${extensionsGlob}`
440
+ `${basename}.${extensionsGlob}`
393
441
  ]);
442
+ const deps = await getDepFiles(`${basename}.wxml`);
443
+ for (const dep of deps) {
444
+ if (Array.isArray(tailwindcssConfig.content)) {
445
+ tailwindcssConfig.content.push(`${removeFileExtension(dep)}.${extensionsGlob}`);
446
+ }
447
+ }
394
448
  }
395
449
  const { root: newRoot } = await postcss([
396
450
  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.0",
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",