transform.cjs 1.1 KB

12345678910111213141516171819202122232425
  1. const require_context = require('../../context-DwbtaXxf.cjs');
  2. const require_context$1 = require('../../context-PvRDlMNZ.cjs');
  3. //#region src/rspack/loaders/transform.ts
  4. async function transform(source, map) {
  5. const callback = this.async();
  6. const { plugin } = this.query;
  7. if (!plugin?.transform) return callback(null, source, map);
  8. const id = this.resource;
  9. const context = require_context$1.createContext(this);
  10. const { handler, filter } = require_context.normalizeObjectHook("transform", plugin.transform);
  11. if (!filter(this.resource, source)) return callback(null, source, map);
  12. try {
  13. const res = await handler.call(Object.assign({}, this._compilation && require_context$1.createBuildContext(this._compiler, this._compilation, this), context), source, id);
  14. if (res == null) callback(null, source, map);
  15. else if (typeof res !== "string") callback(null, res.code, map == null ? map : res.map || map);
  16. else callback(null, res, map);
  17. } catch (error) {
  18. if (error instanceof Error) callback(error);
  19. else callback(new Error(String(error)));
  20. }
  21. }
  22. //#endregion
  23. module.exports = transform;