Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fresh] Add options to configure RefreshReg and RefreshSig identifiers #17340

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/react-refresh/src/ReactFreshBabelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default function(babel, opts = {}) {
}

const {types: t} = babel;
const refreshReg = t.identifier(opts.refreshReg || '$RefreshReg$');
const refreshSig = t.identifier(opts.refreshSig || '$RefreshSig$');

const registrationsByProgramPath = new Map();
function createRegistration(programPath, persistentID) {
Expand Down Expand Up @@ -517,7 +519,7 @@ export default function(babel, opts = {}) {
const sigCallID = path.scope.generateUidIdentifier('_s');
path.scope.parent.push({
id: sigCallID,
init: t.callExpression(t.identifier('$RefreshSig$'), []),
init: t.callExpression(refreshSig, []),
});

// The signature call is split in two parts. One part is called inside the function.
Expand Down Expand Up @@ -579,7 +581,7 @@ export default function(babel, opts = {}) {
const sigCallID = path.scope.generateUidIdentifier('_s');
path.scope.parent.push({
id: sigCallID,
init: t.callExpression(t.identifier('$RefreshSig$'), []),
init: t.callExpression(refreshSig, []),
});

// The signature call is split in two parts. One part is called inside the function.
Expand Down Expand Up @@ -743,7 +745,7 @@ export default function(babel, opts = {}) {
path.pushContainer(
'body',
t.expressionStatement(
t.callExpression(t.identifier('$RefreshReg$'), [
t.callExpression(refreshReg, [
handle,
t.stringLiteral(persistentID),
]),
Expand Down
18 changes: 18 additions & 0 deletions packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function transform(input, options = {}) {
skipEnvCheck: true,
// To simplify debugging tests:
emitFullSignatures: true,
...options.freshOptions,
},
],
...(options.plugins || []),
Expand Down Expand Up @@ -480,4 +481,21 @@ describe('ReactFreshBabelPlugin', () => {
`),
).toMatchSnapshot();
});

it('uses custom identifiers for $RefreshReg$ and $RefreshSig$', () => {
expect(
transform(
`export default function Bar () {
useContext(X)
return <Foo />
};`,
{
freshOptions: {
refreshReg: 'import.meta.refreshReg',
refreshSig: 'import.meta.refreshSig',
},
},
),
).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,26 @@ $RefreshReg$(_c, "Hello");
$RefreshReg$(_c2, "Bar");
`;

exports[`ReactFreshBabelPlugin uses custom identifiers for $RefreshReg$ and $RefreshSig$ 1`] = `
var _s = import.meta.refreshSig();

export default function Bar() {
_s();

useContext(X);
return <Foo />;
}

_s(Bar, "useContext{}");

_c = Bar;
;

var _c;

import.meta.refreshReg(_c, "Bar");
`;

exports[`ReactFreshBabelPlugin uses original function declaration if it get reassigned 1`] = `
function Hello() {
return <h1>Hi</h1>;
Expand Down