@wyw-in-js/nextjs 1.1.0 → 2.0.0-alpha.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/esm/index.mjs CHANGED
@@ -1,382 +1,302 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- const DEFAULT_EXTENSION = '.wyw-in-js.module.css';
4
- const DEFAULT_TURBO_RULE_KEYS = ['*.js', '*.jsx', '*.ts', '*.tsx'];
1
+ import { createRequire } from "module";
2
+ import { mergeOxcResolverAlias, toNativeResolverAlias } from "@wyw-in-js/shared";
3
+ const DEFAULT_EXTENSION = ".wyw-in-js.module.css";
4
+ const DEFAULT_TURBO_RULE_KEYS = [
5
+ "*.js",
6
+ "*.jsx",
7
+ "*.ts",
8
+ "*.tsx"
9
+ ];
5
10
  const DEFAULT_REACT_IMPORT_OVERRIDES = {
6
- react: {
7
- mock: 'react'
8
- },
9
- 'react/jsx-runtime': {
10
- mock: 'react/jsx-runtime'
11
- },
12
- 'react/jsx-dev-runtime': {
13
- mock: 'react/jsx-dev-runtime'
14
- }
11
+ react: { mock: "react" },
12
+ "react/jsx-runtime": { mock: "react/jsx-runtime" },
13
+ "react/jsx-dev-runtime": { mock: "react/jsx-dev-runtime" }
15
14
  };
16
- const PLACEHOLDER_EXTENSIONS = new Set(['.js', '.jsx', '.ts', '.tsx']);
17
- const PLACEHOLDER_IGNORED_DIRS = new Set(['.git', '.next', '.turbo', 'node_modules']);
15
+ const nodeRequire = createRequire(import.meta.url);
18
16
  function isObject(value) {
19
- return typeof value === 'object' && value !== null;
17
+ return typeof value === "object" && value !== null;
20
18
  }
21
19
  function isUseLoaderObject(item) {
22
- return isObject(item) && typeof item.loader === 'string';
20
+ return isObject(item) && typeof item.loader === "string";
23
21
  }
24
22
  function normalizeUseItems(use) {
25
- if (!use) return null;
26
- if (typeof use === 'function') return null;
27
- const list = (Array.isArray(use) ? use : [use]).filter(Boolean);
28
- return list.length ? list : null;
23
+ if (!use) return null;
24
+ if (typeof use === "function") return null;
25
+ const list = (Array.isArray(use) ? use : [use]).filter(Boolean);
26
+ return list.length ? list : null;
29
27
  }
30
28
  function getLoaderName(item) {
31
- if (typeof item === 'string') return item;
32
- if (isUseLoaderObject(item)) return item.loader;
33
- return '';
29
+ if (typeof item === "string") return item;
30
+ if (isUseLoaderObject(item)) return item.loader;
31
+ return "";
34
32
  }
35
33
  function isWywLoaderPath(loader) {
36
- return loader.includes('@wyw-in-js/webpack-loader') || /[\\/]webpack-loader[\\/]/.test(loader);
34
+ return loader.includes("@wyw-in-js/webpack-loader") || /[\\/]webpack-loader[\\/]/.test(loader);
37
35
  }
