@plumeria/webpack-plugin 0.15.3 → 0.15.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/index.d.ts CHANGED
@@ -1,12 +1,19 @@
1
1
  import type { Compiler } from 'webpack';
2
2
  import { CSSObject } from './types';
3
+ interface PlumeriaPluginOptions {
4
+ entryPaths: string;
5
+ }
3
6
  export declare class PlumeriaPlugin {
4
- private outputFileName;
5
7
  private stylesByFile;
8
+ private currentPageFiles;
6
9
  private outFile;
7
- constructor(outputFileName?: string);
10
+ private options;
11
+ constructor(options: PlumeriaPluginOptions);
8
12
  apply(compiler: Compiler): void;
13
+ private updateCurrentPageFiles;
9
14
  registerFileStyles(filePath: string, styles: CSSObject): void;
15
+ private isCurrentPageFile;
10
16
  private generateOrderedCSS;
11
17
  private writeCSS;
12
18
  }
19
+ export {};
package/dist/index.js CHANGED
@@ -8,20 +8,25 @@ const path_1 = __importDefault(require("path"));
8
8
  const fs_1 = __importDefault(require("fs"));
9
9
  const PLUGIN_NAME = 'PlumeriaPlugin';
10
10
  class PlumeriaPlugin {
11
- outputFileName;
12
11
  stylesByFile = new Map();
12
+ currentPageFiles = new Set();
13
13
  outFile;
14
- constructor(outputFileName = 'zero-virtual.css') {
15
- this.outputFileName = outputFileName;
14
+ options;
15
+ constructor(options) {
16
+ this.options = options;
16
17
  }
17
18
  apply(compiler) {
18
- this.outFile = path_1.default.resolve(__dirname, '..', this.outputFileName);
19
+ this.outFile = path_1.default.resolve(__dirname, '..', 'zero-virtual.css');
19
20
  compiler.hooks.invalid.tap(PLUGIN_NAME, (filename) => {
20
21
  if (filename) {
21
22
  const absPath = path_1.default.resolve(filename);
22
23
  this.stylesByFile.delete(absPath);
24
+ this.currentPageFiles.delete(absPath);
23
25
  }
24
26
  });
27
+ compiler.hooks.watchRun.tap(PLUGIN_NAME, (compiler) => {
28
+ this.updateCurrentPageFiles(compiler);
29
+ });
25
30
  compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (nmf) => {
26
31
  nmf.hooks.createModule.tap(PLUGIN_NAME, (createData) => {
27
32
  const modPath = createData.matchResource ?? createData.resourceResolveData?.path;
@@ -32,43 +37,74 @@ class PlumeriaPlugin {
32
37
  });
33
38
  });
34
39
  }
40
+ updateCurrentPageFiles(compiler) {
41
+ const entries = compiler.options.entry;
42
+ if (entries && typeof entries === 'object') {
43
+ this.currentPageFiles.clear();
44
+ Object.keys(entries).forEach((entryName) => {
45
+ if (entryName.startsWith(this.options.entryPaths)) {
46
+ const entry = entries[entryName];
47
+ if (Array.isArray(entry)) {
48
+ entry.forEach((file) => {
49
+ this.currentPageFiles.add(path_1.default.resolve(file));
50
+ });
51
+ }
52
+ else if (typeof entry === 'string') {
53
+ this.currentPageFiles.add(path_1.default.resolve(entry));
54
+ }
55
+ }
56
+ });
57
+ }
58
+ }
35
59
  registerFileStyles(filePath, styles) {
36
60
  const absPath = path_1.default.resolve(filePath);
37
61
  const prev = this.stylesByFile.get(absPath) || {
38
62
  filePath,
39
- globalStyles: '',
40
63
  keyframeStyles: '',
41
64
  varStyles: '',
42
65
  themeStyles: '',
66
+ globalStyles: '',
43
67
  baseStyles: '',
44
68
  };
45
- this.stylesByFile.set(absPath, { ...prev, ...styles });
69
+ const isCurrentPage = this.isCurrentPageFile(absPath);
70
+ const updatedStyles = {
71
+ ...prev,
72
+ ...styles,
73
+ isCurrentPage,
74
+ lastAccessed: Date.now(),
75
+ };
76
+ this.stylesByFile.set(absPath, updatedStyles);
46
77
  this.writeCSS();
47
78
  }
79
+ isCurrentPageFile(filePath) {
80
+ return (this.currentPageFiles.has(filePath) ||
81
+ Array.from(this.currentPageFiles).some((pageFile) => filePath.includes(path_1.default.dirname(pageFile))));
82
+ }
48
83
  generateOrderedCSS() {
49
84
  const allStyles = Array.from(this.stylesByFile.values());
50
- const globalStylesSet = new Set();
85
+ const sortedStyles = allStyles.sort((a, b) => (b.lastAccessed || 0) - (a.lastAccessed || 0));
51
86
  const keyframeStylesSet = new Set();
52
87
  const varStylesSet = new Set();
53
88
  const themeStylesSet = new Set();
89
+ const globalStylesSet = new Set();
54
90
  const baseStylesSet = new Set();
55
- for (const s of allStyles) {
56
- if (s.globalStyles.trim().length > 0)
57
- globalStylesSet.add(s.globalStyles);
58
- if (s.keyframeStyles.trim().length > 0)
91
+ for (const s of sortedStyles) {
92
+ if (s.keyframeStyles?.trim().length > 0)
59
93
  keyframeStylesSet.add(s.keyframeStyles);
60
- if (s.varStyles.trim().length > 0)
94
+ if (s.varStyles?.trim().length > 0)
61
95
  varStylesSet.add(s.varStyles);
62
- if (s.themeStyles.trim().length > 0)
96
+ if (s.themeStyles?.trim().length > 0)
63
97
  themeStylesSet.add(s.themeStyles);
64
- if (s.baseStyles.trim().length > 0)
98
+ if (s.globalStyles?.trim().length > 0)
99
+ globalStylesSet.add(s.globalStyles);
100
+ if (s.baseStyles?.trim().length > 0)
65
101
  baseStylesSet.add(s.baseStyles);
66
102
  }
67
103
  return [
68
- ...Array.from(globalStylesSet),
69
104
  ...Array.from(keyframeStylesSet),
70
105
  ...Array.from(varStylesSet),
71
106
  ...Array.from(themeStylesSet),
107
+ ...Array.from(globalStylesSet),
72
108
  ...Array.from(baseStylesSet),
73
109
  ]
74
110
  .filter(Boolean)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/webpack-plugin",
3
- "version": "0.15.3",
3
+ "version": "0.15.5",
4
4
  "description": "Plumeria Webpack plugin",
5
5
  "repository": {
6
6
  "type": "git",