@plumeria/webpack-plugin 0.10.0 → 0.11.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/LICENSE ADDED
@@ -0,0 +1,21 @@
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.
package/dist/index.d.ts CHANGED
@@ -1,11 +1,8 @@
1
1
  import type { Compiler } from 'webpack';
2
2
  export declare class PlumeriaPlugin {
3
- private outputFileName;
4
3
  private stylesByFile;
5
- private outFile;
6
- constructor(outputFileName?: string);
4
+ private virtualModules?;
7
5
  apply(compiler: Compiler): void;
8
- registerFileStyles(filePath: string, styles: Partial<any>): void;
6
+ registerStyle(virtualFileName: string, styles: any): void;
9
7
  private generateOrderedCSS;
10
- private writeCSS;
11
8
  }
package/dist/index.js CHANGED
@@ -5,53 +5,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.PlumeriaPlugin = void 0;
7
7
  const webpack_1 = require("webpack");
8
- const path_1 = __importDefault(require("path"));
9
- const fs_1 = __importDefault(require("fs"));
8
+ const webpack_virtual_modules_1 = __importDefault(require("webpack-virtual-modules"));
10
9
  const PLUGIN_NAME = 'PlumeriaPlugin';
11
10
  class PlumeriaPlugin {
12
- outputFileName;
13
11
  stylesByFile = new Map();
14
- outFile;
15
- constructor(outputFileName = 'zero-virtual.css') {
16
- this.outputFileName = outputFileName;
17
- }
12
+ virtualModules;
18
13
  apply(compiler) {
19
- this.outFile = path_1.default.resolve(__dirname, '..', this.outputFileName);
14
+ this.virtualModules = new webpack_virtual_modules_1.default();
15
+ this.virtualModules.apply(compiler);
20
16
  compiler.hooks.compile.tap(PLUGIN_NAME, () => {
21
17
  this.stylesByFile.clear();
22
18
  });
23
- compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (nmf) => {
24
- nmf.hooks.createModule.tap(PLUGIN_NAME, (createData) => {
25
- const modPath = createData.matchResource ?? createData.resourceResolveData?.path;
26
- if (modPath && modPath.includes('zero-virtual.css')) {
27
- createData.settings ??= {};
28
- createData.settings.sideEffects = true;
29
- }
30
- });
31
- });
32
19
  compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
33
- compilation.hooks.processAssets.tap({
20
+ compilation.hooks.processAssets.tapPromise({
34
21
  name: PLUGIN_NAME,
35
22
  stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
36
- }, () => {
37
- this.writeCSS();
23
+ }, async () => {
24
+ const css = this.generateOrderedCSS(this.stylesByFile);
25
+ compilation.emitAsset('plumeria.css', new webpack_1.sources.RawSource(css));
38
26
  });
39
27
  });
40
28
  }
41
- registerFileStyles(filePath, styles) {
42
- const prev = this.stylesByFile.get(filePath) || {
43
- filePath,
44
- globalStyles: '',
45
- keyframeStyles: '',
46
- varStyles: '',
47
- themeStyles: '',
48
- baseStyles: '',
49
- };
50
- this.stylesByFile.set(filePath, { ...prev, ...styles });
51
- this.writeCSS();
29
+ registerStyle(virtualFileName, styles) {
30
+ this.stylesByFile.set(virtualFileName, styles);
31
+ const css = this.generateOrderedCSS(this.stylesByFile);
32
+ this.virtualModules?.writeModule(virtualFileName, css);
52
33
  }
53
- generateOrderedCSS() {
54
- const allStyles = Array.from(this.stylesByFile.values());
34
+ generateOrderedCSS(styles) {
35
+ const allStyles = Array.from(styles.values());
55
36
  const globalStyles = [];
56
37
  const keyframeStylesSet = new Set();
57
38
  const varStylesSet = new Set();
@@ -79,10 +60,5 @@ class PlumeriaPlugin {
79
60
  .filter(Boolean)
80
61
  .join('\n');
81
62
  }
82
- writeCSS() {
83
- const css = this.generateOrderedCSS();
84
- fs_1.default.mkdirSync(path_1.default.dirname(this.outFile), { recursive: true });
85
- fs_1.default.writeFileSync(this.outFile, css, 'utf-8');
86
- }
87
63
  }
88
64
  exports.PlumeriaPlugin = PlumeriaPlugin;
@@ -44,6 +44,7 @@ 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"));
47
48
  const PROJECT_ROOT = process.cwd().split('node_modules')[0];
48
49
  const PATTERN_PATH = path_1.default.join(PROJECT_ROOT, '**/*.{js,jsx,ts,tsx}');
49
50
  const GLOB_OPTIONS = {
@@ -542,6 +543,7 @@ function isCSSDefineFile(filePath, target) {
542
543
  }
543
544
  function loader(source) {
544
545
  const callback = this.async();
546
+ this.clearDependencies();
545
547
  this.addDependency(this.resourcePath);
546
548
  constTable = scanForDefineConsts.call(this);
547
549
  const { keyframesHashTableLocal, keyframesObjectTableLocal } = scanForKeyframes.call(this);
@@ -629,34 +631,29 @@ function loader(source) {
629
631
  .styleSheet)
630
632
  .join('\n');
631
633
  }
632
- const VIRTUAL_CSS_PATH = require.resolve(path_1.default.resolve(__dirname, '..', 'zero-virtual.css'));
633
- function stringifyRequest(loaderContext, request) {
634
- return JSON.stringify(loaderContext.utils.contextify(loaderContext.context || loaderContext.rootContext, request));
635
- }
636
- 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)));
637
- let importPath = virtualCssImportPath;
638
- if (!importPath.startsWith('.')) {
639
- importPath = './' + importPath;
640
- }
641
- const serializedStyleRules = JSON.stringify(fileStyles);
642
- const urlParams = new URLSearchParams({
643
- from: this.resourcePath,
644
- plumeria: serializedStyleRules,
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,
645
637
  });
