From 83ff79d5e360f66d4bd9dcd7d803ed81ea95a3ce Mon Sep 17 00:00:00 2001 From: Wout Mertens Date: Mon, 5 Aug 2024 11:37:37 +0200 Subject: [PATCH] chore(docs): fix several warnings etc --- packages/docs/package.json | 2 +- packages/docs/vite.config.mts | 57 +++++++++++++++---- .../qwik-city/src/buildtime/vite/image-jsx.ts | 19 ++++--- .../qwik-city/src/buildtime/vite/plugin.ts | 2 +- .../qwik/src/optimizer/src/plugins/rollup.ts | 3 +- .../qwik/src/optimizer/src/plugins/vite.ts | 3 +- 6 files changed, 64 insertions(+), 22 deletions(-) diff --git a/packages/docs/package.json b/packages/docs/package.json index 464d94ac4a6..1553a3546ed 100644 --- a/packages/docs/package.json +++ b/packages/docs/package.json @@ -69,7 +69,7 @@ "scripts": { "build": "qwik build", "build.client": "vite build", - "build.preview": "vite build --ssr src/entry.preview.tsx", + "build.preview": "NODE_OPTIONS=--max-old-space-size=8192 vite build --ssr src/entry.preview.tsx", "build.repl-sw": "vite --config vite.config-repl-sw.mts build", "build.server": "NODE_OPTIONS=--max-old-space-size=8192 vite build -c adapters/cloudflare-pages/vite.config.mts", "build.showcase": "pnpm node scripts/showcase.js", diff --git a/packages/docs/vite.config.mts b/packages/docs/vite.config.mts index db8be7e5109..a66ba855cb0 100644 --- a/packages/docs/vite.config.mts +++ b/packages/docs/vite.config.mts @@ -4,7 +4,7 @@ import { qwikInsights } from '@builder.io/qwik-labs/vite'; import { qwikReact } from '@builder.io/qwik-react/vite'; import { qwikVite } from '@builder.io/qwik/optimizer'; import path, { resolve } from 'node:path'; -import { defineConfig, loadEnv } from 'vite'; +import { defineConfig, loadEnv, type Plugin } from 'vite'; import Inspect from 'vite-plugin-inspect'; import { examplesData, playgroundData, rawSource, tutorialData } from './vite.repl-apps'; import { sourceResolver } from './vite.source-resolver'; @@ -12,6 +12,46 @@ import { sourceResolver } from './vite.source-resolver'; export const PUBLIC_QWIK_INSIGHT_KEY = loadEnv('', '.', 'PUBLIC').PUBLIC_QWIK_INSIGHTS_KEY; const docsDir = new URL(import.meta.url).pathname; +// https://github.com/vitejs/vite/issues/15012#issuecomment-1825035992 +const muteWarningsPlugin = (warningsToIgnore: string[][]): Plugin => { + const mutedMessages = new Set(); + return { + name: 'mute-warnings', + enforce: 'pre', + config: (userConfig) => { + const origOnLog = userConfig.build?.rollupOptions?.onLog; + return { + build: { + rollupOptions: { + onLog(type, warning, defaultHandler) { + if (type === 'warn') { + if (warning.code) { + const muted = warningsToIgnore.find( + ([code, message]) => code == warning.code && warning.message.includes(message) + ); + + if (muted) { + mutedMessages.add(muted.join()); + return; + } + } + } + origOnLog ? origOnLog(type, warning, defaultHandler) : defaultHandler(type, warning); + }, + }, + }, + }; + }, + closeBundle() { + const diff = warningsToIgnore.filter((x) => !mutedMessages.has(x.join())); + if (diff.length > 0) { + this.warn('Some of your muted warnings never appeared during the build process:'); + diff.forEach((m) => this.warn(`- ${m.join(': ')}`)); + } + }, + }; +}; + export default defineConfig(async () => { const { default: rehypePrettyCode } = await import('rehype-pretty-code'); @@ -58,6 +98,11 @@ export default defineConfig(async () => { }, plugins: [ + // some imported react code has sourcemap issues + muteWarningsPlugin([ + ['SOURCEMAP_ERROR', "Can't resolve original location of error"], + ['MODULE_LEVEL_DIRECTIVE', 'use client'], + ]), rawSource(), qwikCity({ mdxPlugins: { @@ -128,16 +173,6 @@ export default defineConfig(async () => { build: { sourcemap: true, rollupOptions: { - // For some unknown reason, types don't work from tsc - // Try removing these any casts and see if it works - onLog(level: any, log: any, defaultHandler: any) { - if (level == 'warn' && log.code === 'MODULE_LEVEL_DIRECTIVE') { - // Suppress errors like these: - // FILE Module level directives cause errors when bundled, "use client" in FILE was ignored. - return; - } - defaultHandler(level, log); - }, output: { assetFileNames: 'assets/[hash]-[name].[ext]', }, diff --git a/packages/qwik-city/src/buildtime/vite/image-jsx.ts b/packages/qwik-city/src/buildtime/vite/image-jsx.ts index f597f241d7b..9b708c22f18 100644 --- a/packages/qwik-city/src/buildtime/vite/image-jsx.ts +++ b/packages/qwik-city/src/buildtime/vite/image-jsx.ts @@ -88,23 +88,28 @@ export function imagePlugin(userOpts?: QwikCityVitePluginOptions): PluginOption[ this.error(`Image '${id}' could not be optimized to JSX`); } const index = code.indexOf('export default'); - return ( - code.slice(0, index) + - ` + return { + code: + code.slice(0, index) + + ` import { _jsxQ } from '@builder.io/qwik'; const PROPS = {srcSet, width, height}; export default function (props, key, _, dev) { return _jsxQ('img', {...{decoding: 'async', loading: 'lazy'}, ...props}, PROPS, undefined, 3, key, dev); - }` - ); + }`, + map: null, + }; } else if (extension === '.svg') { const { svgAttributes } = optimizeSvg({ code, path: pathId }, userOpts); - return ` + return { + code: ` import { _jsxQ } from '@builder.io/qwik'; const PROPS = ${JSON.stringify(svgAttributes)}; export default function (props, key, _, dev) { return _jsxQ('svg', props, PROPS, undefined, 3, key, dev); - }`; + }`, + map: null, + }; } } return null; diff --git a/packages/qwik-city/src/buildtime/vite/plugin.ts b/packages/qwik-city/src/buildtime/vite/plugin.ts index ff340f484c5..b7ac8dbb904 100644 --- a/packages/qwik-city/src/buildtime/vite/plugin.ts +++ b/packages/qwik-city/src/buildtime/vite/plugin.ts @@ -202,7 +202,7 @@ function qwikCityPlugin(userOpts?: QwikCityVitePluginOptions): any { const fileName = basename(id); if (isMenuFileName(fileName)) { const menuCode = await transformMenu(ctx.opts, id, code); - return menuCode; + return { code: menuCode, map: null }; } if (mdxTransform) { try { diff --git a/packages/qwik/src/optimizer/src/plugins/rollup.ts b/packages/qwik/src/optimizer/src/plugins/rollup.ts index 759cdb128ed..6a06aaeb377 100644 --- a/packages/qwik/src/optimizer/src/plugins/rollup.ts +++ b/packages/qwik/src/optimizer/src/plugins/rollup.ts @@ -39,11 +39,12 @@ export function qwikRollup(qwikRollupOpts: QwikRollupPluginOptions = {}): any { async options(inputOpts) { await qwikPlugin.init(); + const origOnwarn = inputOpts.onwarn; inputOpts.onwarn = (warning, warn) => { if (warning.plugin === 'typescript' && warning.message.includes('outputToFilesystem')) { return; } - warn(warning); + origOnwarn ? origOnwarn(warning, warn) : warn(warning); }; const pluginOpts: QwikPluginOptions = { diff --git a/packages/qwik/src/optimizer/src/plugins/vite.ts b/packages/qwik/src/optimizer/src/plugins/vite.ts index 6b0eca68cc1..b2997a35e02 100644 --- a/packages/qwik/src/optimizer/src/plugins/vite.ts +++ b/packages/qwik/src/optimizer/src/plugins/vite.ts @@ -355,6 +355,7 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any { updatedViteConfig.build!.cssCodeSplit = false; updatedViteConfig.build!.outDir = buildOutputDir; + const origOnwarn = updatedViteConfig.build!.rollupOptions?.onwarn; updatedViteConfig.build!.rollupOptions = { input: opts.input, output: normalizeRollupOutputOptions( @@ -369,7 +370,7 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any { if (warning.plugin === 'typescript' && warning.message.includes('outputToFilesystem')) { return; } - warn(warning); + origOnwarn ? origOnwarn(warning, warn) : warn(warning); }, };