stylex-webpack 0.1.1-beta.3 → 0.1.1-beta.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/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ import { Options, Rule } from '@stylexjs/babel-plugin';
4
4
  interface StyleXLoaderOptions {
5
5
  stylexImports: string[];
6
6
  stylexOption: Partial<Options>;
7
+ nextjsMode: boolean;
7
8
  }
8
9
 
9
10
  interface StyleXPluginOption {
@@ -20,6 +21,12 @@ interface StyleXPluginOption {
20
21
  * @default false
21
22
  */
22
23
  useCSSLayers?: boolean;
24
+ /**
25
+ * Next.js Mode
26
+ *
27
+ * @default false
28
+ */
29
+ nextjsMode?: boolean;
23
30
  }
24
31
  type RegisterStyleXRules = (resourcePath: string, stylexRules: Rule[]) => void;
25
32
  declare class StyleXPlugin {
@@ -27,7 +34,7 @@ declare class StyleXPlugin {
27
34
  stylexRules: Map<string, readonly Rule[]>;
28
35
  useCSSLayers: boolean;
29
36
  loaderOption: StyleXLoaderOptions;
30
- constructor({ stylexImports, useCSSLayers, stylexOption }?: StyleXPluginOption);
37
+ constructor({ stylexImports, useCSSLayers, stylexOption, nextjsMode }?: StyleXPluginOption);
31
38
  apply(compiler: webpack.Compiler): void;
32
39
  }
33
40
 
package/dist/index.js CHANGED
@@ -137,7 +137,7 @@ class StyleXPlugin {
137
137
  constructor({ stylexImports = [
138
138
  'stylex',
139
139
  '@stylexjs/stylex'
140
- ], useCSSLayers = false, stylexOption = {} } = {}){
140
+ ], useCSSLayers = false, stylexOption = {}, nextjsMode = false } = {}){
141
141
  _define_property(this, "stylexRules", new Map());
142
142
  _define_property(this, "useCSSLayers", void 0);
143
143
  _define_property(this, "loaderOption", void 0);
@@ -152,7 +152,8 @@ class StyleXPlugin {
152
152
  treeshakeCompensation: true,
153
153
  importSources: stylexImports,
154
154
  ...stylexOption
155
- }
155
+ },
156
+ nextjsMode
156
157
  };
157
158
  }
158
159
  }
package/dist/next.d.ts CHANGED
@@ -15,6 +15,12 @@ interface StyleXPluginOption {
15
15
  * @default false
16
16
  */
17
17
  useCSSLayers?: boolean;
18
+ /**
19
+ * Next.js Mode
20
+ *
21
+ * @default false
22
+ */
23
+ nextjsMode?: boolean;
18
24
  }
19
25
 
20
26
  declare const withStyleX: (pluginOptions?: StyleXPluginOption) => (nextConfig?: NextConfig) => NextConfig;
package/dist/next.js CHANGED
@@ -30,21 +30,23 @@ const getNextMiniCssExtractPlugin = (isDev)=>{
30
30
  };
31
31
  // Adopt from Next.js' getGlobalCssLoader
32
32
  // https://github.com/vercel/next.js/blob/d61b0761efae09bd9cb1201ff134ed8950d9deca/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L7
