mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 20:52:56 +01:00
Merge pull request #147 from k15a/use-compiler-webpack
Use compiler.webpack if available
This commit is contained in:
commit
21839da687
2 changed files with 15 additions and 12 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
|
import type * as webpack from 'webpack';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as webpack from 'webpack';
|
|
||||||
import * as loaderUtils from 'loader-utils';
|
import * as loaderUtils from 'loader-utils';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import { AddWorkerEntryPointPlugin } from './plugins/AddWorkerEntryPointPlugin';
|
import { AddWorkerEntryPointPlugin } from './plugins/AddWorkerEntryPointPlugin';
|
||||||
|
|
@ -150,7 +150,7 @@ class MonacoEditorWebpackPlugin implements webpack.WebpackPluginInstance {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const rules = createLoaderRules(languages, features, workers, filename, publicPath, compilationPublicPath, globalAPI);
|
const rules = createLoaderRules(languages, features, workers, filename, publicPath, compilationPublicPath, globalAPI);
|
||||||
const plugins = createPlugins(workers, filename);
|
const plugins = createPlugins(compiler, workers, filename);
|
||||||
addCompilerRules(compiler, rules);
|
addCompilerRules(compiler, rules);
|
||||||
addCompilerPlugins(compiler, plugins);
|
addCompilerPlugins(compiler, plugins);
|
||||||
}
|
}
|
||||||
|
|
@ -259,7 +259,9 @@ function createLoaderRules(languages: IFeatureDefinition[], features: IFeatureDe
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPlugins(workers: ILabeledWorkerDefinition[], filename: string): AddWorkerEntryPointPlugin[] {
|
function createPlugins(compiler: webpack.Compiler, workers: ILabeledWorkerDefinition[], filename: string): AddWorkerEntryPointPlugin[] {
|
||||||
|
const webpack = compiler.webpack ?? require('webpack');
|
||||||
|
|
||||||
return (
|
return (
|
||||||
(<AddWorkerEntryPointPlugin[]>[])
|
(<AddWorkerEntryPointPlugin[]>[])
|
||||||
.concat(workers.map(({ id, entry }) =>
|
.concat(workers.map(({ id, entry }) =>
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,4 @@
|
||||||
import * as webpack from 'webpack';
|
import type * as webpack from 'webpack';
|
||||||
const webpackVersion = require('webpack/package.json').version;
|
|
||||||
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
|
|
||||||
const LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin');
|
|
||||||
const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin');
|
|
||||||
|
|
||||||
export interface IAddWorkerEntryPointPluginOptions {
|
export interface IAddWorkerEntryPointPluginOptions {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
@ -13,6 +9,8 @@ export interface IAddWorkerEntryPointPluginOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCompilerHook(compiler: webpack.Compiler, { id, entry, filename, chunkFilename, plugins }: IAddWorkerEntryPointPluginOptions) {
|
function getCompilerHook(compiler: webpack.Compiler, { id, entry, filename, chunkFilename, plugins }: IAddWorkerEntryPointPluginOptions) {
|
||||||
|
const webpack = compiler.webpack ?? require('webpack');
|
||||||
|
|
||||||
return function (compilation: webpack.Compilation, callback: (error?: Error | null | false) => void) {
|
return function (compilation: webpack.Compilation, callback: (error?: Error | null | false) => void) {
|
||||||
const outputOptions = {
|
const outputOptions = {
|
||||||
filename,
|
filename,
|
||||||
|
|
@ -22,13 +20,14 @@ function getCompilerHook(compiler: webpack.Compiler, { id, entry, filename, chun
|
||||||
globalObject: 'this',
|
globalObject: 'this',
|
||||||
};
|
};
|
||||||
const childCompiler = compilation.createChildCompiler(id, outputOptions, [
|
const childCompiler = compilation.createChildCompiler(id, outputOptions, [
|
||||||
new WebWorkerTemplatePlugin(),
|
new webpack.webworker.WebWorkerTemplatePlugin(),
|
||||||
new LoaderTargetPlugin('webworker'),
|
new webpack.LoaderTargetPlugin('webworker'),
|
||||||
]);
|
]);
|
||||||
|
const SingleEntryPlugin = webpack.EntryPlugin ?? webpack.SingleEntryPlugin;
|
||||||
new SingleEntryPlugin(compiler.context, entry, 'main').apply(childCompiler);
|
new SingleEntryPlugin(compiler.context, entry, 'main').apply(childCompiler);
|
||||||
plugins.forEach((plugin) => plugin.apply(childCompiler));
|
plugins.forEach((plugin) => plugin.apply(childCompiler));
|
||||||
|
|
||||||
childCompiler.runAsChild((err?: Error, entries?: webpack.Chunk[], compilation?: webpack.Compilation) => callback(err));
|
childCompiler.runAsChild((err?: Error) => callback(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -41,8 +40,10 @@ export class AddWorkerEntryPointPlugin implements webpack.WebpackPluginInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
apply(compiler: webpack.Compiler) {
|
apply(compiler: webpack.Compiler) {
|
||||||
|
const webpack = compiler.webpack ?? require('webpack');
|
||||||
const compilerHook = getCompilerHook(compiler, this.options);
|
const compilerHook = getCompilerHook(compiler, this.options);
|
||||||
if (webpackVersion < '4') {
|
const majorVersion = webpack.version.split('.')[0]
|
||||||
|
if (parseInt(majorVersion) < 4) {
|
||||||
(<any>compiler).plugin('make', compilerHook);
|
(<any>compiler).plugin('make', compilerHook);
|
||||||
} else {
|
} else {
|
||||||
compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', compilerHook);
|
compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', compilerHook);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue