Skip to content
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
53 changes: 29 additions & 24 deletions packages/pack-shared/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,45 @@ export type RustifiedEnv = { name: string; value: string }[];

export interface ExperimentalConfig {}

export type TurbopackRuleConfigItem =
| TurbopackRuleConfigItemOptions
| { [condition: string]: TurbopackRuleConfigItem }
| false;
export type JSONValue =
| string
| number
| boolean
| JSONValue[]
| { [k: string]: JSONValue };

// At the moment, Turbopack options must be JSON-serializable, so restrict values.
export type TurbopackLoaderOptions = Record<string, JSONValue>;

/**
* @deprecated Use `TurbopackRuleConfigItem` instead.
*/
export type TurbopackLoaderItem =
| string
| {
loader: string;
// At the moment, Turbopack options must be JSON-serializable, so restrict values.
options: Record<string, JSONValue>;
options?: TurbopackLoaderOptions;
};

export type TurbopackRuleConfigItemOrShortcut =
| TurbopackLoaderItem[]
| TurbopackRuleConfigItem;
export type TurbopackLoaderBuiltinCondition =
| "browser"
| "foreign"
| "development"
| "production"
| "node"
| "edge-light";

export type TurbopackRuleConfigItemOptions = {
export type TurbopackRuleCondition =
| { all: TurbopackRuleCondition[] }
| { any: TurbopackRuleCondition[] }
| { not: TurbopackRuleCondition }
| TurbopackLoaderBuiltinCondition
| {
path?: string | RegExp;
content?: RegExp;
};

export type TurbopackRuleConfigItem = {
loaders: TurbopackLoaderItem[];
as?: string;
};

export type TurbopackRuleCondition = {
path: string | RegExp;
condition?: TurbopackRuleCondition;
};
Comment thread
fireairforce marked this conversation as resolved.

export interface ModuleOptions {
Expand Down Expand Up @@ -229,13 +241,6 @@ export interface EmotionConfig {
};
}

export type JSONValue =
| string
| number
| boolean
| JSONValue[]
| { [k: string]: JSONValue };

export interface BundleOptions {
/**
* The utoo pack configs.
Expand Down
45 changes: 0 additions & 45 deletions packages/pack/src/core/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
ConfigComplete,
TurbopackLoaderItem,
TurbopackRuleConfigItem,
TurbopackRuleConfigItemOptions,
TurbopackRuleConfigItemOrShortcut,
} from "../config/types";
import { rustifyEnv } from "../utils/common";
import { runLoaderWorkerPool } from "./loaderWorkerPool";
Expand Down Expand Up @@ -42,45 +40,6 @@ async function withErrorCause<T>(fn: () => Promise<T>): Promise<T> {
}
}

function ensureLoadersHaveSerializableOptions(
turbopackRules: Record<string, TurbopackRuleConfigItemOrShortcut>,
) {
for (const [glob, rule] of Object.entries(turbopackRules)) {
if (Array.isArray(rule)) {
checkLoaderItems(rule, glob);
} else {
checkConfigItem(rule, glob);
}
}

function checkConfigItem(rule: TurbopackRuleConfigItem, glob: string) {
if (!rule) return;
if ("loaders" in rule) {
checkLoaderItems((rule as TurbopackRuleConfigItemOptions).loaders, glob);
} else {
for (const key in rule) {
const inner = rule[key];
if (typeof inner === "object" && inner) {
checkConfigItem(inner, glob);
}
}
}
}

function checkLoaderItems(loaderItems: TurbopackLoaderItem[], glob: string) {
for (const loaderItem of loaderItems) {
if (
typeof loaderItem !== "string" &&
!isDeepStrictEqual(loaderItem, JSON.parse(JSON.stringify(loaderItem)))
) {
throw new Error(
`loader ${loaderItem.loader} for match "${glob}" does not have serializable options. Ensure that options passed are plain JavaScript objects and values.`,
);
}
}
}
}

async function serializeConfig(config: ConfigComplete): Promise<string> {
const configSerializable = { ...config };

Expand All @@ -91,10 +50,6 @@ async function serializeConfig(config: ConfigComplete): Promise<string> {
});
}

if (configSerializable.module?.rules) {
ensureLoadersHaveSerializableOptions(configSerializable.module.rules);
}

if (configSerializable.optimization) {
configSerializable.optimization = { ...configSerializable.optimization };
const { modularizeImports } = configSerializable.optimization;
Expand Down
Loading