|
|
|
@ -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 })]
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|