@lynx-js/css-extract-webpack-plugin 0.5.1 → 0.5.3

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,19 @@
1
1
  # @lynx-js/css-extract-webpack-plugin
2
2
 
3
+ ## 0.5.3
4
+
5
+ ### Patch Changes
6
+
7
+ - Fix CSS HMR not working with nested entry name. ([#456](https://github.com/lynx-family/lynx-stack/pull/456))
8
+
9
+ - 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))
10
+
11
+ ## 0.5.2
12
+
13
+ ### Patch Changes
14
+
15
+ - feat(css-extra-webpack-plugin): Support css hmr for lazy bundle ([#155](https://github.com/lynx-family/lynx-stack/pull/155))
16
+
3
17
  ## 0.5.1
4
18
 
5
19
  ### 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
  */
@@ -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,19 +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') {
106
- const { path } = compilation.getPathWithInfo(options.filename ?? '[name].css',
107
- // Rspack does not pass JsChunk to Rust.
108
- // See: https://github.com/web-infra-dev/rspack/blob/73c31abcb78472eb5a3d93e4ece19d9f106727a6/crates/rspack_binding_values/src/path_data.rs#L62
109
- { filename: chunk.name });
110
- this.#overrideChunkLoadingRuntimeModule(compiler, runtimeModule, path.replace('.css', `${this.hash ? `.${this.hash}` : ''}.css.hot-update.json`));
100
+ const { RuntimeGlobals, RuntimeModule } = compiler.webpack;
101
+ class CSSHotUpdateRuntimeModule extends RuntimeModule {
102
+ constructor(hash) {
103
+ super('lynx css hot update');
104
+ this.hash = hash;
111
105
  }
112
- });
106
+ generate() {
107
+ const chunk = this.chunk;
108
+ const asyncChunks = Array.from(chunk.getAllAsyncChunks())
109
+ .map(c => {
110
+ const { path } = compilation.getAssetPathWithInfo(options.chunkFilename ?? '.rspeedy/async/[name]/[name].css', { chunk: c });
111
+ return [c.name, path];
112
+ });
113
+ const { path } = compilation.getPathWithInfo(options.filename ?? '[name].css', { chunk });
114
+ const initialChunk = [chunk.name, path];
115
+ const cssHotUpdateList = [...asyncChunks, initialChunk].map(([chunkName, cssHotUpdatePath]) => [
116
+ chunkName,
117
+ cssHotUpdatePath.replace('.css', `${this.hash ? `.${this.hash}` : ''}.css.hot-update.json`),
118
+ ]);
119
+ return `
120
+ ${RuntimeGlobals.require}.cssHotUpdateList = ${cssHotUpdateList ? JSON.stringify(cssHotUpdateList) : 'null'};
121
+ `;
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);
113
150
  compilation.hooks.processAssets.tapPromise({
114
151
  name: this.name,
115
152
  stage: 300,
@@ -134,6 +171,7 @@ class CssExtractRspackPluginImpl {
134
171
  targetSdkVersion: options.targetSdkVersion,
135
172
  enableCSSSelector: options.enableCSSSelector,
136
173
  enableRemoveCSSScope: options.enableRemoveCSSScope,
174
+ enableCSSInvalidation: options.enableCSSInvalidation,
137
175
  }, options.cssPlugins, hooks.encode.taps.length > 0
138
176
  ? async (encodeOptions) => {
139
177
  // @ts-expect-error Only CSS is needed
@@ -148,7 +186,6 @@ class CssExtractRspackPluginImpl {
148
186
  deps: cssDeps,
149
187
  };
150
188
  compilation.emitAsset(filename.replace('.css', `${this.hash ? `.${this.hash}` : ''}.css.hot-update.json`), new compiler.webpack.sources.RawSource(JSON.stringify(result), true));
151
- this.hash = compilation.hash;
152
189
  }
153
190
  catch (error) {
154
191
  if (error && typeof error === 'object' && 'error_msg' in error) {
@@ -162,19 +199,10 @@ class CssExtractRspackPluginImpl {
162
199
  }
163
200
  }
164
201
  }
202
+ this.hash = compilation.hash;
165
203
  });
166
204
  }
167
205
  });
168
206
  }
