Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(bundle): the official-package and Cocos resolvers step aside for one shared marker, so a subpath of an official package no longer resolves forever

yhhce6c1ddb9642
diff --git a/pipeline/src/bundle/bundlerResolve.ts b/pipeline/src/bundle/bundlerResolve.ts
new file mode 100644
index 000000000..4fc4d3fed
--- /dev/null
+++ b/pipeline/src/bundle/bundlerResolve.ts
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file The `pluginData` a resolve plugin passes when it asks the bundler to resolve a path itself.
+ *
+ * Every plugin that re-resolves must pass this one value and step aside when it sees it. Two plugins that each
+ * recognised only their own marker asked each other for the same path forever.
+ */
+export const BUNDLER_RESOLVES = Symbol('estella:bundler-resolves');
diff --git a/pipeline/src/bundle/cocosCompat.ts b/pipeline/src/bundle/cocosCompat.ts
index 76bc16ee8..4b148ebd3 100644
--- a/pipeline/src/bundle/cocosCompat.ts
+++ b/pipeline/src/bundle/cocosCompat.ts
@@ -28,6 +28,7 @@ import { existsSync, readFileSync, statSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import type { Plugin, PluginBuild } from 'esbuild';
+import { BUNDLER_RESOLVES } from './bundlerResolve';
import { loadEsbuild } from './esbuildRuntime';
/** Changes whenever what this plugin does to a script changes, so a bundle built by an older one is not taken for current. */
@@ -40,7 +41,6 @@ const CC = /^cc(\/env)?$/;
const IMPORTS_CC = /(?:from|import)\s*\(?\s*['"]cc(?:\/env)?['"]/;
const TS = /\.tsx?$/;
-const RESOLVING = Symbol('cocos-compat:resolving');
const PACKAGE_FILE = /^(?:@[^/]+\/[^/]+|[^@./][^/]*)\/.+/;
/** `<dir>/node_modules/<specifier>` for the nearest dir upward that has it, as a file. */
@@ -125,9 +125,9 @@ export function cocosCompatPlugin(): Plugin {
setup(build) {
const renames = readClassNames(build);
build.onResolve({ filter: CC }, async (args) => {
- if (args.pluginData === RESOLVING) return undefined;
+ if (args.pluginData === BUNDLER_RESOLVES) return undefined;
const layer = await build.resolve(COCOS_LAYER, {
- kind: args.kind, importer: args.importer, resolveDir: args.resolveDir, pluginData: RESOLVING,
+ kind: args.kind, importer: args.importer, resolveDir: args.resolveDir, pluginData: BUNDLER_RESOLVES,
});
if (layer.errors.length > 0) {
return { errors: [{ text: `"${args.path}" is the Cocos Creator module, which the engine provides as "${COCOS_LAYER}" — and that did not resolve here: ${layer.errors[0]!.text}` }] };
@@ -142,8 +142,8 @@ export function cocosCompatPlugin(): Plugin {
// Only a path the bundler could not resolve itself falls back to the plain file lookup.
build.onResolve({ filter: PACKAGE_FILE }, async (args) => {
- if (args.pluginData === RESOLVING || args.kind === 'entry-point') return undefined;
- const tried = await build.resolve(args.path, { kind: args.kind, importer: args.importer, resolveDir: args.resolveDir, pluginData: RESOLVING });
+ if (args.pluginData === BUNDLER_RESOLVES || args.kind === 'entry-point') return undefined;
+ const tried = await build.resolve(args.path, { kind: args.kind, importer: args.importer, resolveDir: args.resolveDir, pluginData: BUNDLER_RESOLVES });
if (tried.errors.length === 0) return tried;
const file = packageFileFrom(args.resolveDir, args.path);
return file ? { path: file } : undefined;
diff --git a/pipeline/src/bundle/officialPackages.ts b/pipeline/src/bundle/officialPackages.ts
index b32ff3653..0d3dd410c 100644
--- a/pipeline/src/bundle/officialPackages.ts
+++ b/pipeline/src/bundle/officialPackages.ts
@@ -12,6 +12,7 @@
import { existsSync, readdirSync, readFileSync } from 'node:fs';
import path from 'node:path';
import type { Plugin } from 'esbuild';
+import { BUNDLER_RESOLVES } from './bundlerResolve';
const OFFICIAL = /^(estella-plugin-[^/]+)(\/.+)?$/;
@@ -52,8 +53,6 @@ export function readOfficialPackages(packagesDir: string): OfficialPackages {
return out;
}
-const SKIP = Symbol('official-packages:skip');
-
/** esbuild plugin: `estella-plugin-*` from the project's node_modules if it has
* one, otherwise from `packagesDir`. */
export function officialPackagesPlugin(packagesDir: string): Plugin {
@@ -61,9 +60,9 @@ export function officialPackagesPlugin(packagesDir: string): Plugin {
name: 'estella-official-packages',
setup(build) {
build.onResolve({ filter: OFFICIAL }, async (args) => {
- if (args.pluginData === SKIP) return undefined;
+ if (args.pluginData === BUNDLER_RESOLVES) return undefined;
const own = await build.resolve(args.path, {
- kind: args.kind, importer: args.importer, resolveDir: args.resolveDir, pluginData: SKIP,
+ kind: args.kind, importer: args.importer, resolveDir: args.resolveDir, pluginData: BUNDLER_RESOLVES,
});
if (own.errors.length === 0) return { path: own.path, external: own.external, sideEffects: own.sideEffects };