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

perf: define 'typeof window' in webpack to eliminate deadcode #593

Merged
merged 3 commits into from
Jul 17, 2024
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
6 changes: 6 additions & 0 deletions examples/basic/src/routes/home/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import React from 'react';
import { RouterView } from '@shuvi/runtime';

export default function Layout() {
React.useEffect(() => {
if (typeof window !== 'undefined') {
console.log(`[useEffect] layout useEffect`);
}
}, []);

return (
<div
style={{
Expand Down
4 changes: 4 additions & 0 deletions examples/package-esmodule/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ import { consoleLog } from './utils.js';

export default (data?: string) => {
consoleLog(`Hello from package-esmodule ${data}`);

if (typeof window !== 'undefined') {
consoleLog('[package-esmodule] Running in the browser');
}
};
4 changes: 4 additions & 0 deletions packages/toolpack/src/webpack/config/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ export function createBrowserWebpackChain(options: BaseOptions): WebpackChain {
{
...options,
__BROWSER__: true,
/**
* swc.optimizer can't handle `typeof window` correctly for dependencies
*/
'typeof window': JSON.stringify('object'),
// prevent errof of destructing process.env
'process.env': JSON.stringify('{}')
}
Expand Down
6 changes: 5 additions & 1 deletion packages/toolpack/src/webpack/config/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export function createNodeWebpackChain(options: BaseOptions): WebpackChain {
chain.plugin('define').tap(([options]) => [
{
...options,
__BROWSER__: false
__BROWSER__: false,
/**
* swc.optimizer can't handle `typeof window` correctly for dependencies
*/
'typeof window': JSON.stringify('undefined')
}
]);

Expand Down
12 changes: 10 additions & 2 deletions test/e2e/shuvi-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ describe('shuvi/inspect', () => {
const project = createTestCtx('shuvi-cli');
const { message } = await project.run('inspect', ['--verbose']);
expect(message).toMatch(
`definitions: { 'process.env.NODE_ENV': '"development"'`
`definitions: {
'process.env.NODE_ENV': '\"development\"',
__BROWSER__: false,
'typeof window': '\"undefined\"'
}`
);

const project2 = createTestCtx('shuvi-cli');
Expand All @@ -126,7 +130,11 @@ describe('shuvi/inspect', () => {
'--verbose'
]);
expect(message2).toMatch(
`definitions: { 'process.env.NODE_ENV': '"production"'`
`definitions: {
'process.env.NODE_ENV': '\"production\"',
__BROWSER__: false,
'typeof window': '\"undefined\"'
}`
);
});
});
Loading