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

Disable react refresh for library targets #7914

Merged
merged 2 commits into from
Apr 7, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

const Component = () => {
return <div>test</div>;
}

export default Component
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Component from './Component'

export {Component}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"source": "index.js",
"main": "dist/main.js",
"dependencies": {
"@parcel/transformer-react-refresh-wrap": "*",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this dependency needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, probably not. This is probably from when I was experimenting with different package configs. I'll trim this down.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think it is; if this is removed, forcing the the test to fail (by commenting out the fix) doesn't manifest because External dependency "@parcel/transformer-react-refresh-wrap" is not declared in package.json.

"react": "*",
"react-dom": "*"
}
}
44 changes: 42 additions & 2 deletions packages/core/integration-tests/test/react-refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import {
overlayFS as fs,
sleep,
run,
getNextBuildSuccess,
} from '@parcel/test-utils';
import getPort from 'get-port';
import type {BuildEvent} from '@parcel/types';
import type {BuildEvent, Asset} from '@parcel/types';
// flowlint-next-line untyped-import:off
import JSDOM from 'jsdom';
import nullthrows from 'nullthrows';
Expand Down Expand Up @@ -203,9 +204,48 @@ if (MessageChannel) {
},
},
);

await run(b, {}, {require: false});
});

it('does not apply to library targets', async () => {
let port = await getPort();
let parcel = await bundler(
path.join(
__dirname,
'/integration/react-refresh-library-target/index.js',
),
{
hmrOptions: {
port,
},
},
);
let result = await getNextBuildSuccess(parcel);
let bundle = nullthrows(
result.bundleGraph.getBundles().find(b => b.type === 'js'),
);

// Make sure react-refresh transforms were not applied.
let assets: Asset[] = [];
bundle.traverse(node => {
if (node.type === 'asset') {
assets.push(node.value);
} else if (node.type === 'dependency') {
assert(
!node.value.specifier.startsWith('react-refresh/runtime') &&
!node.value.specifier.startsWith(
'@parcel/transformer-react-refresh-wrap',
),
);
}
});
for (let asset of assets) {
let code = await asset.getCode();
assert(
!code.includes('$RefreshReg$') && !code.includes('$RefreshSig$'),
);
}
});
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/runtimes/react-refresh/src/ReactRefreshRuntime.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default (new Runtime({
bundle.type !== 'js' ||
!options.hmrOptions ||
!bundle.env.isBrowser() ||
bundle.env.isLibrary ||
bundle.env.isWorker() ||
bundle.env.isWorklet() ||
options.mode !== 'development' ||
Expand Down
1 change: 1 addition & 0 deletions packages/transformers/js/src/JSTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ export default (new Transformer({
is_development: options.mode === 'development',
react_refresh:
asset.env.isBrowser() &&
!asset.env.isLibrary &&
!asset.env.isWorker() &&
!asset.env.isWorklet() &&
Boolean(config?.reactRefresh),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function shouldExclude(asset, options) {
!asset.isSource ||
!options.hmrOptions ||
!asset.env.isBrowser() ||
asset.env.isLibrary ||
asset.env.isWorker() ||
asset.env.isWorklet() ||
options.mode !== 'development' ||
Expand Down