169
- #overrideChunkLoadingRuntimeModule(compiler, runtimeModule, filename) {
170
- const { RuntimeGlobals } = compiler.webpack;
171
- runtimeModule.source.source = Buffer.concat([
172
- Buffer.from(runtimeModule.source.source),
173
- // lynxCssFileName
174
- Buffer.from(`
175
- ${RuntimeGlobals.require}.lynxCssFileName = ${filename ? JSON.stringify(filename) : 'null'};
176
- `),
177
- ]);
178
- }
179
207
  }
180
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.1",
3
+ "version": "0.5.3",
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,14 +44,15 @@
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.7",
47
+ "@microsoft/api-extractor": "7.52.2",
48
+ "@rspack/core": "1.3.1",
49
49
  "css-loader": "^7.1.2",
50
50
  "sass-loader": "^16.0.5",
51
51
  "webpack": "^5.98.0",
52
52
  "@lynx-js/css-serializer": "0.1.2",
53
- "@lynx-js/template-webpack-plugin": "0.6.4",
54
- "@lynx-js/test-tools": "0.0.0"
53
+ "@lynx-js/template-webpack-plugin": "0.6.8",
54
+ "@lynx-js/test-tools": "0.0.0",
55
+ "@lynx-js/vitest-setup": "0.0.0"
55
56
  },
56
57
  "peerDependencies": {
57
58
  "@lynx-js/template-webpack-plugin": "^0.5.0 || ^0.6.0"
@@ -17,29 +17,38 @@ function debounce(fn, time) {
17
17
  }
18
18
 
19
19
  function updateStyle(cssId = 0) {
20
- const filename = __webpack_require__.lynxCssFileName;
21
- if (!filename) {
22
- throw new Error('Css Filename not found');
20
+ const cssHotUpdateList = __webpack_require__.cssHotUpdateList;
21
+ if (!cssHotUpdateList) {
22
+ throw new Error('cssHotUpdateList is not found');
23
23
  }
24
24
 
25
- lynx.requireModuleAsync(
26
- // lynx.requireModuleAsync has two level hash and we cannot delete
27
- // the LynxGroup level cache here.
28
- // Temporarily using `Date.now` to avoid being cached.
29
- __webpack_require__.p + filename,
30
- (err, ret) => {
31
- if (err) {
32
- throw new Error('Load update css file `' + filename + '` failed');
33
- }
34
-
35
- if (ret.content) {
36
- lynx.getCoreContext().dispatchEvent({
37
- type: 'lynx.hmr.css',
38
- data: { cssId, content: ret.content, deps: ret.deps },
39
- });
40
- }
41
- },
42
- );
25
+ for (const [chunkName, cssHotUpdatePath] of cssHotUpdateList) {
26
+ lynx.requireModuleAsync(
27
+ // lynx.requireModuleAsync has two level hash and we cannot delete
28
+ // the LynxGroup level cache here.
29
+ // Temporarily using `Date.now` to avoid being cached.
30
+ __webpack_require__.p + cssHotUpdatePath,
31
+ (err, ret) => {
32
+ if (err) {
33
+ throw new Error(
34
+ `Failed to load CSS update file: ${cssHotUpdatePath}`,
35
+ );
36
+ }
37
+
38
+ if (ret.content) {
39
+ lynx.getCoreContext().dispatchEvent({
40
+ type: 'lynx.hmr.css',
41
+ data: {
42
+ cssId,
43
+ content: ret.content,
44
+ deps: ret.deps,
45
+ entry: lynx.__chunk_entries__[chunkName],
46
+ },
47
+ });
48
+ }
49
+ },
50
+ );
51
+ }
43
52
  }
44
53
 
45
54
  /**
@@ -2,13 +2,14 @@ function main() {
2
2
  try {
3
3
  lynx.getJSContext().addEventListener('lynx.hmr.css', (event) => {
4
4
  try {
5
- const { data: { cssId, content, deps } } = event;
5
+ const { data: { cssId, content, deps, entry } } = event;
6
6
  // Update the css deps first because the css deps are updated actually.
7
7
  if (Array.isArray(deps[cssId])) {
8
8
  deps[cssId].forEach(depCSSId => {
9
9
  lynx.getDevtool().replaceStyleSheetByIdWithBase64(
10
10
  Number(depCSSId),
11
11
  content,
12
+ entry,
12
13
  );
13
14
  });
14
15
  }
@@ -16,6 +17,7 @@ function main() {
16
17
  lynx.getDevtool().replaceStyleSheetByIdWithBase64(
17
18
  Number(cssId),
18
19
  content,
20
+ entry,
19
21
  );
20
22
 
21
23
  __FlushElementTree();