Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Use compiler.webpack if available
Browse files Browse the repository at this point in the history
This will use compiler.webpack if it is available.
Otherwise, it will fall back to require webpack lazily.
This will make the plugin compatible with NextJS 10.

Closes #141
  • Loading branch information
k15a committed May 7, 2021
1 parent c124699 commit 3e40369
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as webpack from 'webpack';
import * as path from 'path';
import * as webpack from 'webpack';
import * as loaderUtils from 'loader-utils';
import * as fs from 'fs';
import { AddWorkerEntryPointPlugin } from './plugins/AddWorkerEntryPointPlugin';
Expand Down Expand Up @@ -139,7 +139,7 @@ class MonacoEditorWebpackPlugin implements webpack.WebpackPluginInstance {
}
});
const rules = createLoaderRules(languages, features, workers, filename, publicPath, compilationPublicPath);
const plugins = createPlugins(workers, filename);
const plugins = createPlugins(compiler, workers, filename);
addCompilerRules(compiler, rules);
addCompilerPlugins(compiler, plugins);
}
Expand Down Expand Up @@ -247,7 +247,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 (
(<AddWorkerEntryPointPlugin[]>[])
.concat(workers.map(({ id, entry }) =>
Expand Down
19 changes: 10 additions & 9 deletions src/plugins/AddWorkerEntryPointPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import * 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');
import type * as webpack from 'webpack';

export interface IAddWorkerEntryPointPluginOptions {
id: string;
Expand All @@ -13,6 +9,8 @@ export interface 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) {
const outputOptions = {
filename,
Expand All @@ -22,13 +20,14 @@ function getCompilerHook(compiler: webpack.Compiler, { id, entry, filename, chun
globalObject: 'this',
};
const childCompiler = compilation.createChildCompiler(id, outputOptions, [
new WebWorkerTemplatePlugin(),
new LoaderTargetPlugin('webworker'),
new webpack.webworker.WebWorkerTemplatePlugin(),
new webpack.LoaderTargetPlugin('webworker'),
]);
const SingleEntryPlugin = webpack.EntryPlugin ?? webpack.SingleEntryPlugin;
new SingleEntryPlugin(compiler.context, entry, 'main').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));
}
}

Expand All @@ -41,8 +40,10 @@ export class AddWorkerEntryPointPlugin implements webpack.WebpackPluginInstance
}

apply(compiler: webpack.Compiler) {
const webpack = compiler.webpack ?? require('webpack');
const compilerHook = getCompilerHook(compiler, this.options);
if (webpackVersion < '4') {
const majorVersion = webpack.version.split('.')[0]
if (parseInt(majorVersion) < 4) {
(<any>compiler).plugin('make', compilerHook);
} else {
compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', compilerHook);
Expand Down

0 comments on commit 3e40369

Please sign in to comment.