38
36
  function convertLoaderRuleToUseRule(rule, wywLoaderItem) {
39
- const {
40
- loader
41
- } = rule;
42
- if (typeof loader !== 'string') return;
43
- const alreadyInjected = isWywLoaderPath(loader);
44
- if (alreadyInjected) return;
45
- const isNextJsTranspileRule = ['next-swc-loader', 'next-babel-loader', 'babel-loader'].some(needle => loader.includes(needle));
46
- if (!isNextJsTranspileRule) return;
47
- const {
48
- options
49
- } = rule;
50
- const nextRule = rule;
51
- delete nextRule.loader;
52
- delete nextRule.options;
53
-
54
- // Loader order is right-to-left. We want WyW to run first, so it should be last.
55
- Object.assign(nextRule, {
56
- use: [{
57
- loader,
58
- ...(options !== undefined ? {
59
- options
60
- } : {})
61
- }, wywLoaderItem]
62
- });
37
+ const { loader } = rule;
38
+ if (typeof loader !== "string") return;
39
+ const alreadyInjected = isWywLoaderPath(loader);
40
+ if (alreadyInjected) return;
41
+ const isNextJsTranspileRule = [
42
+ "next-swc-loader",
43
+ "next-babel-loader",
44
+ "babel-loader"
45
+ ].some((needle) => loader.includes(needle));
46
+ if (!isNextJsTranspileRule) return;
47
+ const { options } = rule;
48
+ const nextRule = rule;
49
+ delete nextRule.loader;
50
+ delete nextRule.options;
51
+ // Loader order is right-to-left. We want WyW to run first, so it should be last.
52
+ Object.assign(nextRule, { use: [{
53
+ loader,
54
+ ...options !== undefined ? { options } : {}
55
+ }, wywLoaderItem] });
63
56
  }
64
57
  function traverseRules(rules, visitor) {
65
- for (const rule of rules) {
66
- if (rule && typeof rule === 'object') {
67
- visitor(rule);
68
- if (Array.isArray(rule.oneOf)) {
69
- traverseRules(rule.oneOf, visitor);
70
- }
71
- if (Array.isArray(rule.rules)) {
72
- traverseRules(rule.rules, visitor);
73
- }
74
- }
75
- }
58
+ for (const rule of rules) {
59
+ if (rule && typeof rule === "object") {
60
+ visitor(rule);
61
+ if (Array.isArray(rule.oneOf)) {
62
+ traverseRules(rule.oneOf, visitor);
63
+ }
64
+ if (Array.isArray(rule.rules)) {
65
+ traverseRules(rule.rules, visitor);
66
+ }
67
+ }
68
+ }
76
69
  }
77
70
  function escapeRegExp(value) {
78
- return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
71
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
79
72
  }
80
73
  function isPlainObject(value) {
81
- if (!isObject(value)) return false;
82
- const proto = Object.getPrototypeOf(value);
83
- return proto === Object.prototype || proto === null;
74
+ if (!isObject(value)) return false;
75
+ const proto = Object.getPrototypeOf(value);
76
+ return proto === Object.prototype || proto === null;
84
77
  }
