@flairjs/webpack-loader 0.0.1-beta.6 → 0.0.1-beta.8

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/cjs/index.js CHANGED
@@ -96,8 +96,8 @@ var Store = class {
96
96
  };
97
97
  const store = new Store();
98
98
  const __dirname$1 = (0, node_path.dirname)((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
99
- const getUserTheme = async () => {
100
- if (store.getUserTheme()) return store.getUserTheme();
99
+ const getUserTheme = async (options) => {
100
+ if (store.getUserTheme() && !options?.ignoreCache) return store.getUserTheme();
101
101
  try {
102
102
  let userThemeFilePath = path.default.resolve(process.cwd(), "flair.theme.ts");
103
103
  if (!(0, fs.existsSync)(userThemeFilePath)) userThemeFilePath = path.default.resolve(process.cwd(), "flair.theme.js");
@@ -155,38 +155,26 @@ const setupGeneratedCssDir = async (options) => {
155
155
  }
156
156
  return flairGeneratedCssDir;
157
157
  };
158
- const setupUserThemeFile = async ({ buildThemeFile }) => {
158
+ const setupUserThemeFile = async ({ buildThemeFile, onThemeFileChange, deleteBeforeWrite = false }) => {
159
159
  const flairThemeFile = require$1.resolve("@flairjs/client/theme.css");
160
160
  let userTheme = await getUserTheme();
161
161
  const buildThemeCSS = buildThemeFile ?? buildThemeTokens;
162
162
  if (userTheme) {
163
163
  const themeCSS = buildThemeCSS(userTheme.theme);
164
164
  store.setLastThemeUpdate(Date.now());
165
+ if (deleteBeforeWrite) await (0, node_fs_promises.rm)(flairThemeFile, { force: true });
165
166
  await (0, node_fs_promises.writeFile)(flairThemeFile, themeCSS, "utf-8");
166
167
  (0, node_fs.watch)(userTheme.originalPath, async () => {
167
168
  userTheme = await getUserTheme();
168
169
  store.setLastThemeUpdate(Date.now());
169
170
  if (!userTheme) return;
170
171
  const themeCSS$1 = buildThemeCSS(userTheme.theme);
172
+ onThemeFileChange?.();
171
173
  await (0, node_fs_promises.writeFile)(flairThemeFile, themeCSS$1, "utf-8");
172
174
  });
173
175
  }
174
176
  return userTheme;
175
177
  };
176
- const removeOutdatedCssFiles = async (sourceFilePath, cssFilePath, { flairGeneratedCssDir, clearInstantly = false }) => {
177
- const previousGeneratedCssName = store.getGeneratedCssName(sourceFilePath);
178
- if (previousGeneratedCssName && previousGeneratedCssName !== cssFilePath) {
179
- if (clearInstantly) {
180
- await (0, node_fs_promises.rm)(node_path.default.join(flairGeneratedCssDir, previousGeneratedCssName), { force: true });
181
- store.setFileNameToGeneratedCssNameMap(sourceFilePath, cssFilePath);
182
- return;
183
- }
184
- setTimeout(() => {
185
- (0, node_fs_promises.rm)(node_path.default.join(flairGeneratedCssDir, previousGeneratedCssName), { force: true });
186
- }, 2e3);
187
- }
188
- store.setFileNameToGeneratedCssNameMap(sourceFilePath, cssFilePath);
189
- };
190
178
  function shouldProcessFile(id, include, exclude) {
191
179
  const isIncluded = (0, picomatch.default)(include ?? ["**/*.{js,ts,jsx,tsx}"]);
192
180
  const isExcluded = (0, picomatch.default)(exclude ?? ["**/node_modules/**"]);
@@ -252,7 +240,10 @@ async function flairJsLoader(source, sourceMap) {
252
240
  let userTheme = null;
253
241
  if (!initialized) {
254
242
  cssGeneratedDir = await setupGeneratedCssDir();
255
- userTheme = await setupUserThemeFile({ buildThemeFile: options.buildThemeFile });
243
+ userTheme = await setupUserThemeFile({
244
+ buildThemeFile: options.buildThemeFile,
245
+ deleteBeforeWrite: true
246
+ });
256
247
  initialized = true;
257
248
  } else {
258
249
  cssGeneratedDir = getGeneratedCssDir();
@@ -264,7 +255,7 @@ async function flairJsLoader(source, sourceMap) {
264
255
  }
265
256
  try {
266
257
  const result = transformCode(source, fileName, {
267
- appendTimestampToCssFile: true,
258
+ appendTimestampToCssFile: false,
268
259
  classNameList: options?.classNameList,
269
260
  cssPreprocessor: options?.cssPreprocessor ? (css) => options.cssPreprocessor(css, fileName) : void 0,
270
261
  theme: userTheme?.theme,
@@ -272,7 +263,8 @@ async function flairJsLoader(source, sourceMap) {
272
263
  cssOutDir: cssGeneratedDir
273
264
  });
274
265
  if (!result) return callback(null, source, sourceMap);
275
- if (result.generatedCssName) removeOutdatedCssFiles(fileName, result.generatedCssName, { flairGeneratedCssDir: cssGeneratedDir });
266
+ if (!result.generatedCssName) return callback(null, source, sourceMap);
267
+ this.addDependency(path.resolve(result.generatedCssName));
276
268
  callback(null, result.code, result.sourcemap ? JSON.parse(result.sourcemap ?? "{}") : sourceMap);
277
269
  } catch (error) {
278
270
  console.error("[@flairjs/webpack-loader]", error);
package/dist/esm/index.js CHANGED
@@ -5,6 +5,7 @@ import path, { dirname } from "node:path";
5
5
  import * as esbuild from "esbuild";
6
6
  import { existsSync as existsSync$1 } from "fs";
7
7
  import { fileURLToPath } from "node:url";
8
+ import * as path$2 from "path";
8
9
  import path$1 from "path";
9
10
  import { pathToFileURL } from "url";
10
11
  import picomatch from "picomatch";
@@ -62,8 +63,8 @@ var Store = class {
62
63
  };
63
64
  const store = new Store();
64
65
  const __dirname = dirname(fileURLToPath(import.meta.url));
65
- const getUserTheme = async () => {
66
- if (store.getUserTheme()) return store.getUserTheme();
66
+ const getUserTheme = async (options) => {
67
+ if (store.getUserTheme() && !options?.ignoreCache) return store.getUserTheme();
67
68
  try {
68
69
  let userThemeFilePath = path$1.resolve(process.cwd(), "flair.theme.ts");
69
70
  if (!existsSync$1(userThemeFilePath)) userThemeFilePath = path$1.resolve(process.cwd(), "flair.theme.js");
@@ -121,38 +122,26 @@ const setupGeneratedCssDir = async (options) => {
121
122
  }
122
123
  return flairGeneratedCssDir;
123
124
  };
124
- const setupUserThemeFile = async ({ buildThemeFile }) => {
125
+ const setupUserThemeFile = async ({ buildThemeFile, onThemeFileChange, deleteBeforeWrite = false }) => {
125
126
  const flairThemeFile = require.resolve("@flairjs/client/theme.css");
126
127
  let userTheme = await getUserTheme();
127
128
  const buildThemeCSS = buildThemeFile ?? buildThemeTokens;
128
129
  if (userTheme) {
129
130
  const themeCSS = buildThemeCSS(userTheme.theme);
130
131
  store.setLastThemeUpdate(Date.now());
132
+ if (deleteBeforeWrite) await rm(flairThemeFile, { force: true });
131
133
  await writeFile(flairThemeFile, themeCSS, "utf-8");
132
134
  watch(userTheme.originalPath, async () => {
133
135
  userTheme = await getUserTheme();
134
136
  store.setLastThemeUpdate(Date.now());
135
137
  if (!userTheme) return;
136
138
  const themeCSS$1 = buildThemeCSS(userTheme.theme);
139
+ onThemeFileChange?.();
137
140
  await writeFile(flairThemeFile, themeCSS$1, "utf-8");
138
141
  });
139
142
  }
140
143
  return userTheme;
141
144
  };
142
- const removeOutdatedCssFiles = async (sourceFilePath, cssFilePath, { flairGeneratedCssDir, clearInstantly = false }) => {
143
- const previousGeneratedCssName = store.getGeneratedCssName(sourceFilePath);
144
- if (previousGeneratedCssName && previousGeneratedCssName !== cssFilePath) {
145
- if (clearInstantly) {
146
- await rm(path.join(flairGeneratedCssDir, previousGeneratedCssName), { force: true });
147
- store.setFileNameToGeneratedCssNameMap(sourceFilePath, cssFilePath);
148
- return;
149
- }
150
- setTimeout(() => {
151
- rm(path.join(flairGeneratedCssDir, previousGeneratedCssName), { force: true });
152
- }, 2e3);
153
- }
154
- store.setFileNameToGeneratedCssNameMap(sourceFilePath, cssFilePath);
155
- };
156
145
  function shouldProcessFile(id, include, exclude) {
157
146
  const isIncluded = picomatch(include ?? ["**/*.{js,ts,jsx,tsx}"]);
158
147
  const isExcluded = picomatch(exclude ?? ["**/node_modules/**"]);
@@ -218,7 +207,10 @@ async function flairJsLoader(source, sourceMap) {
218
207
  let userTheme = null;
219
208
  if (!initialized) {
220
209
  cssGeneratedDir = await setupGeneratedCssDir();
221
- userTheme = await setupUserThemeFile({ buildThemeFile: options.buildThemeFile });
210
+ userTheme = await setupUserThemeFile({
211
+ buildThemeFile: options.buildThemeFile,
212
+ deleteBeforeWrite: true
213
+ });
222
214
  initialized = true;
223
215
  } else {
224
216
  cssGeneratedDir = getGeneratedCssDir();
@@ -230,7 +222,7 @@ async function flairJsLoader(source, sourceMap) {
230
222
  }
231
223
  try {
232
224
  const result = transformCode$1(source, fileName, {
233
- appendTimestampToCssFile: true,
225
+ appendTimestampToCssFile: false,
234
226
  classNameList: options?.classNameList,
235
227
  cssPreprocessor: options?.cssPreprocessor ? (css) => options.cssPreprocessor(css, fileName) : void 0,
236
228
  theme: userTheme?.theme,
@@ -238,7 +230,8 @@ async function flairJsLoader(source, sourceMap) {
238
230
  cssOutDir: cssGeneratedDir
239
231
  });
240
232
  if (!result) return callback(null, source, sourceMap);
241
- if (result.generatedCssName) removeOutdatedCssFiles(fileName, result.generatedCssName, { flairGeneratedCssDir: cssGeneratedDir });
233
+ if (!result.generatedCssName) return callback(null, source, sourceMap);
234
+ this.addDependency(path$2.resolve(result.generatedCssName));
242
235
  callback(null, result.code, result.sourcemap ? JSON.parse(result.sourcemap ?? "{}") : sourceMap);
243
236
  } catch (error) {
244
237
  console.error("[@flairjs/webpack-loader]", error);
@@ -1,6 +1,33 @@
1
- import { SharedPluginOptions } from "@flairjs/bundler-shared";
2
- import { LoaderContext } from "webpack";
3
- interface FlairJsWebpackLoaderOptions extends SharedPluginOptions {
4
- }
5
- export default function flairJsLoader(this: LoaderContext<FlairJsWebpackLoaderOptions>, source: string, sourceMap: string): Promise<void>;
6
- export {};
1
+ import { FlairThemeConfig } from '@flairjs/client';
2
+ import { LoaderContext } from 'webpack';
3
+
4
+ declare function flairJsLoader(this: LoaderContext<FlairJsWebpackLoaderOptions>, source: string, sourceMap: string): Promise<void>;
5
+ export default flairJsLoader;
6
+
7
+ declare interface FlairJsWebpackLoaderOptions extends SharedPluginOptions {
8
+ }
9
+
10
+ declare interface SharedPluginOptions {
11
+ /**
12
+ * Preprocess the extracted CSS before it is passed to lightningcss
13
+ * @experimental
14
+ * @param css the extracted css
15
+ * @param id the id of the file being processed
16
+ * @returns the processed css
17
+ */
18
+ cssPreprocessor?: (css: string, id: string) => string;
19
+ include?: string | string[];
20
+ exclude?: string | string[];
21
+ /**
22
+ * Override the default theme file content based on the user theme
23
+ * @param theme the user theme
24
+ * @returns the theme file content
25
+ */
26
+ buildThemeFile?: (theme: FlairThemeConfig) => string;
27
+ /**
28
+ * List of class names used in the project. Supports regex.
29
+ */
30
+ classNameList?: string[];
31
+ }
32
+
33
+ export { }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flairjs/webpack-loader",
3
- "version": "0.0.1-beta.6",
3
+ "version": "0.0.1-beta.8",
4
4
  "main": "./dist/cjs/index.js",
5
5
  "module": "./dist/esm/index.js",
6
6
  "types": "./dist/types/index.d.ts",
@@ -13,24 +13,25 @@
13
13
  },
14
14
  "peerDependencies": {
15
15
  "webpack": ">=5.0.0",
16
- "@flairjs/core": "0.0.1-beta.5"
16
+ "@flairjs/core": "0.0.1-beta.7"
17
17
  },
18
18
  "devDependencies": {
19
- "@biomejs/biome": "^1.9.4",
20
- "@rollup/plugin-typescript": "^12.1.4",
19
+ "@biomejs/biome": "^2.2.4",
20
+ "@microsoft/api-extractor": "^7.52.13",
21
21
  "@types/node": "^22.8.1",
22
+ "rimraf": "^6.0.1",
22
23
  "rolldown": "1.0.0-beta.37",
23
24
  "typescript": "^5.8.2",
24
25
  "webpack": "^5.101.0",
25
- "@flairjs/bundler-shared": "0.0.1-beta.9",
26
- "@flairjs/core": "0.0.1-beta.5"
26
+ "@flairjs/bundler-shared": "0.0.1-beta.11",
27
+ "@flairjs/core": "0.0.1-beta.7"
27
28
  },
28
29
  "dependencies": {
29
30
  "esbuild": "^0.25.10",
30
31
  "picomatch": "^4.0.3"
31
32
  },
32
33
  "scripts": {
33
- "build": "tsc && rolldown -c",
34
+ "build": "rimraf dist && tsc && api-extractor run --local && rolldown -c && rimraf dist/types-temp",
34
35
  "check": "biome check --write",
35
36
  "dev": "rolldown -w -c",
36
37
  "format": "biome format --write"