@nuxt/kit 3.0.0-rc.2 → 3.0.0-rc.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,11 +1,13 @@
1
- import { Nuxt, ModuleContainer, ModuleOptions, ModuleDefinition, NuxtModule, NuxtConfig, NuxtOptions, NuxtCompatibility, NuxtCompatibilityIssues, ComponentsDir, Component, NuxtHooks, NuxtPlugin, NuxtPluginTemplate, NuxtTemplate } from '@nuxt/schema';
1
+ import { Nuxt, ModuleContainer, ModuleOptions, ModuleDefinition, NuxtModule, NuxtConfig, NuxtOptions, NuxtCompatibility, NuxtCompatibilityIssues, ComponentsDir, Component, NuxtTemplate, NuxtHooks, NuxtPlugin, NuxtPluginTemplate } from '@nuxt/schema';
2
2
  import { LoadConfigOptions } from 'c12';
3
3
  import { Import } from 'unimport';
4
4
  import { Configuration, WebpackPluginInstance } from 'webpack';
5
5
  import { UserConfig, Plugin } from 'vite';
6
6
  import * as unctx from 'unctx';
7
7
  import { Middleware } from 'h3';
8
+ import { NitroEventHandler, NitroDevEventHandler } from 'nitropack';
8
9
  import * as consola from 'consola';
10
+ import { genSafeVariableName } from 'knitwork';
9
11
 
10
12
  declare function useModuleContainer(nuxt?: Nuxt): ModuleContainer;
11
13
 
