|
| 1 | +// Opt-in Babel 8 compatibility smoke. |
| 2 | +// |
| 3 | +// The normal test suite intentionally keeps repository development pins on |
| 4 | +// Babel 7. Enable this smoke with RUN_BABEL8_SMOKE=1 after `yarn build`; it |
| 5 | +// installs Babel 8 packages into a temp app and runs Webpack through |
| 6 | +// babel-loader 10 with Shakapacker's built Babel preset. |
| 7 | + |
| 8 | +const { spawnSync } = require("child_process") |
| 9 | +const fs = require("fs") |
| 10 | +const { createRequire } = require("module") |
| 11 | +const os = require("os") |
| 12 | +const path = require("path") |
| 13 | +const webpack = require("webpack") |
| 14 | + |
| 15 | +const repoRoot = path.resolve(__dirname, "../..") |
| 16 | +const presetPath = path.join(repoRoot, "package/babel/preset.js") |
| 17 | +const optedIn = process.env.RUN_BABEL8_SMOKE === "1" |
| 18 | +const coreIsBuilt = fs.existsSync(presetPath) |
| 19 | + |
| 20 | +const toolVersion = (cmd) => { |
| 21 | + const r = spawnSync(cmd, ["--version"], { |
| 22 | + encoding: "utf8", |
| 23 | + cwd: os.tmpdir() |
| 24 | + }) |
| 25 | + return r.status === 0 ? r.stdout.trim() : null |
| 26 | +} |
| 27 | + |
| 28 | +const npmVersion = toolVersion("npm") |
| 29 | +const hasNpm = npmVersion !== null |
| 30 | +const shouldRun = optedIn && coreIsBuilt && hasNpm |
| 31 | + |
| 32 | +const run = (cmd, args, options) => { |
| 33 | + const r = spawnSync(cmd, args, { encoding: "utf8", ...options }) |
| 34 | + if (r.status !== 0) { |
| 35 | + throw new Error( |
| 36 | + `${cmd} ${args.join(" ")} failed in ${options.cwd}\nstdout:\n${r.stdout}\nstderr:\n${r.stderr}` |
| 37 | + ) |
| 38 | + } |
| 39 | + return r |
| 40 | +} |
| 41 | + |
| 42 | +const packageVersion = (appRequire, packageName) => { |
| 43 | + const packageJsonPath = appRequire.resolve(`${packageName}/package.json`) |
| 44 | + return appRequire(packageJsonPath).version |
| 45 | +} |
| 46 | + |
| 47 | +const runWebpack = (config) => |
| 48 | + new Promise((resolve, reject) => { |
| 49 | + const compiler = webpack(config) |
| 50 | + compiler.run((error, stats) => { |
| 51 | + const close = () => { |
| 52 | + compiler.close((closeError) => { |
| 53 | + if (closeError) reject(closeError) |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + if (error) { |
| 58 | + close() |
| 59 | + reject(error) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + if (stats.hasErrors()) { |
| 64 | + close() |
| 65 | + reject(new Error(stats.toString({ all: false, errors: true }))) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + close() |
| 70 | + resolve(stats) |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | +const computeSkipReason = () => { |
| 75 | + if (!optedIn) return "RUN_BABEL8_SMOKE=1 not set" |
| 76 | + if (!coreIsBuilt) return "shakapacker not built (run `yarn build`)" |
| 77 | + if (!hasNpm) return "npm unavailable on PATH" |
| 78 | + return "unknown skip reason" |
| 79 | +} |
| 80 | + |
| 81 | +describe("Babel 8 preset smoke (issue #1191)", () => { |
| 82 | + let workRoot |
| 83 | + |
| 84 | + beforeAll(() => { |
| 85 | + if (!shouldRun) return |
| 86 | + |
| 87 | + workRoot = fs.mkdtempSync(path.join(os.tmpdir(), "shaka-babel8-smoke-")) |
| 88 | + fs.writeFileSync( |
| 89 | + path.join(workRoot, "package.json"), |
| 90 | + JSON.stringify({ name: "shaka-babel8-smoke", private: true }, null, 2) |
| 91 | + ) |
| 92 | + run( |
| 93 | + "npm", |
| 94 | + [ |
| 95 | + "install", |
| 96 | + "--no-audit", |
| 97 | + "--no-fund", |
| 98 | + "--save-dev", |
| 99 | + "@babel/core@^8", |
| 100 | + "@babel/plugin-transform-runtime@^8", |
| 101 | + "@babel/preset-env@^8", |
| 102 | + "@babel/runtime@^8", |
| 103 | + "babel-loader@^10" |
| 104 | + ], |
| 105 | + { cwd: workRoot } |
| 106 | + ) |
| 107 | + }, 180000) |
| 108 | + |
| 109 | + afterAll(() => { |
| 110 | + if (!workRoot) return |
| 111 | + fs.rmSync(workRoot, { recursive: true, force: true }) |
| 112 | + }) |
| 113 | + |
| 114 | + if (!shouldRun) { |
| 115 | + test.todo(`Babel 8 smoke skipped (${computeSkipReason()})`) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + test("compiles through babel-loader 10 with the Shakapacker Babel preset and Babel 8", async () => { |
| 120 | + const srcDir = path.join(workRoot, "src") |
| 121 | + const distDir = path.join(workRoot, "dist") |
| 122 | + fs.mkdirSync(srcDir) |
| 123 | + fs.writeFileSync( |
| 124 | + path.join(srcDir, "index.js"), |
| 125 | + "const answer = () => ({ value: 42 });\nconsole.log(answer()?.value);\n" |
| 126 | + ) |
| 127 | + |
| 128 | + const appRequire = createRequire(path.join(workRoot, "package.json")) |
| 129 | + expect(packageVersion(appRequire, "@babel/core")).toMatch(/^8\./) |
| 130 | + expect(packageVersion(appRequire, "babel-loader")).toMatch(/^10\./) |
| 131 | + |
| 132 | + await runWebpack({ |
| 133 | + mode: "development", |
| 134 | + context: workRoot, |
| 135 | + entry: path.join(srcDir, "index.js"), |
| 136 | + output: { |
| 137 | + path: distDir, |
| 138 | + filename: "bundle.js" |
| 139 | + }, |
| 140 | + module: { |
| 141 | + rules: [ |
| 142 | + { |
| 143 | + test: /\.js$/, |
| 144 | + include: srcDir, |
| 145 | + use: [ |
| 146 | + { |
| 147 | + loader: appRequire.resolve("babel-loader"), |
| 148 | + options: { |
| 149 | + cacheDirectory: false, |
| 150 | + presets: [presetPath] |
| 151 | + } |
| 152 | + } |
| 153 | + ] |
| 154 | + } |
| 155 | + ] |
| 156 | + }, |
| 157 | + optimization: { |
| 158 | + minimize: false |
| 159 | + } |
| 160 | + }) |
| 161 | + |
| 162 | + expect(fs.readFileSync(path.join(distDir, "bundle.js"), "utf8")).toContain( |
| 163 | + "42" |
| 164 | + ) |
| 165 | + }, 180000) |
| 166 | +}) |
0 commit comments