@wyw-in-js/nextjs 1.0.9 → 2.0.0-alpha.0

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,298 @@
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
+ const DEFAULT_EXTENSION = ".wyw-in-js.module.css";
3
+ const DEFAULT_TURBO_RULE_KEYS = [
4
+ "*.js",
5
+ "*.jsx",
6
+ "*.ts",
7
+ "*.tsx"
8
+ ];
5
9
  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
- }
10
+ react: { mock: "react" },
11
+ "react/jsx-runtime": { mock: "react/jsx-runtime" },
12
+ "react/jsx-dev-runtime": { mock: "react/jsx-dev-runtime" }
15
13
  };
16
- const PLACEHOLDER_EXTENSIONS = new Set(['.js', '.jsx', '.ts', '.tsx']);
17
- const PLACEHOLDER_IGNORED_DIRS = new Set(['.git', '.next', '.turbo', 'node_modules']);
14
+ const nodeRequire = createRequire(import.meta.url);
18
15
  function isObject(value) {
19
- return typeof value === 'object' && value !== null;
16
+ return typeof value === "object" && value !== null;
20
17
  }
21
18
  function isUseLoaderObject(item) {
22
- return isObject(item) && typeof item.loader === 'string';
19
+ return isObject(item) && typeof item.loader === "string";
23
20
  }
24
21
  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;
22
+ if (!use) return null;
23
+ if (typeof use === "function") return null;
24
+ const list = (Array.isArray(use) ? use : [use]).filter(Boolean);
25
+ return list.length ? list : null;
29
26
  }
30
27
  function getLoaderName(item) {
31
- if (typeof item === 'string') return item;
32
- if (isUseLoaderObject(item)) return item.loader;
33
- return '';
28
+ if (typeof item === "string") return item;
29
+ if (isUseLoaderObject(item)) return item.loader;
30
+ return "";
34
31
  }
35
32
  function isWywLoaderPath(loader) {
36
- return loader.includes('@wyw-in-js/webpack-loader') || /[\\/]webpack-loader[\\/]/.test(loader);
33
+ return loader.includes("@wyw-in-js/webpack-loader") || /[\\/]webpack-loader[\\/]/.test(loader);
37
34
  }
38
35
  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
- });
36
+ const { loader } = rule;
37
+ if (typeof loader !== "string") return;
38
+ const alreadyInjected = isWywLoaderPath(loader);
39
+ if (alreadyInjected) return;
40
+ const isNextJsTranspileRule = [
41
+ "next-swc-loader",
42
+ "next-babel-loader",
43
+ "babel-loader"
44
+ ].some((needle) => loader.includes(needle));
45
+ if (!isNextJsTranspileRule) return;
46
+ const { options } = rule;
47
+ const nextRule = rule;
48
+ delete nextRule.loader;
49
+ delete nextRule.options;
50
+ // Loader order is right-to-left. We want WyW to run first, so it should be last.
51
+ Object.assign(nextRule, { use: [{
52
+ loader,
53
+ ...options !== undefined ? { options } : {}
54
+ }, wywLoaderItem] });
63
55
  }
64
56
  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
- }
57
+ for (const rule of rules) {
58
+ if (rule && typeof rule === "object") {
59
+ visitor(rule);
60
+ if (Array.isArray(rule.oneOf)) {
61
+ traverseRules(rule.oneOf, visitor);
62
+ }
63
+ if (Array.isArray(rule.rules)) {
64
+ traverseRules(rule.rules, visitor);
65
+ }
66
+ }
67
+ }
76
68
  }
77
69
  function escapeRegExp(value) {
78
- return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
70
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
79
71
  }
80
72
  function isPlainObject(value) {
81
- if (!isObject(value)) return false;
82
- const proto = Object.getPrototypeOf(value);
83
- return proto === Object.prototype || proto === null;
73
+ if (!isObject(value)) return false;
74
+ const proto = Object.getPrototypeOf(value);
75
+ return proto === Object.prototype || proto === null;
84
76
  }
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
- }
77
+ function assertJsonSerializable(value, name) {
78
+ const queue = [{
79
+ path: name,
80
+ value
81
+ }];
82
+ const seen = new Set();
83
+ while (queue.length) {
84
+ const current = queue.shift();
85
+ 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") {
86
+ 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.`);
87
+ } else if (Array.isArray(current.value)) {
88
+ if (!seen.has(current.value)) {
89
+ seen.add(current.value);
90
+ current.value.forEach((item, idx) => queue.push({
91
+ path: `${current.path}[${idx}]`,
92
+ value: item
93
+ }));
94
+ }
95
+ } else if (isPlainObject(current.value)) {
96
+ if (!seen.has(current.value)) {
97
+ seen.add(current.value);
98
+ Object.entries(current.value).forEach(([key, item]) => queue.push({
99
+ path: `${current.path}.${key}`,
100
+ value: item
101
+ }));
102
+ }
103
+ } else {
104
+ 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.`);
105
+ }
106
+ }
116
107
  }
