Skip to content

Commit ad30cba

Browse files
authored
fix #22 (#63)
1 parent c68b72e commit ad30cba

File tree

7 files changed

+70
-288
lines changed

7 files changed

+70
-288
lines changed

src/babel/core/create-registry.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { IMPORT_REFRESH, IMPORT_REGISTRY } from './constants';
44
import { getHotIdentifier } from './get-hot-identifier';
55
import { getImportIdentifier } from './get-import-identifier';
66
import { getRootStatementPath } from './get-root-statement-path';
7-
import { generateViteHMRRequirement } from './get-vite-hmr-requirement';
87
import type { StateContext } from './types';
98

109
const REGISTRY = 'REGISTRY';
@@ -34,22 +33,21 @@ export function createRegistry(
3433
)[0],
3534
);
3635
const pathToHot = getHotIdentifier(state);
37-
const statements: t.Statement[] = [
38-
t.expressionStatement(
39-
t.callExpression(getImportIdentifier(state, path, IMPORT_REFRESH), [
40-
t.stringLiteral(state.bundler),
41-
pathToHot,
42-
identifier,
43-
]),
44-
),
45-
];
46-
47-
generateViteHMRRequirement(state, statements, pathToHot);
48-
4936
(
5037
path.scope.getProgramParent().path as babel.NodePath<t.Program>
5138
).pushContainer('body', [
52-
t.ifStatement(pathToHot, t.blockStatement(statements)),
39+
t.ifStatement(
40+
pathToHot,
41+
t.blockStatement([
42+
t.expressionStatement(
43+
t.callExpression(getImportIdentifier(state, path, IMPORT_REFRESH), [
44+
t.stringLiteral(state.bundler),
45+
pathToHot,
46+
identifier,
47+
]),
48+
),
49+
]),
50+
),
5351
]);
5452
state.imports.set(REGISTRY, identifier);
5553
return identifier;
Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
import type * as babel from '@babel/core';
22
import * as t from '@babel/types';
3-
import type { StateContext } from './types';
3+
import { IMPORT_DECLINE } from './constants';
44
import { getHotIdentifier } from './get-hot-identifier';
55
import { getImportIdentifier } from './get-import-identifier';
6-
import { IMPORT_DECLINE } from './constants';
7-
import { generateViteHMRRequirement } from './get-vite-hmr-requirement';
6+
import type { StateContext } from './types';
87

98
export function getHMRDeclineCall(state: StateContext, path: babel.NodePath) {
109
const pathToHot = getHotIdentifier(state);
11-
const statements = [
12-
t.expressionStatement(
13-
t.callExpression(getImportIdentifier(state, path, IMPORT_DECLINE), [
14-
t.stringLiteral(state.bundler),
15-
pathToHot,
16-
]),
17-
),
18-
];
19-
20-
generateViteHMRRequirement(state, statements, pathToHot);
2110

22-
return t.ifStatement(pathToHot, t.blockStatement(statements));
11+
if (state.bundler === 'vite') {
12+
return t.ifStatement(
13+
pathToHot,
14+
t.blockStatement([
15+
t.expressionStatement(
16+
t.callExpression(
17+
t.memberExpression(pathToHot, t.identifier('accept')),
18+
[
19+
t.arrowFunctionExpression(
20+
[],
21+
t.callExpression(
22+
t.memberExpression(pathToHot, t.identifier('invalidate')),
23+
[],
24+
),
25+
),
26+
],
27+
),
28+
),
29+
]),
30+
);
31+
}
32+
return t.ifStatement(
33+
pathToHot,
34+
t.blockStatement([
35+
t.expressionStatement(
36+
t.callExpression(getImportIdentifier(state, path, IMPORT_DECLINE), [
37+
t.stringLiteral(state.bundler),
38+
pathToHot,
39+
]),
40+
),
41+
]),
42+
);
2343
}

src/babel/core/get-vite-hmr-requirement.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)