transform.js 1.1 KB

12345678910111213141516171819202122232425
  1. import { normalizeObjectHook } from "../../context-D49cMElb.js";
  2. import { createBuildContext$1 as createBuildContext, createContext$1 as createContext } from "../../context-DY6ggBKV.js";
  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 = createContext(this);
  10. const { handler, filter } = 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 && 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. export { transform as default };