vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
62 lines (61 loc) • 2.61 kB
JavaScript
// Suppress Rollup warnings `Generated an empty chunk: "index.page.server"`
export { pluginSuppressRollupWarning };
function pluginSuppressRollupWarning() {
return {
name: 'vike:build:pluginSuppressRollupWarning',
apply: 'build',
enforce: 'post',
async configResolved(config) {
const onWarnOriginal = config.build.rollupOptions.onwarn;
config.build.rollupOptions.onwarn = function (warning, warn) {
// Suppress
if (suppressUnusedImport(warning))
return;
if (suppressEmptyBundle(warning))
return;
if (suppressUseClientDirective(warning))
return;
// Pass through
if (onWarnOriginal) {
onWarnOriginal.apply(this, arguments);
}
else {
warn(warning);
}
};
},
};
}
/** Suppress warning about Rollup removing the React Server Components `"use client";` directives */
function suppressUseClientDirective(warning) {
return warning.code === 'MODULE_LEVEL_DIRECTIVE' && warning.message.includes('"use client"');
}
/** Suppress warning about generating empty chunks in dist/ */
function suppressEmptyBundle(warning) {
return warning.code === 'EMPTY_BUNDLE';
}
/** Suppress warning about unused import statements */
function suppressUnusedImport(warning) {
if (warning.code !== 'UNUSED_EXTERNAL_IMPORT')
return false;
// I guess it's expected that JSX contains unused `import React from 'react'`
if (warning.exporter === 'react' && warning.names?.includes('default')) {
warning.names = warning.names.filter((n) => n !== 'default');
if (warning.names.length === 0)
return true;
}
// Suppress:
// ```
// "untrack" is imported from external module "solid-js" but never used in "pages/star-wars/index/+Page.tsx", "pages/index/Counter.tsx", "components/Link.tsx", "pages/todo/TodoList.tsx" and "pages/todo/+Page.tsx".
// ```
// I'm guessing Solid's transformer injects `import { untrack } from 'solid-js'`
if (warning.exporter === 'solid-js' && warning.names?.includes('untrack')) {
warning.names = warning.names.filter((n) => n !== 'untrack');
if (warning.names.length === 0)
return true;
}
// If some library does something unexpected, we suppress since it isn't actionable
if (warning.ids?.every((id) => id.includes('/node_modules/')))
return true;
return false;
}