@plumeria/webpack-plugin 0.11.0 → 0.13.0

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,8 +1,11 @@
1
1
  import type { Compiler } from 'webpack';
2
2
  export declare class PlumeriaPlugin {
3
+ private outputFileName;
3
4
  private stylesByFile;
4
- private virtualModules?;
5
+ private outFile;
6
+ constructor(outputFileName?: string);
5
7
  apply(compiler: Compiler): void;
6
- registerStyle(virtualFileName: string, styles: any): void;
8
+ registerFileStyles(filePath: string, styles: Partial<any>): void;
7
9
  private generateOrderedCSS;
10
+ private writeCSS;
8
11
  }
package/dist/index.js CHANGED
@@ -4,43 +4,53 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.PlumeriaPlugin = void 0;
7
- const webpack_1 = require("webpack");
8
- const webpack_virtual_modules_1 = __importDefault(require("webpack-virtual-modules"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const fs_1 = __importDefault(require("fs"));
9
9
  const PLUGIN_NAME = 'PlumeriaPlugin';
10
10
  class PlumeriaPlugin {
11
+ outputFileName;
11
12
  stylesByFile = new Map();
12
- virtualModules;
13
+ outFile;
14
+ constructor(outputFileName = 'zero-virtual.css') {
15
+ this.outputFileName = outputFileName;
16
+ }
13
17
  apply(compiler) {
14
- this.virtualModules = new webpack_virtual_modules_1.default();
15
- this.virtualModules.apply(compiler);
18
+ this.outFile = path_1.default.resolve(__dirname, '..', this.outputFileName);
16
19
  compiler.hooks.compile.tap(PLUGIN_NAME, () => {
17
20
  this.stylesByFile.clear();
18
21
  });
19
- compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
20
- compilation.hooks.processAssets.tapPromise({
21
- name: PLUGIN_NAME,
22
- stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
23
- }, async () => {
24
- const css = this.generateOrderedCSS(this.stylesByFile);
25
- compilation.emitAsset('plumeria.css', new webpack_1.sources.RawSource(css));
22
+ compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (nmf) => {
23
+ nmf.hooks.createModule.tap(PLUGIN_NAME, (createData) => {
24
+ const modPath = createData.matchResource ?? createData.resourceResolveData?.path;
25
+ if (modPath && modPath.includes('zero-virtual.css')) {
26
+ createData.settings ??= {};
27
+ createData.settings.sideEffects = true;
28
+ }
26
29
  });
27
30
  });
28
31
  }
29
- registerStyle(virtualFileName, styles) {
30
- this.stylesByFile.set(virtualFileName, styles);
31
- const css = this.generateOrderedCSS(this.stylesByFile);
32
- this.virtualModules?.writeModule(virtualFileName, css);
32
+ registerFileStyles(filePath, styles) {
33
+ const prev = this.stylesByFile.get(filePath) || {
34
+ filePath,
35
+ globalStyles: '',
36
+ keyframeStyles: '',
37
+ varStyles: '',
38
+ themeStyles: '',
39
+ baseStyles: '',
40
+ };
41
+ this.stylesByFile.set(filePath, { ...prev, ...styles });
42
+ this.writeCSS();
33
43
  }
34
- generateOrderedCSS(styles) {
35
- const allStyles = Array.from(styles.values());
36
- const globalStyles = [];
44
+ generateOrderedCSS() {
45
+ const allStyles = Array.from(this.stylesByFile.values());
46
+ const globalStylesSet = new Set();
37
47
  const keyframeStylesSet = new Set();
38
48
  const varStylesSet = new Set();
39
49
  const themeStylesSet = new Set();
40
- const baseStyles = [];
50
+ const baseStylesSet = new Set();
41
51
  for (const s of allStyles) {
42
52
  if (s.globalStyles)
43
- globalStyles.push(s.globalStyles);
53
+ globalStylesSet.add(s.globalStyles);
44
54
  if (s.keyframeStyles)
45
55
  keyframeStylesSet.add(s.keyframeStyles);
46
56
  if (s.varStyles)
@@ -48,17 +58,24 @@ class PlumeriaPlugin {
48
58
  if (s.themeStyles)
49
59
  themeStylesSet.add(s.themeStyles);
50
60
  if (s.baseStyles)
51
- baseStyles.push(s.baseStyles);
61
+ baseStylesSet.add(s.baseStyles);
52
62
  }
53
63
  return [
54
- ...globalStyles,
64
+ ...Array.from(globalStylesSet),
55
65
  ...Array.from(keyframeStylesSet),
56
66
  ...Array.from(varStylesSet),
57
67
  ...Array.from(themeStylesSet),
58
- ...baseStyles,
68
+ ...Array.from(baseStylesSet),
59
69
  ]
60
70
  .filter(Boolean)
61
71
  .join('\n');
62
72
  }
73
+ writeCSS() {
74
+ const css = this.generateOrderedCSS();
75
+ if (css.trim().length > 0) {
76
+ fs_1.default.mkdirSync(path_1.default.dirname(this.outFile), { recursive: true });
77
+ fs_1.default.writeFileSync(this.outFile, css, 'utf-8');
78
+ }
79
+ }
63
80
  }
64
81
  exports.PlumeriaPlugin = PlumeriaPlugin;
@@ -44,7 +44,6 @@ const fs_1 = __importDefault(require("fs"));
44
44
  const create_1 = require("./create");
45
45
  const glob_1 = require("@rust-gear/glob");
46
46
  const zss_engine_1 = require("zss-engine");
47
- const loader_utils_1 = __importDefault(require("loader-utils"));
48
47
  const PROJECT_ROOT = process.cwd().split('node_modules')[0];
49
48
  const PATTERN_PATH = path_1.default.join(PROJECT_ROOT, '**/*.{js,jsx,ts,tsx}');
50
49
  const GLOB_OPTIONS = {
@@ -543,8 +542,11 @@ function isCSSDefineFile(filePath, target) {
543
542
  }
544
543
  function loader(source) {
545
544
  const callback = this.async();
546
- this.clearDependencies();
547
545
  this.addDependency(this.resourcePath);
546
+ const files = (0, glob_1.globSync)(PATTERN_PATH, GLOB_OPTIONS);
547
+ for (const file of files) {
548
+ this.addDependency(file);
549
+ }
548
550
  constTable = scanForDefineConsts.call(this);
549
551
  const { keyframesHashTableLocal, keyframesObjectTableLocal } = scanForKeyframes.call(this);
550
552
  keyframesHashTable = keyframesHashTableLocal;
@@ -631,29 +633,35 @@ function loader(source) {
631
633
  .styleSheet)
632
634
  .join('\n');
633
635
  }
634
- const virtualCssFileName = loader_utils_1.default.interpolateName(this, '[path][name].[hash:base64:8].virtual.css', {
635
- content: JSON.stringify(fileStyles),
636
- context: this.rootContext,
636
+ const VIRTUAL_FILE_PATH = path_1.default.resolve(__dirname, '..', 'zero-virtual.css');
637
+ const VIRTUAL_CSS_PATH = require.resolve(VIRTUAL_FILE_PATH);
638
+ function stringifyRequest(loaderContext, request) {
639
+ return JSON.stringify(loaderContext.utils.contextify(loaderContext.context || loaderContext.rootContext, request));
640
+ }
641
+ const virtualCssImportPath = path_1.default.posix.join(path_1.default.posix.relative(path_1.default.dirname(this.resourcePath), path_1.default.resolve(__dirname, '..', VIRTUAL_CSS_PATH)));
642
+ let importPath = virtualCssImportPath;
643
+ if (!importPath.startsWith('.')) {
644
+ importPath = './' + importPath;
645
+ }
646
+ const serializedStyleRules = JSON.stringify(fileStyles);
647
+ const urlParams = new URLSearchParams({
648
+ from: this.resourcePath,
649
+ plumeria: serializedStyleRules,
637
650
  });
638
- const absVirtualCssFileName = path_1.default.resolve(this.rootContext, virtualCssFileName);
651
+ const virtualCssRequest = stringifyRequest(this, `${VIRTUAL_CSS_PATH}?${urlParams.toString()}`);
652
+ const postfix = `\nimport ${virtualCssRequest};`;
639
653
  const pluginInstance = this._compiler?.options?.plugins.find((p) => p?.constructor?.name === 'PlumeriaPlugin');
640
654
  if (pluginInstance) {
641
655
  if (!pluginInstance.__plumeriaRegistered) {
642
656
  pluginInstance.__plumeriaRegistered = new Set();
643
657
  }
644
658
  const cache = pluginInstance.__plumeriaRegistered;
645
- if (!cache.has(absVirtualCssFileName)) {
646
- cache.add(absVirtualCssFileName);
647
- pluginInstance.registerStyle(absVirtualCssFileName, fileStyles);
659
+ if (!cache.has(virtualCssRequest)) {
660
+ cache.add(virtualCssRequest);
661
+ pluginInstance?.registerFileStyles(virtualCssRequest, fileStyles);
648
662
  }
649
663
  }
650
- let importPath = path_1.default.posix.relative(path_1.default.dirname(this.resourcePath), absVirtualCssFileName);
651
- if (!importPath.startsWith('.')) {
652
- importPath = './' + importPath;
653
- }
654
- importPath = importPath.replace(/\\/g, '/');
655
- const resultSource = source + `\nimport ${JSON.stringify(importPath)};`;
656
664
  if (callback)
657
- return callback(null, resultSource);
658
- return resultSource;
665
+ callback(null, source + postfix);
666
+ return source + postfix;
659
667
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/webpack-plugin",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "description": "Plumeria Webpack plugin",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,28 +12,26 @@
12
12
  "main": "dist/index.js",
13
13
  "types": "dist/index.d.ts",
14
14
  "files": [
15
- "dist/"
15
+ "dist/",
16
+ "zero-virtual.css"
16
17
  ],
18
+ "scripts": {
19
+ "build": "rimraf dist && pnpm cjs",
20
+ "cjs": "tsc --project tsconfig.cjs.json"
21
+ },
17
22
  "dependencies": {
18
23
  "@babel/core": "^7.28.0",
19
24
  "@babel/preset-react": "^7.27.1",
20
25
  "@babel/preset-typescript": "^7.27.1",
21
- "@babel/types": "^7.28.2",
22
- "loader-utils": "^3.3.1",
23
- "webpack-virtual-modules": "^0.6.2"
26
+ "@babel/types": "^7.28.2"
24
27
  },
25
28
  "devDependencies": {
26
29
  "@rust-gear/glob": "^0.2.2",
27
30
  "@types/babel__core": "^7.20.5",
28
- "@types/loader-utils": "^2.0.6",
29
31
  "webpack": "^5.101.0",
30
32
  "zss-engine": "^0.2.75"
31
33
  },
32
34
  "publishConfig": {
33
35
  "access": "public"
34
- },
35
- "scripts": {
36
- "build": "rimraf dist && pnpm cjs",
37
- "cjs": "tsc --project tsconfig.cjs.json"
38
36
  }
39
- }
37
+ }
File without changes
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) zss-in-js contributer
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.