@@ -51,8 +53,6 @@ interface ExtendConfigOptions {
51
53
  * @default true
52
54
  */
53
55
  build?: boolean;
54
- }
55
- interface ExtendWebpackConfigOptions extends ExtendConfigOptions {
56
56
  /**
57
57
  * Install plugin on server side
58
58
  *
@@ -65,6 +65,8 @@ interface ExtendWebpackConfigOptions extends ExtendConfigOptions {
65
65
  * @default true
66
66
  */
67
67
  client?: boolean;
68
+ }
69
+ interface ExtendWebpackConfigOptions extends ExtendConfigOptions {
68
70
  /**
69
71
  * Install plugin on modern build
70
72
  *
@@ -170,6 +172,8 @@ declare function tryUseNuxt(): Nuxt | null;
170
172
  */
171
173
  declare function isIgnored(pathname: string): boolean;
172
174
 
175
+ declare function addLayout(tmpl: NuxtTemplate, name?: string): void;
176
+
173
177
  declare function extendPages(cb: NuxtHooks['pages:extend']): void;
174
178
 
175
179
  /**
@@ -233,12 +237,28 @@ interface Resolver {
233
237
  declare function createResolver(base: string | URL): Resolver;
234
238
  declare function resolveFiles(path: string, pattern: string | string[]): Promise<string[]>;
235
239
 
236
- interface ServerMiddleware {
240
+ interface LegacyServerMiddleware {
237
241
  route?: string;
242
+ path?: string;
243
+ handle?: Middleware | string;
238
244
  handler: Middleware | string;
239
245
  }
240
- /** Adds a new server middleware to the end of the server middleware array. */
241
- declare function addServerMiddleware(middleware: ServerMiddleware): void;
246
+ /**
247
+ * Adds a new server middleware to the end of the server middleware array.
248
+ *
249
+ * @deprecated Use addServerHandler instead
250
+ */
251
+ declare function addServerMiddleware(middleware: LegacyServerMiddleware): void;
252
+ /**
253
+ * Adds a nitro server handler
254
+ *
255
+ */
256
+ declare function addServerHandler(handler: NitroEventHandler): void;
257
+ /**
258
+ * Adds a nitro server handler for development-only
259
+ *
260
+ */
261
+ declare function addDevServerHandler(handler: NitroDevEventHandler): void;
242
262
 
243
263
  /**
244
264
  * Renders given template using lodash template during build into the project buildDir
@@ -282,10 +302,10 @@ declare function tryRequireModule(id: string, opts?: RequireModuleOptions): any;
282
302
  declare function compileTemplate(template: NuxtTemplate, ctx: any): Promise<string>;
283
303
  declare const templateUtils: {
284
304
  serialize: (data: any) => string;
285
- importName: (src: string) => string;
305
+ importName: typeof genSafeVariableName;
286
306
  importSources: (sources: string | string[], { lazy }?: {
287
307
  lazy?: boolean;
288
308
  }) => string;
289
309
  };
290
310
 
291
- export { AddComponentOptions, AddPluginOptions, ExtendConfigOptions, ExtendViteConfigOptions, ExtendWebpackConfigOptions, LoadNuxtConfigOptions, LoadNuxtOptions, RequireModuleOptions, ResolveModuleOptions, ResolvePathOptions, Resolver, ServerMiddleware, addAutoImport, addAutoImportDir, addComponent, addComponentsDir, addPlugin, addPluginTemplate, addServerMiddleware, addTemplate, addVitePlugin, addWebpackPlugin, assertNuxtCompatibility, buildNuxt, checkNuxtCompatibility, clearRequireCache, compileTemplate, createResolver, defineNuxtModule, extendPages, extendViteConfig, extendWebpackConfig, findPath, getNuxtVersion, getRequireCacheItem, hasNuxtCompatibility, importModule, installModule, isIgnored, isNodeModules, isNuxt2, isNuxt3, loadNuxt, loadNuxtConfig, logger, normalizePlugin, normalizeTemplate, nuxtCtx, requireModule, requireModulePkg, resolveAlias, resolveFiles, resolveModule, resolvePath, scanRequireTree, templateUtils, tryImportModule, tryRequireModule, tryResolveModule, tryUseNuxt, useLogger, useModuleContainer, useNuxt };
311
+ export { AddComponentOptions, AddPluginOptions, ExtendConfigOptions, ExtendViteConfigOptions, ExtendWebpackConfigOptions, LegacyServerMiddleware, LoadNuxtConfigOptions, LoadNuxtOptions, RequireModuleOptions, ResolveModuleOptions, ResolvePathOptions, Resolver, addAutoImport, addAutoImportDir, addComponent, addComponentsDir, addDevServerHandler, addLayout, addPlugin, addPluginTemplate, addServerHandler, addServerMiddleware, addTemplate, addVitePlugin, addWebpackPlugin, assertNuxtCompatibility, buildNuxt, checkNuxtCompatibility, clearRequireCache, compileTemplate, createResolver, defineNuxtModule, extendPages, extendViteConfig, extendWebpackConfig, findPath, getNuxtVersion, getRequireCacheItem, hasNuxtCompatibility, importModule, installModule, isIgnored, isNodeModules, isNuxt2, isNuxt3, loadNuxt, loadNuxtConfig, logger, normalizePlugin, normalizeTemplate, nuxtCtx, requireModule, requireModulePkg, resolveAlias, resolveFiles, resolveModule, resolvePath, scanRequireTree, templateUtils, tryImportModule, tryRequireModule, tryResolveModule, tryUseNuxt, useLogger, useModuleContainer, useNuxt };
package/dist/index.mjs CHANGED
@@ -1,9 +1,10 @@
1
- import { parse, basename, resolve, normalize, join, relative, isAbsolute, dirname, extname } from 'pathe';
2
- import consola from 'consola';
1
+ import { parse, basename, resolve, relative, join, normalize, isAbsolute, dirname } from 'pathe';
3
2
  import { existsSync, readFileSync, promises } from 'node:fs';
4
3
  import hash from 'hash-sum';
5
4
  import { getContext } from 'unctx';
5
+ import { kebabCase, pascalCase } from 'scule';
6
6
  import satisfies from 'semver/functions/satisfies.js';
7
+ import consola from 'consola';
7
8
  import { pathToFileURL, fileURLToPath } from 'node:url';
8
9
  import { interopDefault } from 'mlly';
9
10
  import jiti from 'jiti';
@@ -12,17 +13,11 @@ import ignore from 'ignore';
12
13
  import defu from 'defu';
13
14
  import { applyDefaults } from 'untyped';
14
15
  import lodashTemplate from 'lodash.template';
15
- import { camelCase, kebabCase, pascalCase } from 'scule';
16
- import { genDynamicImport, genImport } from 'knitwork';
16
+ import { genSafeVariableName, genDynamicImport, genImport } from 'knitwork';
17
17
  import { loadConfig } from 'c12';
18
18
  import { NuxtConfigSchema } from '@nuxt/schema';
19
19
  import { resolvePackageJSON, readPackageJSON } from 'pkg-types';
20
20
 
21
- const logger = consola;
22
- function useLogger(scope) {
23
- return scope ? logger.withScope(scope) : logger;
24
- }
25
-
26
21
  function chainFn(base, fn) {
27
22
  if (typeof fn !== "function") {
28
23
  return base;
@@ -96,10 +91,6 @@ function normalizeTemplate(template) {
96
91
  return template;
97
92
  }
98
93
 
99
- function addServerMiddleware(middleware) {
100
- useNuxt().options.serverMiddleware.push(middleware);
101
- }
102
-
103
94
  async function checkNuxtCompatibility(constraints, nuxt = useNuxt()) {
104
95
  const issues = [];
105
96
  if (constraints.nuxt) {
@@ -113,7 +104,7 @@ async function checkNuxtCompatibility(constraints, nuxt = useNuxt()) {
113
104
  }
114
105
  }
115
106
  if (isNuxt2(nuxt)) {
116
- const bridgeRequirement = constraints?.bridge;
107
+ const bridgeRequirement = constraints.bridge;
117
108
  const hasBridge = !!nuxt.options.bridge;
118
109
  if (bridgeRequirement === true && !hasBridge) {
119
110
  issues.push({
@@ -151,11 +142,60 @@ function isNuxt3(nuxt = useNuxt()) {
151
142
  function getNuxtVersion(nuxt = useNuxt()) {
152
143
  const version = (nuxt?._version || nuxt?.version || nuxt?.constructor?.version || "").replace(/^v/g, "");
153
144
  if (!version) {
154
- throw new Error("Cannot determine nuxt version! Is currect instance passed?");
145
+ throw new Error("Cannot determine nuxt version! Is current instance passed?");
155
146
  }
156
147
  return version;
157
148
  }
158
149
 
150
+ const logger = consola;
151
+ function useLogger(scope) {
152
+ return scope ? logger.withScope(scope) : logger;
153
+ }
154
+
155
+ function addLayout(tmpl, name) {
156
+ const nuxt = useNuxt();
157
+ const { filename, src } = addTemplate(tmpl);
158
+ const layoutName = kebabCase(name || parse(tmpl.filename).name).replace(/["']/g, "");
159
+ if (isNuxt2(nuxt)) {
160
+ const layout = nuxt.options.layouts[layoutName];
161
+ if (layout) {
162
+ return logger.warn(`Not overriding \`${layoutName}\` (provided by \`${layout}\`) with \`${src || filename}\`.`);
163
+ }
164
+ nuxt.options.layouts[layoutName] = `./${filename}`;
165
+ if (name === "error") {
166
+ this.addErrorLayout(filename);
167
+ }
168
+ return;
169
+ }
170
+ nuxt.hook("app:templates", (app) => {
171
+ if (layoutName in app.layouts) {
172
+ const relativePath = relative(nuxt.options.srcDir, app.layouts[layoutName].file);
173
+ return logger.warn(`Not overriding \`${layoutName}\` (provided by \`~/${relativePath}\`) with \`${src || filename}\`.`);
174
+ }
175
+ app.layouts[layoutName] = {
176
+ file: join("#build", filename),
177
+ name: layoutName
178
+ };
179
+ });
180
+ }
181
+
182
+ function normalizeHandlerMethod(handler) {
183
+ const [, method = void 0] = handler.handler.match(/\.(get|head|patch|post|put|delete|connect|options|trace)(\.\w+)*$/) || [];
184
+ return {
185
+ method,
186
+ ...handler
187
+ };
188
+ }
189
+ function addServerMiddleware(middleware) {
190
+ useNuxt().options.serverMiddleware.push(middleware);
191
+ }
192
+ function addServerHandler(handler) {
193
+ useNuxt().options.serverHandlers.push(normalizeHandlerMethod(handler));
194
+ }
195
+ function addDevServerHandler(handler) {
196
+ useNuxt().options.devServerHandlers.push(handler);
197
+ }
198
+
159
199
  function normalizePlugin(plugin) {
160
200
  if (typeof plugin === "string") {
161
201
  plugin = { src: plugin };
@@ -383,7 +423,7 @@ async function existsSensitive(path) {
383
423
  }
384
424
  async function resolveFiles(path, pattern) {
385
425
  const files = await globby(pattern, { cwd: path, followSymbolicLinks: true });
386
- return files.filter((p) => !isIgnored(p)).map((p) => resolve(path, p));
426
+ return files.map((p) => resolve(path, p)).filter((p) => !isIgnored(p));
387
427
  }
388
428
 
389
429
  async function installModule(moduleToInstall, _inlineOptions, _nuxt) {
@@ -460,16 +500,7 @@ function useModuleContainer(nuxt = useNuxt()) {
460
500
  return addPluginTemplate(pluginTemplate);
461
501
  },
462
502
  addLayout(tmpl, name) {
463
- const { filename, src } = addTemplate(tmpl);
464
- const layoutName = name || parse(src).name;
465
- const layout = nuxt.options.layouts[layoutName];
466
- if (layout) {
467
- logger.warn(`Duplicate layout registration, "${layoutName}" has been registered as "${layout}"`);
468
- }
469
- nuxt.options.layouts[layoutName] = `./${filename}`;
470
- if (name === "error") {
471
- this.addErrorLayout(filename);
472
- }
503
+ return addLayout(tmpl, name);
473
504
  },
474
505
  addErrorLayout(dst) {
475
506
  const relativeBuildDir = relative(nuxt.options.rootDir, nuxt.options.buildDir);
@@ -514,19 +545,18 @@ async function compileTemplate(template, ctx) {
514
545
  throw new Error("Invalid template: " + JSON.stringify(template));
515
546
  }
516
547
  const serialize = (data) => JSON.stringify(data, null, 2).replace(/"{(.+)}"(?=,?$)/gm, (r) => JSON.parse(r).replace(/^{(.*)}$/, "$1"));
517
- const importName = (src) => `${camelCase(basename(src, extname(src))).replace(/[^a-zA-Z?\d\s:]/g, "")}_${hash(src)}`;
518
548
  const importSources = (sources, { lazy = false } = {}) => {
519
549
  if (!Array.isArray(sources)) {
520
550
  sources = [sources];
521
551
  }
522
552
  return sources.map((src) => {
523
553
  if (lazy) {
524
- return `const ${importName(src)} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`;
554
+ return `const ${genSafeVariableName(src)} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`;
525
555
  }
526
- return genImport(src, importName(src));
556
+ return genImport(src, genSafeVariableName(src));
527
557
  }).join("\n");
528
558
  };
529
- const templateUtils = { serialize, importName, importSources };
559
+ const templateUtils = { serialize, importName: genSafeVariableName, importSources };
530
560
 
531
561
  function defineNuxtModule(definition) {
532
562
  if (typeof definition === "function") {
@@ -538,7 +568,7 @@ function defineNuxtModule(definition) {
538
568
  }
539
569
  if (!definition.meta.configKey) {
540
570
  definition.meta.name = definition.meta.name || definition.name;
541
- definition.meta.configKey = definition.meta.configKey || definition.configKey || definition.meta.name;
571
+ definition.meta.configKey = definition.configKey || definition.meta.name;
542
572
  }
543
573
  function getOptions(inlineOptions, nuxt = useNuxt()) {
544
574
  const configKey = definition.meta.configKey || definition.meta.name;
@@ -729,7 +759,14 @@ function extendViteConfig(fn, options = {}) {
729
759
  if (options.build === false && nuxt.options.build) {
730
760
  return;
731
761
  }
732
- nuxt.hook("vite:extend", ({ config }) => fn(config));
762
+ nuxt.hook("vite:extendConfig", (config, { isClient, isServer }) => {
763
+ if (options.server !== false && isServer) {
764
+ return fn(config);
765
+ }
766
+ if (options.client !== false && isClient) {
767
+ return fn(config);
768
+ }
769
+ });
733
770
  }
734
771
  function addWebpackPlugin(plugin, options) {
735
772
  extendWebpackConfig((config) => {
@@ -773,7 +810,7 @@ async function addComponent(opts) {
773
810
  ...opts
774
811
  };
775
812
  nuxt.hook("components:extend", (components) => {
776
- const existingComponent = components.find((c) => c.pascalName === component.pascalName || c.kebabName === component.kebabName);
813
+ const existingComponent = components.find((c) => (c.pascalName === component.pascalName || c.kebabName === component.kebabName) && c.mode === component.mode);
777
814
  if (existingComponent) {
778
815
  const name = existingComponent.pascalName || existingComponent.kebabName;
779
816
  console.warn(`Overriding ${name} component.`);
@@ -793,4 +830,4 @@ function extendPages(cb) {
793
830
  }
794
831
  }
795
832
 
796
- export { addAutoImport, addAutoImportDir, addComponent, addComponentsDir, addPlugin, addPluginTemplate, addServerMiddleware, addTemplate, addVitePlugin, addWebpackPlugin, assertNuxtCompatibility, buildNuxt, checkNuxtCompatibility, clearRequireCache, compileTemplate, createResolver, defineNuxtModule, extendPages, extendViteConfig, extendWebpackConfig, findPath, getNuxtVersion, getRequireCacheItem, hasNuxtCompatibility, importModule, installModule, isIgnored, isNodeModules, isNuxt2, isNuxt3, loadNuxt, loadNuxtConfig, logger, normalizePlugin, normalizeTemplate, nuxtCtx, requireModule, requireModulePkg, resolveAlias, resolveFiles, resolveModule, resolvePath, scanRequireTree, templateUtils, tryImportModule, tryRequireModule, tryResolveModule, tryUseNuxt, useLogger, useModuleContainer, useNuxt };
833
+ export { addAutoImport, addAutoImportDir, addComponent, addComponentsDir, addDevServerHandler, addLayout, addPlugin, addPluginTemplate, addServerHandler, addServerMiddleware, addTemplate, addVitePlugin, addWebpackPlugin, assertNuxtCompatibility, buildNuxt, checkNuxtCompatibility, clearRequireCache, compileTemplate, createResolver, defineNuxtModule, extendPages, extendViteConfig, extendWebpackConfig, findPath, getNuxtVersion, getRequireCacheItem, hasNuxtCompatibility, importModule, installModule, isIgnored, isNodeModules, isNuxt2, isNuxt3, loadNuxt, loadNuxtConfig, logger, normalizePlugin, normalizeTemplate, nuxtCtx, requireModule, requireModulePkg, resolveAlias, resolveFiles, resolveModule, resolvePath, scanRequireTree, templateUtils, tryImportModule, tryRequireModule, tryResolveModule, tryUseNuxt, useLogger, useModuleContainer, useNuxt };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/kit",
3
- "version": "3.0.0-rc.2",
3
+ "version": "3.0.0-rc.5",
4
4
  "repository": "nuxt/framework",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -13,23 +13,23 @@
13
13
  "prepack": "unbuild"
14
14
  },
15
15
  "dependencies": {
16
- "@nuxt/schema": "^3.0.0-rc.2",
17
- "c12": "^0.2.7",
16
+ "@nuxt/schema": "^3.0.0-rc.5",
17
+ "c12": "^0.2.8",
18
18
  "consola": "^2.15.3",
19
19
  "defu": "^6.0.0",
20
- "globby": "^13.1.1",
20
+ "globby": "^13.1.2",
21
21
  "hash-sum": "^2.0.0",
22
22
  "ignore": "^5.2.0",
23
- "jiti": "^1.13.0",
24
- "knitwork": "^0.1.1",
23
+ "jiti": "^1.14.0",
24
+ "knitwork": "^0.1.2",
25
25
  "lodash.template": "^4.5.0",
26
- "mlly": "^0.5.2",
27
- "pathe": "^0.2.0",
28
- "pkg-types": "^0.3.2",
26
+ "mlly": "^0.5.4",
27
+ "pathe": "^0.3.2",
28
+ "pkg-types": "^0.3.3",
29
29
  "scule": "^0.2.1",
30
30
  "semver": "^7.3.7",
31
31
  "unctx": "^1.1.4",
32
- "unimport": "^0.1.8",
32
+ "unimport": "^0.4.4",
33
33
  "untyped": "^0.4.4"
34
34
  },
35
35
  "devDependencies": {