in packages/ice/src/service/ServerRunner.ts [251:315]
load: async (args) => {
const id = args.path;
if (externals.includes(id)) {
return { externalize: id };
} else {
let code = '';
// Call esbuild lifecycle `onLoad` of esbuild plugin.
for (const plugin of esbuildPlugins) {
let res: OnLoadResult;
const lifecycles = getPluginLifecycle(plugin, 'onLoad');
for (const [options, callback] of lifecycles) {
const { filter, namespace } = options;
// Remove suffix of id.
const formatedId = id.replace(/\?\w+$/, '');
if (namespace ? namespace == args.namespace : filter && filter.test(formatedId)) {
try {
res = await callback({
namespace: '',
suffix: id.match(/(\?\w+)$/)?.[1] || '',
pluginData: {},
...args,
path: formatedId,
});
// If res is undefined, it means the plugin does not handle the file, fallback to default handler.
if (!res && FALLBACK_LOADERS[path.extname(formatedId)]) {
res = {
loader: FALLBACK_LOADERS[path.extname(formatedId)],
};
}
if (res) {
const { contents, loader } = res;
if (['json', 'text'].includes(loader)) {
if (contents) {
code = `__ice_exports__.default = ${contents}`;
} else {
const contents = await fse.readFile(formatedId, 'utf-8');
code = `__ice_exports__.default = ${loader === 'text' ? JSON.stringify(contents) : contents}`;
}
} else {
code = typeof contents === 'string' ? contents : contents.toString();
}
break;
}
} catch (err) {
logger.error(`Failed to load module ${id}, Esbuild name ${plugin.name}`);
throw err;
}
}
}
if (res) {
break;
}
}
if (!code && !path.isAbsolute(id)) {
// If id is runtime dependency, bundle it and return externalized id.
const bundlePath = await runtimeMeta.bundle(id);
return { externalize: bundlePath };
}
return {
code: await transformJsxRuntime(code),
};
}
},