| 1 |
import * as babel from "@babel/core"; |
| 2 |
import definePolyfillProvider from "../lib"; |
| 3 |
import astToCode from "./helpers/ast-to-code-serializer.js"; |
| 4 |
import pluginCJS from "@babel/plugin-transform-modules-commonjs"; |
| 5 |
|
| 6 |
function withUtils(code, fn, opts) { |
| 7 |
let result; |
| 8 |
|
| 9 |
const provider = definePolyfillProvider(() => ({ |
| 10 |
usageGlobal(_, utils) { |
| 11 |
result = fn(utils); |
| 12 |
}, |
| 13 |
})); |
| 14 |
|
| 15 |
const { ast } = babel.transformSync(code, { |
| 16 |
configFile: false, |
| 17 |
plugins: [[provider, { method: "usage-global" }]], |
| 18 |
ast: true, |
| 19 |
code: false, |
| 20 |
sourceType: "module", |
| 21 |
...opts, |
| 22 |
}); |
| 23 |
|
| 24 |
return { ast, result }; |
| 25 |
} |
| 26 |
|
| 27 |
expect.addSnapshotSerializer(astToCode); |
| 28 |
|
| 29 |
describe("injectors", () => { |
| 30 |
describe("injectGlobalImport", () => { |
| 31 |
it("in module", () => { |
| 32 |
const { ast, result } = withUtils("foo", utils => |
| 33 |
utils.injectGlobalImport("./polyfill/foo"), |
| 34 |
); |
| 35 |
|
| 36 |
expect(ast).toMatchInlineSnapshot(` |
| 37 |
import "./polyfill/foo"; |
| 38 |
foo; |
| 39 |
`); |
| 40 |
|
| 41 |
expect(result).toBeUndefined(); |
| 42 |
}); |
| 43 |
|
| 44 |
it("in script", () => { |
| 45 |
const { ast, result } = withUtils( |
| 46 |
"foo", |
| 47 |
utils => utils.injectGlobalImport("./polyfill/foo"), |
| 48 |
{ sourceType: "script" }, |
| 49 |
); |
| 50 |
|
| 51 |
expect(ast).toMatchInlineSnapshot(` |
| 52 |
require("./polyfill/foo"); |
| 53 |
|
| 54 |
foo; |
| 55 |
`); |
| 56 |
|
| 57 |
expect(result).toBeUndefined(); |
| 58 |
}); |
| 59 |
|
| 60 |
it("multiple imports, different path", () => { |
| 61 |
const { ast } = withUtils("foo", utils => { |
| 62 |
utils.injectGlobalImport("./polyfill/foo"); |
| 63 |
utils.injectGlobalImport("./polyfill/bar"); |
| 64 |
}); |
| 65 |
|
| 66 |
expect(ast).toMatchInlineSnapshot(` |
| 67 |
import "./polyfill/foo"; |
| 68 |
import "./polyfill/bar"; |
| 69 |
foo; |
| 70 |
`); |
| 71 |
}); |
| 72 |
|
| 73 |
it("multiple imports, same path", () => { |
| 74 |
const { ast } = withUtils("foo", utils => { |
| 75 |
utils.injectGlobalImport("./polyfill/foo"); |
| 76 |
utils.injectGlobalImport("./polyfill/foo"); |
| 77 |
}); |
| 78 |
|
| 79 |
expect(ast).toMatchInlineSnapshot(` |
| 80 |
import "./polyfill/foo"; |
| 81 |
foo; |
| 82 |
`); |
| 83 |
}); |
| 84 |
}); |
| 85 |
|
| 86 |
describe("injectNamedImport", () => { |
| 87 |
it("returns an identifier", () => { |
| 88 |
const { result } = withUtils("foo", utils => |
| 89 |
utils.injectNamedImport("./polyfill/foo", "fooPolyfill"), |
| 90 |
); |
| 91 |
|
| 92 |
expect(result).toEqual( |
| 93 |
expect.objectContaining({ |
| 94 |
type: "Identifier", |
| 95 |
}), |
| 96 |
); |
| 97 |
}); |
| 98 |
|
| 99 |
it("in module", () => { |
| 100 |
const { ast } = withUtils("foo", utils => { |
| 101 |
utils.injectNamedImport("./polyfill/foo", "fooPolyfill"); |
| 102 |
}); |
| 103 |
|
| 104 |
expect(ast).toMatchInlineSnapshot(` |
| 105 |
import { fooPolyfill as _fooPolyfill } from "./polyfill/foo"; |
| 106 |
foo; |
| 107 |
`); |
| 108 |
}); |
| 109 |
|
| 110 |
it("in script", () => { |
| 111 |
const { ast } = withUtils( |
| 112 |
"foo", |
| 113 |
utils => |
| 114 |
utils.injectNamedImport("./polyfill/foo", "foo", "fooPolyfill"), |
| 115 |
{ sourceType: "script" }, |
| 116 |
); |
| 117 |
|
| 118 |
expect(ast).toMatchInlineSnapshot(` |
| 119 |
var _fooPolyfill = require("./polyfill/foo").foo; |
| 120 |
|
| 121 |
foo; |
| 122 |
`); |
| 123 |
}); |
| 124 |
|
| 125 |
it("respects name hint", () => { |
| 126 |
const { ast, result } = withUtils("foo", utils => |
| 127 |
utils.injectNamedImport("./polyfill/foo", "fooPolyfill", "dog"), |
| 128 |
); |
| 129 |
|
| 130 |
expect(ast).toMatchInlineSnapshot(` |
| 131 |
import { fooPolyfill as _dog } from "./polyfill/foo"; |
| 132 |
foo; |
| 133 |
`); |
| 134 |
|
| 135 |
expect(result.name).toEqual(expect.stringContaining("dog")); |
| 136 |
}); |
| 137 |
|
| 138 |
it("multiple imports, different path, different name", () => { |
| 139 |
const { ast, result } = withUtils("foo", utils => [ |
| 140 |
utils.injectNamedImport("./polyfill/foo", "fooPolyfill"), |
| 141 |
utils.injectNamedImport("./polyfill/bar", "barPolyfill"), |
| 142 |
]); |
| 143 |
|
| 144 |
expect(ast).toMatchInlineSnapshot(` |
| 145 |
import { fooPolyfill as _fooPolyfill } from "./polyfill/foo"; |
| 146 |
import { barPolyfill as _barPolyfill } from "./polyfill/bar"; |
| 147 |
foo; |
| 148 |
`); |
| 149 |
|
| 150 |
expect(result[0].name).not.toBe(result[1].name); |
| 151 |
}); |
| 152 |
|
| 153 |
it("multiple imports, same path, different name", () => { |
| 154 |
const { ast, result } = withUtils("foo", utils => [ |
| 155 |
utils.injectNamedImport("./polyfill/foo", "fooPolyfill"), |
| 156 |
utils.injectNamedImport("./polyfill/foo", "barPolyfill"), |
| 157 |
]); |
| 158 |
|
| 159 |
|
| 160 |
expect(ast).toMatchInlineSnapshot(` |
| 161 |
import { fooPolyfill as _fooPolyfill } from "./polyfill/foo"; |
| 162 |
import { barPolyfill as _barPolyfill } from "./polyfill/foo"; |
| 163 |
foo; |
| 164 |
`); |
| 165 |
|
| 166 |
expect(result[0].name).not.toBe(result[1].name); |
| 167 |
}); |
| 168 |
|
| 169 |
it("multiple imports, different path, same name", () => { |
| 170 |
const { ast, result } = withUtils("foo", utils => [ |
| 171 |
utils.injectNamedImport("./polyfill/foo", "fooPolyfill"), |
| 172 |
utils.injectNamedImport("./polyfill/bar", "fooPolyfill"), |
| 173 |
]); |
| 174 |
|
| 175 |
expect(ast).toMatchInlineSnapshot(` |
| 176 |
import { fooPolyfill as _fooPolyfill } from "./polyfill/foo"; |
| 177 |
import { fooPolyfill as _fooPolyfill2 } from "./polyfill/bar"; |
| 178 |
foo; |
| 179 |
`); |
| 180 |
|
| 181 |
expect(result[0].name).not.toBe(result[1].name); |
| 182 |
}); |
| 183 |
|
| 184 |
it("multiple imports, same path, same name", () => { |
| 185 |
const { ast, result } = withUtils("foo", utils => [ |
| 186 |
utils.injectNamedImport("./polyfill/foo", "fooPolyfill"), |
| 187 |
utils.injectNamedImport("./polyfill/foo", "fooPolyfill"), |
| 188 |
]); |
| 189 |
|
| 190 |
expect(ast).toMatchInlineSnapshot(` |
| 191 |
import { fooPolyfill as _fooPolyfill } from "./polyfill/foo"; |
| 192 |
foo; |
| 193 |
`); |
| 194 |
|
| 195 |
expect(result[0].name).toBe(result[1].name); |
| 196 |
}); |
| 197 |
}); |
| 198 |
|
| 199 |
describe("injectDefaultImport", () => { |
| 200 |
it("returns an identifier", () => { |
| 201 |
const { result } = withUtils("foo", utils => |
| 202 |
utils.injectDefaultImport("./polyfill/foo"), |
| 203 |
); |
| 204 |
|
| 205 |
expect(result).toEqual( |
| 206 |
expect.objectContaining({ |
| 207 |
type: "Identifier", |
| 208 |
}), |
| 209 |
); |
| 210 |
}); |
| 211 |
|
| 212 |
it("in module", () => { |
| 213 |
const { ast } = withUtils("foo", utils => { |
| 214 |
utils.injectDefaultImport("./polyfill/foo"); |
| 215 |
}); |
| 216 |
|
| 217 |
expect(ast).toMatchInlineSnapshot(` |
| 218 |
import _polyfillFoo from "./polyfill/foo"; |
| 219 |
foo; |
| 220 |
`); |
| 221 |
}); |
| 222 |
|
| 223 |
it("in script", () => { |
| 224 |
const { ast } = withUtils( |
| 225 |
"foo", |
| 226 |
utils => utils.injectDefaultImport("./polyfill/foo", "foo"), |
| 227 |
{ sourceType: "script" }, |
| 228 |
); |
| 229 |
|
| 230 |
expect(ast).toMatchInlineSnapshot(` |
| 231 |
var _foo = require("./polyfill/foo"); |
| 232 |
|
| 233 |
foo; |
| 234 |
`); |
| 235 |
}); |
| 236 |
|
| 237 |
it("respects name hint", () => { |
| 238 |
const { ast, result } = withUtils("foo", utils => |
| 239 |
utils.injectDefaultImport("./polyfill/foo", "dog"), |
| 240 |
); |
| 241 |
|
| 242 |
expect(ast).toMatchInlineSnapshot(` |
| 243 |
import _dog from "./polyfill/foo"; |
| 244 |
foo; |
| 245 |
`); |
| 246 |
|
| 247 |
expect(result.name).toEqual(expect.stringContaining("dog")); |
| 248 |
}); |
| 249 |
|
| 250 |
it("multiple imports, different path", () => { |
| 251 |
const { ast, result } = withUtils("foo", utils => [ |
| 252 |
utils.injectDefaultImport("./polyfill/foo"), |
| 253 |
utils.injectDefaultImport("./polyfill/bar"), |
| 254 |
]); |
| 255 |
|
| 256 |
expect(ast).toMatchInlineSnapshot(` |
| 257 |
import _polyfillFoo from "./polyfill/foo"; |
| 258 |
import _polyfillBar from "./polyfill/bar"; |
| 259 |
foo; |
| 260 |
`); |
| 261 |
|
| 262 |
expect(result[0].name).not.toBe(result[1].name); |
| 263 |
}); |
| 264 |
|
| 265 |
it("multiple imports, same path", () => { |
| 266 |
const { ast, result } = withUtils("foo", utils => [ |
| 267 |
utils.injectDefaultImport("./polyfill/foo"), |
| 268 |
utils.injectDefaultImport("./polyfill/foo"), |
| 269 |
]); |
| 270 |
|
| 271 |
expect(ast).toMatchInlineSnapshot(` |
| 272 |
import _polyfillFoo from "./polyfill/foo"; |
| 273 |
foo; |
| 274 |
`); |
| 275 |
|
| 276 |
expect(result[0].name).toBe(result[1].name); |
| 277 |
}); |
| 278 |
}); |
| 279 |
|
| 280 |
describe("integration", () => { |
| 281 |
it("all together, same path", () => { |
| 282 |
const { ast } = withUtils("foo", utils => [ |
| 283 |
utils.injectNamedImport("./polyfill/foo", "foo"), |
| 284 |
utils.injectNamedImport("./polyfill/foo", "bar"), |
| 285 |
utils.injectDefaultImport("./polyfill/foo"), |
| 286 |
utils.injectGlobalImport("./polyfill/foo"), |
| 287 |
]); |
| 288 |
|
| 289 |
|
| 290 |
expect(ast).toMatchInlineSnapshot(` |
| 291 |
import { foo as _foo } from "./polyfill/foo"; |
| 292 |
import { bar as _bar } from "./polyfill/foo"; |
| 293 |
import _polyfillFoo from "./polyfill/foo"; |
| 294 |
import "./polyfill/foo"; |
| 295 |
foo; |
| 296 |
`); |
| 297 |
}); |
| 298 |
|
| 299 |
it("all together, different path", () => { |
| 300 |
const { ast } = withUtils("foo", utils => [ |
| 301 |
utils.injectNamedImport("./polyfill/foo", "foo"), |
| 302 |
utils.injectNamedImport("./polyfill/bar", "bar"), |
| 303 |
utils.injectDefaultImport("./polyfill/dog"), |
| 304 |
utils.injectGlobalImport("./polyfill/cat"), |
| 305 |
]); |
| 306 |
|
| 307 |
expect(ast).toMatchInlineSnapshot(` |
| 308 |
import { foo as _foo } from "./polyfill/foo"; |
| 309 |
import { bar as _bar } from "./polyfill/bar"; |
| 310 |
import _polyfillDog from "./polyfill/dog"; |
| 311 |
import "./polyfill/cat"; |
| 312 |
foo; |
| 313 |
`); |
| 314 |
}); |
| 315 |
|
| 316 |
describe("with the commonjs transform", () => { |
| 317 |
function testImportsCacheWithModulesTransform(method, provider) { |
| 318 |
const source = "foo;"; |
| 319 |
|
| 320 |
return babel.transformSync(source, { |
| 321 |
configFile: false, |
| 322 |
plugins: [ |
| 323 |
[definePolyfillProvider(() => provider), { method }], |
| 324 |
pluginCJS, |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
({ template }) => ({ |
| 329 |
visitor: { |
| 330 |
Program: { |
| 331 |
exit(path) { |
| 332 |
path.pushContainer("body", template.ast(source)); |
| 333 |
}, |
| 334 |
}, |
| 335 |
}, |
| 336 |
}), |
| 337 |
], |
| 338 |
ast: true, |
| 339 |
code: false, |
| 340 |
sourceType: "module", |
| 341 |
}); |
| 342 |
} |
| 343 |
|
| 344 |
it("injectGlobalImport", () => { |
| 345 |
const { ast } = testImportsCacheWithModulesTransform("usage-global", { |
| 346 |
usageGlobal(meta, utils) { |
| 347 |
if (meta.kind === "global" && meta.name === "foo") { |
| 348 |
utils.injectGlobalImport("./polyfill/foo"); |
| 349 |
} |
| 350 |
}, |
| 351 |
}); |
| 352 |
|
| 353 |
expect(ast).toMatchInlineSnapshot(` |
| 354 |
"use strict"; |
| 355 |
|
| 356 |
require("./polyfill/foo"); |
| 357 |
|
| 358 |
foo; |
| 359 |
foo; |
| 360 |
`); |
| 361 |
}); |
| 362 |
|
| 363 |
it("injectNamedImport", () => { |
| 364 |
const { ast } = testImportsCacheWithModulesTransform("usage-global", { |
| 365 |
usageGlobal(meta, utils, path) { |
| 366 |
if (meta.kind === "global" && meta.name === "foo") { |
| 367 |
path.replaceWith( |
| 368 |
utils.injectNamedImport("./polyfill/foo", "foo"), |
| 369 |
); |
| 370 |
} |
| 371 |
}, |
| 372 |
}); |
| 373 |
|
| 374 |
expect(ast).toMatchInlineSnapshot(` |
| 375 |
"use strict"; |
| 376 |
|
| 377 |
var _foo3 = require("./polyfill/foo").foo; |
| 378 |
|
| 379 |
var _foo2 = require("./polyfill/foo"); |
| 380 |
|
| 381 |
_foo2.foo; |
| 382 |
_foo3; |
| 383 |
`); |
| 384 |
}); |
| 385 |
|
| 386 |
it("injectDefaultImport", () => { |
| 387 |
const { ast } = testImportsCacheWithModulesTransform("usage-global", { |
| 388 |
usageGlobal(meta, utils, path) { |
| 389 |
if (meta.kind === "global" && meta.name === "foo") { |
| 390 |
path.replaceWith( |
| 391 |
utils.injectDefaultImport("./polyfill/foo", "foo"), |
| 392 |
); |
| 393 |
} |
| 394 |
}, |
| 395 |
}); |
| 396 |
|
| 397 |
expect(ast).toMatchInlineSnapshot(` |
| 398 |
"use strict"; |
| 399 |
|
| 400 |
var _foo3 = require("./polyfill/foo"); |
| 401 |
|
| 402 |
var _foo2 = _interopRequireDefault(require("./polyfill/foo")); |
| 403 |
|
| 404 |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
| 405 |
|
| 406 |
_foo2.default; |
| 407 |
_foo3; |
| 408 |
`); |
| 409 |
}); |
| 410 |
}); |
| 411 |
}); |
| 412 |
}); |