Unverified Commit d77eb040 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Turbopack: extract core hmr client logic into shared runtime (#88912)

This PR mostly moves existing code. Most of the diff churn is from re-recording shapshots. This PR is nowhere as scary as the diffstat would have you believe.

This extracts the core HMR client logic, abstracting away hooks for platform-specific features like the websocket transport and page reload.
    
In a future PR this will be implemented as an in-process backend for nodejs server hmr.
    
Test Plan: CI, existing hmr tests
parent f7dd09fd
/// <reference path="../../../shared/runtime-types.d.ts" />
/// <reference path="../../runtime/base/dev-globals.d.ts" />
/// <reference path="../../runtime/base/dev-protocol.d.ts" />
/// <reference path="../../runtime/base/dev-extensions.ts" />
/// <reference path="../../../shared/runtime/runtime-types.d.ts" />
/// <reference path="../../../shared/runtime/dev-globals.d.ts" />
/// <reference path="../../../shared/runtime/dev-protocol.d.ts" />
/// <reference path="../../../shared/runtime/dev-extensions.ts" />
type SendMessage = (msg: any) => void
export type WebSocketMessage =
......
......@@ -58,27 +58,6 @@ type ChunkList = {
source: 'entry' | 'dynamic'
}
enum SourceType {
/**
* The module was instantiated because it was included in an evaluated chunk's
* runtime.
* SourceData is a ChunkPath.
*/
Runtime = 0,
/**
* The module was instantiated because a parent module imported it.
* SourceData is a ModuleId.
*/
Parent = 1,
/**
* The module was instantiated because it was included in a chunk's hot module
* update.
* SourceData is an array of ModuleIds or undefined.
*/
Update = 2,
}
type SourceData = ChunkPath | ModuleId | ModuleId[] | undefined
interface RuntimeBackend {
registerChunk: (
chunkPath: ChunkPath | ChunkScript,
......
......@@ -76,6 +76,14 @@ type LoadWebAssemblyModule = (
type ModuleCache<M> = Record<ModuleId, M>
// TODO properly type values here
type ModuleFactories = Map<ModuleId, Function>
/**
* Constraint for module factory functions used in HMR.
* Platform-specific runtimes define their own ModuleFactory types that must satisfy this constraint.
*/
type ModuleFactoryFunction<
TModule extends Module,
TContext extends TurbopackBaseContext<TModule>,
> = (this: TContext, ...args: unknown[]) => unknown
/**
* This is an alternating, non-empty arrow of module factory functions and module ids
* `[id1, id2..., factory1, id3, factory2, id4, id5, factory3]`
......
......@@ -11,6 +11,32 @@
type EsmNamespaceObject = Record<string, any>
/**
* Describes why a module was instantiated.
* Shared between browser and Node.js runtimes.
*/
enum SourceType {
/**
* The module was instantiated because it was included in an evaluated chunk's
* runtime.
* SourceData is a ChunkPath.
*/
Runtime = 0,
/**
* The module was instantiated because a parent module imported it.
* SourceData is a ModuleId.
*/
Parent = 1,
/**
* The module was instantiated because it was included in a chunk's hot module
* update.
* SourceData is an array of ModuleIds or undefined.
*/
Update = 2,
}
type SourceData = ChunkPath | ModuleId | ModuleId[] | undefined
// @ts-ignore Defined in `dev-base.ts`
declare function getOrInstantiateModuleFromParent<M>(
id: ModuleId,
......@@ -105,6 +131,17 @@ function createModuleObject(id: ModuleId): Module {
}
}
function createModuleWithDirection(id: ModuleId): ModuleWithDirection {
return {
exports: {},
error: undefined,
id,
namespaceObject: undefined,
parents: [],
children: [],
}
}
type BindingTag = 0
const BindingTag_Value = 0 as BindingTag
......@@ -512,9 +549,21 @@ function installCompressedModuleFactories(
if (end === chunkModules.length) {
throw new Error('malformed chunk format, expected a factory function')
}
// Each chunk item has a 'primary id' and optional additional ids. If the primary id is already
// present we know all the additional ids are also present, so we don't need to check.
if (!moduleFactories.has(moduleId)) {
// Check if ANY of the module IDs in this group already have factories (e.g., from HMR updates).
// If so, skip installing the old factory from disk to preserve the HMR-updated code.
let hasExistingFactory = false
const groupIds: ModuleId[] = []
for (let j = i; j < end; j++) {
const id = chunkModules[j] as ModuleId
groupIds.push(id)
if (moduleFactories.has(id)) {
hasExistingFactory = true
break
}
}
if (!hasExistingFactory) {
const moduleFactoryFn = chunkModules[end] as Function
applyModuleFactoryName(moduleFactoryFn)
newModuleId?.(moduleId)
......
......@@ -38,6 +38,7 @@ pub async fn get_browser_runtime_code(
match runtime_type {
RuntimeType::Production => runtime_base_code.push("browser/runtime/base/build-base.ts"),
RuntimeType::Development => {
runtime_base_code.push("shared/runtime/hmr-runtime.ts");
runtime_base_code.push("browser/runtime/base/dev-base.ts");
}
#[cfg(feature = "test")]
......
......@@ -8,6 +8,26 @@ const WORKER_FORWARDED_GLOBALS = [];
*
* It will be prepended to the runtime code of each runtime.
*/ /* eslint-disable @typescript-eslint/no-unused-vars */ /// <reference path="./runtime-types.d.ts" />
/**
* Describes why a module was instantiated.
* Shared between browser and Node.js runtimes.
*/ var SourceType = /*#__PURE__*/ function(SourceType) {
/**
* The module was instantiated because it was included in an evaluated chunk's
* runtime.
* SourceData is a ChunkPath.
*/ SourceType[SourceType["Runtime"] = 0] = "Runtime";
/**
* The module was instantiated because a parent module imported it.
* SourceData is a ModuleId.
*/ SourceType[SourceType["Parent"] = 1] = "Parent";
/**
* The module was instantiated because it was included in a chunk's hot module
* update.
* SourceData is an array of ModuleIds or undefined.
*/ SourceType[SourceType["Update"] = 2] = "Update";
return SourceType;
}(SourceType || {});
const REEXPORTED_OBJECTS = new WeakMap();
/**
* Constructs the `__turbopack_context__` object for a module.
......@@ -48,6 +68,16 @@ function getOverwrittenModule(moduleCache, id) {
namespaceObject: undefined
};
}
function createModuleWithDirection(id) {
return {
exports: {},
error: undefined,
id,
namespaceObject: undefined,
parents: [],
children: []
};
}
const BindingTag_Value = 0;
/**
* Adds the getters to the exports object.
......@@ -341,9 +371,19 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
if (end === chunkModules.length) {
throw new Error('malformed chunk format, expected a factory function');
}
// Each chunk item has a 'primary id' and optional additional ids. If the primary id is already
// present we know all the additional ids are also present, so we don't need to check.
if (!moduleFactories.has(moduleId)) {
// Check if ANY of the module IDs in this group already have factories (e.g., from HMR updates).
// If so, skip installing the old factory from disk to preserve the HMR-updated code.
let hasExistingFactory = false;
const groupIds = [];
for(let j = i; j < end; j++){
const id = chunkModules[j];
groupIds.push(id);
if (moduleFactories.has(id)) {
hasExistingFactory = true;
break;
}
}
if (!hasExistingFactory) {
const moduleFactoryFn = chunkModules[end];
applyModuleFactoryName(moduleFactoryFn);
newModuleId?.(moduleId);
......
......@@ -8,6 +8,26 @@ const WORKER_FORWARDED_GLOBALS = [];
*
* It will be prepended to the runtime code of each runtime.
*/ /* eslint-disable @typescript-eslint/no-unused-vars */ /// <reference path="./runtime-types.d.ts" />
/**
* Describes why a module was instantiated.
* Shared between browser and Node.js runtimes.
*/ var SourceType = /*#__PURE__*/ function(SourceType) {
/**
* The module was instantiated because it was included in an evaluated chunk's
* runtime.
* SourceData is a ChunkPath.
*/ SourceType[SourceType["Runtime"] = 0] = "Runtime";
/**
* The module was instantiated because a parent module imported it.
* SourceData is a ModuleId.
*/ SourceType[SourceType["Parent"] = 1] = "Parent";
/**
* The module was instantiated because it was included in a chunk's hot module
* update.
* SourceData is an array of ModuleIds or undefined.
*/ SourceType[SourceType["Update"] = 2] = "Update";
return SourceType;
}(SourceType || {});
const REEXPORTED_OBJECTS = new WeakMap();
/**
* Constructs the `__turbopack_context__` object for a module.
......@@ -48,6 +68,16 @@ function getOverwrittenModule(moduleCache, id) {
namespaceObject: undefined
};
}
function createModuleWithDirection(id) {
return {
exports: {},
error: undefined,
id,
namespaceObject: undefined,
parents: [],
children: []
};
}
const BindingTag_Value = 0;
/**
* Adds the getters to the exports object.
......@@ -341,9 +371,19 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
if (end === chunkModules.length) {
throw new Error('malformed chunk format, expected a factory function');
}
// Each chunk item has a 'primary id' and optional additional ids. If the primary id is already
// present we know all the additional ids are also present, so we don't need to check.
if (!moduleFactories.has(moduleId)) {
// Check if ANY of the module IDs in this group already have factories (e.g., from HMR updates).
// If so, skip installing the old factory from disk to preserve the HMR-updated code.
let hasExistingFactory = false;
const groupIds = [];
for(let j = i; j < end; j++){
const id = chunkModules[j];
groupIds.push(id);
if (moduleFactories.has(id)) {
hasExistingFactory = true;
break;
}
}
if (!hasExistingFactory) {
const moduleFactoryFn = chunkModules[end];
applyModuleFactoryName(moduleFactoryFn);
newModuleId?.(moduleId);
......
......@@ -163,6 +163,26 @@ function _ts_generator(thisArg, body) {
};
}
}
/**
* Describes why a module was instantiated.
* Shared between browser and Node.js runtimes.
*/ var SourceType = /*#__PURE__*/ function(SourceType) {
/**
* The module was instantiated because it was included in an evaluated chunk's
* runtime.
* SourceData is a ChunkPath.
*/ SourceType[SourceType["Runtime"] = 0] = "Runtime";
/**
* The module was instantiated because a parent module imported it.
* SourceData is a ModuleId.
*/ SourceType[SourceType["Parent"] = 1] = "Parent";
/**
* The module was instantiated because it was included in a chunk's hot module
* update.
* SourceData is an array of ModuleIds or undefined.
*/ SourceType[SourceType["Update"] = 2] = "Update";
return SourceType;
}(SourceType || {});
var REEXPORTED_OBJECTS = new WeakMap();
/**
* Constructs the `__turbopack_context__` object for a module.
......@@ -203,6 +223,16 @@ function getOverwrittenModule(moduleCache, id) {
namespaceObject: undefined
};
}
function createModuleWithDirection(id) {
return {
exports: {},
error: undefined,
id: id,
namespaceObject: undefined,
parents: [],
children: []
};
}
var BindingTag_Value = 0;
/**
* Adds the getters to the exports object.
......@@ -588,9 +618,19 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
if (end === chunkModules.length) {
throw new Error('malformed chunk format, expected a factory function');
}
// Each chunk item has a 'primary id' and optional additional ids. If the primary id is already
// present we know all the additional ids are also present, so we don't need to check.
if (!moduleFactories.has(moduleId)) {
// Check if ANY of the module IDs in this group already have factories (e.g., from HMR updates).
// If so, skip installing the old factory from disk to preserve the HMR-updated code.
var hasExistingFactory = false;
var groupIds = [];
for(var j = i; j < end; j++){
var id = chunkModules[j];
groupIds.push(id);
if (moduleFactories.has(id)) {
hasExistingFactory = true;
break;
}
}
if (!hasExistingFactory) {
var moduleFactoryFn = chunkModules[end];
applyModuleFactoryName(moduleFactoryFn);
newModuleId === null || newModuleId === void 0 ? void 0 : newModuleId(moduleId);
......@@ -951,23 +991,6 @@ function _ts_generator(thisArg, body) {
}
}
var browserContextPrototype = Context.prototype;
var SourceType = /*#__PURE__*/ function(SourceType) {
/**
* The module was instantiated because it was included in an evaluated chunk's
* runtime.
* SourceData is a ChunkPath.
*/ SourceType[SourceType["Runtime"] = 0] = "Runtime";
/**
* The module was instantiated because a parent module imported it.
* SourceData is a ModuleId.
*/ SourceType[SourceType["Parent"] = 1] = "Parent";
/**
* The module was instantiated because it was included in a chunk's hot module
* update.
* SourceData is an array of ModuleIds or undefined.
*/ SourceType[SourceType["Update"] = 2] = "Update";
return SourceType;
}(SourceType || {});
var moduleFactories = new Map();
contextPrototype.M = moduleFactories;
var availableModules = new Map();
......@@ -975,13 +998,13 @@ var availableModuleChunks = new Map();
function factoryNotAvailableMessage(moduleId, sourceType, sourceData) {
var instantiationReason;
switch(sourceType){
case 0:
case SourceType.Runtime:
instantiationReason = `as a runtime entry of chunk ${sourceData}`;
break;
case 1:
case SourceType.Parent:
instantiationReason = `because it was required from module ${sourceData}`;
break;
case 2:
case SourceType.Update:
instantiationReason = 'because of an HMR update';
break;
default:
......@@ -992,11 +1015,11 @@ function factoryNotAvailableMessage(moduleId, sourceType, sourceData) {
return `Module ${moduleId} was instantiated ${instantiationReason}, but the module factory is not available.`;
}
function loadChunk(chunkData) {
return loadChunkInternal(1, this.m.id, chunkData);
return loadChunkInternal(SourceType.Parent, this.m.id, chunkData);
}
browserContextPrototype.l = loadChunk;
function loadInitialChunk(chunkPath, chunkData) {
return loadChunkInternal(0, chunkPath, chunkData);
return loadChunkInternal(SourceType.Runtime, chunkPath, chunkData);
}
function loadChunkInternal(sourceType, sourceData, chunkData) {
return _async_to_generator(function() {
......@@ -1177,7 +1200,7 @@ var loadedChunk = Promise.resolve(undefined);
var instrumentedBackendLoadChunks = new WeakMap();
// Do not make this async. React relies on referential equality of the returned Promise.
function loadChunkByUrl(chunkUrl) {
return loadChunkByUrlInternal(1, this.m.id, chunkUrl);
return loadChunkByUrlInternal(SourceType.Parent, this.m.id, chunkUrl);
}
browserContextPrototype.L = loadChunkByUrl;
// Do not make this async. React relies on referential equality of the returned Promise.
......@@ -1189,13 +1212,13 @@ function loadChunkByUrlInternal(sourceType, sourceData, chunkUrl) {
entry = thenable.then(resolve).catch(function(cause) {
var loadReason;
switch(sourceType){
case 0:
case SourceType.Runtime:
loadReason = `as a runtime dependency of chunk ${sourceData}`;
break;
case 1:
case SourceType.Parent:
loadReason = `from module ${sourceData}`;
break;
case 2:
case SourceType.Update:
loadReason = 'from an HMR update';
break;
default:
......@@ -1299,7 +1322,7 @@ browserContextPrototype.b = createWorker;
/**
* Instantiates a runtime module.
*/ function instantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, 0, chunkPath);
return instantiateModule(moduleId, SourceType.Runtime, chunkPath);
}
/**
* Returns the URL relative to the origin where a chunk can be fetched from.
......@@ -1359,11 +1382,11 @@ var regexCssUrl = /\.css(?:\?[^#]*)?(?:#.*)?$/;
return regexCssUrl.test(chunkUrl);
}
function loadWebAssembly(chunkPath, edgeModule, importsObj) {
return BACKEND.loadWebAssembly(1, this.m.id, chunkPath, edgeModule, importsObj);
return BACKEND.loadWebAssembly(SourceType.Parent, this.m.id, chunkPath, edgeModule, importsObj);
}
contextPrototype.w = loadWebAssembly;
function loadWebAssemblyModule(chunkPath, edgeModule) {
return BACKEND.loadWebAssemblyModule(1, this.m.id, chunkPath, edgeModule);
return BACKEND.loadWebAssemblyModule(SourceType.Parent, this.m.id, chunkPath, edgeModule);
}
contextPrototype.u = loadWebAssemblyModule;
/// <reference path="./runtime-base.ts" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment