浏览代码

Adopt `loader-utils` breaking changes

Alex Dima 3 年之前
父节点
当前提交
009bc9b864
共有 2 个文件被更改,包括 26 次插入12 次删除
  1. 7 5
      webpack-plugin/src/index.ts
  2. 19 7
      webpack-plugin/src/loaders/include.ts

+ 7 - 5
webpack-plugin/src/index.ts

@@ -6,6 +6,7 @@ import { AddWorkerEntryPointPlugin } from './plugins/AddWorkerEntryPointPlugin';
 import { languagesArr, EditorLanguage } from './languages';
 import { featuresArr, EditorFeature, NegatedEditorFeature } from './features';
 import { IFeatureDefinition } from './types';
+import { ILoaderOptions } from './loaders/include';
 
 const INCLUDE_LOADER_PATH = require.resolve('./loaders/include');
 
@@ -282,17 +283,18 @@ function createLoaderRules(
       };
     })(${JSON.stringify(workerPaths, null, 2)})`
 	};
+	const options: ILoaderOptions = {
+		globals,
+		pre: featurePaths.map((importPath) => resolveMonacoPath(importPath, monacoEditorPath)),
+		post: languagePaths.map((importPath) => resolveMonacoPath(importPath, monacoEditorPath))
+	};
 	return [
 		{
 			test: /esm[/\\]vs[/\\]editor[/\\]editor.(api|main).js/,
 			use: [
 				{
 					loader: INCLUDE_LOADER_PATH,
-					options: {
-						globals,
-						pre: featurePaths.map((importPath) => resolveMonacoPath(importPath, monacoEditorPath)),
-						post: languagePaths.map((importPath) => resolveMonacoPath(importPath, monacoEditorPath))
-					}
+					options
 				}
 			]
 		}

+ 19 - 7
webpack-plugin/src/loaders/include.ts

@@ -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');
-}
+};