tailwindcss-injector 0.1.3 → 0.1.5

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
@@ -323,15 +323,40 @@ function md5(input) {
323
323
  hash.update(input);
324
324
  return hash.digest("hex");
325
325
  }
326
+ function isRegexp(value) {
327
+ return Object.prototype.toString.call(value) === "[object RegExp]";
328
+ }
329
+ function regExpTest(arr = [], str) {
330
+ if (Array.isArray(arr)) {
331
+ for (const item of arr) {
332
+ if (typeof item === "string") {
333
+ if (str.includes(item)) {
334
+ return true;
335
+ }
336
+ } else if (isRegexp(item)) {
337
+ item.lastIndex = 0;
338
+ if (item.test(str)) {
339
+ return true;
340
+ }
341
+ }
342
+ }
343
+ return false;
344
+ }
345
+ throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
346
+ }
326
347
 
327
348
  // src/config.ts
328
349
  function getDefaultConfig() {
329
350
  return {
330
351
  filter: () => true,
331
352
  config: void 0,
353
+ // 倒叙插入
332
354
  directiveParams: ["utilities", "components"],
333
355
  cwd: _process2.default.cwd(),
334
- extensions: ["wxml", "js", "ts"]
356
+ extensions: ["wxml", "js", "ts"],
357
+ // @import and @use (scss)
358
+ insertAfterAtRulesNames: ["import", "use"],
359
+ insertAfterComments: ["@import"]
335
360
  };
336
361
  }
