transform.js 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import { normalizeObjectHook } from "../../context-D49cMElb.js";
  2. import { createBuildContext, createContext } from "../../context-BZKy5Nsn.js";
  3. //#region src/webpack/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 context = createContext(this);
  9. const { handler, filter } = normalizeObjectHook("transform", plugin.transform);
  10. if (!filter(this.resource, source)) return callback(null, source, map);
  11. try {
  12. const res = await handler.call(Object.assign({}, createBuildContext({
  13. addWatchFile: (file) => {
  14. this.addDependency(file);
  15. },
  16. getWatchFiles: () => {
  17. return this.getDependencies();
  18. }
  19. }, this._compiler, this._compilation, this), context), source, this.resource);
  20. if (res == null) callback(null, source, map);
  21. else if (typeof res !== "string") callback(null, res.code, map == null ? map : res.map || map);
  22. else callback(null, res, map);
  23. } catch (error) {
  24. if (error instanceof Error) callback(error);
  25. else callback(new Error(String(error)));
  26. }
  27. }
  28. //#endregion
  29. export { transform as default };