646
- const virtualCssRequest = stringifyRequest(this, `${VIRTUAL_CSS_PATH}?${urlParams.toString()}`);
647
- const postfix = `\nimport ${virtualCssRequest};`;
638
+ const absVirtualCssFileName = path_1.default.resolve(this.rootContext, virtualCssFileName);
648
639
  const pluginInstance = this._compiler?.options?.plugins.find((p) => p?.constructor?.name === 'PlumeriaPlugin');
649
640
  if (pluginInstance) {
650
641
  if (!pluginInstance.__plumeriaRegistered) {
651
642
  pluginInstance.__plumeriaRegistered = new Set();
652
643
  }
653
644
  const cache = pluginInstance.__plumeriaRegistered;
654
- if (!cache.has(virtualCssRequest)) {
655
- cache.add(virtualCssRequest);
656
- pluginInstance?.registerFileStyles(virtualCssRequest, fileStyles);
645
+ if (!cache.has(absVirtualCssFileName)) {
646
+ cache.add(absVirtualCssFileName);
647
+ pluginInstance.registerStyle(absVirtualCssFileName, fileStyles);
657
648
  }
658
649
  }
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)};`;
659
656
  if (callback)
660
- callback(null, source + postfix);
661
- return source + postfix;
657
+ return callback(null, resultSource);
658
+ return resultSource;
662
659
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/webpack-plugin",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Plumeria Webpack plugin",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,26 +12,28 @@
12
12
  "main": "dist/index.js",
13
13
  "types": "dist/index.d.ts",
14
14
  "files": [
15
- "dist/",
16
- "zero-virtual.css"
15
+ "dist/"
17
16
  ],
18
- "scripts": {
19
- "build": "rimraf dist && pnpm cjs",
20
- "cjs": "tsc --project tsconfig.cjs.json"
21
- },
22
17
  "dependencies": {
23
18
  "@babel/core": "^7.28.0",
24
19
  "@babel/preset-react": "^7.27.1",
25
20
  "@babel/preset-typescript": "^7.27.1",
26
- "@babel/types": "^7.28.2"
21
+ "@babel/types": "^7.28.2",
22
+ "loader-utils": "^3.3.1",
23
+ "webpack-virtual-modules": "^0.6.2"
27
24
  },
28
25
  "devDependencies": {
29
26
  "@rust-gear/glob": "^0.2.2",
30
27
  "@types/babel__core": "^7.20.5",
28
+ "@types/loader-utils": "^2.0.6",
31
29
  "webpack": "^5.101.0",
32
30
  "zss-engine": "^0.2.75"
33
31
  },
34
32
  "publishConfig": {
35
33
  "access": "public"
34
+ },
35
+ "scripts": {
36
+ "build": "rimraf dist && pnpm cjs",
37
+ "cjs": "tsc --project tsconfig.cjs.json"
36
38
  }
37
- }
39
+ }
package/zero-virtual.css DELETED
File without changes