From 009bc9b8647dddd23bf1efa7fe666b076b04ac83 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Tue, 16 Nov 2021 09:41:37 +0100 Subject: [PATCH] Adopt `loader-utils` breaking changes --- webpack-plugin/src/index.ts | 12 +++++++----- webpack-plugin/src/loaders/include.ts | 26 +++++++++++++++++++------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/webpack-plugin/src/index.ts b/webpack-plugin/src/index.ts index 57aa838d..7562fd34 100644 --- a/webpack-plugin/src/index.ts +++ b/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 } ] } diff --git a/webpack-plugin/src/loaders/include.ts b/webpack-plugin/src/loaders/include.ts index 52731f3b..3713c63b 100644 --- a/webpack-plugin/src/loaders/include.ts +++ b/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 = 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'); -} +};