diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json index 0dabaa3e9f..f27fb7790f 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -64,7 +64,7 @@ "unctx": "^2.0.1", "unenv": "^0.5.4", "unimport": "^0.6.7", - "unplugin": "^0.9.0", + "unplugin": "^0.9.2", "untyped": "^0.4.5", "vue": "^3.2.37", "vue-bundle-renderer": "^0.4.2", diff --git a/packages/nuxt/src/core/plugins/unctx.ts b/packages/nuxt/src/core/plugins/unctx.ts index eb77025b72..aba7416bd9 100644 --- a/packages/nuxt/src/core/plugins/unctx.ts +++ b/packages/nuxt/src/core/plugins/unctx.ts @@ -23,7 +23,9 @@ export const UnctxTransformPlugin = (nuxt: Nuxt) => { if (result) { return { code: result.code, - map: options.sourcemap && result.magicString.generateMap({ source: id, includeContent: true }) + map: options.sourcemap + ? result.magicString.generateMap({ source: id, includeContent: true }) + : undefined } } } diff --git a/packages/nuxt/src/pages/macros.ts b/packages/nuxt/src/pages/macros.ts index a15e9fea8a..ec4e9a7ab6 100644 --- a/packages/nuxt/src/pages/macros.ts +++ b/packages/nuxt/src/pages/macros.ts @@ -16,7 +16,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi name: 'nuxt:pages-macros-transform', enforce: 'post', transformInclude (id) { - if (!id || id.startsWith('\x00')) { return } + if (!id || id.startsWith('\x00')) { return false } const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href)) return pathname.endsWith('.vue') || !!parseQuery(search).macro }, @@ -26,7 +26,12 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi function result () { if (s.hasChanged()) { - return { code: s.toString(), map: options.sourcemap && s.generateMap({ source: id, includeContent: true }) } + return { + code: s.toString(), + map: options.sourcemap + ? s.generateMap({ source: id, includeContent: true }) + : undefined + } } } @@ -35,7 +40,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi for (const macro in options.macros) { const match = code.match(new RegExp(`\\b${macro}\\s*\\(\\s*`)) if (match?.[0]) { - s.overwrite(match.index, match.index + match[0].length, `/*#__PURE__*/ false && ${match[0]}`) + s.overwrite(match.index!, match.index! + match[0].length, `/*#__PURE__*/ false && ${match[0]}`) } } @@ -113,13 +118,13 @@ function extractObject (code: string) { // Strip comments code = code.replace(/^\s*\/\/.*$/gm, '') - const stack = [] + const stack: string[] = [] let result = '' do { if (stack[0] === code[0] && result.slice(-1) !== '\\') { stack.shift() } else if (code[0] in starts && !QUOTE_RE.test(stack[0])) { - stack.unshift(starts[code[0]]) + stack.unshift(starts[code[0] as keyof typeof starts]) } result += code[0] code = code.slice(1) diff --git a/packages/vite/package.json b/packages/vite/package.json index d1c8aa06db..8d7a5ffe72 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -47,7 +47,7 @@ "rollup": "^2.78.0", "rollup-plugin-visualizer": "^5.7.1", "ufo": "^0.8.5", - "unplugin": "^0.9.0", + "unplugin": "^0.9.2", "vite": "~3.0.7", "vite-node": "^0.21.1", "vite-plugin-checker": "^0.4.9", diff --git a/packages/vite/src/client.ts b/packages/vite/src/client.ts index f254a365f2..35001c19ed 100644 --- a/packages/vite/src/client.ts +++ b/packages/vite/src/client.ts @@ -71,11 +71,11 @@ export async function buildClient (ctx: ViteBuildContext) { // In build mode we explicitly override any vite options that vite is relying on // to detect whether to inject production or development code (such as HMR code) if (!ctx.nuxt.options.dev) { - clientConfig.server.hmr = false + clientConfig.server!.hmr = false } // We want to respect users' own rollup output options - ctx.config.build.rollupOptions = defu(ctx.config.build.rollupOptions, { + ctx.config.build!.rollupOptions = defu(ctx.config.build!.rollupOptions!, { output: { // https://github.com/vitejs/vite/tree/main/packages/vite/src/node/build.ts#L464-L478 assetFileNames: ctx.nuxt.options.dev ? undefined : withoutLeadingSlash(join(ctx.nuxt.options.app.buildAssetsDir, '[name].[hash].[ext]')), @@ -84,7 +84,7 @@ export async function buildClient (ctx: ViteBuildContext) { } }) - if (clientConfig.server.hmr !== false) { + if (clientConfig.server && clientConfig.server.hmr !== false) { const hmrPortDefault = 24678 // Vite's default HMR port const hmrPort = await getPort({ port: hmrPortDefault, @@ -99,7 +99,7 @@ export async function buildClient (ctx: ViteBuildContext) { // Add analyze plugin if needed if (ctx.nuxt.options.build.analyze) { - clientConfig.plugins.push(...await import('./plugins/analyze').then(r => r.analyzePlugin(ctx))) + clientConfig.plugins!.push(...await import('./plugins/analyze').then(r => r.analyzePlugin(ctx))) } await ctx.nuxt.callHook('vite:extendConfig', clientConfig, { isClient: true, isServer: false }) @@ -113,9 +113,9 @@ export async function buildClient (ctx: ViteBuildContext) { const BASE_RE = new RegExp(`^${escapeRE(withTrailingSlash(withLeadingSlash(baseURL)))}`) const viteMiddleware: Connect.NextHandleFunction = (req, res, next) => { // Workaround: vite devmiddleware modifies req.url - const originalURL = req.url - req.url = req.url.replace(BASE_RE, '/') - viteServer.middlewares.handle(req, res, (err) => { + const originalURL = req.url! + req.url = originalURL.replace(BASE_RE, '/') + viteServer.middlewares.handle(req, res, (err: unknown) => { req.url = originalURL next(err) }) diff --git a/packages/vite/src/css.ts b/packages/vite/src/css.ts index 71b05746d4..d5338e4ef9 100644 --- a/packages/vite/src/css.ts +++ b/packages/vite/src/css.ts @@ -4,7 +4,7 @@ import type { ViteOptions } from './vite' import { distDir } from './dirs' export function resolveCSSOptions (nuxt: Nuxt): ViteOptions['css'] { - const css: ViteOptions['css'] & { postcss: Exclude } = { + const css: ViteOptions['css'] & { postcss: NonNullable['postcss'], string>> } = { postcss: { plugins: [] } diff --git a/packages/vite/src/dev-bundler.ts b/packages/vite/src/dev-bundler.ts index f9306649da..cee67ac425 100644 --- a/packages/vite/src/dev-bundler.ts +++ b/packages/vite/src/dev-bundler.ts @@ -55,7 +55,7 @@ async function transformRequest (opts: TransformOptions, id: string) { const externalId = id.replace(/\?v=\w+$|^\/@fs/, '') if (await opts.isExternal(externalId)) { - const path = builtinModules.includes(externalId.split('node:').pop()) + const path = builtinModules.includes(externalId.split('node:').pop()!) ? externalId : isAbsolute(externalId) ? pathToFileURL(externalId).href : externalId return { @@ -90,7 +90,7 @@ ${res.code || '/* empty */'}; return { code, deps: res.deps || [], dynamicDeps: res.dynamicDeps || [] } } -async function transformRequestRecursive (opts: TransformOptions, id, parent = '', chunks: Record = {}) { +async function transformRequestRecursive (opts: TransformOptions, id: string, parent = '', chunks: Record = {}) { if (chunks[id]) { chunks[id].parents.push(parent) return @@ -111,7 +111,7 @@ async function transformRequestRecursive (opts: TransformOptions, id, parent = ' } export async function bundleRequest (opts: TransformOptions, entryURL: string) { - const chunks = await transformRequestRecursive(opts, entryURL) + const chunks = (await transformRequestRecursive(opts, entryURL))! const listIds = (ids: string[]) => ids.map(id => `// - ${id} (${hashId(id)})`).join('\n') const chunksCode = chunks.map(chunk => ` @@ -227,7 +227,7 @@ async function __instantiateModule__(url, urlStack) { } export async function initViteDevBundler (ctx: ViteBuildContext, onBuild: () => Promise) { - const viteServer = ctx.ssrServer + const viteServer = ctx.ssrServer! const options: TransformOptions = { viteServer, isExternal: createIsExternal(viteServer, ctx.nuxt.options.rootDir) diff --git a/packages/vite/src/manifest.ts b/packages/vite/src/manifest.ts index 068c77da7a..2461ffec4a 100644 --- a/packages/vite/src/manifest.ts +++ b/packages/vite/src/manifest.ts @@ -39,7 +39,7 @@ export async function writeManifest (ctx: ViteBuildContext, css: string[] = []) } for (const item of ['css', 'assets']) { if (clientManifest[key][item]) { - clientManifest[key][item] = clientManifest[key][item].map(i => i.replace(BASE_RE, '')) + clientManifest[key][item] = clientManifest[key][item].map((i: string) => i.replace(BASE_RE, '')) } } } diff --git a/packages/vite/src/plugins/analyze.ts b/packages/vite/src/plugins/analyze.ts index 764cb85f67..9a33621117 100644 --- a/packages/vite/src/plugins/analyze.ts +++ b/packages/vite/src/plugins/analyze.ts @@ -17,7 +17,6 @@ export function analyzePlugin (ctx: ViteBuildContext): Plugin[] { })) bundle.modules = Object.fromEntries(minifiedEntries) } - return null } }, // @ts-ignore diff --git a/packages/vite/src/plugins/cache-dir.ts b/packages/vite/src/plugins/cache-dir.ts index 6d931ae78e..8ee8f0df14 100644 --- a/packages/vite/src/plugins/cache-dir.ts +++ b/packages/vite/src/plugins/cache-dir.ts @@ -6,7 +6,7 @@ export function cacheDirPlugin (rootDir: string, name: string) { return { name: 'nuxt:cache-dir', configResolved (resolvedConfig) { - // @ts-ignore + // @ts-expect-error resolvedConfig.optimizeCacheDir = optimizeCacheDir } } diff --git a/packages/vite/src/plugins/composable-keys.ts b/packages/vite/src/plugins/composable-keys.ts index fd8fbd644e..4b365ee473 100644 --- a/packages/vite/src/plugins/composable-keys.ts +++ b/packages/vite/src/plugins/composable-keys.ts @@ -8,8 +8,8 @@ import type { CallExpression } from 'estree' import { parseURL } from 'ufo' export interface ComposableKeysOptions { - sourcemap?: boolean - rootDir?: string + sourcemap: boolean + rootDir: string } const keyedFunctions = [ @@ -17,7 +17,7 @@ const keyedFunctions = [ ] const KEYED_FUNCTIONS_RE = new RegExp(`(${keyedFunctions.join('|')})`) -export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptions = {}) => { +export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptions) => { return { name: 'nuxt:composable-keys', enforce: 'post', @@ -34,9 +34,10 @@ export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptio sourceType: 'module', ecmaVersion: 'latest' }), { - enter (node: CallExpression) { - if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier') { return } - if (keyedFunctions.includes(node.callee.name) && node.arguments.length < 4) { + enter (_node) { + if (_node.type !== 'CallExpression' || (_node as CallExpression).callee.type !== 'Identifier') { return } + const node: CallExpression = _node as CallExpression + if (keyedFunctions.includes((node.callee as any).name) && node.arguments.length < 4) { const end = (node as any).end s.appendLeft( codeIndex + end - 1, @@ -48,7 +49,9 @@ export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptio if (s.hasChanged()) { return { code: s.toString(), - map: options.sourcemap && s.generateMap({ source: id, includeContent: true }) + map: options.sourcemap + ? s.generateMap({ source: id, includeContent: true }) + : undefined } } } diff --git a/packages/vite/src/plugins/virtual.ts b/packages/vite/src/plugins/virtual.ts index f1c2ff9996..b6c083ed24 100644 --- a/packages/vite/src/plugins/virtual.ts +++ b/packages/vite/src/plugins/virtual.ts @@ -5,7 +5,7 @@ const PREFIX = 'virtual:nuxt:' export default function virtual (vfs: Record): Plugin { const extensions = ['', '.ts', '.vue', '.mjs', '.cjs', '.js', '.json'] - const resolveWithExt = (id) => { + const resolveWithExt = (id: string) => { for (const ext of extensions) { const rId = id + ext if (rId in vfs) { diff --git a/packages/vite/src/server.ts b/packages/vite/src/server.ts index bdae8491d4..0983ae264b 100644 --- a/packages/vite/src/server.ts +++ b/packages/vite/src/server.ts @@ -92,9 +92,10 @@ export async function buildServer (ctx: ViteBuildContext) { format: 'module' }, onwarn (warning, rollupWarn) { - if (!['UNUSED_EXTERNAL_IMPORT'].includes(warning.code)) { - rollupWarn(warning) + if (warning.code && ['UNUSED_EXTERNAL_IMPORT'].includes(warning.code)) { + return } + rollupWarn(warning) } } }, @@ -113,7 +114,7 @@ export async function buildServer (ctx: ViteBuildContext) { // Add type-checking if (ctx.nuxt.options.typescript.typeCheck === true || (ctx.nuxt.options.typescript.typeCheck === 'build' && !ctx.nuxt.options.dev)) { const checker = await import('vite-plugin-checker').then(r => r.default) - serverConfig.plugins.push(checker({ + serverConfig.plugins!.push(checker({ vueTsc: { tsconfigPath: await resolveTSConfig(ctx.nuxt.options.rootDir) } diff --git a/packages/vite/src/utils/external.ts b/packages/vite/src/utils/external.ts index 09764a3393..c2e4fd8ef3 100644 --- a/packages/vite/src/utils/external.ts +++ b/packages/vite/src/utils/external.ts @@ -6,11 +6,11 @@ export function createIsExternal (viteServer: ViteDevServer, rootDir: string) { inline: [ /virtual:/, /\.ts$/, - ...ExternalsDefaults.inline, + ...ExternalsDefaults.inline || [], ...viteServer.config.ssr.noExternal as string[] ], external: [ - ...viteServer.config.ssr.external, + ...viteServer.config.ssr.external || [], /node_modules/ ], resolve: { diff --git a/packages/vite/src/utils/index.ts b/packages/vite/src/utils/index.ts index 94b2bd5b13..b17df0679d 100644 --- a/packages/vite/src/utils/index.ts +++ b/packages/vite/src/utils/index.ts @@ -28,12 +28,12 @@ export function hashId (id: string) { return '$id_' + hash(id) } -export function readDirRecursively (dir: string) { +export function readDirRecursively (dir: string): string[] { return readdirSync(dir).reduce((files, file) => { const name = join(dir, file) const isDirectory = statSync(name).isDirectory() return isDirectory ? [...files, ...readDirRecursively(name)] : [...files, name] - }, []) + }, [] as string[]) } export async function isDirectory (path: string) { diff --git a/packages/vite/src/utils/wpfs.ts b/packages/vite/src/utils/wpfs.ts index fb67cd2b2d..612bc250fb 100644 --- a/packages/vite/src/utils/wpfs.ts +++ b/packages/vite/src/utils/wpfs.ts @@ -4,4 +4,4 @@ import fse from 'fs-extra' export const wpfs = { ...fse, join -} as any +} as typeof fse & { join: typeof join } diff --git a/packages/vite/src/vite-node.ts b/packages/vite/src/vite-node.ts index 699255b2a0..55bfd7c2af 100644 --- a/packages/vite/src/vite-node.ts +++ b/packages/vite/src/vite-node.ts @@ -25,9 +25,8 @@ export function viteNodePlugin (ctx: ViteBuildContext): VitePlugin { }, handleHotUpdate ({ file, server }) { function markInvalidate (mod: ModuleNode) { - if (invalidates.has(mod.id)) { - return - } + if (!mod.id) { return } + if (invalidates.has(mod.id)) { return } invalidates.add(mod.id) for (const importer of mod.importers) { markInvalidate(importer) @@ -49,7 +48,7 @@ export function registerViteNodeMiddleware (ctx: ViteBuildContext) { } function getManifest (ctx: ViteBuildContext) { - const css = Array.from(ctx.ssrServer.moduleGraph.urlToModuleMap.keys()) + const css = Array.from(ctx.ssrServer!.moduleGraph.urlToModuleMap.keys()) .filter(i => isCSS(i)) const manifest = normalizeViteManifest({ @@ -81,7 +80,7 @@ function createViteNodeMiddleware (ctx: ViteBuildContext, invalidates: Set { // When a file has been invalidated, we also invalidate the entry module if (invalidates.size) { - for (const key of ctx.ssrServer.moduleGraph.fileToModulesMap.keys()) { + for (const key of ctx.ssrServer!.moduleGraph.fileToModulesMap.keys()) { if (key.startsWith(ctx.nuxt.options.appDir + '/entry')) { invalidates.add(key) } @@ -93,7 +92,7 @@ function createViteNodeMiddleware (ctx: ViteBuildContext, invalidates: Set { - const viteServer = ctx.ssrServer + const viteServer = ctx.ssrServer! const node: ViteNodeServer = new ViteNodeServer(viteServer, { deps: { inline: [ @@ -117,7 +116,7 @@ function createViteNodeMiddleware (ctx: ViteBuildContext, invalidates: Set { - const moduleId = decodeURI(event.req.url).substring(1) + const moduleId = decodeURI(event.req.url!).substring(1) if (moduleId === '/') { throw createError({ statusCode: 400 }) } @@ -144,7 +143,7 @@ export async function initViteNodeServer (ctx: ViteBuildContext) { baseURL: `${protocol}://${host}:${port}/__nuxt_vite_node__`, root: ctx.nuxt.options.srcDir, entryPath, - base: ctx.ssrServer.config.base || '/_nuxt/' + base: ctx.ssrServer!.config.base || '/_nuxt/' } process.env.NUXT_VITE_NODE_OPTIONS = JSON.stringify(viteNodeServerOptions) diff --git a/packages/vite/src/vite.ts b/packages/vite/src/vite.ts index fcaacedf23..d4d3ca8c22 100644 --- a/packages/vite/src/vite.ts +++ b/packages/vite/src/vite.ts @@ -30,7 +30,7 @@ export interface ViteBuildContext { export async function bundle (nuxt: Nuxt) { const ctx: ViteBuildContext = { nuxt, - entry: null, + entry: null!, config: vite.mergeConfig( { resolve: { @@ -85,8 +85,8 @@ export async function bundle (nuxt: Nuxt) { // In build mode we explicitly override any vite options that vite is relying on // to detect whether to inject production or development code (such as HMR code) if (!nuxt.options.dev) { - ctx.config.server.watch = undefined - ctx.config.build.watch = undefined + ctx.config.server!.watch = undefined + ctx.config.build!.watch = undefined } await nuxt.callHook('vite:extend', ctx) diff --git a/packages/vite/tsconfig.json b/packages/vite/tsconfig.json new file mode 100644 index 0000000000..35a35f0192 --- /dev/null +++ b/packages/vite/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "strict": true, + "noImplicitAny": true + }, + "include": [ + "./src/**/*.ts", + "./test/**/*.ts" + ] +} diff --git a/packages/webpack/package.json b/packages/webpack/package.json index 9447f68eb6..c969be502f 100644 --- a/packages/webpack/package.json +++ b/packages/webpack/package.json @@ -45,7 +45,7 @@ "style-resources-loader": "^1.5.0", "time-fix-plugin": "^2.0.7", "ufo": "^0.8.5", - "unplugin": "^0.9.0", + "unplugin": "^0.9.2", "url-loader": "^4.1.1", "vue-bundle-renderer": "^0.4.2", "vue-loader": "^17.0.0", diff --git a/yarn.lock b/yarn.lock index 6ee6b42105..d3880b7cb2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1806,7 +1806,7 @@ __metadata: rollup-plugin-visualizer: ^5.7.1 ufo: ^0.8.5 unbuild: latest - unplugin: ^0.9.0 + unplugin: ^0.9.2 vite: ~3.0.7 vite-node: ^0.21.1 vite-plugin-checker: ^0.4.9 @@ -1857,7 +1857,7 @@ __metadata: time-fix-plugin: ^2.0.7 ufo: ^0.8.5 unbuild: latest - unplugin: ^0.9.0 + unplugin: ^0.9.2 url-loader: ^4.1.1 vue: 3.2.37 vue-bundle-renderer: ^0.4.2 @@ -9959,7 +9959,7 @@ __metadata: unctx: ^2.0.1 unenv: ^0.5.4 unimport: ^0.6.7 - unplugin: ^0.9.0 + unplugin: ^0.9.2 untyped: ^0.4.5 vue: ^3.2.37 vue-bundle-renderer: ^0.4.2 @@ -13105,7 +13105,7 @@ __metadata: languageName: node linkType: hard -"unplugin@npm:^0.9.0": +"unplugin@npm:^0.9.0, unplugin@npm:^0.9.2": version: 0.9.2 resolution: "unplugin@npm:0.9.2" dependencies: