|
| 1 | +import { logger } from '@sentry/utils'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +import { isESM } from '../../utils/isESM'; |
| 6 | +import { findDeclarations, findExports, findIdentifiers, makeAST, removeComments } from './ast'; |
| 7 | +import { LoaderThis } from './types'; |
| 8 | +const DATA_FETCHING_FUNCTIONS = ['getServerSideProps', 'getStaticProps', 'getStaticPaths']; |
| 9 | + |
| 10 | +type LoaderOptions = { |
| 11 | + projectDir: string; |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * Find any data-fetching functions the user's code contains and rename them to prevent clashes, then whittle the |
| 16 | + * template exporting wrapped versions instead down to only the functions found. |
| 17 | + * |
| 18 | + * @param userCode The source code of the current page file |
| 19 | + * @param templateCode The source code of the full template, including all functions |
| 20 | + * @param filepath The path to the current pagefile, within the project directory |
| 21 | + * @returns A tuple of modified user and template code |
| 22 | + */ |
| 23 | +function wrapFunctions(userCode: string, templateCode: string, filepath: string): string[] { |
| 24 | + let userAST, templateAST; |
| 25 | + const isTS = new RegExp('\\.tsx?$').test(filepath); |
| 26 | + |
| 27 | + try { |
| 28 | + userAST = makeAST(userCode, isTS); |
| 29 | + templateAST = makeAST(templateCode, false); |
| 30 | + } catch (err) { |
| 31 | + logger.warn(`Couldn't add Sentry to ${filepath} because there was a parsing error: ${err}`); |
| 32 | + } |
| 33 | + |
| 34 | + // This will be true iff `makeAST` errored, but TS can't quite make the logical leap, so we check it directly |
| 35 | + if (!userAST || !templateAST) { |
| 36 | + // Replace the template code with an empty string, so in the end the user code is untouched |
| 37 | + return [userCode, '']; |
| 38 | + } |
| 39 | + |
| 40 | + // Comments are useful to have in the template for anyone reading it, but don't make sense to be injected into user |
| 41 | + // code, because they're about the template-i-ness of the template, not the code itself |
| 42 | + removeComments(templateAST); |
| 43 | + |
| 44 | + for (const fnName of DATA_FETCHING_FUNCTIONS) { |
| 45 | + const matchingNodes = findIdentifiers(userAST, fnName); |
| 46 | + |
| 47 | + // If the current function exists in a user's code, prefix all references to it with an underscore, so as not to |
| 48 | + // conflict with the wrapped version we're going to create |
| 49 | + if (matchingNodes.length > 0) { |
| 50 | + matchingNodes.forEach(nodePath => (nodePath.node.name = `_${fnName}`)); |
| 51 | + } |
| 52 | + |
| 53 | + // Otherwise, if the current function doesn't exist anywhere in the user's code, delete the code in the template |
| 54 | + // wrapping that function |
| 55 | + // |
| 56 | + // Note: We start with all of the possible wrapper lines in the template and delete the ones we don't need (rather |
| 57 | + // than starting with none and adding in the ones we do need) because it allows them to live in our souce code as |
| 58 | + // *code*. If we added them in, they'd have to be strings containing code, and we'd lose all of the benefits of |
| 59 | + // syntax highlighting, linting, etc. |
| 60 | + else { |
| 61 | + // We have to look for declarations and exports separately because when we build the SDK, Rollup turns |
| 62 | + // export const XXX = ... |
| 63 | + // into |
| 64 | + // const XXX = ... |
| 65 | + // export { XXX } |
| 66 | + findExports(templateAST, fnName).remove(); |
| 67 | + findDeclarations(templateAST, fnName).remove(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return [userAST.toSource(), templateAST.toSource()]; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Wrap `getStaticPaths`, `getStaticProps`, and `getServerSideProps` (if they exist) in the given page code |
| 76 | + */ |
| 77 | +function wrapDataFetchersLoader(this: LoaderThis<LoaderOptions>, userCode: string): string { |
| 78 | + // We know one or the other will be defined, depending on the version of webpack being used |
| 79 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 80 | + const { projectDir } = this.getOptions ? this.getOptions() : this.query!; |
| 81 | + |
| 82 | + // For now this loader only works for ESM code |
| 83 | + // TODO: Can you even write nextjs pages in CJS? |
| 84 | + if (!isESM(userCode)) { |
| 85 | + return userCode; |
| 86 | + } |
| 87 | + |
| 88 | + // If none of the functions we want to wrap appears in the page's code, there's nothing to do |
| 89 | + if (DATA_FETCHING_FUNCTIONS.every(functionName => !userCode.includes(functionName))) { |
| 90 | + return userCode; |
| 91 | + } |
| 92 | + |
| 93 | + const templatePath = path.resolve(__dirname, '../templates/dataFetchersLoaderTemplate.js'); |
| 94 | + // make sure the template is included when runing `webpack watch` |
| 95 | + this.addDependency(templatePath); |
| 96 | + |
| 97 | + const templateCode = fs.readFileSync(templatePath).toString(); |
| 98 | + |
| 99 | + const [modifiedUserCode, injectedCode] = wrapFunctions( |
| 100 | + userCode, |
| 101 | + templateCode, |
| 102 | + path.relative(projectDir, this.resourcePath), |
| 103 | + ); |
| 104 | + return `${modifiedUserCode}\n${injectedCode}`; |
| 105 | +} |
| 106 | + |
| 107 | +export { wrapDataFetchersLoader as default }; |
0 commit comments