[79a0317] | 1 | var path = require('path');
|
---|
| 2 |
|
---|
| 3 | // We only register on the final extension (like `.js`) due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353
|
---|
| 4 | // However, we use these matchers to apply the transform only if the full extension matches
|
---|
| 5 | function endsInJsx(filename) {
|
---|
| 6 | return filename.endsWith('.jsx');
|
---|
| 7 | }
|
---|
| 8 | function endsInTs(filename) {
|
---|
| 9 | return filename.endsWith('.ts');
|
---|
| 10 | }
|
---|
| 11 | function endsInTsx(filename) {
|
---|
| 12 | return filename.endsWith('.tsx');
|
---|
| 13 | }
|
---|
| 14 | function endsInBabelJs(filename) {
|
---|
| 15 | return filename.endsWith('.babel.js');
|
---|
| 16 | }
|
---|
| 17 | function endsInBabelJsx(filename) {
|
---|
| 18 | return filename.endsWith('.babel.jsx');
|
---|
| 19 | }
|
---|
| 20 | function endsInBabelTs(filename) {
|
---|
| 21 | return filename.endsWith('.babel.ts');
|
---|
| 22 | }
|
---|
| 23 | function endsInBabelTsx(filename) {
|
---|
| 24 | return filename.endsWith('.babel.tsx');
|
---|
| 25 | }
|
---|
| 26 | function endsInEsbuildJs(filename) {
|
---|
| 27 | return filename.endsWith('.esbuild.js');
|
---|
| 28 | }
|
---|
| 29 | function endsInEsbuildJsx(filename) {
|
---|
| 30 | return filename.endsWith('.esbuild.jsx');
|
---|
| 31 | }
|
---|
| 32 | function endsInEsbuildTs(filename) {
|
---|
| 33 | return filename.endsWith('.esbuild.ts');
|
---|
| 34 | }
|
---|
| 35 | function endsInEsbuildTsx(filename) {
|
---|
| 36 | return filename.endsWith('.esbuild.tsx');
|
---|
| 37 | }
|
---|
| 38 | function endsInSucraseJs(filename) {
|
---|
| 39 | return filename.endsWith('.sucrase.js');
|
---|
| 40 | }
|
---|
| 41 | function endsInSucraseJsx(filename) {
|
---|
| 42 | return filename.endsWith('.sucrase.jsx');
|
---|
| 43 | }
|
---|
| 44 | function endsInSucraseTs(filename) {
|
---|
| 45 | return filename.endsWith('.sucrase.ts');
|
---|
| 46 | }
|
---|
| 47 | function endsInSucraseTsx(filename) {
|
---|
| 48 | return filename.endsWith('.sucrase.tsx');
|
---|
| 49 | }
|
---|
| 50 | function endsInSwcJs(filename) {
|
---|
| 51 | return filename.endsWith('.swc.js');
|
---|
| 52 | }
|
---|
| 53 | function endsInSwcJsx(filename) {
|
---|
| 54 | return filename.endsWith('.swc.jsx');
|
---|
| 55 | }
|
---|
| 56 | function endsInSwcTs(filename) {
|
---|
| 57 | return filename.endsWith('.swc.ts');
|
---|
| 58 | }
|
---|
| 59 | function endsInSwcTsx(filename) {
|
---|
| 60 | return filename.endsWith('.swc.tsx');
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | var cjsStub = path.join(__dirname, 'cjs-stub');
|
---|
| 64 | var mjsStub = path.join(__dirname, 'mjs-stub');
|
---|
| 65 |
|
---|
| 66 | function isNodeModules(file) {
|
---|
| 67 | return path.relative(process.cwd(), file).includes('node_modules');
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | var extensions = {
|
---|
| 71 | '.babel.js': {
|
---|
| 72 | module: '@babel/register',
|
---|
| 73 | register: function (hook, config) {
|
---|
| 74 | config = config || {
|
---|
| 75 | rootMode: 'upward-optional',
|
---|
| 76 | overrides: [{ only: [endsInBabelJs], presets: ['@babel/preset-env'] }],
|
---|
| 77 | };
|
---|
| 78 |
|
---|
| 79 | hook(Object.assign({}, config, { extensions: '.js' }));
|
---|
| 80 | },
|
---|
| 81 | },
|
---|
| 82 | '.babel.jsx': {
|
---|
| 83 | module: '@babel/register',
|
---|
| 84 | register: function (hook, config) {
|
---|
| 85 | config = config || {
|
---|
| 86 | rootMode: 'upward-optional',
|
---|
| 87 | overrides: [
|
---|
| 88 | {
|
---|
| 89 | only: [endsInBabelJsx],
|
---|
| 90 | presets: ['@babel/preset-env', '@babel/preset-react'],
|
---|
| 91 | },
|
---|
| 92 | ],
|
---|
| 93 | };
|
---|
| 94 |
|
---|
| 95 | hook(Object.assign({}, config, { extensions: '.jsx' }));
|
---|
| 96 | },
|
---|
| 97 | },
|
---|
| 98 | '.babel.ts': [
|
---|
| 99 | {
|
---|
| 100 | module: '@babel/register',
|
---|
| 101 | register: function (hook, config) {
|
---|
| 102 | config = config || {
|
---|
| 103 | rootMode: 'upward-optional',
|
---|
| 104 | overrides: [
|
---|
| 105 | {
|
---|
| 106 | only: [endsInBabelTs],
|
---|
| 107 | presets: ['@babel/preset-env', '@babel/preset-typescript'],
|
---|
| 108 | },
|
---|
| 109 | ],
|
---|
| 110 | };
|
---|
| 111 |
|
---|
| 112 | hook(Object.assign({}, config, { extensions: '.ts' }));
|
---|
| 113 | },
|
---|
| 114 | },
|
---|
| 115 | ],
|
---|
| 116 | '.babel.tsx': {
|
---|
| 117 | module: '@babel/register',
|
---|
| 118 | register: function (hook, config) {
|
---|
| 119 | config = config || {
|
---|
| 120 | rootMode: 'upward-optional',
|
---|
| 121 | overrides: [
|
---|
| 122 | {
|
---|
| 123 | only: [endsInBabelTsx],
|
---|
| 124 | presets: [
|
---|
| 125 | '@babel/preset-env',
|
---|
| 126 | '@babel/preset-react',
|
---|
| 127 | [
|
---|
| 128 | '@babel/preset-typescript',
|
---|
| 129 | {
|
---|
| 130 | isTSX: true,
|
---|
| 131 | allExtensions: true,
|
---|
| 132 | },
|
---|
| 133 | ],
|
---|
| 134 | ],
|
---|
| 135 | },
|
---|
| 136 | ],
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | hook(Object.assign({}, config, { extensions: '.tsx' }));
|
---|
| 140 | },
|
---|
| 141 | },
|
---|
| 142 | '.cjs': cjsStub,
|
---|
| 143 | '.coffee': 'coffeescript/register',
|
---|
| 144 | '.coffee.md': 'coffeescript/register',
|
---|
| 145 | '.esbuild.js': {
|
---|
| 146 | module: 'esbuild-register/dist/node',
|
---|
| 147 | register: function (mod, config) {
|
---|
| 148 | config = config || {
|
---|
| 149 | target: 'node' + process.version.slice(1),
|
---|
| 150 | hookMatcher: endsInEsbuildJs,
|
---|
| 151 | };
|
---|
| 152 |
|
---|
| 153 | mod.register(Object.assign({}, config, { extensions: ['.js'] }));
|
---|
| 154 | },
|
---|
| 155 | },
|
---|
| 156 | '.esbuild.jsx': {
|
---|
| 157 | module: 'esbuild-register/dist/node',
|
---|
| 158 | register: function (mod, config) {
|
---|
| 159 | config = config || {
|
---|
| 160 | target: 'node' + process.version.slice(1),
|
---|
| 161 | hookMatcher: endsInEsbuildJsx,
|
---|
| 162 | };
|
---|
| 163 |
|
---|
| 164 | mod.register(Object.assign({}, config, { extensions: ['.jsx'] }));
|
---|
| 165 | },
|
---|
| 166 | },
|
---|
| 167 | '.esbuild.ts': {
|
---|
| 168 | module: 'esbuild-register/dist/node',
|
---|
| 169 | register: function (mod, config) {
|
---|
| 170 | config = config || {
|
---|
| 171 | target: 'node' + process.version.slice(1),
|
---|
| 172 | hookMatcher: endsInEsbuildTs,
|
---|
| 173 | };
|
---|
| 174 |
|
---|
| 175 | mod.register(Object.assign({}, config, { extensions: ['.ts'] }));
|
---|
| 176 | },
|
---|
| 177 | },
|
---|
| 178 | '.esbuild.tsx': {
|
---|
| 179 | module: 'esbuild-register/dist/node',
|
---|
| 180 | register: function (mod, config) {
|
---|
| 181 | config = config || {
|
---|
| 182 | target: 'node' + process.version.slice(1),
|
---|
| 183 | hookMatcher: endsInEsbuildTsx,
|
---|
| 184 | };
|
---|
| 185 |
|
---|
| 186 | mod.register(Object.assign({}, config, { extensions: ['.tsx'] }));
|
---|
| 187 | },
|
---|
| 188 | },
|
---|
| 189 | '.esm.js': {
|
---|
| 190 | module: 'esm',
|
---|
| 191 | register: function (hook) {
|
---|
| 192 | // register on .js extension due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353
|
---|
| 193 | // which only captures the final extension (.esm.js -> .js)
|
---|
| 194 | var esmLoader = hook(module);
|
---|
| 195 | require.extensions['.js'] = esmLoader('module')._extensions['.js'];
|
---|
| 196 | },
|
---|
| 197 | },
|
---|
| 198 | '.js': null,
|
---|
| 199 | '.json': null,
|
---|
| 200 | '.json5': 'json5/lib/register',
|
---|
| 201 | '.jsx': [
|
---|
| 202 | {
|
---|
| 203 | module: '@babel/register',
|
---|
| 204 | register: function (hook, config) {
|
---|
| 205 | config = config || {
|
---|
| 206 | rootMode: 'upward-optional',
|
---|
| 207 | overrides: [
|
---|
| 208 | {
|
---|
| 209 | only: [endsInJsx],
|
---|
| 210 | presets: ['@babel/preset-env', '@babel/preset-react'],
|
---|
| 211 | },
|
---|
| 212 | ],
|
---|
| 213 | };
|
---|
| 214 |
|
---|
| 215 | hook(Object.assign({}, config, { extensions: '.jsx' }));
|
---|
| 216 | },
|
---|
| 217 | },
|
---|
| 218 | 'sucrase/register/jsx',
|
---|
| 219 | ],
|
---|
| 220 | '.litcoffee': 'coffeescript/register',
|
---|
| 221 | // The mdx loader hooks both `.md` and `.mdx` when it is imported
|
---|
| 222 | // but we only install the hook if `.mdx` is the first file
|
---|
| 223 | '.mdx': '@mdx-js/register',
|
---|
| 224 | '.mjs': mjsStub,
|
---|
| 225 | '.node': null,
|
---|
| 226 | '.sucrase.js': {
|
---|
| 227 | module: 'sucrase/dist/register',
|
---|
| 228 | register: function (hook, config) {
|
---|
| 229 | config = config || {
|
---|
| 230 | matcher: endsInSucraseJs,
|
---|
| 231 | };
|
---|
| 232 |
|
---|
| 233 | hook.registerJS(config);
|
---|
| 234 | },
|
---|
| 235 | },
|
---|
| 236 | '.sucrase.jsx': {
|
---|
| 237 | module: 'sucrase/dist/register',
|
---|
| 238 | register: function (hook, config) {
|
---|
| 239 | config = config || {
|
---|
| 240 | matcher: endsInSucraseJsx,
|
---|
| 241 | };
|
---|
| 242 |
|
---|
| 243 | hook.registerJSX(config);
|
---|
| 244 | },
|
---|
| 245 | },
|
---|
| 246 | '.sucrase.ts': {
|
---|
| 247 | module: 'sucrase/dist/register',
|
---|
| 248 | register: function (hook, config) {
|
---|
| 249 | config = config || {
|
---|
| 250 | matcher: endsInSucraseTs,
|
---|
| 251 | };
|
---|
| 252 |
|
---|
| 253 | hook.registerTS(config);
|
---|
| 254 | },
|
---|
| 255 | },
|
---|
| 256 | '.sucrase.tsx': {
|
---|
| 257 | module: 'sucrase/dist/register',
|
---|
| 258 | register: function (hook, config) {
|
---|
| 259 | config = config || {
|
---|
| 260 | matcher: endsInSucraseTsx,
|
---|
| 261 | };
|
---|
| 262 |
|
---|
| 263 | hook.registerTSX(config);
|
---|
| 264 | },
|
---|
| 265 | },
|
---|
| 266 | '.swc.js': {
|
---|
| 267 | module: '@swc/register',
|
---|
| 268 | register: function (hook, config) {
|
---|
| 269 | config = config || {
|
---|
| 270 | only: [endsInSwcJs],
|
---|
| 271 | ignore: [isNodeModules],
|
---|
| 272 | jsc: {
|
---|
| 273 | parser: {
|
---|
| 274 | syntax: 'ecmascript',
|
---|
| 275 | },
|
---|
| 276 | },
|
---|
| 277 | module: {
|
---|
| 278 | type: 'commonjs',
|
---|
| 279 | },
|
---|
| 280 | };
|
---|
| 281 |
|
---|
| 282 | hook(
|
---|
| 283 | Object.assign({}, config, {
|
---|
| 284 | extensions: '.js',
|
---|
| 285 | })
|
---|
| 286 | );
|
---|
| 287 | },
|
---|
| 288 | },
|
---|
| 289 | '.swc.jsx': {
|
---|
| 290 | module: '@swc/register',
|
---|
| 291 | register: function (hook, config) {
|
---|
| 292 | config = config || {
|
---|
| 293 | only: [endsInSwcJsx],
|
---|
| 294 | ignore: [isNodeModules],
|
---|
| 295 | jsc: {
|
---|
| 296 | parser: {
|
---|
| 297 | syntax: 'ecmascript',
|
---|
| 298 | jsx: true,
|
---|
| 299 | },
|
---|
| 300 | },
|
---|
| 301 | module: {
|
---|
| 302 | type: 'commonjs',
|
---|
| 303 | },
|
---|
| 304 | };
|
---|
| 305 |
|
---|
| 306 | hook(
|
---|
| 307 | Object.assign({}, config, {
|
---|
| 308 | extensions: '.jsx',
|
---|
| 309 | })
|
---|
| 310 | );
|
---|
| 311 | },
|
---|
| 312 | },
|
---|
| 313 | '.swc.ts': {
|
---|
| 314 | module: '@swc/register',
|
---|
| 315 | register: function (hook, config) {
|
---|
| 316 | config = config || {
|
---|
| 317 | only: [endsInSwcTs],
|
---|
| 318 | ignore: [isNodeModules],
|
---|
| 319 | jsc: {
|
---|
| 320 | parser: {
|
---|
| 321 | syntax: 'typescript',
|
---|
| 322 | },
|
---|
| 323 | },
|
---|
| 324 | module: {
|
---|
| 325 | type: 'commonjs',
|
---|
| 326 | },
|
---|
| 327 | };
|
---|
| 328 |
|
---|
| 329 | hook(
|
---|
| 330 | Object.assign({}, config, {
|
---|
| 331 | extensions: '.ts',
|
---|
| 332 | })
|
---|
| 333 | );
|
---|
| 334 | },
|
---|
| 335 | },
|
---|
| 336 | '.swc.tsx': {
|
---|
| 337 | module: '@swc/register',
|
---|
| 338 | register: function (hook, config) {
|
---|
| 339 | config = config || {
|
---|
| 340 | only: [endsInSwcTsx],
|
---|
| 341 | ignore: [isNodeModules],
|
---|
| 342 | jsc: {
|
---|
| 343 | parser: {
|
---|
| 344 | syntax: 'typescript',
|
---|
| 345 | tsx: true,
|
---|
| 346 | },
|
---|
| 347 | },
|
---|
| 348 | module: {
|
---|
| 349 | type: 'commonjs',
|
---|
| 350 | },
|
---|
| 351 | };
|
---|
| 352 |
|
---|
| 353 | hook(
|
---|
| 354 | Object.assign({}, config, {
|
---|
| 355 | extensions: '.tsx',
|
---|
| 356 | })
|
---|
| 357 | );
|
---|
| 358 | },
|
---|
| 359 | },
|
---|
| 360 | '.toml': {
|
---|
| 361 | module: 'toml-require',
|
---|
| 362 | register: function (hook, config) {
|
---|
| 363 | hook.install(config);
|
---|
| 364 | },
|
---|
| 365 | },
|
---|
| 366 | '.ts': [
|
---|
| 367 | 'ts-node/register',
|
---|
| 368 | 'sucrase/register/ts',
|
---|
| 369 | {
|
---|
| 370 | module: '@babel/register',
|
---|
| 371 | register: function (hook, config) {
|
---|
| 372 | config = config || {
|
---|
| 373 | rootMode: 'upward-optional',
|
---|
| 374 | overrides: [
|
---|
| 375 | {
|
---|
| 376 | only: [endsInTs],
|
---|
| 377 | presets: ['@babel/preset-env', '@babel/preset-typescript'],
|
---|
| 378 | },
|
---|
| 379 | ],
|
---|
| 380 | };
|
---|
| 381 |
|
---|
| 382 | hook(
|
---|
| 383 | Object.assign({}, config, {
|
---|
| 384 | extensions: '.ts',
|
---|
| 385 | })
|
---|
| 386 | );
|
---|
| 387 | },
|
---|
| 388 | },
|
---|
| 389 | {
|
---|
| 390 | module: 'esbuild-register/dist/node',
|
---|
| 391 | register: function (mod, config) {
|
---|
| 392 | config = config || {
|
---|
| 393 | target: 'node' + process.version.slice(1),
|
---|
| 394 | hookMatcher: endsInTs,
|
---|
| 395 | };
|
---|
| 396 |
|
---|
| 397 | mod.register(
|
---|
| 398 | Object.assign({}, config, {
|
---|
| 399 | extensions: ['.ts'],
|
---|
| 400 | })
|
---|
| 401 | );
|
---|
| 402 | },
|
---|
| 403 | },
|
---|
| 404 | {
|
---|
| 405 | module: '@swc/register',
|
---|
| 406 | register: function (hook, config) {
|
---|
| 407 | config = config || {
|
---|
| 408 | only: [endsInTs],
|
---|
| 409 | ignore: [isNodeModules],
|
---|
| 410 | jsc: {
|
---|
| 411 | parser: {
|
---|
| 412 | syntax: 'typescript',
|
---|
| 413 | },
|
---|
| 414 | },
|
---|
| 415 | module: {
|
---|
| 416 | type: 'commonjs',
|
---|
| 417 | },
|
---|
| 418 | };
|
---|
| 419 |
|
---|
| 420 | hook(
|
---|
| 421 | Object.assign({}, config, {
|
---|
| 422 | extensions: '.ts',
|
---|
| 423 | })
|
---|
| 424 | );
|
---|
| 425 | },
|
---|
| 426 | },
|
---|
| 427 | ],
|
---|
| 428 | '.cts': ['ts-node/register'],
|
---|
| 429 | '.tsx': [
|
---|
| 430 | 'ts-node/register',
|
---|
| 431 | 'sucrase/register/tsx',
|
---|
| 432 | {
|
---|
| 433 | module: '@babel/register',
|
---|
| 434 | register: function (hook, config) {
|
---|
| 435 | config = config || {
|
---|
| 436 | rootMode: 'upward-optional',
|
---|
| 437 | overrides: [
|
---|
| 438 | {
|
---|
| 439 | only: [endsInTsx],
|
---|
| 440 | presets: [
|
---|
| 441 | '@babel/preset-env',
|
---|
| 442 | '@babel/preset-react',
|
---|
| 443 | [
|
---|
| 444 | '@babel/preset-typescript',
|
---|
| 445 | {
|
---|
| 446 | isTSX: true,
|
---|
| 447 | allExtensions: true,
|
---|
| 448 | },
|
---|
| 449 | ],
|
---|
| 450 | ],
|
---|
| 451 | },
|
---|
| 452 | ],
|
---|
| 453 | };
|
---|
| 454 |
|
---|
| 455 | hook(
|
---|
| 456 | Object.assign({}, config, {
|
---|
| 457 | extensions: '.tsx',
|
---|
| 458 | })
|
---|
| 459 | );
|
---|
| 460 | },
|
---|
| 461 | },
|
---|
| 462 | {
|
---|
| 463 | module: 'esbuild-register/dist/node',
|
---|
| 464 | register: function (mod, config) {
|
---|
| 465 | config = config || {
|
---|
| 466 | target: 'node' + process.version.slice(1),
|
---|
| 467 | hookMatcher: endsInTsx,
|
---|
| 468 | };
|
---|
| 469 |
|
---|
| 470 | mod.register(
|
---|
| 471 | Object.assign({}, config, {
|
---|
| 472 | extensions: ['.tsx'],
|
---|
| 473 | })
|
---|
| 474 | );
|
---|
| 475 | },
|
---|
| 476 | },
|
---|
| 477 | {
|
---|
| 478 | module: '@swc/register',
|
---|
| 479 | register: function (hook, config) {
|
---|
| 480 | config = config || {
|
---|
| 481 | only: [endsInTsx],
|
---|
| 482 | ignore: [isNodeModules],
|
---|
| 483 | jsc: {
|
---|
| 484 | parser: {
|
---|
| 485 | syntax: 'typescript',
|
---|
| 486 | tsx: true,
|
---|
| 487 | },
|
---|
| 488 | },
|
---|
| 489 | module: {
|
---|
| 490 | type: 'commonjs',
|
---|
| 491 | },
|
---|
| 492 | };
|
---|
| 493 |
|
---|
| 494 | hook(
|
---|
| 495 | Object.assign({}, config, {
|
---|
| 496 | extensions: '.tsx',
|
---|
| 497 | })
|
---|
| 498 | );
|
---|
| 499 | },
|
---|
| 500 | },
|
---|
| 501 | ],
|
---|
| 502 | '.yaml': 'yaml-hook/register',
|
---|
| 503 | '.yml': 'yaml-hook/register',
|
---|
| 504 | };
|
---|
| 505 |
|
---|
| 506 | var jsVariantExtensions = [
|
---|
| 507 | '.js',
|
---|
| 508 | '.babel.js',
|
---|
| 509 | '.babel.jsx',
|
---|
| 510 | '.babel.ts',
|
---|
| 511 | '.babel.tsx',
|
---|
| 512 | '.esbuild.js',
|
---|
| 513 | '.esbuild.jsx',
|
---|
| 514 | '.esbuild.ts',
|
---|
| 515 | '.esbuild.tsx',
|
---|
| 516 | '.cjs',
|
---|
| 517 | '.coffee',
|
---|
| 518 | '.coffee.md',
|
---|
| 519 | '.esm.js',
|
---|
| 520 | '.jsx',
|
---|
| 521 | '.litcoffee',
|
---|
| 522 | '.mdx',
|
---|
| 523 | '.mjs',
|
---|
| 524 | '.sucrase.js',
|
---|
| 525 | '.sucrase.jsx',
|
---|
| 526 | '.sucrase.ts',
|
---|
| 527 | '.sucrase.tsx',
|
---|
| 528 | '.swc.js',
|
---|
| 529 | '.swc.jsx',
|
---|
| 530 | '.swc.ts',
|
---|
| 531 | '.swc.tsx',
|
---|
| 532 | '.ts',
|
---|
| 533 | '.tsx',
|
---|
| 534 | ];
|
---|
| 535 |
|
---|
| 536 | module.exports = {
|
---|
| 537 | extensions: extensions,
|
---|
| 538 | jsVariants: jsVariantExtensions.reduce(function (result, ext) {
|
---|
| 539 | result[ext] = extensions[ext];
|
---|
| 540 | return result;
|
---|
| 541 | }, {}),
|
---|
| 542 | };
|
---|