@vanilla-extract/next-plugin 0.0.0-inline-prototype-202342012537 → 0.0.0-vite-plugin-new-20231229135000

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