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

Webpack 3 support #5

Closed
mheavenor opened this issue Apr 26, 2018 · 8 comments
Closed

Webpack 3 support #5

mheavenor opened this issue Apr 26, 2018 · 8 comments

Comments

@mheavenor
Copy link

mheavenor commented Apr 26, 2018

It would be nice if there was support for webpack 3 as well. This is a requirement for me as I am using create-react-app. The below comment also mentions that vue uses webpack 3 as well.

This comment mentions the problem code. I made the fix as per timkendrick's following comment but ran into issues with webpack.webworker being undefined. I changed the way that the SingleEntryPlugin, LoaderTargetPlugin and WebWorkerTemplatePlugin get required and this fix works for me locally. It would be great if this could make it into the repo somehow.

Below is my working local version of AddWorkerEntryPointPlugin.js

const singleEntry = require('webpack/lib/SingleEntryPlugin');
const loaderTarget = require('webpack/lib/LoaderTargetPlugin');
const webworker = require('webpack/lib/webworker/WebWorkerTemplatePlugin');

class AddWorkerEntryPointPlugin {
  constructor({
    id,
    entry,
    filename,
    chunkFilename = undefined,
    plugins = undefined,
  }) {
    this.options = { id, entry, filename, chunkFilename, plugins };
  }

  apply(compiler) {
    const { id, entry, filename, chunkFilename, plugins } = this.options;
    compiler.plugin('make', (compilation, callback) => {
      const outputOptions = {
        filename,
        chunkFilename,
        publicPath: compilation.outputOptions.publicPath,
        // HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642
        globalObject: 'this',
      };
      const childCompiler = compilation.createChildCompiler(id, outputOptions, [
        new webworker(),
        new loaderTarget('webworker'),
        new singleEntry(compiler.context, entry, 'main'),
      ]);
      plugins.forEach((plugin) => plugin.apply(childCompiler));
      childCompiler.runAsChild(callback);
    });
  }
}

module.exports = AddWorkerEntryPointPlugin;

@xeoneux
Copy link

xeoneux commented May 16, 2018

@alexandrudima will this be supported for webpack 3 in near future?

@alexdima
Copy link
Member

alexdima commented May 17, 2018

@xeoneux PR welcome to the plugin (is it possible to check the version at runtime?) or to the documentation in https://github.com/Microsoft/monaco-editor or in https://github.com/Microsoft/monaco-editor-samples.

@xeoneux
Copy link

xeoneux commented May 23, 2018

@alexandrudima I'd send a PR if I could find a way to check the webpack version at runtime.

@brijeshb42
Copy link
Contributor

brijeshb42 commented Jun 18, 2018

Based on @mheavenor 's snippet, this code supports both webpack v3 and 4

const webpack = require('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');


function getCompilerHook(compiler, {id, entry, filename, chunkFilename, plugins}) {
  return function(compilation, callback) {
    const outputOptions = {
      filename,
      chunkFilename,
      publicPath: compilation.outputOptions.publicPath,
      // HACK: globalObject is necessary to fix https://github.com/webpack/webpack/issues/6642
      globalObject: 'this',
    };
    const childCompiler = compilation.createChildCompiler(id, outputOptions, [
      new WebWorkerTemplatePlugin(),
      new LoaderTargetPlugin('webworker'),
      new SingleEntryPlugin(compiler.context, entry, 'main'),
    ]);
    plugins.forEach((plugin) => plugin.apply(childCompiler));
    childCompiler.runAsChild(callback);
  }
}


class AddWorkerEntryPointPlugin {
  constructor({
    id,
    entry,
    filename,
    chunkFilename = undefined,
    plugins = undefined,
  }) {
    this.options = { id, entry, filename, chunkFilename, plugins };
  }

  apply(compiler) {
    const compilerHook = getCompilerHook(compiler, this.options);
    if (webpackVersion < '4') {
      compiler.plugin('make', compilerHook);
    } else {
      compiler.hooks.make.tapAsync('AddWorkerEntryPointPlugin', compilerHook);
    }
  }
}

module.exports = AddWorkerEntryPointPlugin;

@ktalebian
Copy link

@brijeshb42 how are you supposed to use this? This is to replace AddWorkerEntryPointPlugin that's within monaco-editor-webpack-plugin module. So how do I use this without having to touch the source code?

@brijeshb42
Copy link
Contributor

@ktalebian You can't use it without modifying the source. I forked this repo and am using my own fork for now.

@xeoneux
Copy link

xeoneux commented Jun 21, 2018

@brijeshb42 could you create a PR?

@brijeshb42
Copy link
Contributor

Created #19

@vscodebot vscodebot bot locked and limited conversation to collaborators Dec 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants