|
|
|
@ -1,7 +1,15 @@
|
|
|
|
|
const loaderUtils = require('loader-utils');
|
|
|
|
|
import type { PitchLoaderDefinitionFunction } from 'webpack';
|
|
|
|
|
|
|
|
|
|
export function pitch(this: any, remainingRequest: any) {
|
|
|
|
|
const { globals = undefined, pre = [], post = [] } = loaderUtils.getOptions(this) || {};
|
|
|
|
|
export interface ILoaderOptions {
|
|
|
|
|
globals?: { [key: string]: string };
|
|
|
|
|
pre?: string[];
|
|
|
|
|
post?: string[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const pitch: PitchLoaderDefinitionFunction<ILoaderOptions> = function pitch(
|
|
|
|
|
remainingRequest
|
|
|
|
|
) {
|
|
|
|
|
const { globals = undefined, pre = [], post = [] } = this.getOptions() || {};
|
|
|
|
|
|
|
|
|
|
// HACK: NamedModulesPlugin overwrites existing modules when requesting the same module via
|
|
|
|
|
// different loaders, so we need to circumvent this by appending a suffix to make the name unique
|
|
|
|
@ -10,12 +18,16 @@ export function pitch(this: any, remainingRequest: any) {
|
|
|
|
|
this._module.userRequest = `include-loader!${this._module.userRequest}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stringifyRequest = (request: string) => {
|
|
|
|
|
return JSON.stringify(this.utils.contextify(this.context || this.rootContext, request));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
...(globals
|
|
|
|
|
? Object.keys(globals).map((key) => `self[${JSON.stringify(key)}] = ${globals[key]};`)
|
|
|
|
|
: []),
|
|
|
|
|
...pre.map((include: any) => `require(${loaderUtils.stringifyRequest(this, include)});`),
|
|
|
|
|
`module.exports = require(${loaderUtils.stringifyRequest(this, `!!${remainingRequest}`)});`,
|
|
|
|
|
...post.map((include: any) => `require(${loaderUtils.stringifyRequest(this, include)});`)
|
|
|
|
|
...pre.map((include: any) => `require(${stringifyRequest(include)});`),
|
|
|
|
|
`module.exports = require(${stringifyRequest(`!!${remainingRequest}`)});`,
|
|
|
|
|
...post.map((include: any) => `require(${stringifyRequest(include)});`)
|
|
|
|
|
].join('\n');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|