Skip to content

Commit

Permalink
fix #22 (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc authored Apr 22, 2024
1 parent c68b72e commit ad30cba
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 288 deletions.
26 changes: 12 additions & 14 deletions src/babel/core/create-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { IMPORT_REFRESH, IMPORT_REGISTRY } from './constants';
import { getHotIdentifier } from './get-hot-identifier';
import { getImportIdentifier } from './get-import-identifier';
import { getRootStatementPath } from './get-root-statement-path';
import { generateViteHMRRequirement } from './get-vite-hmr-requirement';
import type { StateContext } from './types';

const REGISTRY = 'REGISTRY';
Expand Down Expand Up @@ -34,22 +33,21 @@ export function createRegistry(
)[0],
);
const pathToHot = getHotIdentifier(state);
const statements: t.Statement[] = [
t.expressionStatement(
t.callExpression(getImportIdentifier(state, path, IMPORT_REFRESH), [
t.stringLiteral(state.bundler),
pathToHot,
identifier,
]),
),
];

generateViteHMRRequirement(state, statements, pathToHot);

(
path.scope.getProgramParent().path as babel.NodePath<t.Program>
).pushContainer('body', [
t.ifStatement(pathToHot, t.blockStatement(statements)),
t.ifStatement(
pathToHot,
t.blockStatement([
t.expressionStatement(
t.callExpression(getImportIdentifier(state, path, IMPORT_REFRESH), [
t.stringLiteral(state.bundler),
pathToHot,
identifier,
]),
),
]),
),
]);
state.imports.set(REGISTRY, identifier);
return identifier;
Expand Down
48 changes: 34 additions & 14 deletions src/babel/core/get-hmr-decline-call.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
import type * as babel from '@babel/core';
import * as t from '@babel/types';
import type { StateContext } from './types';
import { IMPORT_DECLINE } from './constants';
import { getHotIdentifier } from './get-hot-identifier';
import { getImportIdentifier } from './get-import-identifier';
import { IMPORT_DECLINE } from './constants';
import { generateViteHMRRequirement } from './get-vite-hmr-requirement';
import type { StateContext } from './types';

export function getHMRDeclineCall(state: StateContext, path: babel.NodePath) {
const pathToHot = getHotIdentifier(state);
const statements = [
t.expressionStatement(
t.callExpression(getImportIdentifier(state, path, IMPORT_DECLINE), [
t.stringLiteral(state.bundler),
pathToHot,
]),
),
];

generateViteHMRRequirement(state, statements, pathToHot);

return t.ifStatement(pathToHot, t.blockStatement(statements));
if (state.bundler === 'vite') {
return t.ifStatement(
pathToHot,
t.blockStatement([
t.expressionStatement(
t.callExpression(
t.memberExpression(pathToHot, t.identifier('accept')),
[
t.arrowFunctionExpression(
[],
t.callExpression(
t.memberExpression(pathToHot, t.identifier('invalidate')),
[],
),
),
],
),
),
]),
);
}
return t.ifStatement(
pathToHot,
t.blockStatement([
t.expressionStatement(
t.callExpression(getImportIdentifier(state, path, IMPORT_DECLINE), [
t.stringLiteral(state.bundler),
pathToHot,
]),
),
]),
);
}
20 changes: 0 additions & 20 deletions src/babel/core/get-vite-hmr-requirement.ts

This file was deleted.

Loading

0 comments on commit ad30cba

Please sign in to comment.