stylex-webpack 0.4.0 → 0.4.1

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.js CHANGED
@@ -4,13 +4,16 @@ var stylexBabelPlugin = require('@stylexjs/babel-plugin');
4
4
  var path = require('node:path');
5
5
  var process = require('node:process');
6
6
  var VirtualModulesPlugin = require('webpack-virtual-modules');
7
+ var identity = require('foxts/identity');
8
+
9
+ var version = "0.4.1";
7
10
 
8
11
  const PLUGIN_NAME = 'stylex';
9
12
  const VIRTUAL_ENTRYPOINT_CSS_PATH = require.resolve('./stylex.css');
10
13
  const VIRTUAL_ENTRYPOINT_CSS_PATTERN = /stylex\.css/;
11
14
  const STYLEX_CHUNK_NAME = '_stylex-webpack-generated';
12
- require.resolve('./stylex.fuck-nextjs.virtual-carrier.js');
13
- const FUCK_NEXTJS_VIRTUAL_CARRIER_PATTERN = /stylex\.fuck-nextjs\.virtual-carrier\.js/;
15
+ require.resolve('./stylex.fuck-nextjs.virtual-carrier.css');
16
+ const FUCK_NEXTJS_VIRTUAL_CARRIER_PATTERN = /stylex\.fuck-nextjs\.virtual-carrier\.css/;
14
17
  const FUCK_NEXTJS_VIRTUAL_CARRIERCHUNK_NAME = '_stylex-fuck-nextjs-collect-stylex-rules';
15
18
  const INCLUDE_REGEXP = /\.[cm]?[jt]sx?$/;
16
19
 
@@ -37,7 +40,6 @@ function getStyleXRules(stylexRules, useCSSLayers) {
37
40
  const allRules = Array.from(stylexRules.values()).flat();
38
41
  return stylexBabelPlugin.processStylexRules(allRules, useCSSLayers);
39
42
  }
40
- const identityTransfrom = (css)=>css;
41
43
  class StyleXPlugin {
42
44
  apply(compiler) {
43
45
  var _compiler_options_optimization_splitChunks;
@@ -61,6 +63,7 @@ class StyleXPlugin {
61
63
  compiler.options.optimization.splitChunks.cacheGroups[FUCK_NEXTJS_VIRTUAL_CARRIERCHUNK_NAME] = {
62
64
  name: FUCK_NEXTJS_VIRTUAL_CARRIERCHUNK_NAME,
63
65
  test: FUCK_NEXTJS_VIRTUAL_CARRIER_PATTERN,
66
+ type: 'css/mini-extract',
64
67
  chunks: 'all',
65
68
  enforce: true
66
69
  };
@@ -80,10 +83,16 @@ class StyleXPlugin {
80
83
  }
81
84
  });
82
85
  });
83
- const { Compilation, NormalModule } = compiler.webpack;
84
- // const { RawSource } = sources;
86
+ const { Compilation, NormalModule, sources } = compiler.webpack;
87
+ const { RawSource } = sources;
88
+ const meta = JSON.stringify({
89
+ name: 'stylex-webpack',
90
+ packageVersion: version,
91
+ opt: this.loaderOption
92
+ });
85
93
  // Apply loader to JS modules
