98 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			98 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| // src/webpack/context.ts
 | |
| import { resolve } from "path";
 | |
| import { Buffer } from "buffer";
 | |
| import process from "process";
 | |
| import sources from "webpack-sources";
 | |
| import { Parser } from "acorn";
 | |
| function createBuildContext(options, compilation) {
 | |
|   return {
 | |
|     parse(code, opts = {}) {
 | |
|       return Parser.parse(code, {
 | |
|         sourceType: "module",
 | |
|         ecmaVersion: "latest",
 | |
|         locations: true,
 | |
|         ...opts
 | |
|       });
 | |
|     },
 | |
|     addWatchFile(id) {
 | |
|       options.addWatchFile(resolve(process.cwd(), id));
 | |
|     },
 | |
|     emitFile(emittedFile) {
 | |
|       const outFileName = emittedFile.fileName || emittedFile.name;
 | |
|       if (emittedFile.source && outFileName) {
 | |
|         if (!compilation)
 | |
|           throw new Error("unplugin/webpack: emitFile outside supported hooks  (buildStart, buildEnd, load, transform, watchChange)");
 | |
|         compilation.emitAsset(
 | |
|           outFileName,
 | |
|           sources ? new sources.RawSource(
 | |
|             // @ts-expect-error types mismatch
 | |
|             typeof emittedFile.source === "string" ? emittedFile.source : Buffer.from(emittedFile.source)
 | |
|           ) : {
 | |
|             source: () => emittedFile.source,
 | |
|             size: () => emittedFile.source.length
 | |
|           }
 | |
|         );
 | |
|       }
 | |
|     },
 | |
|     getWatchFiles() {
 | |
|       return options.getWatchFiles();
 | |
|     }
 | |
|   };
 | |
| }
 | |
| function createContext(loader) {
 | |
|   return {
 | |
|     error: (error) => loader.emitError(normalizeMessage(error)),
 | |
|     warn: (message) => loader.emitWarning(normalizeMessage(message))
 | |
|   };
 | |
| }
 | |
| function normalizeMessage(error) {
 | |
|   const err = new Error(typeof error === "string" ? error : error.message);
 | |
|   if (typeof error === "object") {
 | |
|     err.stack = error.stack;
 | |
|     err.cause = error.meta;
 | |
|   }
 | |
|   return err;
 | |
| }
 | |
| 
 | |
| // src/utils.ts
 | |
| import { isAbsolute, normalize } from "path";
 | |
| function normalizeAbsolutePath(path) {
 | |
|   if (isAbsolute(path))
 | |
|     return normalize(path);
 | |
|   else
 | |
|     return path;
 | |
| }
 | |
| 
 | |
| // src/webpack/loaders/load.ts
 | |
| async function load(source, map) {
 | |
|   const callback = this.async();
 | |
|   const { unpluginName } = this.query;
 | |
|   const plugin = this._compiler?.$unpluginContext[unpluginName];
 | |
|   let id = this.resource;
 | |
|   if (!plugin?.load || !id)
 | |
|     return callback(null, source, map);
 | |
|   if (id.startsWith(plugin.__virtualModulePrefix))
 | |
|     id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
 | |
|   const context = createContext(this);
 | |
|   const res = await plugin.load.call(
 | |
|     { ...createBuildContext({
 | |
|       addWatchFile: (file) => {
 | |
|         this.addDependency(file);
 | |
|       },
 | |
|       getWatchFiles: () => {
 | |
|         return this.getDependencies();
 | |
|       }
 | |
|     }, this._compilation), ...context },
 | |
|     normalizeAbsolutePath(id)
 | |
|   );
 | |
|   if (res == null)
 | |
|     callback(null, source, map);
 | |
|   else if (typeof res !== "string")
 | |
|     callback(null, res.code, res.map ?? map);
 | |
|   else
 | |
|     callback(null, res, map);
 | |
| }
 | |
| export {
 | |
|   load as default
 | |
| };
 |