| 1 |
import path from "path"; |
| 2 |
import { readdirSync } from "fs"; |
| 3 |
import { rollup } from "rollup"; |
| 4 |
import terser from "terser"; |
| 5 |
import babel from "rollup-plugin-babel"; |
| 6 |
import nodeResolve from "rollup-plugin-node-resolve"; |
| 7 |
import chalk from "chalk"; |
| 8 |
import bytes from "pretty-bytes"; |
| 9 |
import preset from ".."; |
| 10 |
import { compressedSize, writeFile, mkdirp } from "./_util"; |
| 11 |
|
| 12 |
jest.setTimeout(30000); |
| 13 |
|
| 14 |
const FIXTURES = path.resolve(__dirname, "integration"); |
| 15 |
|
| 16 |
async function writeBeautified(filename, code) { |
| 17 |
await writeFile( |
| 18 |
filename, |
| 19 |
terser.minify(code, { |
| 20 |
mangle: false, |
| 21 |
compress: false, |
| 22 |
sourceMap: false, |
| 23 |
output: { beautify: true }, |
| 24 |
}).code, |
| 25 |
() => {} |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
const IGNORE_WARNINGS = [ |
| 30 |
"UNRESOLVED_IMPORT", |
| 31 |
"MISSING_GLOBAL_NAME", |
| 32 |
"UNUSED_EXTERNAL_IMPORT", |
| 33 |
]; |
| 34 |
|
| 35 |
async function compile(dir, babelOptions) { |
| 36 |
let pkg = {}; |
| 37 |
try { |
| 38 |
pkg = require(path.resolve(dir, "package.json")); |
| 39 |
} catch (e) {} |
| 40 |
const bundle = await rollup({ |
| 41 |
context: dir, |
| 42 |
input: path.resolve(dir, pkg.main || "index.js"), |
| 43 |
plugins: [ |
| 44 |
nodeResolve({ |
| 45 |
jail: dir, |
| 46 |
}), |
| 47 |
babelOptions !== false && |
| 48 |
babel({ |
| 49 |
babelrc: false, |
| 50 |
...babelOptions, |
| 51 |
}), |
| 52 |
].filter(Boolean), |
| 53 |
onwarn(warning, warn) { |
| 54 |
if (IGNORE_WARNINGS.indexOf(warning.code) === -1) warn(warning); |
| 55 |
}, |
| 56 |
}); |
| 57 |
const { output } = await bundle.generate({ |
| 58 |
format: "iife", |
| 59 |
}); |
| 60 |
return output[0].code; |
| 61 |
} |
| 62 |
|
| 63 |
describe("integration", () => { |
| 64 |
let summary = ""; |
| 65 |
|
| 66 |
for (const f of readdirSync(FIXTURES)) { |
| 67 |
if (f[0] === ".") continue; |
| 68 |
it("should compile " + f + " to less code than preset-env", async () => { |
| 69 |
const context = path.resolve(FIXTURES, f); |
| 70 |
const before = await compile(context, { |
| 71 |
presets: ["@babel/preset-env"], |
| 72 |
plugins: ["@babel/plugin-transform-react-jsx"], |
| 73 |
}); |
| 74 |
const out = await compile(context, { |
| 75 |
presets: [preset], |
| 76 |
plugins: ["@babel/plugin-transform-react-jsx"], |
| 77 |
}); |
| 78 |
const outEnv = await compile(context, { |
| 79 |
presets: [["@babel/preset-env", { targets: { esmodules: true } }]], |
| 80 |
plugins: ["@babel/plugin-transform-react-jsx"], |
| 81 |
}); |
| 82 |
|
| 83 |
|
| 84 |
try { |
| 85 |
mkdirp(path.join(context, ".dist")); |
| 86 |
writeBeautified(path.join(context, ".dist/preset-modules.js"), out); |
| 87 |
writeBeautified(path.join(context, ".dist/preset-env.js"), outEnv); |
| 88 |
} catch (e) {} |
| 89 |
|
| 90 |
const sizeBefore = await compressedSize(before, { |
| 91 |
module: false, |
| 92 |
}); |
| 93 |
|
| 94 |
const size = await compressedSize(out, { module: false }); |
| 95 |
|
| 96 |
const sizeEnv = await compressedSize(outEnv, { |
| 97 |
module: false, |
| 98 |
}); |
| 99 |
|
| 100 |
summary += `\n${f}: ${bytes(size)}`; |
| 101 |
summary += |
| 102 |
chalk.dim( |
| 103 |
`\n → ${sizeEnv - |
| 104 |
size}b smaller than preset-env's targets.esmodules (` |
| 105 |
) + |
| 106 |
chalk.green(`${(((sizeEnv - size) / sizeEnv) * 100) | 0}% savings`) + |
| 107 |
chalk.dim(`)`); |
| 108 |
summary += |
| 109 |
chalk.dim( |
| 110 |
`\n → ${sizeBefore - size}b smaller than preset-env's default (` |
| 111 |
) + |
| 112 |
chalk.green( |
| 113 |
`${(((sizeBefore - size) / sizeBefore) * 100) | 0}% savings` |
| 114 |
) + |
| 115 |
chalk.dim(`)`); |
| 116 |
expect(size).toBeLessThan(sizeEnv); |
| 117 |
}); |
| 118 |
} |
| 119 |
|
| 120 |
afterAll(() => { |
| 121 |
console.log( |
| 122 |
chalk.blueBright( |
| 123 |
`Compared to @babel/preset-env's targets.esmodules output:` |
| 124 |
) + summary |
| 125 |
); |
| 126 |
}); |
| 127 |
}); |