esmLoader.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. var _fs = _interopRequireDefault(require("fs"));
  3. var _url = _interopRequireDefault(require("url"));
  4. var _compilationCache = require("./compilationCache");
  5. var _transform = require("./transform");
  6. var _portTransport = require("./portTransport");
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. // Node < 18.6: defaultResolve takes 3 arguments.
  24. // Node >= 18.6: nextResolve from the chain takes 2 arguments.
  25. async function resolve(specifier, context, defaultResolve) {
  26. var _currentFileDepsColle;
  27. if (context.parentURL && context.parentURL.startsWith('file://')) {
  28. const filename = _url.default.fileURLToPath(context.parentURL);
  29. const resolved = (0, _transform.resolveHook)(filename, specifier);
  30. if (resolved !== undefined) specifier = _url.default.pathToFileURL(resolved).toString();
  31. }
  32. const result = await defaultResolve(specifier, context, defaultResolve);
  33. if (result !== null && result !== void 0 && result.url && result.url.startsWith('file://')) (_currentFileDepsColle = (0, _compilationCache.currentFileDepsCollector)()) === null || _currentFileDepsColle === void 0 ? void 0 : _currentFileDepsColle.add(_url.default.fileURLToPath(result.url));
  34. return result;
  35. }
  36. // Node < 18.6: defaultLoad takes 3 arguments.
  37. // Node >= 18.6: nextLoad from the chain takes 2 arguments.
  38. async function load(moduleUrl, context, defaultLoad) {
  39. var _transport;
  40. // Bail out for wasm, json, etc.
  41. // non-js files have context.format === undefined
  42. if (context.format !== 'commonjs' && context.format !== 'module' && context.format !== undefined) return defaultLoad(moduleUrl, context, defaultLoad);
  43. // Bail for built-in modules.
  44. if (!moduleUrl.startsWith('file://')) return defaultLoad(moduleUrl, context, defaultLoad);
  45. const filename = _url.default.fileURLToPath(moduleUrl);
  46. // Bail for node_modules.
  47. if (!(0, _transform.shouldTransform)(filename)) return defaultLoad(moduleUrl, context, defaultLoad);
  48. const code = _fs.default.readFileSync(filename, 'utf-8');
  49. const source = (0, _transform.transformHook)(code, filename, moduleUrl);
  50. // Flush the source maps to the main thread.
  51. await ((_transport = transport) === null || _transport === void 0 ? void 0 : _transport.send('pushToCompilationCache', {
  52. cache: (0, _compilationCache.serializeCompilationCache)()
  53. }));
  54. // Output format is always the same as input format, if it was unknown, we always report modules.
  55. // shortCircuit is required by Node >= 18.6 to designate no more loaders should be called.
  56. return {
  57. format: context.format || 'module',
  58. source,
  59. shortCircuit: true
  60. };
  61. }
  62. let transport;
  63. // Node.js < 20
  64. function globalPreload(context) {
  65. transport = createTransport(context.port);
  66. return `
  67. globalThis.__esmLoaderPortPreV20 = port;
  68. `;
  69. }
  70. // Node.js >= 20
  71. function initialize(data) {
  72. transport = createTransport(data === null || data === void 0 ? void 0 : data.port);
  73. }
  74. function createTransport(port) {
  75. return new _portTransport.PortTransport(port, async (method, params) => {
  76. if (method === 'setTransformConfig') {
  77. (0, _transform.setTransformConfig)(params.config);
  78. return;
  79. }
  80. if (method === 'addToCompilationCache') {
  81. (0, _compilationCache.addToCompilationCache)(params.cache);
  82. return;
  83. }
  84. if (method === 'getCompilationCache') return {
  85. cache: (0, _compilationCache.serializeCompilationCache)()
  86. };
  87. if (method === 'startCollectingFileDeps') {
  88. (0, _compilationCache.startCollectingFileDeps)();
  89. return;
  90. }
  91. if (method === 'stopCollectingFileDeps') {
  92. (0, _compilationCache.stopCollectingFileDeps)(params.file);
  93. return;
  94. }
  95. });
  96. }
  97. module.exports = {
  98. resolve,
  99. load,
  100. globalPreload,
  101. initialize
  102. };