|
| 1 | +import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; |
| 2 | +import { envVarRuleCreator } from "@opennextjs/aws/build/patch/patches/patchEnvVar.js"; |
| 3 | +import { describe, it } from "vitest"; |
| 4 | + |
| 5 | +const moduleCompiledCode = ` |
| 6 | +"use strict"; |
| 7 | +if (process.env.NEXT_RUNTIME === 'edge') { |
| 8 | + module.exports = require('next/dist/server/route-modules/app-page/module.js'); |
| 9 | +} else { |
| 10 | + if (process.env.__NEXT_EXPERIMENTAL_REACT) { |
| 11 | + if (process.env.NODE_ENV === 'development') { |
| 12 | + module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.dev.js'); |
| 13 | + } else if (process.env.TURBOPACK) { |
| 14 | + module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js'); |
| 15 | + } else { |
| 16 | + module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.prod.js'); |
| 17 | + } |
| 18 | + } else { |
| 19 | + if (process.env.NODE_ENV === 'development') { |
| 20 | + module.exports = require('next/dist/compiled/next-server/app-page.runtime.dev.js'); |
| 21 | + } else if (process.env.TURBOPACK) { |
| 22 | + module.exports = require('next/dist/compiled/next-server/app-page-turbo.runtime.prod.js'); |
| 23 | + } else { |
| 24 | + module.exports = require('next/dist/compiled/next-server/app-page.runtime.prod.js'); |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | +`; |
| 29 | + |
| 30 | +const reactJSXRuntimeCode = ` |
| 31 | +'use strict'; |
| 32 | +
|
| 33 | +if (process.env.NODE_ENV === 'production') { |
| 34 | + module.exports = require('./cjs/react-jsx-runtime.production.js'); |
| 35 | +} else { |
| 36 | + module.exports = require('./cjs/react-jsx-runtime.development.js'); |
| 37 | +} |
| 38 | +`; |
| 39 | + |
| 40 | +describe("patch NODE_ENV", () => { |
| 41 | + it("should patch NODE_ENV in module.compiled", () => { |
| 42 | + expect( |
| 43 | + patchCode( |
| 44 | + moduleCompiledCode, |
| 45 | + envVarRuleCreator("NODE_ENV", '"production"'), |
| 46 | + ), |
| 47 | + ).toMatchInlineSnapshot(` |
| 48 | +""use strict"; |
| 49 | +if (process.env.NEXT_RUNTIME === 'edge') { |
| 50 | + module.exports = require('next/dist/server/route-modules/app-page/module.js'); |
| 51 | +} else { |
| 52 | + if (process.env.__NEXT_EXPERIMENTAL_REACT) { |
| 53 | + if ("production" === 'development') { |
| 54 | + module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.dev.js'); |
| 55 | + } else if (process.env.TURBOPACK) { |
| 56 | + module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js'); |
| 57 | + } else { |
| 58 | + module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.prod.js'); |
| 59 | + } |
| 60 | + } else { |
| 61 | + if ("production" === 'development') { |
| 62 | + module.exports = require('next/dist/compiled/next-server/app-page.runtime.dev.js'); |
| 63 | + } else if (process.env.TURBOPACK) { |
| 64 | + module.exports = require('next/dist/compiled/next-server/app-page-turbo.runtime.prod.js'); |
| 65 | + } else { |
| 66 | + module.exports = require('next/dist/compiled/next-server/app-page.runtime.prod.js'); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +"`); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should patch NODE_ENV in react/jsx-runtime", () => { |
| 74 | + expect( |
| 75 | + patchCode( |
| 76 | + reactJSXRuntimeCode, |
| 77 | + envVarRuleCreator("NODE_ENV", '"production"'), |
| 78 | + ), |
| 79 | + ).toMatchInlineSnapshot(` |
| 80 | +"'use strict'; |
| 81 | +
|
| 82 | +if ("production" === 'production') { |
| 83 | + module.exports = require('./cjs/react-jsx-runtime.production.js'); |
| 84 | +} else { |
| 85 | + module.exports = require('./cjs/react-jsx-runtime.development.js'); |
| 86 | +} |
| 87 | +"`); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe("patch NEXT_RUNTIME", () => { |
| 92 | + it("should patch NEXT_RUNTIME in module.compiled", () => { |
| 93 | + expect( |
| 94 | + patchCode( |
| 95 | + moduleCompiledCode, |
| 96 | + envVarRuleCreator("NEXT_RUNTIME", '"node"'), |
| 97 | + ), |
| 98 | + ).toMatchInlineSnapshot(` |
| 99 | +""use strict"; |
| 100 | +if ("node" === 'edge') { |
| 101 | + module.exports = require('next/dist/server/route-modules/app-page/module.js'); |
| 102 | +} else { |
| 103 | + if (process.env.__NEXT_EXPERIMENTAL_REACT) { |
| 104 | + if (process.env.NODE_ENV === 'development') { |
| 105 | + module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.dev.js'); |
| 106 | + } else if (process.env.TURBOPACK) { |
| 107 | + module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js'); |
| 108 | + } else { |
| 109 | + module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.prod.js'); |
| 110 | + } |
| 111 | + } else { |
| 112 | + if (process.env.NODE_ENV === 'development') { |
| 113 | + module.exports = require('next/dist/compiled/next-server/app-page.runtime.dev.js'); |
| 114 | + } else if (process.env.TURBOPACK) { |
| 115 | + module.exports = require('next/dist/compiled/next-server/app-page-turbo.runtime.prod.js'); |
| 116 | + } else { |
| 117 | + module.exports = require('next/dist/compiled/next-server/app-page.runtime.prod.js'); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | +"`); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe("patch TURBOPACK", () => { |
| 126 | + it("should patch TURBOPACK in module.compiled", () => { |
| 127 | + expect( |
| 128 | + patchCode(moduleCompiledCode, envVarRuleCreator("TURBOPACK", "false")), |
| 129 | + ).toMatchInlineSnapshot(` |
| 130 | +""use strict"; |
| 131 | +if (process.env.NEXT_RUNTIME === 'edge') { |
| 132 | + module.exports = require('next/dist/server/route-modules/app-page/module.js'); |
| 133 | +} else { |
| 134 | + if (process.env.__NEXT_EXPERIMENTAL_REACT) { |
| 135 | + if (process.env.NODE_ENV === 'development') { |
| 136 | + module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.dev.js'); |
| 137 | + } else if (false) { |
| 138 | + module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js'); |
| 139 | + } else { |
| 140 | + module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.prod.js'); |
| 141 | + } |
| 142 | + } else { |
| 143 | + if (process.env.NODE_ENV === 'development') { |
| 144 | + module.exports = require('next/dist/compiled/next-server/app-page.runtime.dev.js'); |
| 145 | + } else if (false) { |
| 146 | + module.exports = require('next/dist/compiled/next-server/app-page-turbo.runtime.prod.js'); |
| 147 | + } else { |
| 148 | + module.exports = require('next/dist/compiled/next-server/app-page.runtime.prod.js'); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | +"`); |
| 153 | + }); |
| 154 | +}); |
0 commit comments