| 1 |
import * as babel from "../lib/index.js"; |
| 2 |
import path from "path"; |
| 3 |
import { fileURLToPath } from "url"; |
| 4 |
|
| 5 |
describe("addon resolution", function () { |
| 6 |
const base = path.join( |
| 7 |
path.dirname(fileURLToPath(import.meta.url)), |
| 8 |
"fixtures", |
| 9 |
"resolution", |
| 10 |
); |
| 11 |
let cwd; |
| 12 |
|
| 13 |
beforeEach(function () { |
| 14 |
cwd = process.cwd(); |
| 15 |
process.chdir(base); |
| 16 |
}); |
| 17 |
|
| 18 |
afterEach(function () { |
| 19 |
process.chdir(cwd); |
| 20 |
}); |
| 21 |
|
| 22 |
it("should find module: presets", function () { |
| 23 |
process.chdir("module-paths"); |
| 24 |
|
| 25 |
babel.transformSync("", { |
| 26 |
filename: "filename.js", |
| 27 |
babelrc: false, |
| 28 |
presets: ["module:preset"], |
| 29 |
}); |
| 30 |
}); |
| 31 |
|
| 32 |
it("should find module: plugins", function () { |
| 33 |
process.chdir("module-paths"); |
| 34 |
|
| 35 |
babel.transformSync("", { |
| 36 |
filename: "filename.js", |
| 37 |
babelrc: false, |
| 38 |
plugins: ["module:plugin"], |
| 39 |
}); |
| 40 |
}); |
| 41 |
|
| 42 |
it("should find standard presets", function () { |
| 43 |
process.chdir("standard-paths"); |
| 44 |
|
| 45 |
babel.transformSync("", { |
| 46 |
filename: "filename.js", |
| 47 |
babelrc: false, |
| 48 |
presets: ["mod"], |
| 49 |
}); |
| 50 |
}); |
| 51 |
|
| 52 |
it("should find standard plugins", function () { |
| 53 |
process.chdir("standard-paths"); |
| 54 |
|
| 55 |
babel.transformSync("", { |
| 56 |
filename: "filename.js", |
| 57 |
babelrc: false, |
| 58 |
plugins: ["mod"], |
| 59 |
}); |
| 60 |
}); |
| 61 |
|
| 62 |
it("should find standard presets with an existing prefix", function () { |
| 63 |
process.chdir("standard-paths"); |
| 64 |
|
| 65 |
babel.transformSync("", { |
| 66 |
filename: "filename.js", |
| 67 |
babelrc: false, |
| 68 |
presets: ["babel-preset-mod"], |
| 69 |
}); |
| 70 |
}); |
| 71 |
|
| 72 |
it("should find standard plugins with an existing prefix", function () { |
| 73 |
process.chdir("standard-paths"); |
| 74 |
|
| 75 |
babel.transformSync("", { |
| 76 |
filename: "filename.js", |
| 77 |
babelrc: false, |
| 78 |
plugins: ["babel-plugin-mod"], |
| 79 |
}); |
| 80 |
}); |
| 81 |
|
| 82 |
it("should find @babel scoped presets", function () { |
| 83 |
process.chdir("babel-org-paths"); |
| 84 |
|
| 85 |
babel.transformSync("", { |
| 86 |
filename: "filename.js", |
| 87 |
babelrc: false, |
| 88 |
presets: ["@babel/foo"], |
| 89 |
}); |
| 90 |
}); |
| 91 |
|
| 92 |
it("should find @babel scoped plugins", function () { |
| 93 |
process.chdir("babel-org-paths"); |
| 94 |
|
| 95 |
babel.transformSync("", { |
| 96 |
filename: "filename.js", |
| 97 |
babelrc: false, |
| 98 |
plugins: ["@babel/foo"], |
| 99 |
}); |
| 100 |
}); |
| 101 |
|
| 102 |
it("should find @babel scoped presets with an existing prefix", function () { |
| 103 |
process.chdir("babel-org-paths"); |
| 104 |
|
| 105 |
babel.transformSync("", { |
| 106 |
filename: "filename.js", |
| 107 |
babelrc: false, |
| 108 |
presets: ["@babel/preset-foo"], |
| 109 |
}); |
| 110 |
}); |
| 111 |
|
| 112 |
it("should find @babel scoped plugins", function () { |
| 113 |
process.chdir("babel-org-paths"); |
| 114 |
|
| 115 |
babel.transformSync("", { |
| 116 |
filename: "filename.js", |
| 117 |
babelrc: false, |
| 118 |
plugins: ["@babel/plugin-foo"], |
| 119 |
}); |
| 120 |
}); |
| 121 |
|
| 122 |
it("should find @foo scoped presets", function () { |
| 123 |
process.chdir("foo-org-paths"); |
| 124 |
|
| 125 |
babel.transformSync("", { |
| 126 |
filename: "filename.js", |
| 127 |
babelrc: false, |
| 128 |
presets: ["@foo/mod"], |
| 129 |
}); |
| 130 |
}); |
| 131 |
|
| 132 |
it("should find @foo scoped plugins", function () { |
| 133 |
process.chdir("foo-org-paths"); |
| 134 |
|
| 135 |
babel.transformSync("", { |
| 136 |
filename: "filename.js", |
| 137 |
babelrc: false, |
| 138 |
plugins: ["@foo/mod"], |
| 139 |
}); |
| 140 |
}); |
| 141 |
|
| 142 |
it("should find @foo scoped presets with an inner babel-preset", function () { |
| 143 |
process.chdir("foo-org-paths"); |
| 144 |
|
| 145 |
babel.transformSync("", { |
| 146 |
filename: "filename.js", |
| 147 |
babelrc: false, |
| 148 |
presets: ["@foo/thing.babel-preset-convert"], |
| 149 |
}); |
| 150 |
}); |
| 151 |
|
| 152 |
it("should find @foo scoped plugins with an inner babel-plugin", function () { |
| 153 |
process.chdir("foo-org-paths"); |
| 154 |
|
| 155 |
babel.transformSync("", { |
| 156 |
filename: "filename.js", |
| 157 |
babelrc: false, |
| 158 |
plugins: ["@foo/thing.babel-plugin-convert"], |
| 159 |
}); |
| 160 |
}); |
| 161 |
|
| 162 |
it("should find @foo scoped presets with an babel-preset suffix", function () { |
| 163 |
process.chdir("foo-org-paths"); |
| 164 |
|
| 165 |
babel.transformSync("", { |
| 166 |
filename: "filename.js", |
| 167 |
babelrc: false, |
| 168 |
presets: ["@foo/thing-babel-preset"], |
| 169 |
}); |
| 170 |
}); |
| 171 |
|
| 172 |
it("should find @foo scoped plugins with an babel-plugin suffix", function () { |
| 173 |
process.chdir("foo-org-paths"); |
| 174 |
|
| 175 |
babel.transformSync("", { |
| 176 |
filename: "filename.js", |
| 177 |
babelrc: false, |
| 178 |
plugins: ["@foo/thing-babel-plugin"], |
| 179 |
}); |
| 180 |
}); |
| 181 |
|
| 182 |
it("should find @foo scoped presets with an existing prefix", function () { |
| 183 |
process.chdir("foo-org-paths"); |
| 184 |
|
| 185 |
babel.transformSync("", { |
| 186 |
filename: "filename.js", |
| 187 |
babelrc: false, |
| 188 |
presets: ["@foo/babel-preset-mod"], |
| 189 |
}); |
| 190 |
}); |
| 191 |
|
| 192 |
it("should find @foo scoped plugins with an existing prefix", function () { |
| 193 |
process.chdir("foo-org-paths"); |
| 194 |
|
| 195 |
babel.transformSync("", { |
| 196 |
filename: "filename.js", |
| 197 |
babelrc: false, |
| 198 |
plugins: ["@foo/babel-plugin-mod"], |
| 199 |
}); |
| 200 |
}); |
| 201 |
|
| 202 |
it("should find @foo/babel-plugin when specified", function () { |
| 203 |
process.chdir("foo-org-paths"); |
| 204 |
|
| 205 |
babel.transformSync("", { |
| 206 |
filename: "filename.js", |
| 207 |
babelrc: false, |
| 208 |
plugins: ["@foo/babel-plugin"], |
| 209 |
}); |
| 210 |
}); |
| 211 |
|
| 212 |
it("should find @foo/babel-preset when specified", function () { |
| 213 |
process.chdir("foo-org-paths"); |
| 214 |
|
| 215 |
babel.transformSync("", { |
| 216 |
filename: "filename.js", |
| 217 |
babelrc: false, |
| 218 |
presets: ["@foo/babel-preset"], |
| 219 |
}); |
| 220 |
}); |
| 221 |
|
| 222 |
it("should find @foo/babel-plugin/index when specified", function () { |
| 223 |
process.chdir("foo-org-paths"); |
| 224 |
|
| 225 |
babel.transformSync("", { |
| 226 |
filename: "filename.js", |
| 227 |
babelrc: false, |
| 228 |
plugins: ["@foo/babel-plugin/index"], |
| 229 |
}); |
| 230 |
}); |
| 231 |
|
| 232 |
it("should find @foo/babel-preset/index when specified", function () { |
| 233 |
process.chdir("foo-org-paths"); |
| 234 |
|
| 235 |
babel.transformSync("", { |
| 236 |
filename: "filename.js", |
| 237 |
babelrc: false, |
| 238 |
presets: ["@foo/babel-preset/index"], |
| 239 |
}); |
| 240 |
}); |
| 241 |
|
| 242 |
it("should find @foo/babel-plugin when just scope given", function () { |
| 243 |
process.chdir("foo-org-paths"); |
| 244 |
|
| 245 |
babel.transformSync("", { |
| 246 |
filename: "filename.js", |
| 247 |
babelrc: false, |
| 248 |
plugins: ["@foo"], |
| 249 |
}); |
| 250 |
}); |
| 251 |
|
| 252 |
it("should find @foo/babel-preset when just scope given", function () { |
| 253 |
process.chdir("foo-org-paths"); |
| 254 |
|
| 255 |
babel.transformSync("", { |
| 256 |
filename: "filename.js", |
| 257 |
babelrc: false, |
| 258 |
presets: ["@foo"], |
| 259 |
}); |
| 260 |
}); |
| 261 |
|
| 262 |
it("should find relative path presets", function () { |
| 263 |
process.chdir("relative-paths"); |
| 264 |
|
| 265 |
babel.transformSync("", { |
| 266 |
filename: "filename.js", |
| 267 |
babelrc: false, |
| 268 |
presets: ["./dir/preset.js"], |
| 269 |
}); |
| 270 |
}); |
| 271 |
|
| 272 |
it("should find relative path plugins", function () { |
| 273 |
process.chdir("relative-paths"); |
| 274 |
|
| 275 |
babel.transformSync("", { |
| 276 |
filename: "filename.js", |
| 277 |
babelrc: false, |
| 278 |
plugins: ["./dir/plugin.js"], |
| 279 |
}); |
| 280 |
}); |
| 281 |
|
| 282 |
it("should find module file presets", function () { |
| 283 |
process.chdir("nested-module-paths"); |
| 284 |
|
| 285 |
babel.transformSync("", { |
| 286 |
filename: "filename.js", |
| 287 |
babelrc: false, |
| 288 |
presets: ["mod/preset"], |
| 289 |
}); |
| 290 |
}); |
| 291 |
|
| 292 |
it("should find module file plugins", function () { |
| 293 |
process.chdir("nested-module-paths"); |
| 294 |
|
| 295 |
babel.transformSync("", { |
| 296 |
filename: "filename.js", |
| 297 |
babelrc: false, |
| 298 |
plugins: ["mod/plugin"], |
| 299 |
}); |
| 300 |
}); |
| 301 |
|
| 302 |
it("should find @foo scoped module file presets", function () { |
| 303 |
process.chdir("scoped-nested-module-paths"); |
| 304 |
|
| 305 |
babel.transformSync("", { |
| 306 |
filename: "filename.js", |
| 307 |
babelrc: false, |
| 308 |
presets: ["@foo/mod/preset"], |
| 309 |
}); |
| 310 |
}); |
| 311 |
|
| 312 |
it("should find @foo scoped module file plugins", function () { |
| 313 |
process.chdir("scoped-nested-module-paths"); |
| 314 |
|
| 315 |
babel.transformSync("", { |
| 316 |
filename: "filename.js", |
| 317 |
babelrc: false, |
| 318 |
plugins: ["@foo/mod/plugin"], |
| 319 |
}); |
| 320 |
}); |
| 321 |
|
| 322 |
it("should find @babel scoped module file presets", function () { |
| 323 |
process.chdir("babel-scoped-nested-module-paths"); |
| 324 |
|
| 325 |
babel.transformSync("", { |
| 326 |
filename: "filename.js", |
| 327 |
babelrc: false, |
| 328 |
presets: ["@babel/mod/preset"], |
| 329 |
}); |
| 330 |
}); |
| 331 |
|
| 332 |
it("should find @babel scoped module file plugins", function () { |
| 333 |
process.chdir("babel-scoped-nested-module-paths"); |
| 334 |
|
| 335 |
babel.transformSync("", { |
| 336 |
filename: "filename.js", |
| 337 |
babelrc: false, |
| 338 |
plugins: ["@babel/mod/plugin"], |
| 339 |
}); |
| 340 |
}); |
| 341 |
|
| 342 |
// TODO(Babel 8): remove node version check. |
| 343 |
it("should throw about module: usage for presets", function () { |
| 344 |
process.chdir("throw-module-paths"); |
| 345 |
|
| 346 |
expect(() => { |
| 347 |
babel.transformSync("", { |
| 348 |
filename: "filename.js", |
| 349 |
babelrc: false, |
| 350 |
presets: ["foo"], |
| 351 |
}); |
| 352 |
}).toThrow( |
| 353 |
// Todo(Babel 8): remove node checks in this file. We cannot test the desired behaviour |
| 354 |
// because Jest 24 has an issue on setting the MODULE_NOT_FOUND error when the native |
| 355 |
// `require.resolve` is provided. |
| 356 |
// see https://github.com/babel/babel/pull/12439/files#r535996000 |
| 357 |
parseInt(process.versions.node, 10) <= 10 |
| 358 |
? /Cannot (?:find|resolve) module 'babel-preset-foo'/ |
| 359 |
: /Cannot (?:find|resolve) module 'babel-preset-foo'.*\n- If you want to resolve "foo", use "module:foo"/s, |
| 360 |
); |
| 361 |
}); |
| 362 |
|
| 363 |
it("should throw about module: usage for plugins", function () { |
| 364 |
process.chdir("throw-module-paths"); |
| 365 |
|
| 366 |
expect(() => { |
| 367 |
babel.transformSync("", { |
| 368 |
filename: "filename.js", |
| 369 |
babelrc: false, |
| 370 |
plugins: ["foo"], |
| 371 |
}); |
| 372 |
}).toThrow( |
| 373 |
parseInt(process.versions.node, 10) <= 10 |
| 374 |
? /Cannot (?:find|resolve) module 'babel-plugin-foo'/ |
| 375 |
: /Cannot (?:find|resolve) module 'babel-plugin-foo'.*\n- If you want to resolve "foo", use "module:foo"/s, |
| 376 |
); |
| 377 |
}); |
| 378 |
|
| 379 |
it("should throw about @babel usage for presets", function () { |
| 380 |
process.chdir("throw-babel-paths"); |
| 381 |
|
| 382 |
expect(() => { |
| 383 |
babel.transformSync("", { |
| 384 |
filename: "filename.js", |
| 385 |
babelrc: false, |
| 386 |
presets: ["foo"], |
| 387 |
}); |
| 388 |
}).toThrow( |
| 389 |
parseInt(process.versions.node, 10) <= 10 |
| 390 |
? /Cannot (?:find|resolve) module 'babel-preset-foo'/ |
| 391 |
: /Cannot (?:find|resolve) module 'babel-preset-foo'.*\n- Did you mean "@babel\/foo"\?/s, |
| 392 |
); |
| 393 |
}); |
| 394 |
|
| 395 |
it("should throw about @babel usage for plugins", function () { |
| 396 |
process.chdir("throw-babel-paths"); |
| 397 |
|
| 398 |
expect(() => { |
| 399 |
babel.transformSync("", { |
| 400 |
filename: "filename.js", |
| 401 |
babelrc: false, |
| 402 |
plugins: ["foo"], |
| 403 |
}); |
| 404 |
}).toThrow( |
| 405 |
parseInt(process.versions.node, 10) <= 10 |
| 406 |
? /Cannot (?:find|resolve) module 'babel-plugin-foo'/ |
| 407 |
: /Cannot (?:find|resolve) module 'babel-plugin-foo'.*\n- Did you mean "@babel\/foo"\?/s, |
| 408 |
); |
| 409 |
}); |
| 410 |
|
| 411 |
it("should throw about passing a preset as a plugin", function () { |
| 412 |
process.chdir("throw-opposite-paths"); |
| 413 |
|
| 414 |
expect(() => { |
| 415 |
babel.transformSync("", { |
| 416 |
filename: "filename.js", |
| 417 |
babelrc: false, |
| 418 |
presets: ["testplugin"], |
| 419 |
}); |
| 420 |
}).toThrow( |
| 421 |
parseInt(process.versions.node, 10) <= 10 |
| 422 |
? /Cannot (?:find|resolve) module 'babel-preset-testplugin'/ |
| 423 |
: /Cannot (?:find|resolve) module 'babel-preset-testplugin'.*\n- Did you accidentally pass a plugin as a preset\?/s, |
| 424 |
); |
| 425 |
}); |
| 426 |
|
| 427 |
it("should throw about passing a plugin as a preset", function () { |
| 428 |
process.chdir("throw-opposite-paths"); |
| 429 |
|
| 430 |
expect(() => { |
| 431 |
babel.transformSync("", { |
| 432 |
filename: "filename.js", |
| 433 |
babelrc: false, |
| 434 |
plugins: ["testpreset"], |
| 435 |
}); |
| 436 |
}).toThrow( |
| 437 |
parseInt(process.versions.node, 10) <= 10 |
| 438 |
? /Cannot (?:find|resolve) module 'babel-plugin-testpreset'/ |
| 439 |
: /Cannot (?:find|resolve) module 'babel-plugin-testpreset'.*\n- Did you accidentally pass a preset as a plugin\?/s, |
| 440 |
); |
| 441 |
}); |
| 442 |
|
| 443 |
it("should throw about missing presets", function () { |
| 444 |
process.chdir("throw-missing-paths"); |
| 445 |
|
| 446 |
expect(() => { |
| 447 |
babel.transformSync("", { |
| 448 |
filename: "filename.js", |
| 449 |
babelrc: false, |
| 450 |
presets: ["foo"], |
| 451 |
}); |
| 452 |
}).toThrow(/Cannot (?:find|resolve) module 'babel-preset-foo'/); |
| 453 |
}); |
| 454 |
|
| 455 |
it("should throw about missing plugins", function () { |
| 456 |
process.chdir("throw-missing-paths"); |
| 457 |
|
| 458 |
expect(() => { |
| 459 |
babel.transformSync("", { |
| 460 |
filename: "filename.js", |
| 461 |
babelrc: false, |
| 462 |
plugins: ["foo"], |
| 463 |
}); |
| 464 |
}).toThrow(/Cannot (?:find|resolve) module 'babel-plugin-foo'/); |
| 465 |
}); |
| 466 |
|
| 467 |
const nodeGte12 = parseInt(process.versions.node, 10) >= 12 ? it : it.skip; |
| 468 |
|
| 469 |
nodeGte12("should respect package.json#exports", async function () { |
| 470 |
process.chdir("pkg-exports"); |
| 471 |
|
| 472 |
expect( |
| 473 |
babel.transformSync("", { |
| 474 |
filename: "filename.js", |
| 475 |
babelrc: false, |
| 476 |
configFile: false, |
| 477 |
plugins: ["babel-plugin-dual"], |
| 478 |
}).code, |
| 479 |
).toBe(`"CJS"`); |
| 480 |
|
| 481 |
expect( |
| 482 |
( |
| 483 |
await babel.transformAsync("", { |
| 484 |
filename: "filename.js", |
| 485 |
babelrc: false, |
| 486 |
configFile: false, |
| 487 |
plugins: ["babel-plugin-dual"], |
| 488 |
}) |
| 489 |
).code, |
| 490 |
).toBe(`"ESM"`); |
| 491 |
}); |
| 492 |
}); |