transform.cjs 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. const require_context = require('../../context-DwbtaXxf.cjs');
  2. const require_context$1 = require('../../context-Df-kn6rn.cjs');
  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 = require_context$1.createContext$1(this);
  9. const { handler, filter } = require_context.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({}, require_context$1.createBuildContext$1({
  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. module.exports = transform;