Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: some bugs #1991

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/core/src/plugin/js/farm-to-vite-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WatchOptions } from 'chokidar';
import type { UserConfig as ViteUserConfig } from 'vite';
import type { UserConfig } from '../../config/types.js';
import { Logger } from '../../index.js';
Expand Down Expand Up @@ -389,7 +390,9 @@ export function viteConfigToFarmConfig(
};
}
// TODO think about vite has two watch options `server.watch` | `build.watch`
farmConfig.watch = config.server.watch;
if (config.mode === 'development') {
farmConfig.watch = config.server.watch;
}
}

if (typeof config.server.host === 'string') {
Expand All @@ -406,6 +409,9 @@ export function viteConfigToFarmConfig(
}

if (config.build) {
if (config.mode === 'production') {
farmConfig.watch = config.build.watch as WatchOptions;
}
farmConfig.compilation.output ??= {};
farmConfig.compilation.output.path = config.build.outDir;

Expand Down
85 changes: 83 additions & 2 deletions packages/core/src/plugin/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ export function processVitePlugin(
mode: CompilationMode
) {
const processPlugin = (plugin: any) => {
let vitePluginAdapter = new VitePluginAdapter(
const vitePluginAdapter = new VitePluginAdapter(
plugin as any,
userConfig,
filters,
mode
);
// @ts-ignore
vitePluginAdapter = convertPlugin(vitePluginAdapter);
convertPluginVite(vitePluginAdapter);
jsPlugins.push(vitePluginAdapter);
};

Expand Down Expand Up @@ -161,3 +161,84 @@ export function convertPlugin(plugin: JsPlugin) {
);
}
}

export function convertPluginVite(plugin: JsPlugin): void {
if (
plugin.transform &&
!plugin.transform.filters?.moduleTypes &&
!plugin.transform.filters?.resolvedPaths
) {
throw new Error(
`transform hook of plugin ${plugin.name} must have at least one filter(like moduleTypes or resolvedPaths)`
);
}
if (plugin.transform) {
if (!plugin.transform.filters.moduleTypes) {
plugin.transform.filters.moduleTypes = [];
} else if (!plugin.transform.filters.resolvedPaths) {
plugin.transform.filters.resolvedPaths = [];
}
}

if (plugin.renderResourcePot) {
plugin.renderResourcePot.filters ??= {};

if (
!plugin.renderResourcePot?.filters?.moduleIds &&
!plugin.renderResourcePot?.filters?.resourcePotTypes
) {
throw new Error(
`renderResourcePot hook of plugin ${plugin.name} must have at least one filter(like moduleIds or resourcePotTypes)`
);
}

if (!plugin.renderResourcePot.filters?.resourcePotTypes) {
plugin.renderResourcePot.filters.resourcePotTypes = [];
} else if (!plugin.renderResourcePot.filters?.moduleIds) {
plugin.renderResourcePot.filters.moduleIds = [];
}
}

if (plugin.augmentResourceHash) {
plugin.augmentResourceHash.filters ??= {};

if (
!plugin.augmentResourceHash?.filters?.moduleIds &&
!plugin.augmentResourceHash?.filters?.resourcePotTypes
) {
throw new Error(
`augmentResourceHash hook of plugin ${plugin.name} must have at least one filter(like moduleIds or resourcePotTypes)`
);
}

if (!plugin.augmentResourceHash.filters?.resourcePotTypes) {
plugin.augmentResourceHash.filters.resourcePotTypes = [];
} else if (!plugin.augmentResourceHash.filters?.moduleIds) {
plugin.augmentResourceHash.filters.moduleIds = [];
}
}

if (plugin.resolve?.filters?.importers?.length) {
plugin.resolve.filters.importers =
plugin.resolve.filters.importers.map(normalizeFilterPath);
}

if (plugin.load?.filters?.resolvedPaths?.length) {
plugin.load.filters.resolvedPaths =
plugin.load.filters.resolvedPaths.map(normalizeFilterPath);
}

if (plugin.transform?.filters?.resolvedPaths?.length) {
plugin.transform.filters.resolvedPaths =
plugin.transform.filters.resolvedPaths.map(normalizeFilterPath);
}
if (plugin.augmentResourceHash?.filters?.moduleIds) {
plugin.augmentResourceHash.filters.moduleIds =
plugin.augmentResourceHash.filters.moduleIds.map(normalizeFilterPath);
}

if (plugin.renderResourcePot?.filters?.moduleIds) {
plugin.renderResourcePot.filters.moduleIds =
plugin.renderResourcePot.filters.moduleIds.map(normalizeFilterPath);
}
}
3 changes: 2 additions & 1 deletion packages/core/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class Server extends httpServer {
*/
constructor(readonly inlineConfig: FarmCliOptions & UserConfig) {
super();
this.logger = new Logger();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I'm not quite sure what this line of code does, it looks like we're correctly creating a logger when we call createServer anyway, is it really necessary here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to resolveConfig is made after the server instance, which will cause if an error occurs, the logger is null.

const server = new Server(inlineConfig)
// not has logger

server.createServer()

// has logger

Maybe we can find other ways to solve this problem. createServer is mainly for restart Server

}

/**
Expand Down Expand Up @@ -436,7 +437,7 @@ export class Server extends httpServer {
}
} catch (error) {
this.resolvedUserConfig.logger.error(
`start farm dev server error: ${error} \n ${error.stack}`
`Start DevServer Error: ${error} \n ${error.stack}`
);
// throw error;
}
Expand Down
17 changes: 7 additions & 10 deletions packages/core/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ export class Logger implements ILogger {
this.customLogger.logMessage(level, message, color, clearScreen);
return;
}
const loggerMethod =
level in LOGGER_METHOD
? LOGGER_METHOD[level as keyof typeof LOGGER_METHOD]
: 'log';
if (this.levelValues[level] <= this.levelValues[level]) {

const minLevel = process.env.LOG_LEVEL || 'info';
if (
this.levelValues[level] >= this.levelValues[minLevel as LogLevelNames]
) {
if (this.canClearScreen && clearScreen) {
this.clear();
}
Expand All @@ -145,12 +145,9 @@ export class Logger implements ILogger {
} else {
loggerMessage = message.message;
}
loggerMessage = color ? color(loggerMessage) : loggerMessage;

if (color && typeof message === 'string') {
loggerMessage = color(loggerMessage);
}

console[loggerMethod](prefixColored + loggerMessage);
console.log(prefixColored + loggerMessage);
}
}

Expand Down
11 changes: 0 additions & 11 deletions packages/utils/src/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,6 @@ export function interpolateColor(
export const PersistentCacheBrand =
brandColor('⚡️') +
gradientString(`FULL EXTREME!`, [
// gradientPurpleColor,
// interpolateColor(gradientPurpleColor, gradientPinkColor, 0.1),
// interpolateColor(gradientPurpleColor, gradientPinkColor, 0.2),
// interpolateColor(gradientPurpleColor, gradientPinkColor, 0.3),
// interpolateColor(gradientPurpleColor, gradientPinkColor, 0.4),
// interpolateColor(gradientPurpleColor, gradientPinkColor, 0.5),
// interpolateColor(gradientPurpleColor, gradientPinkColor, 0.6),
// interpolateColor(gradientPurpleColor, gradientPinkColor, 0.7),
// interpolateColor(gradientPurpleColor, gradientPinkColor, 0.8),
// interpolateColor(gradientPurpleColor, gradientPinkColor, 0.9),
// gradientPinkColor
gradientPurpleColor,
interpolateColor(gradientPurpleColor, gradientPinkColor, 0.2),
interpolateColor(gradientPurpleColor, gradientPinkColor, 0.4),
Expand Down
Loading
Loading