-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
[Script] Remove next/script
code from main bundle unless it's used in a page
#37567
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { promises } from 'fs' | ||
import { join } from 'path' | ||
|
||
export async function checkNextScriptImport( | ||
pagePaths: string[], | ||
pagesDir: string | ||
): Promise<boolean> { | ||
for (const page of pagePaths) { | ||
const fileContent = await promises.readFile(join(pagesDir, page), 'utf8') | ||
if (/import[^}]*.*('|")next\/script('|")/.test(fileContent)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using let isNextScriptImported = false;
await Promise.all(pagePaths.map(
async (page) => {
// bail out early
if (isNextScriptImported) return;
const fileContent = await promises.readFile(join(pagesDir, page), 'utf8')
if (fileContent.includes('next/script') || fileContent.includes('next/dist/client/script')) {
isNextScriptImported = true;
}
}
))
return isNextScriptImported; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. Updated the PR to use |
||
return true | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better way to detect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we utilize SWC? See #37589 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooh this is great. I'll hold off on this PR until that gets merged and use |
||
|
||
return false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ import { ImageConfigContext } from '../shared/lib/image-config-context' | |
import { ImageConfigComplete } from '../shared/lib/image-config' | ||
import { removeBasePath } from './remove-base-path' | ||
import { hasBasePath } from './has-base-path' | ||
import { initalizeScriptLoader } from './initialize-script-loader' | ||
|
||
const ReactDOM = process.env.__NEXT_REACT_ROOT | ||
? require('react-dom/client') | ||
|
@@ -252,9 +253,8 @@ export async function initialize(opts: { webpackHMR?: any } = {}): Promise<{ | |
} | ||
} | ||
|
||
if (initialData.scriptLoader) { | ||
const { initScriptLoader } = require('./script') | ||
initScriptLoader(initialData.scriptLoader) | ||
if (initialData.scriptLoader && initialData.scriptLoader.length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the That way dead code elimination will remove it all |
||
initalizeScriptLoader(initialData.scriptLoader) | ||
} | ||
|
||
pageLoader = new PageLoader(initialData.buildId, prefix) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { initScriptLoader } from './script' | ||
|
||
export const initalizeScriptLoader: typeof initScriptLoader = (...args) => { | ||
if (process.env.__NEXT_SCRIPT_IMPORTED) { | ||
return require('./script').initScriptLoader(...args) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern here can not match the following cases:
IMHO we could simply use
fileContent.includes('next/script') || fileContent.includes('next/dist/client/script')
.Or, maybe we could implement an swc AST based build analysis, see:
next/build/analysis/extract-const-value
,next/build/analysis/get-page-static-info
andnext/build/analysis/parse-module
.