33
- function getStyleXVirtualCssLoader(options, MiniCssExtractPlugin) {
33
+ function getStyleXVirtualCssLoader(ctx, MiniCssExtractPlugin) {
34
34
  const loaders = [];
35
35
  // Adopt from Next.js' getClientStyleLoader
36
36
  // https://github.com/vercel/next.js/blob/56d35ede8ed2ab25fa8e29583d4e81e3e76a0e29/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L7
37
- if (!options.isServer) {
37
+ if (!ctx.isServer) {
38
38
  // https://github.com/vercel/next.js/blob/56d35ede8ed2ab25fa8e29583d4e81e3e76a0e29/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L18
39
39
  // https://github.com/vercel/next.js/blob/56d35ede8ed2ab25fa8e29583d4e81e3e76a0e29/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L3
40
40
  loaders.push({
41
41
  loader: MiniCssExtractPlugin.loader,
42
42
  options: {
43
- publicPath: `${options.assetPrefix}/_next/`,
43
+ publicPath: `${ctx.assetPrefix}/_next/`,
44
44
  esModule: false
45
45
  }
46
46
  });
47
47
  }
48
+ // We don't actually need to run postcss-loader or css-loader here
49
+ // As stylex virtual css won't contain any real css
48
50
  return loaders;
49
51
  }
50
52
  const withStyleX = (pluginOptions)=>(nextConfig = {})=>{
@@ -107,7 +109,9 @@ const withStyleX = (pluginOptions)=>(nextConfig = {})=>{
107
109
  stylexOption: {
108
110
  ...pluginOptions?.stylexOption,
109
111
  dev: ctx.dev
110
- }
112
+ },
113
+ // Enforce nextjsMode to true
114
+ nextjsMode: true
111
115
  }));
112
116
  return config;
113
117
  }
@@ -17,7 +17,7 @@ const isSupplementedLoaderContext = (context)=>{
17
17
  const PLUGIN_NAME = 'stylex';
18
18
  async function stylexLoader(inputCode, inputSourceMap) {
19
19
  const callback = this.async();
20
- const { stylexImports, stylexOption } = this.getOptions();
20
+ const { stylexImports, stylexOption, nextjsMode } = this.getOptions();
21
21
  // bail out early if the input doesn't contain stylex imports
22
22
  if (!stylexImports.some((importName)=>inputCode.includes(importName))) {
23
23
  return callback(null, inputCode, inputSourceMap);
@@ -55,10 +55,19 @@ async function stylexLoader(inputCode, inputSourceMap) {
55
55
  // Find a better way to register stylex rules to the compiler instance
56
56
  this.StyleXWebpackContextKey.registerStyleXRules(this.resourcePath, metadata.stylex);
57
57
  const serializedStyleXRules = JSON.stringify(metadata.stylex);
58
- const virtualFileName = loaderUtils.interpolateName(this, '[path][name].[hash:base64:8].stylex.virtual.css', {
59
- content: serializedStyleXRules
60
- });
61
- const virtualCssRequest = stringifyRequest(this, `${virtualFileName}!=!${VIRTUAL_CSS_PATH}?${serializedStyleXRules}`);
58
+ if (!nextjsMode) {
59
+ // Normal webpack mode
60
+ // We generate a virtual css file that looks like it is relative to the source
61
+ const virtualFileName = loaderUtils.interpolateName(this, '[path][name].[hash:base64:8].stylex.virtual.css', {
62
+ content: serializedStyleXRules
63
+ });
64
+ const virtualCssRequest = stringifyRequest(this, `${virtualFileName}!=!${VIRTUAL_CSS_PATH}?${serializedStyleXRules}`);
65
+ const postfix = `\nimport ${virtualCssRequest};`;
66
+ return callback(null, code + postfix, map ?? undefined);
67
+ }
68
+ // Next.js App Router doesn't support inline matchResource and inline loaders
69
+ // So we adapt Next.js' "external" css import approach instead
70
+ const virtualCssRequest = stringifyRequest(this, `${VIRTUAL_CSS_PATH}?${serializedStyleXRules}`);
62
71
  const postfix = `\nimport ${virtualCssRequest};`;
63
72
  return callback(null, code + postfix, map ?? undefined);
64
73
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylex-webpack",
3
- "version": "0.1.1-beta.3",
3
+ "version": "0.1.1-beta.4",
4
4
  "description": "The another Webpack Plugin for Facebook's StyleX",
5
5
  "homepage": "https://github.com/SukkaW/style9-webpack#readme",
6
6
  "repository": {