|
1 | 1 | import type * as babel from '@babel/core'; |
2 | 2 | import * as t from '@babel/types'; |
3 | | -import type { StateContext } from './types'; |
| 3 | +import { IMPORT_DECLINE } from './constants'; |
4 | 4 | import { getHotIdentifier } from './get-hot-identifier'; |
5 | 5 | 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'; |
8 | 7 |
|
9 | 8 | export function getHMRDeclineCall(state: StateContext, path: babel.NodePath) { |
10 | 9 | 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); |
21 | 10 |
|
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 | + ); |
23 | 43 | } |
0 commit comments