-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ref(nextjs): Use loader to set RewriteFrames
helper value
#5445
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
81056c0
add template for prepended code
lobsterkatie 993f3c1
add prefix loader
lobsterkatie 27593bc
add `module` to webpack config type
lobsterkatie ac622db
unpack `buildContext` values
lobsterkatie 0e610a9
switch to using loader for setting `RewriteFrames` global variable
lobsterkatie 92fcf90
fix tests
lobsterkatie 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 hidden or 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 |
---|---|---|
@@ -1,12 +1,47 @@ | ||
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js'; | ||
|
||
export default makeNPMConfigVariants( | ||
makeBaseNPMConfig({ | ||
// We need to include `instrumentServer.ts` separately because it's only conditionally required, and so rollup | ||
// doesn't automatically include it when calculating the module dependency tree. | ||
entrypoints: ['src/index.server.ts', 'src/index.client.ts', 'src/utils/instrumentServer.ts'], | ||
// prevent this nextjs code from ending up in our built package (this doesn't happen automatially because the name | ||
// doesn't match an SDK dependency) | ||
packageSpecificConfig: { external: ['next/router'] }, | ||
}), | ||
); | ||
export default [ | ||
...makeNPMConfigVariants( | ||
makeBaseNPMConfig({ | ||
// We need to include `instrumentServer.ts` separately because it's only conditionally required, and so rollup | ||
// doesn't automatically include it when calculating the module dependency tree. | ||
entrypoints: ['src/index.server.ts', 'src/index.client.ts', 'src/utils/instrumentServer.ts'], | ||
|
||
// prevent this internal nextjs code from ending up in our built package (this doesn't happen automatially because | ||
// the name doesn't match an SDK dependency) | ||
packageSpecificConfig: { external: ['next/router'] }, | ||
}), | ||
), | ||
...makeNPMConfigVariants( | ||
makeBaseNPMConfig({ | ||
entrypoints: ['src/config/prefixLoaderTemplate.ts'], | ||
|
||
packageSpecificConfig: { | ||
output: { | ||
// preserve the original file structure (i.e., so that everything is still relative to `src`) | ||
entryFileNames: 'config/[name].js', | ||
|
||
// this is going to be add-on code, so it doesn't need the trappings of a full module (and in fact actively | ||
// shouldn't have them, lest they muck with the module to which we're adding it) | ||
sourcemap: false, | ||
esModule: false, | ||
}, | ||
}, | ||
}), | ||
), | ||
...makeNPMConfigVariants( | ||
makeBaseNPMConfig({ | ||
entrypoints: ['src/config/prefixLoader.ts'], | ||
|
||
packageSpecificConfig: { | ||
output: { | ||
// make it so Rollup calms down about the fact that we're doing `export { loader as default }` | ||
exports: 'default', | ||
|
||
// preserve the original file structure (i.e., so that everything is still relative to `src`) | ||
entryFileNames: 'config/[name].js', | ||
}, | ||
}, | ||
}), | ||
), | ||
]; |
This file contains hidden or 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,34 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
type LoaderOptions = { | ||
distDir: string; | ||
}; | ||
// TODO Use real webpack types | ||
type LoaderThis = { | ||
// Webpack 4 | ||
query?: LoaderOptions; | ||
// Webpack 5 | ||
getOptions?: () => LoaderOptions; | ||
addDependency: (filepath: string) => void; | ||
}; | ||
|
||
/** | ||
* Inject templated code into the beginning of a module. | ||
*/ | ||
function prefixLoader(this: LoaderThis, userCode: string): string { | ||
// We know one or the other will be defined, depending on the version of webpack being used | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const { distDir } = this.getOptions ? this.getOptions() : this.query!; | ||
|
||
const templatePath = path.resolve(__dirname, 'prefixLoaderTemplate.js'); | ||
this.addDependency(templatePath); | ||
|
||
// Fill in the placeholder | ||
let templateCode = fs.readFileSync(templatePath).toString(); | ||
templateCode = templateCode.replace('__DIST_DIR__', distDir); | ||
|
||
return `${templateCode}\n${userCode}`; | ||
} | ||
|
||
export { prefixLoader as default }; |
This file contains hidden or 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,5 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any | ||
(global as any).__rewriteFramesDistDir__ = '__DIST_DIR__'; | ||
|
||
// We need this to make this file an ESM module, which TS requires when using `isolatedModules`. | ||
export {}; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Is this gonna be done in a follow-up PR?
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.
Probably? Maybe? It's low priority, and slightly complicated by the need to be compatible with both webpack 4 and webpack 5. Not a blocker at the moment.