117
108
  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;
109
+ const use = normalizeUseItems(baseRule.use) ?? [];
110
+ const patchedUse = use.map((item) => {
111
+ if (!isUseLoaderObject(item) || !item.loader.includes("css-loader")) {
112
+ return item;
113
+ }
114
+ const itemOptions = item.options;
115
+ if (!isObject(itemOptions)) {
116
+ return item;
117
+ }
118
+ const { modules } = itemOptions;
119
+ if (!isObject(modules)) {
120
+ return item;
121
+ }
122
+ const nextModules = {
123
+ ...modules,
124
+ mode: "global",
125
+ getLocalIdent: (_context, _localIdentName, localName) => localName
126
+ };
127
+ return {
128
+ ...item,
129
+ options: {
130
+ ...itemOptions,
131
+ modules: nextModules
132
+ }
133
+ };
134
+ });
135
+ const nextRule = {
136
+ ...baseRule,
137
+ sideEffects: true,
138
+ test: new RegExp(`${escapeRegExp(extensionSuffix)}$`),
139
+ use: patchedUse
140
+ };
141
+ return nextRule;
153
142
  }
154
143
  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
- });
144
+ traverseRules(config.module?.rules ?? [], (rule) => {
145
+ if (!Array.isArray(rule.oneOf) || rule.oneOf.length === 0) {
146
+ return;
147
+ }
148
+ const expectedTestSource = `${escapeRegExp(extensionSuffix)}$`;
149
+ const alreadyPresent = rule.oneOf.some((candidate) => {
150
+ if (!candidate || typeof candidate !== "object") return false;
151
+ const { test } = candidate;
152
+ return test instanceof RegExp && test.source === expectedTestSource;
153
+ });
154
+ if (alreadyPresent) {
155
+ return;
156
+ }
157
+ const oneOf = rule.oneOf;
158
+ for (let idx = 0; idx < oneOf.length; idx += 1) {
159
+ const candidate = oneOf[idx];
160
+ if (candidate && typeof candidate === "object") {
161
+ const candidateRule = candidate;
162
+ const { test } = candidateRule;
163
+ const isModuleCssRule = test instanceof RegExp && test.source.includes("\\.module\\.css");
164
+ if (isModuleCssRule) {
165
+ const use = normalizeUseItems(candidateRule.use);
166
+ if (use) {
167
+ const hasCssLoader = use.some((item) => isUseLoaderObject(item) && item.loader.includes("css-loader"));
168
+ if (hasCssLoader) {
169
+ oneOf.splice(idx, 0, createWywCssModuleRule(candidateRule, extensionSuffix));
170
+ break;
171
+ }
172
+ }
173
+ }
174
+ }
175
+ }
176
+ });
192
177
  }
193
178
  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
- }
179
+ const loader = nodeRequire.resolve("@wyw-in-js/webpack-loader");
180
+ const extension = wywNext.loaderOptions?.extension ?? DEFAULT_EXTENSION;
181
+ const userImportOverrides = wywNext.loaderOptions?.importOverrides;
182
+ const importOverrides = userImportOverrides ? {
183
+ ...DEFAULT_REACT_IMPORT_OVERRIDES,
184
+ ...userImportOverrides
185
+ } : DEFAULT_REACT_IMPORT_OVERRIDES;
186
+ const loaderOptions = {
187
+ cssImport: "import",
188
+ ...wywNext.loaderOptions,
189
+ extension,
190
+ importOverrides,
191
+ sourceMap: wywNext.loaderOptions?.sourceMap ?? nextOptions.dev
192
+ };
193
+ const wywLoaderItem = {
194
+ loader,
195
+ options: loaderOptions
196
+ };
197
+ traverseRules(config.module?.rules ?? [], (rule) => {
198
+ convertLoaderRuleToUseRule(rule, wywLoaderItem);
199
+ const use = normalizeUseItems(rule.use);
200
+ if (!use) return;
201
+ const loaders = use.map(getLoaderName);
202
+ const alreadyInjected = loaders.some((l) => l === loader || isWywLoaderPath(l));
203
+ if (alreadyInjected) return;
204
+ const isNextJsTranspileRule = loaders.some((l) => [
205
+ "next-swc-loader",
206
+ "next-babel-loader",
207
+ "babel-loader"
208
+ ].some((needle) => l.includes(needle)));
209
+ if (!isNextJsTranspileRule) return;
210
+ // Loader order is right-to-left. We want WyW to run first, so it should be last.
211
+ Object.assign(rule, { use: [...use, wywLoaderItem] });
212
+ });
213
+ ensureWywCssModuleRules(config, extension);
277
214
  }
