@vanilla-extract/next-plugin 2.2.2 → 2.3.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.
@@ -1,5 +1,5 @@
1
- import { VanillaExtractPlugin } from '@vanilla-extract/webpack-plugin';
2
- import { NextConfig } from 'next/types';
1
+ import { VanillaExtractPlugin } from '@vanilla-extract/webpack-plugin/next';
2
+ import { NextConfig } from 'next/dist/server/config-shared';
3
3
 
4
4
  type PluginOptions = ConstructorParameters<typeof VanillaExtractPlugin>[0];
5
5
  declare const createVanillaExtractPlugin: (pluginOptions?: PluginOptions) => (nextConfig?: NextConfig) => NextConfig;
@@ -2,36 +2,42 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var webpackPlugin = require('@vanilla-extract/webpack-plugin');
6
- var browserslist = require('browserslist');
7
- var css = require('next/dist/build/webpack/config/blocks/css');
5
+ var browserslist = require('next/dist/compiled/browserslist');
6
+ var NextMiniCssExtractPluginDefault = require('next/dist/build/webpack/plugins/mini-css-extract-plugin');
7
+ var next = require('@vanilla-extract/webpack-plugin/next');
8
8
  var findPagesDir = require('next/dist/lib/find-pages-dir');
9
+ var css = require('next/dist/build/webpack/config/blocks/css');
9
10
  var fileResolve = require('next/dist/build/webpack/config/blocks/css/loaders/file-resolve');
10
- var NextMiniCssExtractPluginDefault = require('next/dist/build/webpack/plugins/mini-css-extract-plugin');
11
11
 
12
12
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
13
13
 
14
14
  var browserslist__default = /*#__PURE__*/_interopDefault(browserslist);
15
15
  var NextMiniCssExtractPluginDefault__default = /*#__PURE__*/_interopDefault(NextMiniCssExtractPluginDefault);
16
16
 
17
+ // @ts-expect-error
17
18
  const NextMiniCssExtractPlugin = NextMiniCssExtractPluginDefault__default["default"];
