@lynx-js/css-extract-webpack-plugin 0.5.2 → 0.5.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @lynx-js/css-extract-webpack-plugin
2
2
 
3
+ ## 0.5.4
4
+
5
+ ### Patch Changes
6
+
7
+ - Support `@lynx-js/template-webpack-plugin` v0.7.0. ([#880](https://github.com/lynx-family/lynx-stack/pull/880))
8
+
9
+ - Support Rspack v1.3.11. ([#866](https://github.com/lynx-family/lynx-stack/pull/866))
10
+
11
+ ## 0.5.3
12
+
13
+ ### Patch Changes
14
+
15
+ - Fix CSS HMR not working with nested entry name. ([#456](https://github.com/lynx-family/lynx-stack/pull/456))
16
+
17
+ - fix: add enableCSSInvalidation for encodeCSS of css HMR, this will fix pseudo-class (such as `:active`) not working in HMR. ([#435](https://github.com/lynx-family/lynx-stack/pull/435))
18
+
3
19
  ## 0.5.2
4
20
 
5
21
  ### Patch Changes
@@ -1,7 +1,7 @@
1
1
  import type { Compiler, CssExtractRspackPluginOptions as ExternalCssExtractRspackPluginOptions } from '@rspack/core';
2
2
  import { LynxTemplatePlugin } from '@lynx-js/template-webpack-plugin';
3
3
  /**
4
- * The options for {@link @lynx-js/webpack/css-extract-webpack-plugin#CssExtractRspackPlugin}
4
+ * The options for {@link @lynx-js/css-extract-webpack-plugin#CssExtractRspackPlugin}
5
5
  *
6
6
  * @public
7
7
  */
@@ -14,6 +14,10 @@ interface CssExtractRspackPluginOptions extends ExternalCssExtractRspackPluginOp
14
14
  * {@inheritdoc @lynx-js/template-webpack-plugin#LynxTemplatePluginOptions.enableCSSSelector}
15
15
  */
16
16
  enableCSSSelector: boolean;
17
+ /**
18
+ * {@inheritdoc @lynx-js/template-webpack-plugin#LynxTemplatePluginOptions.enableCSSInvalidation}
19
+ */
20
+ enableCSSInvalidation: boolean;
17
21
  /**
18
22
  * {@inheritdoc @lynx-js/template-webpack-plugin#LynxTemplatePluginOptions.targetSdkVersion}
19
23
  */
@@ -22,6 +26,14 @@ interface CssExtractRspackPluginOptions extends ExternalCssExtractRspackPluginOp
22
26
  * plugins passed to parser
23
27
  */
24
28
  cssPlugins: Parameters<typeof LynxTemplatePlugin.convertCSSChunksToMap>[1];
29
+ /**
30
+ * The name of non-initial CSS chunk files
31
+ */
32
+ chunkFilename?: string;
33
+ /**
34
+ * The name of each output bundle.
35
+ */
36
+ filename?: string;
25
37
  }
26
38
  /**
27
39
  * @public
@@ -66,7 +66,7 @@ class CssExtractRspackPlugin {
66
66
  .freeze({
67
67
  enableRemoveCSSScope: false,
68
68
  enableCSSSelector: true,
69
- // TODO: version
69
+ enableCSSInvalidation: true,
70
70
  targetSdkVersion: '3.2',
71
71
  filename: '[name].css',
72
72
  cssPlugins: [],
@@ -97,29 +97,56 @@ class CssExtractRspackPluginImpl {
97
97
  compiler.hooks.thisCompilation.tap(this.name, (compilation) => {
98
98
  if (compiler.options.mode === 'development'
99
99
  || process.env['NODE_ENV'] === 'development') {
100
- // We require publicPath to get css.hot-update.json
101
- compilation.hooks.additionalTreeRuntimeRequirements.tap(this.name, (_, set) => {
102
- set.add(compiler.webpack.RuntimeGlobals.publicPath);
103
- });
104
- compilation.hooks.runtimeModule.tap(this.name, (runtimeModule, chunk) => {
105
- if (runtimeModule.name === 'require_chunk_loading') {
100
+ const { RuntimeGlobals, RuntimeModule } = compiler.webpack;
101
+ class CSSHotUpdateRuntimeModule extends RuntimeModule {
102
+ constructor(hash) {
103
+ super('lynx css hot update');
104
+ this.hash = hash;
105
+ }
106
+ generate() {
107
+ const chunk = this.chunk;
106
108
  const asyncChunks = Array.from(chunk.getAllAsyncChunks())
107
109
  .map(c => {
108
110
  const { path } = compilation.getAssetPathWithInfo(options.chunkFilename ?? '.rspeedy/async/[name]/[name].css', { chunk: c });
109
111
  return [c.name, path];
110
112
  });
111
- const { path } = compilation.getPathWithInfo(options.filename ?? '[name].css',
112
- // Rspack does not pass JsChunk to Rust.
113
- // See: https://github.com/web-infra-dev/rspack/blob/73c31abcb78472eb5a3d93e4ece19d9f106727a6/crates/rspack_binding_values/src/path_data.rs#L62
114
- { filename: chunk.name });
113
+ const { path } = compilation.getPathWithInfo(options.filename ?? '[name].css', { chunk });
115
114
  const initialChunk = [chunk.name, path];
116
115
  const cssHotUpdateList = [...asyncChunks, initialChunk].map(([chunkName, cssHotUpdatePath]) => [
117
116
  chunkName,
118
117
  cssHotUpdatePath.replace('.css', `${this.hash ? `.${this.hash}` : ''}.css.hot-update.json`),
119
118
  ]);
120
- this.#overrideChunkLoadingRuntimeModule(compiler, runtimeModule, cssHotUpdateList);
119
+ return `
120
+ ${RuntimeGlobals.require}.cssHotUpdateList = ${cssHotUpdateList ? JSON.stringify(cssHotUpdateList) : 'null'};
121
+ `;
121
122
  }
122
- });
123
+ }
124
+ const onceForChunkSet = new WeakSet();
125
+ const handler = (chunk, runtimeRequirements) => {
126
+ if (onceForChunkSet.has(chunk))
127
+ return;
128
+ onceForChunkSet.add(chunk);
129
+ runtimeRequirements.add(RuntimeGlobals.publicPath);
130
+ compilation.addRuntimeModule(chunk, new CSSHotUpdateRuntimeModule(this.hash));
131
+ };
132
+ compilation.hooks.runtimeRequirementInTree
133
+ .for(RuntimeGlobals.ensureChunkHandlers)
134
+ .tap(this.name, handler);
135
+ compilation.hooks.runtimeRequirementInTree
136
+ .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
137
+ .tap(this.name, handler);
138
+ compilation.hooks.runtimeRequirementInTree
139
+ .for(RuntimeGlobals.hmrDownloadManifest)
140
+ .tap(this.name, handler);
141
+ compilation.hooks.runtimeRequirementInTree
142
+ .for(RuntimeGlobals.baseURI)
143
+ .tap(this.name, handler);
144
+ compilation.hooks.runtimeRequirementInTree
145
+ .for(RuntimeGlobals.externalInstallChunk)
146
+ .tap(this.name, handler);
147
+ compilation.hooks.runtimeRequirementInTree
148
+ .for(RuntimeGlobals.onChunksLoaded)
149
+ .tap(this.name, handler);
123
150
  compilation.hooks.processAssets.tapPromise({
124
151
  name: this.name,
125
152
  stage: 300,
@@ -130,7 +157,7 @@ class CssExtractRspackPluginImpl {
130
157
  }
131
158
  // TODO: sourcemap
132
159
  const content = source.source().toString('utf-8');
133
- const { cssMap } = LynxTemplatePlugin.convertCSSChunksToMap([content], options.cssPlugins);
160
+ const { cssMap } = LynxTemplatePlugin.convertCSSChunksToMap([content], options.cssPlugins, options.enableCSSSelector);
134
161
  const cssDeps = Object.entries(cssMap).reduce((acc, [key, value]) => {
135
162
  const importRuleNodes = value.filter((node) => node.type === 'ImportRule');
136
163
  acc[key] = importRuleNodes.map(({ href }) => href);
@@ -144,6 +171,7 @@ class CssExtractRspackPluginImpl {
144
171
  targetSdkVersion: options.targetSdkVersion,
145
172
  enableCSSSelector: options.enableCSSSelector,
146
173
  enableRemoveCSSScope: options.enableRemoveCSSScope,
174
+ enableCSSInvalidation: options.enableCSSInvalidation,
147
175
  }, options.cssPlugins, hooks.encode.taps.length > 0
148
176
  ? async (encodeOptions) => {
149
177
  // @ts-expect-error Only CSS is needed
@@ -176,15 +204,5 @@ class CssExtractRspackPluginImpl {
176
204
  }
177
205
  });
178
206
  }
179
- #overrideChunkLoadingRuntimeModule(compiler, runtimeModule, cssHotUpdateList) {
180
- const { RuntimeGlobals } = compiler.webpack;
181
- runtimeModule.source.source = Buffer.concat([
182
- Buffer.from(runtimeModule.source.source),
183
- // cssHotUpdateList
184
- Buffer.from(`
185
- ${RuntimeGlobals.require}.cssHotUpdateList = ${cssHotUpdateList ? JSON.stringify(cssHotUpdateList) : 'null'};
186
- `),
187
- ]);
188
- }
189
207
  }
190
208
  //# sourceMappingURL=CssExtractRspackPlugin.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/css-extract-webpack-plugin",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "This plugin extracts CSS into separate files. It creates a CSS file per JS file which contains CSS.",
5
5
  "keywords": [
6
6
  "webpack",
@@ -44,17 +44,18 @@
44
44
  "mini-css-extract-plugin": "^2.9.2"
45
45
  },
46
46
  "devDependencies": {
47
- "@microsoft/api-extractor": "7.51.1",
48
- "@rspack/core": "1.2.8",
47
+ "@microsoft/api-extractor": "7.52.8",
48
+ "@rspack/core": "1.3.11",
49
49
  "css-loader": "^7.1.2",
50
50
  "sass-loader": "^16.0.5",
51
- "webpack": "^5.98.0",
51
+ "webpack": "^5.99.9",
52
52
  "@lynx-js/css-serializer": "0.1.2",
53
- "@lynx-js/template-webpack-plugin": "0.6.5",
54
- "@lynx-js/test-tools": "0.0.0"
53
+ "@lynx-js/template-webpack-plugin": "0.7.0",
54
+ "@lynx-js/test-tools": "0.0.0",
55
+ "@lynx-js/vitest-setup": "0.0.0"
55
56
  },
56
57
  "peerDependencies": {
57
- "@lynx-js/template-webpack-plugin": "^0.5.0 || ^0.6.0"
58
+ "@lynx-js/template-webpack-plugin": "^0.5.0 || ^0.6.0 || ^0.7.0"
58
59
  },
59
60
  "engines": {
60
61
  "node": ">=18"