278
215
  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
- }
216
+ const explicit = nextConfig.turbopack;
217
+ if (typeof explicit !== "undefined") {
218
+ return true;
219
+ }
220
+ try {
221
+ const pkgPath = nodeRequire.resolve("next/package.json", { paths: [process.cwd()] });
222
+ const pkg = nodeRequire(pkgPath);
223
+ const version = typeof pkg.version === "string" ? pkg.version : "";
224
+ const major = Number.parseInt(version.split(".")[0] ?? "", 10);
225
+ return Number.isFinite(major) && major >= 16;
226
+ } catch {
227
+ return false;
228
+ }
294
229
  }
295
230
  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
- };
231
+ const loader = nodeRequire.resolve("@wyw-in-js/turbopack-loader");
232
+ const userOptions = wywNext.turbopackLoaderOptions ?? {};
233
+ assertJsonSerializable(userOptions, "turbopackLoaderOptions");
234
+ const userImportOverrides = isPlainObject(userOptions.importOverrides) ? userOptions.importOverrides : undefined;
235
+ const loaderOptions = {
236
+ sourceMap: process.env.NODE_ENV !== "production",
237
+ ...userOptions,
238
+ importOverrides: userImportOverrides ? {
239
+ ...DEFAULT_REACT_IMPORT_OVERRIDES,
240
+ ...userImportOverrides
241
+ } : DEFAULT_REACT_IMPORT_OVERRIDES
242
+ };
243
+ const useTurbopackConfig = shouldUseTurbopackConfig(nextConfig);
244
+ const ruleValue = useTurbopackConfig ? {
245
+ loaders: [{
246
+ loader,
247
+ options: loaderOptions
248
+ }],
249
+ condition: { all: [{ not: "foreign" }, { not: { path: /(?:^|[\\/])middleware\.[jt]sx?$/ } }] }
250
+ } : [{
251
+ loader,
252
+ options: loaderOptions
253
+ }];
254
+ const wywRules = Object.fromEntries(DEFAULT_TURBO_RULE_KEYS.map((key) => [key, ruleValue]));
255
+ if (useTurbopackConfig) {
256
+ const turbopackConfig = nextConfig.turbopack;
257
+ const userTurbopack = isPlainObject(turbopackConfig) ? turbopackConfig : {};
258
+ const userRules = isPlainObject(userTurbopack.rules) ? userTurbopack.rules : {};
259
+ return {
260
+ ...nextConfig,
261
+ turbopack: {
262
+ ...userTurbopack,
263
+ rules: {
264
+ ...wywRules,
265
+ ...userRules
266
+ }
267
+ }
268
+ };
269
+ }
270
+ const userExperimental = isPlainObject(nextConfig.experimental) ? nextConfig.experimental : {};
271
+ const userTurbo = isPlainObject(userExperimental.turbo) ? userExperimental.turbo : {};
272
+ const userRules = isPlainObject(userTurbo.rules) ? userTurbo.rules : {};
273
+ return {
274
+ ...nextConfig,
275
+ experimental: {
276
+ ...userExperimental,
277
+ turbo: {
278
+ ...userTurbo,
279
+ rules: {
280
+ ...wywRules,
281
+ ...userRules
282
+ }
283
+ }
284
+ }
285
+ };
370
286
  }
371
287
  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
- };
288
+ const userWebpack = nextConfig.webpack;
289
+ return {
290
+ ...injectWywTurbopackRules(nextConfig, wywNext),
291
+ webpack(config, options) {
292
+ const resolvedConfig = typeof userWebpack === "function" ? userWebpack(config, options) : config;
293
+ injectWywLoader(resolvedConfig, options, wywNext);
294
+ return resolvedConfig;
295
+ }
296
+ };
381
297
  }
382
- //# sourceMappingURL=index.mjs.map
298
+ //# sourceMappingURL=index.mjs.map