19
+
20
+ // Adopted from https://github.com/vercel/next.js/blob/1f1632979c78b3edfe59fd85d8cce62efcdee688/packages/next/build/webpack-config.ts#L60-L72
18
21
  function getSupportedBrowsers(dir, isDevelopment) {
19
- let browsers;
20
22
  try {
21
- browsers = browserslist__default["default"].loadConfig({
23
+ return browserslist__default["default"].loadConfig({
22
24
  path: dir,
23
25
  env: isDevelopment ? 'development' : 'production'
24
26
  });
25
- } catch {}
26
- return browsers;
27
+ } catch (_) {
28
+ return undefined;
29
+ }
27
30
  }
28
- // https://github.com/vercel/next.js/blob/canary/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L7
31
+
32
+ // Adopt from Next.js' getGlobalCssLoader
33
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L7
29
34
  const getVanillaExtractCssLoaders = (options, assetPrefix) => {
30
35
  const loaders = [];
31
36
 
32
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L14
37
+ // Adopt from Next.js' getClientStyleLoader
38
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L3
33
39
  if (!options.isServer) {
34
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L44
40
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L44
35
41
  // next-style-loader will mess up css order in development mode.
36
42
  // Next.js appDir doesn't use next-style-loader either.
37
43
  // So we always use css-loader here, to simplify things and get proper order of output CSS
@@ -45,7 +51,7 @@ const getVanillaExtractCssLoaders = (options, assetPrefix) => {
45
51
  }
46
52
  const postcss = () => css.lazyPostCSS(options.dir, getSupportedBrowsers(options.dir, options.dev), undefined);
47
53
 
48
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L28
54
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L28
49
55
  loaders.push({
50
56
  loader: require.resolve('next/dist/build/webpack/loaders/css-loader/src'),
51
57
  options: {
@@ -63,85 +69,96 @@ const getVanillaExtractCssLoaders = (options, assetPrefix) => {
63
69
  }
64
70
  });
65
71
 
66
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L43
72
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L29-L38
67
73
  loaders.push({
68
74
  loader: require.resolve('next/dist/build/webpack/loaders/postcss-loader/src'),
69
75
  options: {
70
76
  postcss
71
77
  }
72
78
  });
79
+
80
+ // https://github.com/SukkaW/style9-webpack/blob/f51c46bbcd95ea3b988d3559c3b35cc056874366/src/next-appdir/index.ts#L103-L105
81
+ loaders.push({
82
+ loader: next.VanillaExtractPlugin.loader
83
+ });
73
84
  return loaders;
74
85
  };
75
- const createVanillaExtractPlugin = (pluginOptions = {}) => (nextConfig = {}) => Object.assign({}, nextConfig, {
76
- webpack(config, options) {
77
- var _resolvedNextConfig$e, _resolvedNextConfig$e2;
78
- const {
79
- dir,
80
- dev,
81
- isServer,
82
- config: resolvedNextConfig
83
- } = options;
84
- const findPagesDirResult = findPagesDir.findPagesDir(dir, (_resolvedNextConfig$e = resolvedNextConfig.experimental) === null || _resolvedNextConfig$e === void 0 ? void 0 : _resolvedNextConfig$e.appDir);
85
-
86
- // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/index.ts#L336
87
- // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/webpack-config.ts#L626
88
- // https://github.com/vercel/next.js/pull/43916
89
- const hasAppDir =
90
- // on Next.js 12, findPagesDirResult is a string. on Next.js 13, findPagesDirResult is an object
91
- !!((_resolvedNextConfig$e2 = resolvedNextConfig.experimental) !== null && _resolvedNextConfig$e2 !== void 0 && _resolvedNextConfig$e2.appDir) && !!(findPagesDirResult && findPagesDirResult.appDir);
92
- const outputCss = hasAppDir ?
93
- // Always output css since Next.js App Router needs to collect Server CSS from React Server Components
94
- true :
95
- // There is no appDir, do not output css on server build
96
- !isServer;
97
- const cssRules = config.module.rules.find(rule => Array.isArray(rule.oneOf) && rule.oneOf.some(({
98
- test
99
- }) => typeof test === 'object' && typeof test.test === 'function' && test.test('filename.css'))).oneOf;
100
- cssRules.unshift({
101
- test: /\.vanilla\.css$/i,
102
- sideEffects: true,
103
- use: getVanillaExtractCssLoaders(options, resolvedNextConfig.assetPrefix)
104
- });
86
+ const createVanillaExtractPlugin = (pluginOptions = {}) => {
87
+ return (nextConfig = {}) => ({
88
+ ...nextConfig,
89
+ webpack(config, options) {
90
+ var _resolvedNextConfig$e, _resolvedNextConfig$e2, _resolvedNextConfig$e3;
91
+ const {
92
+ dir,
93
+ dev,
94
+ isServer,
95
+ config: resolvedNextConfig
96
+ } = options;
97
+
98
+ // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/index.ts#L336
99
+ // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/webpack-config.ts#L626
100
+ // https://github.com/vercel/next.js/pull/43916
101
+ // on Next.js 12, findPagesDirResult is a string. on Next.js 13, findPagesDirResult is an object
102
+ const findPagesDirResult = findPagesDir.findPagesDir(dir, (_resolvedNextConfig$e = (_resolvedNextConfig$e2 = resolvedNextConfig.experimental) === null || _resolvedNextConfig$e2 === void 0 ? void 0 : _resolvedNextConfig$e2.appDir) !== null && _resolvedNextConfig$e !== void 0 ? _resolvedNextConfig$e : false);
103
+ const hasAppDir = !!((_resolvedNextConfig$e3 = resolvedNextConfig.experimental) !== null && _resolvedNextConfig$e3 !== void 0 && _resolvedNextConfig$e3.appDir) && !!(findPagesDirResult && findPagesDirResult.appDir);
104
+ const outputCss = hasAppDir ?
105
+ // Always output css since Next.js App Router needs to collect Server CSS from React Server Components
106
+ true :
107
+ // There is no appDir, do not output css on server build
108
+ !isServer;
109
+
110
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/helpers.ts#L12-L21
111
+ const cssRules = config.module.rules.find(rule => Array.isArray(rule.oneOf) && rule.oneOf.some(({
112
+ test
113
+ }) => typeof test === 'object' && typeof test.test === 'function' && test.test('filename.css'))).oneOf;
105
114
 
106
- // vanilla-extract need to emit the css file on both server and client, both during the
107
- // development and production.
108
- // However, Next.js only add MiniCssExtractPlugin on pages dir + client build + production mode.
109
- //
110
- // To simplify the logic at our side, we will add MiniCssExtractPlugin based on
111
- // the "instanceof" check (We will only add our required MiniCssExtractPlugin if
112
- // Next.js hasn't added it yet).
113
- // This also prevent multiple MiniCssExtractPlugin being added (which will cause
114
- // RealContentHashPlugin to panic)
115
- if (!config.plugins.some(plugin => plugin instanceof NextMiniCssExtractPlugin)) {
116
- // HMR reloads the CSS file when the content changes but does not use
117
- // the new file name, which means it can't contain a hash.
118
- const filename = dev ? 'static/css/[name].css' : 'static/css/[contenthash].css';
119
- config.plugins.push(new NextMiniCssExtractPlugin({
120
- filename,
121
- chunkFilename: filename,
122
- // Next.js guarantees that CSS order "doesn't matter", due to imposed
123
- // restrictions:
124
- // 1. Global CSS can only be defined in a single entrypoint (_app)
125
- // 2. CSS Modules generate scoped class names by default and cannot
126
- // include Global CSS (:global() selector).
127
- //
128
- // While not a perfect guarantee (e.g. liberal use of `:global()`
129
- // selector), this assumption is required to code-split CSS.
130
- //
131
- // If this warning were to trigger, it'd be unactionable by the user,
132
- // but likely not valid -- so just disable it.
133
- ignoreOrder: true
115
+ // https://github.com/SukkaW/style9-webpack/blob/f51c46bbcd95ea3b988d3559c3b35cc056874366/src/next-appdir/index.ts#L187-L190
116
+ cssRules.unshift({
117
+ test: /vanilla\.virtual\.css/i,
118
+ sideEffects: true,
119
+ use: getVanillaExtractCssLoaders(options, resolvedNextConfig.assetPrefix)
120
+ });
121
+
122
+ // vanilla-extract need to emit the css file on both server and client, both during the
123
+ // development and production.
124
+ // However, Next.js only add MiniCssExtractPlugin on pages dir + client build + production mode.
125
+ //
126
+ // To simplify the logic at our side, we will add MiniCssExtractPlugin based on
127
+ // the "instanceof" check (We will only add our required MiniCssExtractPlugin if
128
+ // Next.js hasn't added it yet).
129
+ // This also prevent multiple MiniCssExtractPlugin being added (which will cause
130
+ // RealContentHashPlugin to panic)
131
+ if (!config.plugins.some(p => p instanceof NextMiniCssExtractPlugin)) {
132
+ // HMR reloads the CSS file when the content changes but does not use
133
+ // the new file name, which means it can't contain a hash.
134
+ const filename = dev ? 'static/css/[name].css' : 'static/css/[contenthash].css';
135
+ config.plugins.push(new NextMiniCssExtractPlugin({
136
+ filename,
137
+ chunkFilename: filename,
138
+ // Next.js guarantees that CSS order "doesn't matter", due to imposed
139
+ // restrictions:
140
+ // 1. Global CSS can only be defined in a single entrypoint (_app)
141
+ // 2. CSS Modules generate scoped class names by default and cannot
142
+ // include Global CSS (:global() selector).
143
+ //
144
+ // While not a perfect guarantee (e.g. liberal use of `:global()`
145
+ // selector), this assumption is required to code-split CSS.
146
+ //
147
+ // If this warning were to trigger, it'd be unactionable by the user,
148
+ // but likely not valid -- so just disable it.
149
+ ignoreOrder: true
150
+ }));
151
+ }
152
+ config.plugins.push(new next.VanillaExtractPlugin({
153
+ outputCss,
154
+ ...pluginOptions
134
155
  }));
156
+ if (typeof nextConfig.webpack === 'function') {
157
+ return nextConfig.webpack(config, options);
158
+ }
159
+ return config;
135
160
  }
136
- config.plugins.push(new webpackPlugin.VanillaExtractPlugin({
137
- outputCss,
138
- ...pluginOptions
139
- }));
140
- if (typeof nextConfig.webpack === 'function') {
141
- return nextConfig.webpack(config, options);
142
- }
143
- return config;
144
- }
145
- });
161
+ });
162
+ };
146
163
 
147
164
  exports.createVanillaExtractPlugin = createVanillaExtractPlugin;
@@ -2,36 +2,42 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var webpackPlugin = require('@vanilla-extract/webpack-plugin');
6
- var browserslist = require('browserslist');
7
- var css = require('next/dist/build/webpack/config/blocks/css');
5
+ var browserslist = require('next/dist/compiled/browserslist');
6
+ var NextMiniCssExtractPluginDefault = require('next/dist/build/webpack/plugins/mini-css-extract-plugin');
7
+ var next = require('@vanilla-extract/webpack-plugin/next');
8
8
  var findPagesDir = require('next/dist/lib/find-pages-dir');
9
+ var css = require('next/dist/build/webpack/config/blocks/css');
9
10
  var fileResolve = require('next/dist/build/webpack/config/blocks/css/loaders/file-resolve');
10
- var NextMiniCssExtractPluginDefault = require('next/dist/build/webpack/plugins/mini-css-extract-plugin');
11
11
 
12
12
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
13
13
 
14
14
  var browserslist__default = /*#__PURE__*/_interopDefault(browserslist);
15
15
  var NextMiniCssExtractPluginDefault__default = /*#__PURE__*/_interopDefault(NextMiniCssExtractPluginDefault);
16
16
 
17
+ // @ts-expect-error
17
18
  const NextMiniCssExtractPlugin = NextMiniCssExtractPluginDefault__default["default"];
19
+
20
+ // Adopted from https://github.com/vercel/next.js/blob/1f1632979c78b3edfe59fd85d8cce62efcdee688/packages/next/build/webpack-config.ts#L60-L72
18
21
  function getSupportedBrowsers(dir, isDevelopment) {
19
- let browsers;
20
22
  try {
21
- browsers = browserslist__default["default"].loadConfig({
23
+ return browserslist__default["default"].loadConfig({
22
24
  path: dir,
23
25
  env: isDevelopment ? 'development' : 'production'
24
26
  });
25
- } catch {}
26
- return browsers;
27
+ } catch (_) {
28
+ return undefined;
29
+ }
27
30
  }
28
- // https://github.com/vercel/next.js/blob/canary/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L7
31
+
32
+ // Adopt from Next.js' getGlobalCssLoader
33
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L7
29
34
  const getVanillaExtractCssLoaders = (options, assetPrefix) => {
30
35
  const loaders = [];
31
36
 
32
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L14
37
+ // Adopt from Next.js' getClientStyleLoader
38
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L3
33
39
  if (!options.isServer) {
34
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L44
40
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L44
35
41
  // next-style-loader will mess up css order in development mode.
36
42
  // Next.js appDir doesn't use next-style-loader either.
37
43
  // So we always use css-loader here, to simplify things and get proper order of output CSS
@@ -45,7 +51,7 @@ const getVanillaExtractCssLoaders = (options, assetPrefix) => {
45
51
  }
46
52
  const postcss = () => css.lazyPostCSS(options.dir, getSupportedBrowsers(options.dir, options.dev), undefined);
47
53
 
48
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L28
54
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L28
49
55
  loaders.push({
50
56
  loader: require.resolve('next/dist/build/webpack/loaders/css-loader/src'),
51
57
  options: {
@@ -63,85 +69,96 @@ const getVanillaExtractCssLoaders = (options, assetPrefix) => {
63
69
  }
64
70
  });
65
71
 
66
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L43
72
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L29-L38
67
73
  loaders.push({
68
74
  loader: require.resolve('next/dist/build/webpack/loaders/postcss-loader/src'),
69
75
  options: {
70
76
  postcss
71
77
  }
72
78
  });
79
+
80
+ // https://github.com/SukkaW/style9-webpack/blob/f51c46bbcd95ea3b988d3559c3b35cc056874366/src/next-appdir/index.ts#L103-L105
81
+ loaders.push({
82
+ loader: next.VanillaExtractPlugin.loader
83
+ });
73
84
  return loaders;
74
85
  };
75
- const createVanillaExtractPlugin = (pluginOptions = {}) => (nextConfig = {}) => Object.assign({}, nextConfig, {
76
- webpack(config, options) {
77
- var _resolvedNextConfig$e, _resolvedNextConfig$e2;
78
- const {
79
- dir,
80
- dev,
81
- isServer,
82
- config: resolvedNextConfig
83
- } = options;
84
- const findPagesDirResult = findPagesDir.findPagesDir(dir, (_resolvedNextConfig$e = resolvedNextConfig.experimental) === null || _resolvedNextConfig$e === void 0 ? void 0 : _resolvedNextConfig$e.appDir);
85
-
86
- // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/index.ts#L336
87
- // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/webpack-config.ts#L626
88
- // https://github.com/vercel/next.js/pull/43916
89
- const hasAppDir =
90
- // on Next.js 12, findPagesDirResult is a string. on Next.js 13, findPagesDirResult is an object
91
- !!((_resolvedNextConfig$e2 = resolvedNextConfig.experimental) !== null && _resolvedNextConfig$e2 !== void 0 && _resolvedNextConfig$e2.appDir) && !!(findPagesDirResult && findPagesDirResult.appDir);
92
- const outputCss = hasAppDir ?
93
- // Always output css since Next.js App Router needs to collect Server CSS from React Server Components
94
- true :
95
- // There is no appDir, do not output css on server build
96
- !isServer;
97
- const cssRules = config.module.rules.find(rule => Array.isArray(rule.oneOf) && rule.oneOf.some(({
98
- test
99
- }) => typeof test === 'object' && typeof test.test === 'function' && test.test('filename.css'))).oneOf;
100
- cssRules.unshift({
101
- test: /\.vanilla\.css$/i,
102
- sideEffects: true,
103
- use: getVanillaExtractCssLoaders(options, resolvedNextConfig.assetPrefix)
104
- });
86
+ const createVanillaExtractPlugin = (pluginOptions = {}) => {
87
+ return (nextConfig = {}) => ({
88
+ ...nextConfig,
89
+ webpack(config, options) {
90
+ var _resolvedNextConfig$e, _resolvedNextConfig$e2, _resolvedNextConfig$e3;
91
+ const {
92
+ dir,
93
+ dev,
94
+ isServer,
95
+ config: resolvedNextConfig
96
+ } = options;
97
+
98
+ // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/index.ts#L336
99
+ // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/webpack-config.ts#L626
100
+ // https://github.com/vercel/next.js/pull/43916
101
+ // on Next.js 12, findPagesDirResult is a string. on Next.js 13, findPagesDirResult is an object
102
+ const findPagesDirResult = findPagesDir.findPagesDir(dir, (_resolvedNextConfig$e = (_resolvedNextConfig$e2 = resolvedNextConfig.experimental) === null || _resolvedNextConfig$e2 === void 0 ? void 0 : _resolvedNextConfig$e2.appDir) !== null && _resolvedNextConfig$e !== void 0 ? _resolvedNextConfig$e : false);
103
+ const hasAppDir = !!((_resolvedNextConfig$e3 = resolvedNextConfig.experimental) !== null && _resolvedNextConfig$e3 !== void 0 && _resolvedNextConfig$e3.appDir) && !!(findPagesDirResult && findPagesDirResult.appDir);
104
+ const outputCss = hasAppDir ?
105
+ // Always output css since Next.js App Router needs to collect Server CSS from React Server Components
106
+ true :
107
+ // There is no appDir, do not output css on server build
108
+ !isServer;
109
+
110
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/helpers.ts#L12-L21
111
+ const cssRules = config.module.rules.find(rule => Array.isArray(rule.oneOf) && rule.oneOf.some(({
112
+ test
113
+ }) => typeof test === 'object' && typeof test.test === 'function' && test.test('filename.css'))).oneOf;
105
114
 
106
- // vanilla-extract need to emit the css file on both server and client, both during the
107
- // development and production.
108
- // However, Next.js only add MiniCssExtractPlugin on pages dir + client build + production mode.
109
- //
110
- // To simplify the logic at our side, we will add MiniCssExtractPlugin based on
111
- // the "instanceof" check (We will only add our required MiniCssExtractPlugin if
112
- // Next.js hasn't added it yet).
113
- // This also prevent multiple MiniCssExtractPlugin being added (which will cause
114
- // RealContentHashPlugin to panic)
115
- if (!config.plugins.some(plugin => plugin instanceof NextMiniCssExtractPlugin)) {
116
- // HMR reloads the CSS file when the content changes but does not use
117
- // the new file name, which means it can't contain a hash.
118
- const filename = dev ? 'static/css/[name].css' : 'static/css/[contenthash].css';
119
- config.plugins.push(new NextMiniCssExtractPlugin({
120
- filename,
121
- chunkFilename: filename,
122
- // Next.js guarantees that CSS order "doesn't matter", due to imposed
123
- // restrictions:
124
- // 1. Global CSS can only be defined in a single entrypoint (_app)
125
- // 2. CSS Modules generate scoped class names by default and cannot
126
- // include Global CSS (:global() selector).
127
- //
128
- // While not a perfect guarantee (e.g. liberal use of `:global()`
129
- // selector), this assumption is required to code-split CSS.
130
- //
131
- // If this warning were to trigger, it'd be unactionable by the user,
132
- // but likely not valid -- so just disable it.
133
- ignoreOrder: true
115
+ // https://github.com/SukkaW/style9-webpack/blob/f51c46bbcd95ea3b988d3559c3b35cc056874366/src/next-appdir/index.ts#L187-L190
116
+ cssRules.unshift({
117
+ test: /vanilla\.virtual\.css/i,
118
+ sideEffects: true,
119
+ use: getVanillaExtractCssLoaders(options, resolvedNextConfig.assetPrefix)
120
+ });
121
+
122
+ // vanilla-extract need to emit the css file on both server and client, both during the
123
+ // development and production.
124
+ // However, Next.js only add MiniCssExtractPlugin on pages dir + client build + production mode.
125
+ //
126
+ // To simplify the logic at our side, we will add MiniCssExtractPlugin based on
127
+ // the "instanceof" check (We will only add our required MiniCssExtractPlugin if
128
+ // Next.js hasn't added it yet).
129
+ // This also prevent multiple MiniCssExtractPlugin being added (which will cause
130
+ // RealContentHashPlugin to panic)
131
+ if (!config.plugins.some(p => p instanceof NextMiniCssExtractPlugin)) {
132
+ // HMR reloads the CSS file when the content changes but does not use
133
+ // the new file name, which means it can't contain a hash.
134
+ const filename = dev ? 'static/css/[name].css' : 'static/css/[contenthash].css';
135
+ config.plugins.push(new NextMiniCssExtractPlugin({
136
+ filename,
137
+ chunkFilename: filename,
138
+ // Next.js guarantees that CSS order "doesn't matter", due to imposed
139
+ // restrictions:
140
+ // 1. Global CSS can only be defined in a single entrypoint (_app)
141
+ // 2. CSS Modules generate scoped class names by default and cannot
142
+ // include Global CSS (:global() selector).
143
+ //
144
+ // While not a perfect guarantee (e.g. liberal use of `:global()`
145
+ // selector), this assumption is required to code-split CSS.
146
+ //
147
+ // If this warning were to trigger, it'd be unactionable by the user,
148
+ // but likely not valid -- so just disable it.
149
+ ignoreOrder: true
150
+ }));
151
+ }
152
+ config.plugins.push(new next.VanillaExtractPlugin({
153
+ outputCss,
154
+ ...pluginOptions
134
155
  }));
156
+ if (typeof nextConfig.webpack === 'function') {
157
+ return nextConfig.webpack(config, options);
158
+ }
159
+ return config;
135
160
  }
136
- config.plugins.push(new webpackPlugin.VanillaExtractPlugin({
137
- outputCss,
138
- ...pluginOptions
139
- }));
140
- if (typeof nextConfig.webpack === 'function') {
141
- return nextConfig.webpack(config, options);
142
- }
143
- return config;
144
- }
145
- });
161
+ });
162
+ };
146
163
 
147
164
  exports.createVanillaExtractPlugin = createVanillaExtractPlugin;
@@ -1,28 +1,34 @@
1
- import { VanillaExtractPlugin } from '@vanilla-extract/webpack-plugin';
2
- import browserslist from 'browserslist';
3
- import { lazyPostCSS } from 'next/dist/build/webpack/config/blocks/css';
1
+ import browserslist from 'next/dist/compiled/browserslist';
2
+ import NextMiniCssExtractPluginDefault from 'next/dist/build/webpack/plugins/mini-css-extract-plugin';
3
+ import { VanillaExtractPlugin } from '@vanilla-extract/webpack-plugin/next';
4
4
  import { findPagesDir } from 'next/dist/lib/find-pages-dir';
5
+ import { lazyPostCSS } from 'next/dist/build/webpack/config/blocks/css';
5
6
  import { cssFileResolve } from 'next/dist/build/webpack/config/blocks/css/loaders/file-resolve';
6
- import NextMiniCssExtractPluginDefault from 'next/dist/build/webpack/plugins/mini-css-extract-plugin';
7
7
 
8
+ // @ts-expect-error
8
9
  const NextMiniCssExtractPlugin = NextMiniCssExtractPluginDefault;
10
+
11
+ // Adopted from https://github.com/vercel/next.js/blob/1f1632979c78b3edfe59fd85d8cce62efcdee688/packages/next/build/webpack-config.ts#L60-L72
9
12
  function getSupportedBrowsers(dir, isDevelopment) {
10
- let browsers;
11
13
  try {
12
- browsers = browserslist.loadConfig({
14
+ return browserslist.loadConfig({
13
15
  path: dir,
14
16
  env: isDevelopment ? 'development' : 'production'
15
17
  });
16
- } catch {}
17
- return browsers;
18
+ } catch (_) {
19
+ return undefined;
20
+ }
18
21
  }
19
- // https://github.com/vercel/next.js/blob/canary/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L7
22
+
23
+ // Adopt from Next.js' getGlobalCssLoader
24
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L7
20
25
  const getVanillaExtractCssLoaders = (options, assetPrefix) => {
21
26
  const loaders = [];
22
27
 
23
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L14
28
+ // Adopt from Next.js' getClientStyleLoader
29
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L3
24
30
  if (!options.isServer) {
25
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L44
31
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/client.ts#L44
26
32
  // next-style-loader will mess up css order in development mode.
27
33
  // Next.js appDir doesn't use next-style-loader either.
28
34
  // So we always use css-loader here, to simplify things and get proper order of output CSS
@@ -36,7 +42,7 @@ const getVanillaExtractCssLoaders = (options, assetPrefix) => {
36
42
  }
37
43
  const postcss = () => lazyPostCSS(options.dir, getSupportedBrowsers(options.dir, options.dev), undefined);
38
44
 
39
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L28
45
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L28
40
46
  loaders.push({
41
47
  loader: require.resolve('next/dist/build/webpack/loaders/css-loader/src'),
42
48
  options: {
@@ -54,85 +60,96 @@ const getVanillaExtractCssLoaders = (options, assetPrefix) => {
54
60
  }
55
61
  });
56
62
 
57
- // https://github.com/vercel/next.js/blob/a4f2bbbe2047d4ed88e9b6f32f6b0adfc8d0c46a/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L43
63
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/blocks/css/loaders/global.ts#L29-L38
58
64
  loaders.push({
59
65
  loader: require.resolve('next/dist/build/webpack/loaders/postcss-loader/src'),
60
66
  options: {
61
67
  postcss
62
68
  }
63
69
  });
70
+
71
+ // https://github.com/SukkaW/style9-webpack/blob/f51c46bbcd95ea3b988d3559c3b35cc056874366/src/next-appdir/index.ts#L103-L105
72
+ loaders.push({
73
+ loader: VanillaExtractPlugin.loader
74
+ });
64
75
  return loaders;
65
76
  };
66
- const createVanillaExtractPlugin = (pluginOptions = {}) => (nextConfig = {}) => Object.assign({}, nextConfig, {
67
- webpack(config, options) {
68
- var _resolvedNextConfig$e, _resolvedNextConfig$e2;
69
- const {
70
- dir,
71
- dev,
72
- isServer,
73
- config: resolvedNextConfig
74
- } = options;
75
- const findPagesDirResult = findPagesDir(dir, (_resolvedNextConfig$e = resolvedNextConfig.experimental) === null || _resolvedNextConfig$e === void 0 ? void 0 : _resolvedNextConfig$e.appDir);
77
+ const createVanillaExtractPlugin = (pluginOptions = {}) => {
78
+ return (nextConfig = {}) => ({
79
+ ...nextConfig,
80
+ webpack(config, options) {
81
+ var _resolvedNextConfig$e, _resolvedNextConfig$e2, _resolvedNextConfig$e3;
82
+ const {
83
+ dir,
84
+ dev,
85
+ isServer,
86
+ config: resolvedNextConfig
87
+ } = options;
76
88
 
77
- // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/index.ts#L336
78
- // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/webpack-config.ts#L626
79
- // https://github.com/vercel/next.js/pull/43916
80
- const hasAppDir =
81
- // on Next.js 12, findPagesDirResult is a string. on Next.js 13, findPagesDirResult is an object
82
- !!((_resolvedNextConfig$e2 = resolvedNextConfig.experimental) !== null && _resolvedNextConfig$e2 !== void 0 && _resolvedNextConfig$e2.appDir) && !!(findPagesDirResult && findPagesDirResult.appDir);
83
- const outputCss = hasAppDir ?
84
- // Always output css since Next.js App Router needs to collect Server CSS from React Server Components
85
- true :
86
- // There is no appDir, do not output css on server build
87
- !isServer;
88
- const cssRules = config.module.rules.find(rule => Array.isArray(rule.oneOf) && rule.oneOf.some(({
89
- test
90
- }) => typeof test === 'object' && typeof test.test === 'function' && test.test('filename.css'))).oneOf;
91
- cssRules.unshift({
92
- test: /\.vanilla\.css$/i,
93
- sideEffects: true,
94
- use: getVanillaExtractCssLoaders(options, resolvedNextConfig.assetPrefix)
95
- });
89
+ // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/index.ts#L336
90
+ // https://github.com/vercel/next.js/blob/1fb4cad2a8329811b5ccde47217b4a6ae739124e/packages/next/build/webpack-config.ts#L626
91
+ // https://github.com/vercel/next.js/pull/43916
92
+ // on Next.js 12, findPagesDirResult is a string. on Next.js 13, findPagesDirResult is an object
93
+ const findPagesDirResult = findPagesDir(dir, (_resolvedNextConfig$e = (_resolvedNextConfig$e2 = resolvedNextConfig.experimental) === null || _resolvedNextConfig$e2 === void 0 ? void 0 : _resolvedNextConfig$e2.appDir) !== null && _resolvedNextConfig$e !== void 0 ? _resolvedNextConfig$e : false);
94
+ const hasAppDir = !!((_resolvedNextConfig$e3 = resolvedNextConfig.experimental) !== null && _resolvedNextConfig$e3 !== void 0 && _resolvedNextConfig$e3.appDir) && !!(findPagesDirResult && findPagesDirResult.appDir);
95
+ const outputCss = hasAppDir ?
96
+ // Always output css since Next.js App Router needs to collect Server CSS from React Server Components
97
+ true :
98
+ // There is no appDir, do not output css on server build
99
+ !isServer;
100
+
101
+ // https://github.com/vercel/next.js/blob/6e5b935fd7a61497f6854a81aec7df3a5dbf61ac/packages/next/src/build/webpack/config/helpers.ts#L12-L21
102
+ const cssRules = config.module.rules.find(rule => Array.isArray(rule.oneOf) && rule.oneOf.some(({
103
+ test
104
+ }) => typeof test === 'object' && typeof test.test === 'function' && test.test('filename.css'))).oneOf;
105
+
106
+ // https://github.com/SukkaW/style9-webpack/blob/f51c46bbcd95ea3b988d3559c3b35cc056874366/src/next-appdir/index.ts#L187-L190
107
+ cssRules.unshift({
108
+ test: /vanilla\.virtual\.css/i,
109
+ sideEffects: true,
110
+ use: getVanillaExtractCssLoaders(options, resolvedNextConfig.assetPrefix)
111
+ });
96
112
 
97
- // vanilla-extract need to emit the css file on both server and client, both during the
98
- // development and production.
99
- // However, Next.js only add MiniCssExtractPlugin on pages dir + client build + production mode.
100
- //
101
- // To simplify the logic at our side, we will add MiniCssExtractPlugin based on
102
- // the "instanceof" check (We will only add our required MiniCssExtractPlugin if
103
- // Next.js hasn't added it yet).
104
- // This also prevent multiple MiniCssExtractPlugin being added (which will cause
105
- // RealContentHashPlugin to panic)
106
- if (!config.plugins.some(plugin => plugin instanceof NextMiniCssExtractPlugin)) {
107
- // HMR reloads the CSS file when the content changes but does not use
108
- // the new file name, which means it can't contain a hash.
109
- const filename = dev ? 'static/css/[name].css' : 'static/css/[contenthash].css';
110
- config.plugins.push(new NextMiniCssExtractPlugin({
111
- filename,
112
- chunkFilename: filename,
113
- // Next.js guarantees that CSS order "doesn't matter", due to imposed
114
- // restrictions:
115
- // 1. Global CSS can only be defined in a single entrypoint (_app)
116
- // 2. CSS Modules generate scoped class names by default and cannot
117
- // include Global CSS (:global() selector).
118
- //
119
- // While not a perfect guarantee (e.g. liberal use of `:global()`
120
- // selector), this assumption is required to code-split CSS.
121
- //
122
- // If this warning were to trigger, it'd be unactionable by the user,
123
- // but likely not valid -- so just disable it.
124
- ignoreOrder: true
113
+ // vanilla-extract need to emit the css file on both server and client, both during the
114
+ // development and production.
115
+ // However, Next.js only add MiniCssExtractPlugin on pages dir + client build + production mode.
116
+ //
117
+ // To simplify the logic at our side, we will add MiniCssExtractPlugin based on
118
+ // the "instanceof" check (We will only add our required MiniCssExtractPlugin if
119
+ // Next.js hasn't added it yet).
120
+ // This also prevent multiple MiniCssExtractPlugin being added (which will cause
121
+ // RealContentHashPlugin to panic)
122
+ if (!config.plugins.some(p => p instanceof NextMiniCssExtractPlugin)) {
123
+ // HMR reloads the CSS file when the content changes but does not use
124
+ // the new file name, which means it can't contain a hash.
125
+ const filename = dev ? 'static/css/[name].css' : 'static/css/[contenthash].css';
126
+ config.plugins.push(new NextMiniCssExtractPlugin({
127
+ filename,
128
+ chunkFilename: filename,
129
+ // Next.js guarantees that CSS order "doesn't matter", due to imposed
130
+ // restrictions:
131
+ // 1. Global CSS can only be defined in a single entrypoint (_app)
132
+ // 2. CSS Modules generate scoped class names by default and cannot
133
+ // include Global CSS (:global() selector).
134
+ //
135
+ // While not a perfect guarantee (e.g. liberal use of `:global()`
136
+ // selector), this assumption is required to code-split CSS.
137
+ //
138
+ // If this warning were to trigger, it'd be unactionable by the user,
139
+ // but likely not valid -- so just disable it.
140
+ ignoreOrder: true
141
+ }));
142
+ }
143
+ config.plugins.push(new VanillaExtractPlugin({
144
+ outputCss,
145
+ ...pluginOptions
125
146
  }));
147
+ if (typeof nextConfig.webpack === 'function') {
148
+ return nextConfig.webpack(config, options);
149
+ }
150
+ return config;
126
151
  }
127
- config.plugins.push(new VanillaExtractPlugin({
128
- outputCss,
129
- ...pluginOptions
130
- }));
131
- if (typeof nextConfig.webpack === 'function') {
132
- return nextConfig.webpack(config, options);
133
- }
134
- return config;
135
- }
136
- });
152
+ });
153
+ };
137
154
 
138
155
  export { createVanillaExtractPlugin };
package/package.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
2
  "name": "@vanilla-extract/next-plugin",
3
- "version": "2.2.2",
3
+ "version": "2.3.1",
4
4
  "description": "Zero-runtime Stylesheets-in-TypeScript",
5
5
  "main": "dist/vanilla-extract-next-plugin.cjs.js",
6
6
  "module": "dist/vanilla-extract-next-plugin.esm.js",
7
+ "preconstruct": {
8
+ "entrypoints": [
9
+ "index.ts"
10
+ ]
11
+ },
7
12
  "files": [
8
13
  "/dist"
9
14
  ],
@@ -15,8 +20,7 @@
15
20
  "author": "SEEK",
16
21
  "license": "MIT",
17
22
  "dependencies": {
18
- "@vanilla-extract/webpack-plugin": "^2.2.0",
19
- "browserslist": "^4.19.1"
23
+ "@vanilla-extract/webpack-plugin": "^2.3.1"
20
24
  },
21
25
  "peerDependencies": {
22
26
  "next": ">=12.1.7"