tailwindcss-injector 1.0.12 → 1.0.14

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/README.en.md ADDED
@@ -0,0 +1,9 @@
1
+ # tailwindcss-injector
2
+
3
+ > English | [简体中文](./README.md)
4
+
5
+ This package injects Tailwind directives during PostCSS processing and tracks WXML import/include dependencies so mini program templates can participate in Tailwind content scanning.
6
+
7
+ ## Website
8
+
9
+ For setup guides, configuration references, and framework examples, see the [official weapp-tailwindcss documentation](https://tw.icebreaker.top).
package/README.md CHANGED
@@ -1,112 +1,9 @@
1
- # TailwindCSS Injector
1
+ # tailwindcss-injector
2
2
 
3
- **English | [中文](./README-cn.md)**
3
+ > 简体中文 | [English](./README.en.md)
4
4
 
5
- - [TailwindCSS Injector](#tailwindcss-injector)
6
- - [Overview](#overview)
7
- - [Features](#features)
8
- - [Installation](#installation)
9
- - [Usage](#usage)
10
- - [Basic Example](#basic-example)
11
- - [Options](#options)
12
- - [Example Configuration](#example-configuration)
13
- - [How It Works](#how-it-works)
14
- - [License](#license)
5
+ 这个包用于在 PostCSS 流程中动态注入 Tailwind 指令,并追踪 WXML import/include 依赖,让小程序模板能进入 Tailwind content 扫描范围。
15
6
 
16
- ---
7
+ ## 官网
17
8
 
18
- ## Overview
19
-
20
- `tailwindcss-injector` is a library designed to dynamically inject TailwindCSS directives and configurations into your CSS workflow. It includes a PostCSS plugin that supports multiple file extensions, dynamic configuration, and flexible filtering, enabling seamless integration into modern build pipelines.
21
-
22
- ---
23
-
24
- ## Features
25
-
26
- - **Dynamic Directive Injection**: Automatically inserts TailwindCSS directives like `@tailwind base`, `@tailwind components`, and `@tailwind utilities`.
27
- - **Multi-extension Support**: Processes files with customizable extensions (e.g., `.html`, `.js`, `.ts`).
28
- - **Inline or External Configuration**: Accepts inline configuration objects or paths to configuration files.
29
- - **Customizable Filters**: Use a filter function to process specific files.
30
- - **TailwindCSS Integration**: Dynamically adjusts TailwindCSS configurations per file.
31
-
32
- ---
33
-
34
- ## Installation
35
-
36
- Install the package and its dependencies:
37
-
38
- ```bash
39
- npm install tailwindcss-injector tailwindcss postcss --save-dev
40
- ```
41
-
42
- ---
43
-
44
- ## Usage
45
-
46
- To use the PostCSS plugin, import it from `tailwindcss-injector/postcss`:
47
-
48
- ### Basic Example
49
-
50
- ```javascript
51
- const tailwindInjector = require('tailwindcss-injector/postcss')
52
-
53
- module.exports = {
54
- plugins: [
55
- tailwindInjector({
56
- cwd: process.cwd(),
57
- config: './tailwind.config.js',
58
- directiveParams: ['base', 'components', 'utilities'],
59
- extensions: ['html', 'js', 'ts'],
60
- filter: input => !!input?.file && input.file.endsWith('.css'),
61
- }),
62
- ],
63
- }
64
- ```
65
-
66
- ---
67
-
68
- ## Options
69
-
70
- The plugin accepts the following options:
71
-
72
- | Option | Type | Description |
73
- | ----------------- | ------------------------------------------------------------------ | ------------------------------------------------------------------- |
74
- | `cwd` | `string` | Current working directory. |
75
- | `config` | `string \| Partial<Config> \| (input) => InlineTailwindcssOptions` | TailwindCSS configuration file path or inline configuration object. |
76
- | `directiveParams` | `('base' \| 'components' \| 'utilities' \| 'variants')[]` | Array of TailwindCSS directives to inject. |
77
- | `extensions` | `string[]` | Array of supported file extensions. |
78
- | `filter` | `(input?: postcss.Input) => boolean` | Custom function to filter files to process. |
79
-
80
- ### Example Configuration
81
-
82
- ```javascript
83
- {
84
- cwd: process.cwd(), // Set current working directory
85
- config: './tailwind.config.js', // Use an external TailwindCSS configuration file
86
- directiveParams: ['base', 'components', 'utilities'], // Inject directives dynamically
87
- extensions: ['html', 'js', 'ts'], // Process specific file extensions
88
- filter: (input) => !!input?.file && input.file.endsWith('.css'), // Filter for CSS files only
89
- }
90
- ```
91
-
92
- ---
93
-
94
- ## How It Works
95
-
96
- 1. **Directive Injection**:
97
- The plugin ensures that the specified TailwindCSS directives (`@tailwind base`, `@tailwind components`, etc.) are injected into the CSS files.
98
-
99
- 2. **Dynamic Configuration**:
100
- TailwindCSS configurations are dynamically adjusted based on the processed files, including custom extensions.
101
-
102
- 3. **File Filtering**:
103
- Files to process can be filtered using the `filter` option.
104
-
105
- 4. **PostCSS Processing**:
106
- The plugin integrates with PostCSS to apply TailwindCSS transformations seamlessly.
107
-
108
- ---
109
-
110
- ## License
111
-
112
- This project is licensed under the **MIT License**. See the `LICENSE` file for details.
9
+ 更多接入方式、配置说明和框架示例见 [weapp-tailwindcss 官方文档](https://tw.icebreaker.top)。
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { };
1
+ export {}
package/dist/postcss.cjs CHANGED
@@ -20,11 +20,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
20
20
  enumerable: true
21
21
  }) : target, mod));
22
22
  //#endregion
23
- let postcss = require("postcss");
24
- postcss = __toESM(postcss, 1);
25
- let tailwindcss = require("tailwindcss");
26
- tailwindcss = __toESM(tailwindcss, 1);
27
- let tailwindcss_config = require("tailwindcss-config");
28
23
  let node_process = require("node:process");
29
24
  node_process = __toESM(node_process, 1);
30
25
  let node_path = require("node:path");
@@ -131,32 +126,8 @@ async function getDepFiles(file) {
131
126
  }
132
127
  //#endregion
133
128
  //#region src/postcss.ts
134
- const configCache = /* @__PURE__ */ new Map();
135
- async function loadTailwindConfigOnce(cwd, configPath) {
136
- const cacheKey = `${cwd}::${configPath}`;
137
- let cached = configCache.get(cacheKey);
138
- if (!cached) {
139
- cached = (0, tailwindcss_config.loadConfig)({
140
- cwd,
141
- config: configPath
142
- }).then((result) => result?.config);
143
- configCache.set(cacheKey, cached);
144
- }
145
- return cached;
146
- }
147
- function cloneConfig(config) {
148
- if (!config) return config;
149
- return { ...config };
150
- }
151
- async function resolveTailwindConfig(config, cwd, sourceInput) {
152
- if (!config) return;
153
- const resolved = typeof config === "function" ? config(sourceInput) : config;
154
- if (typeof resolved === "string") return cloneConfig(await loadTailwindConfigOnce(cwd, resolved));
155
- return cloneConfig(resolved);
156
- }
157
129
  const creator = (options) => {
158
- const { config, filter, directiveParams, cwd, extensions, insertAfterAtRulesNames, insertAfterComments } = getConfig(options);
159
- const extensionsGlob = `{${extensions.join(",")}}`;
130
+ const { filter, directiveParams, insertAfterAtRulesNames, insertAfterComments } = getConfig(options);
160
131
  return {
161
132
  postcssPlugin,
162
133
  plugins: [{
@@ -187,21 +158,12 @@ const creator = (options) => {
187
158
  directivePresence.add(params);
188
159
  }
189
160
  if (sourceInput?.file) {
190
- const tailwindcssConfig = await resolveTailwindConfig(config, cwd, sourceInput) ?? { content: [] };
191
- const basename = (0, _weapp_tailwindcss_shared_node.removeAllExtensions)(sourceInput.file);
192
- const contentEntries = [`${basename}.${extensionsGlob}`];
193
- const deps = await getDepFiles(`${basename}.wxml`);
194
- for (const dep of deps) contentEntries.push(`${(0, _weapp_tailwindcss_shared_node.removeAllExtensions)(dep)}.${extensionsGlob}`);
195
- const uniqueEntries = [...new Set(contentEntries)];
196
- const { content: existingContent } = tailwindcssConfig;
197
- if (!existingContent || Array.isArray(existingContent)) tailwindcssConfig.content = uniqueEntries;
198
- else if (typeof existingContent === "object" && existingContent !== null) {
199
- const normalizedContent = { ...existingContent };
200
- const currentFiles = normalizedContent.files;
201
- normalizedContent.files = [...new Set([...Array.isArray(currentFiles) ? currentFiles : typeof currentFiles === "string" ? [currentFiles] : [], ...uniqueEntries])];
202
- tailwindcssConfig.content = normalizedContent;
203
- } else tailwindcssConfig.content = uniqueEntries;
204
- await (0, postcss.default)([(0, tailwindcss.default)(tailwindcssConfig)]).process(root, { from: sourceInput.file }).async();
161
+ const deps = await getDepFiles(sourceInput.file.replace(/\.[^.]+$/, ".wxml"));
162
+ for (const dep of deps) helpers.result.messages.push({
163
+ type: "dependency",
164
+ plugin: postcssPlugin,
165
+ file: dep
166
+ });
205
167
  }
206
168
  }
207
169
  }
package/dist/postcss.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import postcss, { PluginCreator } from "postcss";
2
2
  import { Config } from "tailwindcss";
3
-
4
3
  //#region src/types.d.ts
5
4
  type InlineTailwindcssOptions = string | Partial<Config> | undefined;
6
5
  interface Options {
package/dist/postcss.js CHANGED
@@ -1,9 +1,6 @@
1
- import postcss from "postcss";
2
- import tailwindcss from "tailwindcss";
3
- import { loadConfig } from "tailwindcss-config";
4
1
  import process from "node:process";
5
2
  import { defuOverrideArray, regExpTest } from "@weapp-tailwindcss/shared";
6
- import { md5, removeAllExtensions as removeFileExtension } from "@weapp-tailwindcss/shared/node";
3
+ import { md5 } from "@weapp-tailwindcss/shared/node";
7
4
  import path from "node:path";
8
5
  import fs from "fs-extra";
9
6
  import { Parser } from "htmlparser2";
@@ -103,32 +100,8 @@ async function getDepFiles(file) {
103
100
  }
104
101
  //#endregion
105
102
  //#region src/postcss.ts
106
- const configCache = /* @__PURE__ */ new Map();
107
- async function loadTailwindConfigOnce(cwd, configPath) {
108
- const cacheKey = `${cwd}::${configPath}`;
109
- let cached = configCache.get(cacheKey);
110
- if (!cached) {
111
- cached = loadConfig({
112
- cwd,
113
- config: configPath
114
- }).then((result) => result?.config);
115
- configCache.set(cacheKey, cached);
116
- }
117
- return cached;
118
- }
119
- function cloneConfig(config) {
120
- if (!config) return config;
121
- return { ...config };
122
- }
123
- async function resolveTailwindConfig(config, cwd, sourceInput) {
124
- if (!config) return;
125
- const resolved = typeof config === "function" ? config(sourceInput) : config;
126
- if (typeof resolved === "string") return cloneConfig(await loadTailwindConfigOnce(cwd, resolved));
127
- return cloneConfig(resolved);
128
- }
129
103
  const creator = (options) => {
130
- const { config, filter, directiveParams, cwd, extensions, insertAfterAtRulesNames, insertAfterComments } = getConfig(options);
131
- const extensionsGlob = `{${extensions.join(",")}}`;
104
+ const { filter, directiveParams, insertAfterAtRulesNames, insertAfterComments } = getConfig(options);
132
105
  return {
133
106
  postcssPlugin,
134
107
  plugins: [{
@@ -159,21 +132,12 @@ const creator = (options) => {
159
132
  directivePresence.add(params);
160
133
  }
161
134
  if (sourceInput?.file) {
162
- const tailwindcssConfig = await resolveTailwindConfig(config, cwd, sourceInput) ?? { content: [] };
163
- const basename = removeFileExtension(sourceInput.file);
164
- const contentEntries = [`${basename}.${extensionsGlob}`];
165
- const deps = await getDepFiles(`${basename}.wxml`);
166
- for (const dep of deps) contentEntries.push(`${removeFileExtension(dep)}.${extensionsGlob}`);
167
- const uniqueEntries = [...new Set(contentEntries)];
168
- const { content: existingContent } = tailwindcssConfig;
169
- if (!existingContent || Array.isArray(existingContent)) tailwindcssConfig.content = uniqueEntries;
170
- else if (typeof existingContent === "object" && existingContent !== null) {
171
- const normalizedContent = { ...existingContent };
172
- const currentFiles = normalizedContent.files;
173
- normalizedContent.files = [...new Set([...Array.isArray(currentFiles) ? currentFiles : typeof currentFiles === "string" ? [currentFiles] : [], ...uniqueEntries])];
174
- tailwindcssConfig.content = normalizedContent;
175
- } else tailwindcssConfig.content = uniqueEntries;
176
- await postcss([tailwindcss(tailwindcssConfig)]).process(root, { from: sourceInput.file }).async();
135
+ const deps = await getDepFiles(sourceInput.file.replace(/\.[^.]+$/, ".wxml"));
136
+ for (const dep of deps) helpers.result.messages.push({
137
+ type: "dependency",
138
+ plugin: postcssPlugin,
139
+ file: dep
140
+ });
177
141
  }
178
142
  }
179
143
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tailwindcss-injector",
3
3
  "type": "module",
4
- "version": "1.0.12",
4
+ "version": "1.0.14",
5
5
  "description": "tsdown build package template",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -34,13 +34,13 @@
34
34
  "node": "^20.19.0 || >=22.12.0"
35
35
  },
36
36
  "dependencies": {
37
- "fs-extra": "^11.3.5",
37
+ "fs-extra": "^11.3.6",
38
38
  "htmlparser2": "^12.0.0",
39
- "magic-string": "^0.30.21",
40
- "postcss": "^8.5.15",
41
- "tailwindcss": "^3.4.19",
42
- "tailwindcss-config": "2.0.0",
43
- "@weapp-tailwindcss/shared": "2.0.0"
39
+ "magic-string": "^1.0.0",
40
+ "postcss": "^8.5.21",
41
+ "tailwindcss": "^4.3.3",
42
+ "@weapp-tailwindcss/shared": "2.0.1",
43
+ "tailwindcss-config": "2.0.2"
44
44
  },
45
45
  "scripts": {
46
46
  "dev": "tsdown --watch --sourcemap",