From 8430c538b4f3755b6f29045c9fbff87a2eacacf6 Mon Sep 17 00:00:00 2001
From: Alex Dima <alexdima@microsoft.com>
Date: Thu, 11 Nov 2021 19:27:59 +0100
Subject: [PATCH] Have the webpack plugin tests use the `/release/` folder

---
 .../package-lock.json                         |  6 --
 monaco-editor-webpack-plugin/package.json     |  1 -
 monaco-editor-webpack-plugin/src/index.ts     | 57 ++++++++++++-------
 .../test/webpack-cross-origin.config.js       |  9 ++-
 .../test/webpack.config.js                    |  9 ++-
 5 files changed, 54 insertions(+), 28 deletions(-)

diff --git a/monaco-editor-webpack-plugin/package-lock.json b/monaco-editor-webpack-plugin/package-lock.json
index 442c42c8..5c4820ae 100644
--- a/monaco-editor-webpack-plugin/package-lock.json
+++ b/monaco-editor-webpack-plugin/package-lock.json
@@ -785,12 +785,6 @@
 			"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
 			"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
 		},
-		"monaco-editor": {
-			"version": "0.30.0",
-			"resolved": "https://registry.npmjs.org/monaco-editor/-/monaco-editor-0.30.0.tgz",
-			"integrity": "sha512-/k++/ofRmwnwWTpOWYOMGVcqBrqrlt3MP0Mt/cRTQojW7A9fnekcvPQ2iIFA0YSZdPWPN9yYXrYq0xqiUuxT/A==",
-			"dev": true
-		},
 		"neo-async": {
 			"version": "2.6.2",
 			"resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz",
diff --git a/monaco-editor-webpack-plugin/package.json b/monaco-editor-webpack-plugin/package.json
index 3d22ca50..ba23125e 100644
--- a/monaco-editor-webpack-plugin/package.json
+++ b/monaco-editor-webpack-plugin/package.json
@@ -35,7 +35,6 @@
 		"css-loader": "^5.1.1",
 		"file-loader": "^6.2.0",
 		"glob": "^7.1.6",
-		"monaco-editor": "^0.30.0",
 		"style-loader": "^2.0.0",
 		"typescript": "^4.2.3",
 		"webpack": "^5.24.3",
diff --git a/monaco-editor-webpack-plugin/src/index.ts b/monaco-editor-webpack-plugin/src/index.ts
index 7a38f621..57aa838d 100644
--- a/monaco-editor-webpack-plugin/src/index.ts
+++ b/monaco-editor-webpack-plugin/src/index.ts
@@ -27,24 +27,32 @@ featuresArr.forEach((feature) => (featuresById[feature.label] = feature));
 /**
  * Return a resolved path for a given Monaco file.
  */
-function resolveMonacoPath(filePath: string): string {
+function resolveMonacoPath(filePath: string, monacoEditorPath: string | undefined): string {
+	if (monacoEditorPath) {
+		return require.resolve(path.join(monacoEditorPath, 'esm', filePath));
+	}
+
 	try {
 		return require.resolve(path.join('monaco-editor/esm', filePath));
-	} catch (err) {
-		try {
-			return require.resolve(path.join(process.cwd(), 'node_modules/monaco-editor/esm', filePath));
-		} catch (err) {
-			return require.resolve(filePath);
-		}
-	}
+	} catch (err) {}
+
+	try {
+		return require.resolve(path.join(process.cwd(), 'node_modules/monaco-editor/esm', filePath));
+	} catch (err) {}
+
+	return require.resolve(filePath);
 }
 
 /**
  * Return the interpolated final filename for a worker, respecting the file name template.
  */
-function getWorkerFilename(filename: string, entry: string): string {
+function getWorkerFilename(
+	filename: string,
+	entry: string,
+	monacoEditorPath: string | undefined
+): string {
 	return loaderUtils.interpolateName(<any>{ resourcePath: entry }, filename, {
-		content: fs.readFileSync(resolveMonacoPath(entry))
+		content: fs.readFileSync(resolveMonacoPath(entry, monacoEditorPath))
 	});
 }
 
@@ -92,6 +100,12 @@ interface IMonacoEditorWebpackPluginOpts {
 	 */
 	filename?: string;
 
+	/**
+	 * The absolute file system path to the monaco-editor npm module.
+	 * Use e.g. `C:\projects\my-project\node-modules\monaco-editor`
+	 */
+	monacoEditorPath?: string;
+
 	/**
 	 * Override the public path from which files generated by this plugin will be served.
 	 * This wins out over Webpack's dynamic runtime path and can be useful to avoid attempting to load workers cross-
@@ -114,6 +128,7 @@ interface IInternalMonacoEditorWebpackPluginOpts {
 	languages: IFeatureDefinition[];
 	features: IFeatureDefinition[];
 	filename: string;
+	monacoEditorPath: string | undefined;
 	publicPath: string;
 	globalAPI: boolean;
 }
@@ -129,13 +144,14 @@ class MonacoEditorWebpackPlugin implements webpack.WebpackPluginInstance {
 			languages: coalesce(languages.map((id) => languagesById[id])).concat(customLanguages),
 			features: coalesce(features.map((id) => featuresById[id])),
 			filename: options.filename || '[name].worker.js',
+			monacoEditorPath: options.monacoEditorPath,
 			publicPath: options.publicPath || '',
 			globalAPI: options.globalAPI || false
 		};
 	}
 
 	apply(compiler: webpack.Compiler): void {
-		const { languages, features, filename, publicPath, globalAPI } = this.options;
+		const { languages, features, filename, monacoEditorPath, publicPath, globalAPI } = this.options;
 		const compilationPublicPath = getCompilationPublicPath(compiler);
 		const modules = [EDITOR_MODULE].concat(languages).concat(features);
 		const workers: ILabeledWorkerDefinition[] = [];
@@ -153,11 +169,12 @@ class MonacoEditorWebpackPlugin implements webpack.WebpackPluginInstance {
 			features,
 			workers,
 			filename,
+			monacoEditorPath,
 			publicPath,
 			compilationPublicPath,
 			globalAPI
 		);
-		const plugins = createPlugins(compiler, workers, filename);
+		const plugins = createPlugins(compiler, workers, filename, monacoEditorPath);
 		addCompilerRules(compiler, rules);
 		addCompilerPlugins(compiler, plugins);
 	}
@@ -199,6 +216,7 @@ function createLoaderRules(
 	features: IFeatureDefinition[],
 	workers: ILabeledWorkerDefinition[],
 	filename: string,
+	monacoEditorPath: string | undefined,
 	pluginPublicPath: string,
 	compilationPublicPath: string,
 	globalAPI: boolean
@@ -209,7 +227,7 @@ function createLoaderRules(
 	const languagePaths = flatArr(coalesce(languages.map((language) => language.entry)));
 	const featurePaths = flatArr(coalesce(features.map((feature) => feature.entry)));
 	const workerPaths = fromPairs(
-		workers.map(({ label, entry }) => [label, getWorkerFilename(filename, entry)])
+		workers.map(({ label, entry }) => [label, getWorkerFilename(filename, entry, monacoEditorPath)])
 	);
 	if (workerPaths['typescript']) {
 		// javascript shares the same worker
@@ -266,14 +284,14 @@ function createLoaderRules(
 	};
 	return [
 		{
-			test: /monaco-editor[/\\]esm[/\\]vs[/\\]editor[/\\]editor.(api|main).js/,
+			test: /esm[/\\]vs[/\\]editor[/\\]editor.(api|main).js/,
 			use: [
 				{
 					loader: INCLUDE_LOADER_PATH,
 					options: {
 						globals,
-						pre: featurePaths.map((importPath) => resolveMonacoPath(importPath)),
-						post: languagePaths.map((importPath) => resolveMonacoPath(importPath))
+						pre: featurePaths.map((importPath) => resolveMonacoPath(importPath, monacoEditorPath)),
+						post: languagePaths.map((importPath) => resolveMonacoPath(importPath, monacoEditorPath))
 					}
 				}
 			]
@@ -284,7 +302,8 @@ function createLoaderRules(
 function createPlugins(
 	compiler: webpack.Compiler,
 	workers: ILabeledWorkerDefinition[],
-	filename: string
+	filename: string,
+	monacoEditorPath: string | undefined
 ): AddWorkerEntryPointPlugin[] {
 	const webpack = compiler.webpack ?? require('webpack');
 
@@ -293,8 +312,8 @@ function createPlugins(
 			({ id, entry }) =>
 				new AddWorkerEntryPointPlugin({
 					id,
-					entry: resolveMonacoPath(entry),
-					filename: getWorkerFilename(filename, entry),
+					entry: resolveMonacoPath(entry, monacoEditorPath),
+					filename: getWorkerFilename(filename, entry, monacoEditorPath),
 					plugins: [new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 })]
 				})
 		)
diff --git a/monaco-editor-webpack-plugin/test/webpack-cross-origin.config.js b/monaco-editor-webpack-plugin/test/webpack-cross-origin.config.js
index 472ceb22..a11788ac 100644
--- a/monaco-editor-webpack-plugin/test/webpack-cross-origin.config.js
+++ b/monaco-editor-webpack-plugin/test/webpack-cross-origin.config.js
@@ -12,6 +12,11 @@ module.exports = {
 		filename: 'app.js',
 		publicPath: ASSET_PATH
 	},
+	resolve: {
+		alias: {
+			'monaco-editor': path.resolve(__dirname, '../../release'),
+		},
+	},
 	module: {
 		rules: [
 			{
@@ -24,5 +29,7 @@ module.exports = {
 			}
 		]
 	},
-	plugins: [new MonacoWebpackPlugin()]
+	plugins: [new MonacoWebpackPlugin({
+		monacoEditorPath: path.resolve(__dirname, '../../release')
+	})]
 };
diff --git a/monaco-editor-webpack-plugin/test/webpack.config.js b/monaco-editor-webpack-plugin/test/webpack.config.js
index 5fb98382..89c3ab41 100644
--- a/monaco-editor-webpack-plugin/test/webpack.config.js
+++ b/monaco-editor-webpack-plugin/test/webpack.config.js
@@ -9,6 +9,11 @@ module.exports = {
 		path: path.resolve(__dirname, 'dist'),
 		filename: 'app.js'
 	},
+	resolve: {
+		alias: {
+			'monaco-editor': path.resolve(__dirname, '../../release'),
+		},
+	},
 	module: {
 		rules: [
 			{
@@ -21,5 +26,7 @@ module.exports = {
 			}
 		]
 	},
-	plugins: [new MonacoWebpackPlugin()]
+	plugins: [new MonacoWebpackPlugin({
+		monacoEditorPath: path.resolve(__dirname, '../../release')
+	})]
 };