85
- function assertNoFunctions(value, name) {
86
- const queue = [{
87
- path: name,
88
- value
89
- }];
90
- const seen = new Set();
91
- while (queue.length) {
92
- const current = queue.shift();
93
- if (typeof current.value === 'function') {
94
- throw new Error(`${current.path} must be JSON-serializable (functions are not supported in Turbopack loader options). Use "configFile" to pass non-JSON config.`);
95
- }
96
- if (current.value === null) {
97
- // skip
98
- } else if (Array.isArray(current.value)) {
99
- if (!seen.has(current.value)) {
100
- seen.add(current.value);
101
- current.value.forEach((item, idx) => queue.push({
102
- path: `${current.path}[${idx}]`,
103
- value: item
104
- }));
105
- }
106
- } else if (isPlainObject(current.value)) {
107
- if (!seen.has(current.value)) {
108
- seen.add(current.value);
109
- Object.entries(current.value).forEach(([key, item]) => queue.push({
110
- path: `${current.path}.${key}`,
111
- value: item
112
- }));
113
- }
114
- }
115
- }
78
+ function assertJsonSerializable(value, name) {
79
+ const queue = [{
80
+ path: name,
81
+ value
82
+ }];
83
+ const seen = new Set();
84
+ while (queue.length) {
85
+ const current = queue.shift();
86
+ if (current.value === null) {} else if (typeof current.value === "undefined") {} else if (typeof current.value === "string" || typeof current.value === "number" || typeof current.value === "boolean") {} else if (typeof current.value === "function" || typeof current.value === "symbol" || typeof current.value === "bigint") {
87
+ throw new Error(`${current.path} must be JSON-serializable (functions, symbols and bigint are not supported in Turbopack loader options). Use "configFile" to pass non-JSON config.`);
88
+ } else if (Array.isArray(current.value)) {
89
+ if (!seen.has(current.value)) {
90
+ seen.add(current.value);
91
+ current.value.forEach((item, idx) => queue.push({
92
+ path: `${current.path}[${idx}]`,
93
+ value: item
94
+ }));
95
+ }
96
+ } else if (isPlainObject(current.value)) {
97
+ if (!seen.has(current.value)) {
98
+ seen.add(current.value);
99
+ Object.entries(current.value).forEach(([key, item]) => queue.push({
100
+ path: `${current.path}.${key}`,
101
+ value: item
102
+ }));
103
+ }
104
+ } else {
105
+ throw new Error(`${current.path} must be JSON-serializable (only plain objects, arrays, and primitives are supported in Turbopack loader options). Use "configFile" to pass non-JSON config.`);
106
+ }
107
+ }
116
108
  }
117
109
  function createWywCssModuleRule(baseRule, extensionSuffix) {
118
- const use = normalizeUseItems(baseRule.use) ?? [];
119
- const patchedUse = use.map(item => {
120
- if (!isUseLoaderObject(item) || !item.loader.includes('css-loader')) {
121
- return item;
122
- }
123
- const itemOptions = item.options;
124
- if (!isObject(itemOptions)) {
125
- return item;
126
- }
127
- const {
128
- modules
129
- } = itemOptions;
130
- if (!isObject(modules)) {
131
- return item;
132
- }
133
- const nextModules = {
134
- ...modules,
135
- mode: 'global',
136
- getLocalIdent: (_context, _localIdentName, localName) => localName
137
- };
138
- return {
139
- ...item,
140
- options: {
141
- ...itemOptions,
142
- modules: nextModules
143
- }
144
- };
145
- });
146
- const nextRule = {
147
- ...baseRule,
148
- sideEffects: true,
149
- test: new RegExp(`${escapeRegExp(extensionSuffix)}$`),
150
- use: patchedUse
151
- };
152
- return nextRule;
110
+ const use = normalizeUseItems(baseRule.use) ?? [];
111
+ const patchedUse = use.map((item) => {
112
+ if (!isUseLoaderObject(item) || !item.loader.includes("css-loader")) {
113
+ return item;
114
+ }
115
+ const itemOptions = item.options;
116
+ if (!isObject(itemOptions)) {
117
+ return item;
118
+ }
119
+ const { modules } = itemOptions;
120
+ if (!isObject(modules)) {
121
+ return item;
122
+ }
123
+ const nextModules = {
124
+ ...modules,
125
+ mode: "global",
126
+ getLocalIdent: (_context, _localIdentName, localName) => localName
127
+ };
128
+ return {
129
+ ...item,
130
+ options: {
131
+ ...itemOptions,
132
+ modules: nextModules
133
+ }
134
+ };
135
+ });
136
+ const nextRule = {
137
+ ...baseRule,
138
+ sideEffects: true,
139
+ test: new RegExp(`${escapeRegExp(extensionSuffix)}$`),
140
+ use: patchedUse
141
+ };
142
+ return nextRule;
153
143
  }
154
144
  function ensureWywCssModuleRules(config, extensionSuffix) {
155
- traverseRules(config.module?.rules ?? [], rule => {
156
- if (!Array.isArray(rule.oneOf) || rule.oneOf.length === 0) {
157
- return;
158
- }
159
- const expectedTestSource = `${escapeRegExp(extensionSuffix)}$`;
160
- const alreadyPresent = rule.oneOf.some(candidate => {
161
- if (!candidate || typeof candidate !== 'object') return false;
162
- const {
163
- test
164
- } = candidate;
165
- return test instanceof RegExp && test.source === expectedTestSource;
166
- });
167
- if (alreadyPresent) {
168
- return;
169
- }
170
- const oneOf = rule.oneOf;
171
- for (let idx = 0; idx < oneOf.length; idx += 1) {
172
- const candidate = oneOf[idx];
173
- if (candidate && typeof candidate === 'object') {
174
- const candidateRule = candidate;
175
- const {
176
- test
177
- } = candidateRule;
178
- const isModuleCssRule = test instanceof RegExp && test.source.includes('\\.module\\.css');
179
- if (isModuleCssRule) {
180
- const use = normalizeUseItems(candidateRule.use);
181
- if (use) {
182
- const hasCssLoader = use.some(item => isUseLoaderObject(item) && item.loader.includes('css-loader'));
183
- if (hasCssLoader) {
184
- oneOf.splice(idx, 0, createWywCssModuleRule(candidateRule, extensionSuffix));
185
- break;
186
- }
187
- }
188
- }
189
- }
190
- }
191
- });
145
+ traverseRules(config.module?.rules ?? [], (rule) => {
146
+ if (!Array.isArray(rule.oneOf) || rule.oneOf.length === 0) {
147
+ return;
148
+ }
149
+ const expectedTestSource = `${escapeRegExp(extensionSuffix)}$`;
150
+ const alreadyPresent = rule.oneOf.some((candidate) => {
151
+ if (!candidate || typeof candidate !== "object") return false;
152
+ const { test } = candidate;
153
+ return test instanceof RegExp && test.source === expectedTestSource;
154
+ });
155
+ if (alreadyPresent) {
156
+ return;
157
+ }
158
+ const oneOf = rule.oneOf;
159
+ for (let idx = 0; idx < oneOf.length; idx += 1) {
160
+ const candidate = oneOf[idx];
161
+ if (candidate && typeof candidate === "object") {
162
+ const candidateRule = candidate;
163
+ const { test } = candidateRule;
164
+ const isModuleCssRule = test instanceof RegExp && test.source.includes("\\.module\\.css");
165
+ if (isModuleCssRule) {
166
+ const use = normalizeUseItems(candidateRule.use);
167
+ if (use) {
168
+ const hasCssLoader = use.some((item) => isUseLoaderObject(item) && item.loader.includes("css-loader"));
169
+ if (hasCssLoader) {
170
+ oneOf.splice(idx, 0, createWywCssModuleRule(candidateRule, extensionSuffix));
171
+ break;
172
+ }
173
+ }
174
+ }
175
+ }
176
+ }
177
+ });
192
178
  }
193
179
  function injectWywLoader(config, nextOptions, wywNext) {
194
- const loader = require.resolve('@wyw-in-js/webpack-loader');
195
- const nextBabelPreset = require.resolve('next/babel', {
196
- paths: [process.cwd()]
197
- });
198
- const extension = wywNext.loaderOptions?.extension ?? DEFAULT_EXTENSION;
199
- const babelOptions = wywNext.loaderOptions?.babelOptions ?? {
200
- presets: [nextBabelPreset]
201
- };
202
- const userImportOverrides = wywNext.loaderOptions?.importOverrides;
203
- const importOverrides = userImportOverrides ? {
204
- ...DEFAULT_REACT_IMPORT_OVERRIDES,
205
- ...userImportOverrides
206
- } : DEFAULT_REACT_IMPORT_OVERRIDES;
207
- const loaderOptions = {
208
- cssImport: 'import',
209
- ...wywNext.loaderOptions,
210
- babelOptions,
211
- extension,
212
- importOverrides,
213
- sourceMap: wywNext.loaderOptions?.sourceMap ?? nextOptions.dev
214
- };
215
- const wywLoaderItem = {
216
- loader,
217
- options: loaderOptions
218
- };
219
- traverseRules(config.module?.rules ?? [], rule => {
220
- convertLoaderRuleToUseRule(rule, wywLoaderItem);
221
- const use = normalizeUseItems(rule.use);
222
- if (!use) return;
223
- const loaders = use.map(getLoaderName);
224
- const alreadyInjected = loaders.some(l => l === loader || isWywLoaderPath(l));
225
- if (alreadyInjected) return;
226
- const isNextJsTranspileRule = loaders.some(l => ['next-swc-loader', 'next-babel-loader', 'babel-loader'].some(needle => l.includes(needle)));
227
- if (!isNextJsTranspileRule) return;
228
-
229
- // Loader order is right-to-left. We want WyW to run first, so it should be last.
230
- Object.assign(rule, {
231
- use: [...use, wywLoaderItem]
232
- });
233
- });
234
- ensureWywCssModuleRules(config, extension);
235
- }
236
- function ensureTurbopackCssPlaceholders(projectRoot) {
237
- const queue = [projectRoot];
238
- while (queue.length) {
239
- const dir = queue.pop();
240
- let entries;
241
- try {
242
- entries = fs.readdirSync(dir, {
243
- withFileTypes: true
244
- });
245
- } catch {
246
- entries = [];
247
- }
248
- for (const entry of entries) {
249
- if (entry.name !== '.' && entry.name !== '..') {
250
- const entryPath = path.join(dir, entry.name);
251
- if (entry.isDirectory()) {
252
- if (!PLACEHOLDER_IGNORED_DIRS.has(entry.name)) {
253
- queue.push(entryPath);
254
- }
255
- } else if (entry.isFile()) {
256
- const shouldIgnore = entry.name.startsWith('middleware.') || entry.name.endsWith('.d.ts');
257
- if (!shouldIgnore) {
258
- const ext = path.extname(entry.name);
259
- if (PLACEHOLDER_EXTENSIONS.has(ext)) {
260
- const baseName = path.basename(entry.name, ext);
261
- const cssFilePath = path.join(path.dirname(entryPath), `${baseName}${DEFAULT_EXTENSION}`);
262
- try {
263
- fs.writeFileSync(cssFilePath, '', {
264
- flag: 'wx'
265
- });
266
- } catch (err) {
267
- if (err.code !== 'EEXIST') {
268
- throw err;
269
- }
270
- }
271
- }
272
- }
273
- }
274
- }
275
- }
276
- }
180
+ const loader = nodeRequire.resolve("@wyw-in-js/webpack-loader");
181
+ const extension = wywNext.loaderOptions?.extension ?? DEFAULT_EXTENSION;
182
+ const userImportOverrides = wywNext.loaderOptions?.importOverrides;
183
+ const importOverrides = userImportOverrides ? {
184
+ ...DEFAULT_REACT_IMPORT_OVERRIDES,
185
+ ...userImportOverrides
186
+ } : DEFAULT_REACT_IMPORT_OVERRIDES;
187
+ const loaderOptions = {
188
+ cssImport: "import",
189
+ ...wywNext.loaderOptions,
190
+ extension,
191
+ importOverrides,
192
+ sourceMap: wywNext.loaderOptions?.sourceMap ?? nextOptions.dev
193
+ };
194
+ const wywLoaderItem = {
195
+ loader,
196
+ options: loaderOptions
197
+ };
198
+ traverseRules(config.module?.rules ?? [], (rule) => {
199
+ convertLoaderRuleToUseRule(rule, wywLoaderItem);
200
+ const use = normalizeUseItems(rule.use);
201
+ if (!use) return;
202
+ const loaders = use.map(getLoaderName);
203
+ const alreadyInjected = loaders.some((l) => l === loader || isWywLoaderPath(l));
204
+ if (alreadyInjected) return;
205
+ const isNextJsTranspileRule = loaders.some((l) => [
206
+ "next-swc-loader",
207
+ "next-babel-loader",
208
+ "babel-loader"
209
+ ].some((needle) => l.includes(needle)));
210
+ if (!isNextJsTranspileRule) return;
211
+ // Loader order is right-to-left. We want WyW to run first, so it should be last.
212
+ Object.assign(rule, { use: [...use, wywLoaderItem] });
213
+ });
214
+ ensureWywCssModuleRules(config, extension);
277
215
  }
278
216
  function shouldUseTurbopackConfig(nextConfig) {
279
- const explicit = nextConfig.turbopack;
280
- if (typeof explicit !== 'undefined') {
281
- return true;
282
- }
283
- try {
284
- const pkgPath = require.resolve('next/package.json', {
285
- paths: [process.cwd()]
286
- });
287
- const pkg = require(pkgPath);
288
- const version = typeof pkg.version === 'string' ? pkg.version : '';
289
- const major = Number.parseInt(version.split('.')[0] ?? '', 10);
290
- return Number.isFinite(major) && major >= 16;
291
- } catch {
292
- return false;
293
- }
217
+ const explicit = nextConfig.turbopack;
218
+ if (typeof explicit !== "undefined") {
219
+ return true;
220
+ }
221
+ try {
222
+ const pkgPath = nodeRequire.resolve("next/package.json", { paths: [process.cwd()] });
223
+ const pkg = nodeRequire(pkgPath);
224
+ const version = typeof pkg.version === "string" ? pkg.version : "";
225
+ const major = Number.parseInt(version.split(".")[0] ?? "", 10);
226
+ return Number.isFinite(major) && major >= 16;
227
+ } catch {
228
+ return false;
229
+ }
294
230
  }
295
231
  function injectWywTurbopackRules(nextConfig, wywNext) {
296
- const loader = require.resolve('@wyw-in-js/turbopack-loader');
297
- const nextBabelPreset = require.resolve('next/babel', {
298
- paths: [process.cwd()]
299
- });
300
- const userOptions = wywNext.turbopackLoaderOptions ?? {};
301
- assertNoFunctions(userOptions, 'turbopackLoaderOptions');
302
- const userImportOverrides = isPlainObject(userOptions.importOverrides) ? userOptions.importOverrides : undefined;
303
- const loaderOptions = {
304
- babelOptions: {
305
- presets: [nextBabelPreset]
306
- },
307
- sourceMap: process.env.NODE_ENV !== 'production',
308
- ...userOptions,
309
- importOverrides: userImportOverrides ? {
310
- ...DEFAULT_REACT_IMPORT_OVERRIDES,
311
- ...userImportOverrides
312
- } : DEFAULT_REACT_IMPORT_OVERRIDES
313
- };
314
- const useTurbopackConfig = shouldUseTurbopackConfig(nextConfig);
315
- const isNextBuild = process.argv.includes('build');
316
- const isWebpackBuild = process.argv.includes('--webpack');
317
- if (useTurbopackConfig && process.env.NODE_ENV === 'production' && isNextBuild && !isWebpackBuild) {
318
- ensureTurbopackCssPlaceholders(process.cwd());
319
- }
320
- const ruleValue = useTurbopackConfig ? {
321
- loaders: [{
322
- loader,
323
- options: loaderOptions
324
- }],
325
- condition: {
326
- all: [{
327
- not: 'foreign'
328
- }, {
329
- not: {
330
- path: /(?:^|[\\/])middleware\.[jt]sx?$/
331
- }
332
- }]
333
- }
334
- } : [{
335
- loader,
336
- options: loaderOptions
337
- }];
338
- const wywRules = Object.fromEntries(DEFAULT_TURBO_RULE_KEYS.map(key => [key, ruleValue]));
339
- if (useTurbopackConfig) {
340
- const turbopackConfig = nextConfig.turbopack;
341
- const userTurbopack = isPlainObject(turbopackConfig) ? turbopackConfig : {};
342
- const userRules = isPlainObject(userTurbopack.rules) ? userTurbopack.rules : {};
343
- return {
344
- ...nextConfig,
345
- turbopack: {
346
- ...userTurbopack,
347
- rules: {
348
- ...wywRules,
349
- ...userRules
350
- }
351
- }
352
- };
353
- }
354
- const userExperimental = isPlainObject(nextConfig.experimental) ? nextConfig.experimental : {};
355
- const userTurbo = isPlainObject(userExperimental.turbo) ? userExperimental.turbo : {};
356
- const userRules = isPlainObject(userTurbo.rules) ? userTurbo.rules : {};
357
- return {
358
- ...nextConfig,
359
- experimental: {
360
- ...userExperimental,
361
- turbo: {
362
- ...userTurbo,
363
- rules: {
364
- ...wywRules,
365
- ...userRules
366
- }
367
- }
368
- }
369
- };
232
+ const loader = nodeRequire.resolve("@wyw-in-js/turbopack-loader");
233
+ const userOptions = wywNext.turbopackLoaderOptions ?? {};
234
+ assertJsonSerializable(userOptions, "turbopackLoaderOptions");
235
+ const turbopackConfig = nextConfig.turbopack;
236
+ const userTurbopack = isPlainObject(turbopackConfig) ? turbopackConfig : {};
237
+ const userExperimental = isPlainObject(nextConfig.experimental) ? nextConfig.experimental : {};
238
+ const userTurbo = isPlainObject(userExperimental.turbo) ? userExperimental.turbo : {};
239
+ const nativeResolverAlias = toNativeResolverAlias(userTurbopack.resolveAlias ?? userTurbo.resolveAlias);
240
+ const oxcOptions = mergeOxcResolverAlias(userOptions.oxcOptions, nativeResolverAlias);
241
+ const userImportOverrides = isPlainObject(userOptions.importOverrides) ? userOptions.importOverrides : undefined;
242
+ const loaderOptions = {
243
+ sourceMap: process.env.NODE_ENV !== "production",
244
+ ...userOptions,
245
+ ...oxcOptions ? { oxcOptions } : {},
246
+ importOverrides: userImportOverrides ? {
247
+ ...DEFAULT_REACT_IMPORT_OVERRIDES,
248
+ ...userImportOverrides
249
+ } : DEFAULT_REACT_IMPORT_OVERRIDES
250
+ };
251
+ const useTurbopackConfig = shouldUseTurbopackConfig(nextConfig);
252
+ const ruleValue = useTurbopackConfig ? {
253
+ loaders: [{
254
+ loader,
255
+ options: loaderOptions
256
+ }],
257
+ condition: { all: [{ not: "foreign" }, { not: { path: /(?:^|[\\/])middleware\.[jt]sx?$/ } }] }
258
+ } : [{
259
+ loader,
260
+ options: loaderOptions
261
+ }];
262
+ const wywRules = Object.fromEntries(DEFAULT_TURBO_RULE_KEYS.map((key) => [key, ruleValue]));
263
+ if (useTurbopackConfig) {
264
+ const userRules = isPlainObject(userTurbopack.rules) ? userTurbopack.rules : {};
265
+ return {
266
+ ...nextConfig,
267
+ turbopack: {
268
+ ...userTurbopack,
269
+ rules: {
270
+ ...wywRules,
271
+ ...userRules
272
+ }
273
+ }
274
+ };
275
+ }
276
+ const userRules = isPlainObject(userTurbo.rules) ? userTurbo.rules : {};
277
+ return {
278
+ ...nextConfig,
279
+ experimental: {
280
+ ...userExperimental,
281
+ turbo: {
282
+ ...userTurbo,
283
+ rules: {
284
+ ...wywRules,
285
+ ...userRules
286
+ }
287
+ }
288
+ }
289
+ };
370
290
  }
371
291
  export function withWyw(nextConfig = {}, wywNext = {}) {
372
- const userWebpack = nextConfig.webpack;
373
- return {
374
- ...injectWywTurbopackRules(nextConfig, wywNext),
375
- webpack(config, options) {
376
- const resolvedConfig = typeof userWebpack === 'function' ? userWebpack(config, options) : config;
377
- injectWywLoader(resolvedConfig, options, wywNext);
378
- return resolvedConfig;
379
- }
380
- };
292
+ const userWebpack = nextConfig.webpack;
293
+ return {
294
+ ...injectWywTurbopackRules(nextConfig, wywNext),
295
+ webpack(config, options) {
296
+ const resolvedConfig = typeof userWebpack === "function" ? userWebpack(config, options) : config;
297
+ injectWywLoader(resolvedConfig, options, wywNext);
298
+ return resolvedConfig;
299
+ }
300
+ };
381
301
  }
382
- //# sourceMappingURL=index.mjs.map
302
+ //# sourceMappingURL=index.mjs.map