svelte-ag 1.0.65 → 1.0.66
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite-plugin-component-source-collector.d.ts","sourceRoot":"","sources":["../../src/lib/vite/vite-plugin-component-source-collector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAkB,MAAM,MAAM,CAAC;AAMnD,UAAU,OAAO;IACf;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B;;OAEG;IACH,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB;AA2CD,wBAA8B,wBAAwB,CAAC,IAAI,GAAE,OAA8B,GAAG,OAAO,CAAC,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"vite-plugin-component-source-collector.d.ts","sourceRoot":"","sources":["../../src/lib/vite/vite-plugin-component-source-collector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAkB,MAAM,MAAM,CAAC;AAMnD,UAAU,OAAO;IACf;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B;;OAEG;IACH,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB;AA2CD,wBAA8B,wBAAwB,CAAC,IAAI,GAAE,OAA8B,GAAG,OAAO,CAAC,MAAM,CAAC,CAuN5G"}
|
|
@@ -42,7 +42,7 @@ async function readPackageNameAt(directory) {
|
|
|
42
42
|
export default async function componentSourceCollector(opts = { safePackages: [] }) {
|
|
43
43
|
// constants
|
|
44
44
|
const outFileName = opts.outputFile ?? 'component-sources.css';
|
|
45
|
-
const
|
|
45
|
+
const classAttributeRegex = /\bclass\s*=/;
|
|
46
46
|
const importRegex = /@import\s+['"]([^'"]+)['"]/g;
|
|
47
47
|
// state
|
|
48
48
|
let outputFilePath;
|
|
@@ -50,8 +50,13 @@ export default async function componentSourceCollector(opts = { safePackages: []
|
|
|
50
50
|
let config;
|
|
51
51
|
let initialTransformDone = false;
|
|
52
52
|
let initialTransformTimer = null;
|
|
53
|
-
function shouldAdd(code) {
|
|
54
|
-
|
|
53
|
+
function shouldAdd(code, id) {
|
|
54
|
+
// Svelte's `class:` directive toggles local classes and should not be treated as
|
|
55
|
+
// a Tailwind source signal. Including those files can pull in component-local
|
|
56
|
+
// style modules that Tailwind should never parse directly.
|
|
57
|
+
if (id.includes('?svelte&type=style'))
|
|
58
|
+
return false;
|
|
59
|
+
return classAttributeRegex.test(code);
|
|
55
60
|
}
|
|
56
61
|
async function normalizeCollectedSourceFilePath(file) {
|
|
57
62
|
const cleanedFileName = file.replace(/[?#].*$/, '');
|
|
@@ -77,19 +82,19 @@ export default async function componentSourceCollector(opts = { safePackages: []
|
|
|
77
82
|
}
|
|
78
83
|
}
|
|
79
84
|
async function addPath(file) {
|
|
80
|
-
if (outputFilePath
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (normalizedFilePath === outputFilePath || relativeFilePath === outFileName)
|
|
88
|
-
return;
|
|
89
|
-
// Dont add itself
|
|
90
|
-
componentFiles.add(ensureDotRelative(relativeFilePath));
|
|
91
|
-
}
|
|
85
|
+
if (!outputFilePath || file === '')
|
|
86
|
+
return;
|
|
87
|
+
const normalizedFilePath = await normalizeCollectedSourceFilePath(file);
|
|
88
|
+
if (/\.svelte-kit/.test(normalizedFilePath) ||
|
|
89
|
+
(/(?:\.pnpm|\.vite)/.test(normalizedFilePath) &&
|
|
90
|
+
!opts.safePackages?.some((packageName) => normalizedFilePath.includes(`node_modules/${packageName}`)))) {
|
|
91
|
+
return;
|
|
92
92
|
}
|
|
93
|
+
const relativeFilePath = toPosixPath(relative(dirname(outputFilePath), normalizedFilePath));
|
|
94
|
+
if (normalizedFilePath === outputFilePath || relativeFilePath === outFileName)
|
|
95
|
+
return;
|
|
96
|
+
// Dont add itself
|
|
97
|
+
componentFiles.add(ensureDotRelative(relativeFilePath));
|
|
93
98
|
}
|
|
94
99
|
function scheduleInitialWrite() {
|
|
95
100
|
if (initialTransformTimer)
|
|
@@ -99,7 +104,7 @@ export default async function componentSourceCollector(opts = { safePackages: []
|
|
|
99
104
|
writeOutFile();
|
|
100
105
|
initialTransformDone = true;
|
|
101
106
|
}
|
|
102
|
-
}, 1000);
|
|
107
|
+
}, 1000);
|
|
103
108
|
}
|
|
104
109
|
const writeOutFile = async () => {
|
|
105
110
|
const lines = Array.from(componentFiles)
|
|
@@ -108,7 +113,7 @@ export default async function componentSourceCollector(opts = { safePackages: []
|
|
|
108
113
|
if (outputFilePath) {
|
|
109
114
|
const didWrite = await writeIfDifferent(outputFilePath, lines.join('\n'));
|
|
110
115
|
if (didWrite)
|
|
111
|
-
console.log('
|
|
116
|
+
console.log('tailwind-sources:wrote', lines.length);
|
|
112
117
|
}
|
|
113
118
|
};
|
|
114
119
|
// ---- plugin ---- //
|
|
@@ -137,13 +142,26 @@ export default async function componentSourceCollector(opts = { safePackages: []
|
|
|
137
142
|
componentFiles.clear();
|
|
138
143
|
firstRound = false;
|
|
139
144
|
}
|
|
140
|
-
else if (config.command === 'serve') {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
+
else if (config.command === 'serve' && (await exists(outputFilePath))) {
|
|
146
|
+
const fileLines = (await readFile(outputFilePath, 'utf8')).split('\n');
|
|
147
|
+
for (const fileLine of fileLines) {
|
|
148
|
+
const sourcePath = fileLine.replace(/@source\s+'(.*?)';/, '$1');
|
|
149
|
+
if (sourcePath === fileLine)
|
|
150
|
+
continue;
|
|
151
|
+
const resolvedSourcePath = resolve(dirname(outputFilePath), sourcePath);
|
|
152
|
+
if (resolvedSourcePath.endsWith('.css')) {
|
|
153
|
+
await addPath(sourcePath);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
const code = await readFile(resolvedSourcePath, 'utf8');
|
|
158
|
+
if (shouldAdd(code, resolvedSourcePath)) {
|
|
159
|
+
await addPath(sourcePath);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
// Ignore stale source entries that no longer resolve on disk.
|
|
145
164
|
}
|
|
146
|
-
// console.log('config resolved', componentFiles);
|
|
147
165
|
}
|
|
148
166
|
}
|
|
149
167
|
},
|
|
@@ -193,7 +211,7 @@ export default async function componentSourceCollector(opts = { safePackages: []
|
|
|
193
211
|
}
|
|
194
212
|
}
|
|
195
213
|
// Adds all other files with the classRegex
|
|
196
|
-
if (shouldAdd(code)) {
|
|
214
|
+
if (shouldAdd(code, id)) {
|
|
197
215
|
await addPath(id);
|
|
198
216
|
}
|
|
199
217
|
if (!initialTransformDone) {
|
package/package.json
CHANGED
|
@@ -65,7 +65,7 @@ async function readPackageNameAt(directory: string): Promise<string | null> {
|
|
|
65
65
|
export default async function componentSourceCollector(opts: Options = { safePackages: [] }): Promise<Plugin> {
|
|
66
66
|
// constants
|
|
67
67
|
const outFileName = opts.outputFile ?? 'component-sources.css';
|
|
68
|
-
const
|
|
68
|
+
const classAttributeRegex = /\bclass\s*=/;
|
|
69
69
|
const importRegex = /@import\s+['"]([^'"]+)['"]/g;
|
|
70
70
|
|
|
71
71
|
// state
|
|
@@ -75,8 +75,12 @@ export default async function componentSourceCollector(opts: Options = { safePac
|
|
|
75
75
|
let initialTransformDone = false;
|
|
76
76
|
let initialTransformTimer: NodeJS.Timeout | null = null;
|
|
77
77
|
|
|
78
|
-
function shouldAdd(code: string) {
|
|
79
|
-
|
|
78
|
+
function shouldAdd(code: string, id: string) {
|
|
79
|
+
// Svelte's `class:` directive toggles local classes and should not be treated as
|
|
80
|
+
// a Tailwind source signal. Including those files can pull in component-local
|
|
81
|
+
// style modules that Tailwind should never parse directly.
|
|
82
|
+
if (id.includes('?svelte&type=style')) return false;
|
|
83
|
+
return classAttributeRegex.test(code);
|
|
80
84
|
}
|
|
81
85
|
|
|
82
86
|
async function normalizeCollectedSourceFilePath(file: string): Promise<string> {
|
|
@@ -110,22 +114,22 @@ export default async function componentSourceCollector(opts: Options = { safePac
|
|
|
110
114
|
}
|
|
111
115
|
}
|
|
112
116
|
async function addPath(file: string) {
|
|
113
|
-
if (outputFilePath
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const relativeFilePath = toPosixPath(relative(dirname(outputFilePath), normalizedFilePath));
|
|
123
|
-
|
|
124
|
-
if (normalizedFilePath === outputFilePath || relativeFilePath === outFileName) return;
|
|
125
|
-
// Dont add itself
|
|
126
|
-
componentFiles.add(ensureDotRelative(relativeFilePath));
|
|
127
|
-
}
|
|
117
|
+
if (!outputFilePath || file === '') return;
|
|
118
|
+
|
|
119
|
+
const normalizedFilePath = await normalizeCollectedSourceFilePath(file);
|
|
120
|
+
if (
|
|
121
|
+
/\.svelte-kit/.test(normalizedFilePath) ||
|
|
122
|
+
(/(?:\.pnpm|\.vite)/.test(normalizedFilePath) &&
|
|
123
|
+
!opts.safePackages?.some((packageName) => normalizedFilePath.includes(`node_modules/${packageName}`)))
|
|
124
|
+
) {
|
|
125
|
+
return;
|
|
128
126
|
}
|
|
127
|
+
|
|
128
|
+
const relativeFilePath = toPosixPath(relative(dirname(outputFilePath), normalizedFilePath));
|
|
129
|
+
if (normalizedFilePath === outputFilePath || relativeFilePath === outFileName) return;
|
|
130
|
+
|
|
131
|
+
// Dont add itself
|
|
132
|
+
componentFiles.add(ensureDotRelative(relativeFilePath));
|
|
129
133
|
}
|
|
130
134
|
|
|
131
135
|
function scheduleInitialWrite() {
|
|
@@ -135,7 +139,7 @@ export default async function componentSourceCollector(opts: Options = { safePac
|
|
|
135
139
|
writeOutFile();
|
|
136
140
|
initialTransformDone = true;
|
|
137
141
|
}
|
|
138
|
-
}, 1000);
|
|
142
|
+
}, 1000);
|
|
139
143
|
}
|
|
140
144
|
|
|
141
145
|
const writeOutFile = async () => {
|
|
@@ -145,7 +149,7 @@ export default async function componentSourceCollector(opts: Options = { safePac
|
|
|
145
149
|
|
|
146
150
|
if (outputFilePath) {
|
|
147
151
|
const didWrite = await writeIfDifferent(outputFilePath, lines.join('\n'));
|
|
148
|
-
if (didWrite) console.log('
|
|
152
|
+
if (didWrite) console.log('tailwind-sources:wrote', lines.length);
|
|
149
153
|
}
|
|
150
154
|
};
|
|
151
155
|
|
|
@@ -178,13 +182,26 @@ export default async function componentSourceCollector(opts: Options = { safePac
|
|
|
178
182
|
console.log('tailwind-sources: Clearing files list');
|
|
179
183
|
componentFiles.clear();
|
|
180
184
|
firstRound = false;
|
|
181
|
-
} else if (config.command === 'serve') {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
185
|
+
} else if (config.command === 'serve' && (await exists(outputFilePath))) {
|
|
186
|
+
const fileLines = (await readFile(outputFilePath, 'utf8')).split('\n');
|
|
187
|
+
for (const fileLine of fileLines) {
|
|
188
|
+
const sourcePath = fileLine.replace(/@source\s+'(.*?)';/, '$1');
|
|
189
|
+
if (sourcePath === fileLine) continue;
|
|
190
|
+
|
|
191
|
+
const resolvedSourcePath = resolve(dirname(outputFilePath), sourcePath);
|
|
192
|
+
if (resolvedSourcePath.endsWith('.css')) {
|
|
193
|
+
await addPath(sourcePath);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
const code = await readFile(resolvedSourcePath, 'utf8');
|
|
199
|
+
if (shouldAdd(code, resolvedSourcePath)) {
|
|
200
|
+
await addPath(sourcePath);
|
|
201
|
+
}
|
|
202
|
+
} catch {
|
|
203
|
+
// Ignore stale source entries that no longer resolve on disk.
|
|
186
204
|
}
|
|
187
|
-
// console.log('config resolved', componentFiles);
|
|
188
205
|
}
|
|
189
206
|
}
|
|
190
207
|
},
|
|
@@ -237,7 +254,7 @@ export default async function componentSourceCollector(opts: Options = { safePac
|
|
|
237
254
|
}
|
|
238
255
|
|
|
239
256
|
// Adds all other files with the classRegex
|
|
240
|
-
if (shouldAdd(code)) {
|
|
257
|
+
if (shouldAdd(code, id)) {
|
|
241
258
|
await addPath(id);
|
|
242
259
|
}
|
|
243
260
|
|