337
362
  function getConfig(options) {
@@ -440,7 +465,7 @@ async function getDepFiles(file) {
440
465
 
441
466
  // src/postcss.ts
442
467
  var creator = (options) => {
443
- const { config, filter, directiveParams, cwd, extensions } = getConfig(options);
468
+ const { config, filter, directiveParams, cwd, extensions, insertAfterAtRulesNames, insertAfterComments } = getConfig(options);
444
469
  const extensionsGlob = `{${extensions.join(",")}}`;
445
470
  return {
446
471
  postcssPlugin,
@@ -449,10 +474,18 @@ var creator = (options) => {
449
474
  postcssPlugin: `${postcssPlugin}:post`,
450
475
  async Once(root, helpers) {
451
476
  if (filter(_optionalChain([root, 'access', _ => _.source, 'optionalAccess', _2 => _2.input]))) {
477
+ const atruleLastIndex = root.nodes.findLastIndex((x) => x.type === "atrule" && insertAfterAtRulesNames.includes(x.name));
478
+ const commentLastIndex = root.nodes.findLastIndex((x) => x.type === "comment" && regExpTest(insertAfterComments, x.text));
479
+ const lastIndex = Math.max(atruleLastIndex, commentLastIndex);
452
480
  for (const params of directiveParams) {
453
481
  const node = root.nodes.find((x) => x.type === "atrule" && x.params === params);
454
482
  if (!node) {
455
- root.prepend(helpers.atRule({ name: "tailwind", params }));
483
+ const atRule = helpers.atRule({ name: "tailwind", params });
484
+ if (lastIndex < 0) {
485
+ root.prepend(atRule);
486
+ } else {
487
+ root.insertAfter(lastIndex, atRule);
488
+ }
456
489
  }
457
490
  }
458
491
  const cfg = typeof config === "function" ? config(_optionalChain([root, 'access', _3 => _3.source, 'optionalAccess', _4 => _4.input])) : config;
@@ -482,7 +515,7 @@ var creator = (options) => {
482
515
  _tailwindcss2.default.call(void 0, tailwindcssConfig)
483
516
  ]).process(root, {
484
517
  from: root.source.input.file
485
- });
518
+ }).async();
486
519
  }
487
520
  }
488
521
  }
@@ -8,6 +8,8 @@ interface Options {
8
8
  config: InlineTailwindcssOptions | ((input?: postcss.Input) => InlineTailwindcssOptions);
9
9
  directiveParams: ('base' | 'components' | 'utilities' | 'variants')[];
10
10
  extensions: string[];
11
+ insertAfterAtRulesNames: string[];
12
+ insertAfterComments: (string | RegExp)[];
11
13
  }
12
14
 
13
15
  declare const creator: PluginCreator<Partial<Options>>;
package/dist/postcss.d.ts CHANGED
@@ -8,6 +8,8 @@ interface Options {
8
8
  config: InlineTailwindcssOptions | ((input?: postcss.Input) => InlineTailwindcssOptions);
9
9
  directiveParams: ('base' | 'components' | 'utilities' | 'variants')[];
10
10
  extensions: string[];
11
+ insertAfterAtRulesNames: string[];
12
+ insertAfterComments: (string | RegExp)[];
11
13
  }
12
14
 
13
15
  declare const creator: PluginCreator<Partial<Options>>;
package/dist/postcss.js CHANGED
@@ -323,15 +323,40 @@ function md5(input) {
323
323
  hash.update(input);
324
324
  return hash.digest("hex");
325
325
  }
326
+ function isRegexp(value) {
327
+ return Object.prototype.toString.call(value) === "[object RegExp]";
328
+ }
329
+ function regExpTest(arr = [], str) {
330
+ if (Array.isArray(arr)) {
331
+ for (const item of arr) {
332
+ if (typeof item === "string") {
333
+ if (str.includes(item)) {
334
+ return true;
335
+ }
336
+ } else if (isRegexp(item)) {
337
+ item.lastIndex = 0;
338
+ if (item.test(str)) {
339
+ return true;
340
+ }
341
+ }
342
+ }
343
+ return false;
344
+ }
345
+ throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
346
+ }
326
347
 
327
348
  // src/config.ts
328
349
  function getDefaultConfig() {
329
350
  return {
330
351
  filter: () => true,
331
352
  config: void 0,
353
+ // 倒叙插入
332
354
  directiveParams: ["utilities", "components"],
333
355
  cwd: process.cwd(),
334
- extensions: ["wxml", "js", "ts"]
356
+ extensions: ["wxml", "js", "ts"],
357
+ // @import and @use (scss)
358
+ insertAfterAtRulesNames: ["import", "use"],
359
+ insertAfterComments: ["@import"]
335
360
  };
336
361
  }
337
362
  function getConfig(options) {
@@ -440,7 +465,7 @@ async function getDepFiles(file) {
440
465
 
441
466
  // src/postcss.ts
442
467
  var creator = (options) => {
443
- const { config, filter, directiveParams, cwd, extensions } = getConfig(options);
468
+ const { config, filter, directiveParams, cwd, extensions, insertAfterAtRulesNames, insertAfterComments } = getConfig(options);
444
469
  const extensionsGlob = `{${extensions.join(",")}}`;
445
470
  return {
446
471
  postcssPlugin,
@@ -449,10 +474,18 @@ var creator = (options) => {
449
474
  postcssPlugin: `${postcssPlugin}:post`,
450
475
  async Once(root, helpers) {
451
476
  if (filter(root.source?.input)) {
477
+ const atruleLastIndex = root.nodes.findLastIndex((x) => x.type === "atrule" && insertAfterAtRulesNames.includes(x.name));
478
+ const commentLastIndex = root.nodes.findLastIndex((x) => x.type === "comment" && regExpTest(insertAfterComments, x.text));
479
+ const lastIndex = Math.max(atruleLastIndex, commentLastIndex);
452
480
  for (const params of directiveParams) {
453
481
  const node = root.nodes.find((x) => x.type === "atrule" && x.params === params);
454
482
  if (!node) {
455
- root.prepend(helpers.atRule({ name: "tailwind", params }));
483
+ const atRule = helpers.atRule({ name: "tailwind", params });
484
+ if (lastIndex < 0) {
485
+ root.prepend(atRule);
486
+ } else {
487
+ root.insertAfter(lastIndex, atRule);
488
+ }
456
489
  }
457
490
  }
458
491
  const cfg = typeof config === "function" ? config(root.source?.input) : config;
@@ -482,7 +515,7 @@ var creator = (options) => {
482
515
  tailwindcss(tailwindcssConfig)
483
516
  ]).process(root, {
484
517
  from: root.source.input.file
485
- });
518
+ }).async();
486
519
  }
487
520
  }
488
521
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tailwindcss-injector",
3
3
  "type": "module",
4
- "version": "0.1.3",
4
+ "version": "0.1.5",
5
5
  "description": "tsup(esbuild) build package template",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -33,7 +33,7 @@
33
33
  "dependencies": {
34
34
  "fs-extra": "^11.2.0",
35
35
  "htmlparser2": "^9.1.0",
36
- "magic-string": "^0.30.14",
36
+ "magic-string": "^0.30.15",
37
37
  "postcss": "^8.4.49",
38
38
  "tailwindcss": "^3.4.16",
39
39
  "tailwindcss-config": "0.0.0"