-
Notifications
You must be signed in to change notification settings - Fork 27.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
Bypass webpack compilation for precompiled @next/polyfills-nomodule #27596
Merged
kodiakhq
merged 5 commits into
vercel:canary
from
timneutkens:add/remove-next-polyfill-compilation
Aug 13, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6a2c79d
Bypass webpack compilation for precompiled @next/polyfills-nomodule
timneutkens ee06cdc
Add caching to copy-file-plugin
timneutkens 3633105
Merge branch 'canary' into add/remove-next-polyfill-compilation
kodiakhq[bot] e865e06
Merge branch 'canary' into add/remove-next-polyfill-compilation
kodiakhq[bot] 13e9131
Merge branch 'canary' into add/remove-next-polyfill-compilation
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { promises as fs } from 'fs' | ||
import loaderUtils from 'next/dist/compiled/loader-utils' | ||
import { | ||
isWebpack5, | ||
sources, | ||
webpack, | ||
} from 'next/dist/compiled/webpack/webpack' | ||
|
||
const PLUGIN_NAME = 'CopyFilePlugin' | ||
|
||
export class CopyFilePlugin { | ||
private filePath: string | ||
private name: string | ||
private cacheKey: string | ||
private info?: object | ||
|
||
constructor({ | ||
filePath, | ||
cacheKey, | ||
name, | ||
info, | ||
}: { | ||
filePath: string | ||
cacheKey: string | ||
name: string | ||
minimize: boolean | ||
info?: object | ||
}) { | ||
this.filePath = filePath | ||
this.cacheKey = cacheKey | ||
this.name = name | ||
this.info = info | ||
} | ||
|
||
apply(compiler: webpack.Compiler) { | ||
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation: any) => { | ||
const cache = isWebpack5 ? compilation.getCache('CopyFilePlugin') : null | ||
const hook = isWebpack5 | ||
? // @ts-ignore | ||
compilation.hooks.processAssets | ||
: compilation.hooks.additionalAssets | ||
hook.tapPromise( | ||
isWebpack5 | ||
? { | ||
name: PLUGIN_NAME, | ||
// @ts-ignore TODO: Remove ignore when webpack 5 is stable | ||
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS, | ||
} | ||
: PLUGIN_NAME, | ||
async () => { | ||
if (cache) { | ||
const cachedResult = await cache.getPromise( | ||
this.filePath, | ||
this.cacheKey | ||
) | ||
if (cachedResult) { | ||
const { file, source } = cachedResult | ||
compilation.emitAsset(file, source, { | ||
...this.info, | ||
}) | ||
return | ||
} | ||
} | ||
const content = await fs.readFile(this.filePath, 'utf8') | ||
|
||
const file = loaderUtils.interpolateName( | ||
{ resourcePath: this.filePath }, | ||
this.name, | ||
{ content, context: compiler.context } | ||
) | ||
|
||
const source = new sources.RawSource(content) | ||
|
||
if (cache) { | ||
await cache.storePromise(this.filePath, this.cacheKey, { | ||
file, | ||
source, | ||
}) | ||
} | ||
|
||
// @ts-ignore | ||
compilation.emitAsset(file, source, { | ||
...this.info, | ||
}) | ||
} | ||
) | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need to think about
.cjs
or.mjs
here?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.
Not currently, if an output like that is added then yes.