86
94
  compiler.hooks.make.tap(PLUGIN_NAME, (compilation)=>{
95
+ compilation.hooks.chunkHash.tap(PLUGIN_NAME, (_, hash)=>hash.update(meta));
87
96
  NormalModule.getCompilationHooks(compilation).loader.tap(PLUGIN_NAME, (loaderContext, mod)=>{
88
97
  const extname = path.extname(mod.matchResource || mod.resource);
89
98
  if (this.loaderOption.nextjsMode && FUCK_NEXTJS_VIRTUAL_CARRIER_PATTERN.test(mod.matchResource || mod.resource)) {
@@ -114,7 +123,7 @@ class StyleXPlugin {
114
123
  compilation.hooks.processAssets.tapPromise({
115
124
  name: PLUGIN_NAME,
116
125
  stage: Compilation.PROCESS_ASSETS_STAGE_PRE_PROCESS
117
- }, async (_)=>{
126
+ }, async (assets)=>{
118
127
  if (this.loaderOption.nextjsMode) {
119
128
  // Collect stylex rules from module instead of self maintained map
120
129
  // on previous step, we create a "stylex" chunk to hold all virtual stylex css
@@ -125,7 +134,6 @@ class StyleXPlugin {
125
134
  const cssModulesInFuckNextjsChunk = compilation.chunkGraph.getChunkModulesIterableBySourceType(fuckNextjsChunk, 'css/mini-extract');
126
135
  // we only re-collect stylex rules if we can found css in the stylex chunk
127
136
  if (cssModulesInFuckNextjsChunk) {
128
- this.stylexRules.clear();
129
137
  for (const cssModule of cssModulesInFuckNextjsChunk){
130
138
  if (!('_identifier' in cssModule) || typeof cssModule._identifier !== 'string') {
131
139
  continue;
@@ -149,17 +157,45 @@ class StyleXPlugin {
149
157
  return;
150
158
  }
151
159
  const finalCss = await this.transformCss(stylexCSS);
152
- /**
153
- * Now we write final CSS to virtual module, which acts like `stylex-webpack/stylex.css` has been
154
- * updated locally on the disk, and Next.js and webpack will have no choice but to update the global css
155
- */ this._virtualModuleInstance.writeModule(VIRTUAL_ENTRYPOINT_CSS_PATH, finalCss.toString());
160
+ if (compiler.options.mode === 'development') {
161
+ // In development mode, a.k.a. HMR
162
+ /**
163
+ * Now we write final CSS to virtual module, which acts like `stylex-webpack/stylex.css` has been
164
+ * updated locally on the disk, and Next.js and webpack will have no choice but to update the global css
165
+ */ this._virtualModuleInstance.writeModule(VIRTUAL_ENTRYPOINT_CSS_PATH, finalCss.toString());
166
+ } else {
167
+ const stylexChunk = compilation.namedChunks.get(STYLEX_CHUNK_NAME);
168
+ console.log({
169
+ rulesFrom: Array.from(this.stylexRules.keys()),
170
+ mode: compiler.options.mode,
171
+ stylexChunk,
172
+ asssets: Object.keys(assets)
173
+ });
174
+ if (!stylexChunk) return;
175
+ // Let's find the css file that belongs to the stylex chunk
176
+ const stylexChunkCssAssetNames = Object.keys(assets).filter((assetName)=>stylexChunk.files.has(assetName) && assetName.endsWith('.css'));
177
+ if (stylexChunkCssAssetNames.length === 0) {
178
+ return;
179
+ }
180
+ if (stylexChunkCssAssetNames.length > 1) {
181
+ console.warn('[stylex-webpack] Multiple CSS assets found for the stylex chunk. This should not happen. Please report this issue.');
182
+ }
183
+ const stylexAssetName = stylexChunkCssAssetNames[0];
184
+ console.log({
185
+ finalCss,
186
+ stylexAssetName
187
+ });
188
+ compilation.updateAsset(stylexAssetName, ()=>new RawSource(finalCss), {
189
+ minimized: false
190
+ });
191
+ }
156
192
  });
157
193
  });
158
194
  }
159
195
  constructor({ stylexImports = [
160
196
  'stylex',
161
197
  '@stylexjs/stylex'
162
- ], useCSSLayers = false, stylexOption = {}, nextjsMode = false, transformCss = identityTransfrom } = {}){
198
+ ], useCSSLayers = false, stylexOption = {}, nextjsMode = false, transformCss = identity.identity } = {}){
163
199
  _define_property(this, "stylexRules", new Map());
164
200
  _define_property(this, "useCSSLayers", void 0);
165
201
  _define_property(this, "loaderOption", void 0);
package/dist/next.js CHANGED
@@ -8,7 +8,7 @@ var index = require('./index');
8
8
 
9
9
  require.resolve('./stylex.css');
10
10
  const VIRTUAL_ENTRYPOINT_CSS_PATTERN = /stylex\.css/;
11
- require.resolve('./stylex.fuck-nextjs.virtual-carrier.js');
11
+ require.resolve('./stylex.fuck-nextjs.virtual-carrier.css');
12
12
 
13
13
  /** Next.js' precompilation add "__esModule: true", but doesn't add an actual default exports */ // @ts-expect-error -- Next.js fucks something up
14
14
  const NextMiniCssExtractPlugin = nextMiniCssExtractPluginExports.default;
@@ -17,7 +17,7 @@ function stylexFuckNextjsVirtualCarrierLoader(inputCode, inputSourceMap) {
17
17
  const hash = loaderUtils.getHashDigest(stylex, 'xxhash64', 'base62', 32);
18
18
  const code = tsDedent.dedent`
19
19
  /** stylex rules: ${stylex} */
20
- export default ${JSON.stringify('stylex-webpack-fuck-nextjs-' + hash)};
20
+ .stylex-fuck-nextjs-${hash} {}
21
21
  `;
22
22
  callback(null, inputCode + '\n' + code);
23
23
  } catch (e) {
@@ -4,7 +4,7 @@ var core = require('@babel/core');
4
4
  var stylexBabelPlugin = require('@stylexjs/babel-plugin');
5
5
 
6
6
  require.resolve('./stylex.css');
7
- const FUCK_NEXTJS_VIRTUAL_CARRIER_PATH = require.resolve('./stylex.fuck-nextjs.virtual-carrier.js');
7
+ const FUCK_NEXTJS_VIRTUAL_CARRIER_PATH = require.resolve('./stylex.fuck-nextjs.virtual-carrier.css');
8
8
  function isSupplementedLoaderContext(context) {
9
9
  // eslint-disable-next-line prefer-object-has-own -- target older
10
10
  return Object.prototype.hasOwnProperty.call(context, 'StyleXWebpackContextKey');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylex-webpack",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "The another Webpack Plugin for Facebook's StyleX",
5
5
  "homepage": "https://github.com/SukkaW/style9-webpack#readme",
6
6
  "repository": {
@@ -60,6 +60,7 @@
60
60
  },
61
61
  "devDependencies": {
62
62
  "@eslint-sukka/node": "^6.23.1",
63
+ "@rollup/plugin-json": "^6.1.0",
63
64
  "@swc-node/register": "^1.11.1",
64
65
  "@swc/core": "^1.13.5",
65
66
  "@types/babel__core": "^7.20.5",