google/closure-compiler-js

Webpack 2 processCommonJsModules breaks require

Ch4s3 opened this issue · 1 comments

Ch4s3 commented

Using webpack 2 with processCommonJsModules: true seems to break require.

"use strict";
const path = require("path");
const webpack = require("webpack");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const ClosureCompiler = require("google-closure-compiler-js").webpack;
const cssLoaders = [
  {
    loader: "css-loader",
    options: {
      modules: true,
      minimize: true
    }
  },
  {
    loader: "sass-loader"
  }
]
module.exports = {
  context: __dirname + "/source",
  entry: {
    site: "./javascripts/all.js",
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules)/,
        loader: "babel-loader",
      },
      {
        test: /\.(sass|scss)$/,
        use: [{
          loader: ExtractTextPlugin.extract({
            fallbackLoader: ["style-loader"],
            loader: cssLoaders
          }), //end ExtractTextPlugin loader
        }],
      },
    ],//end rules
  },
  output: {
    path: __dirname + "/build/javascripts",
    filename: "[name].bundle.js",
  },

  plugins: [
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    }),
    new ExtractTextPlugin({
      path: __dirname + "/build/stylesheets",
      filename: "[name].bundle.css",
      disable: false,
      allChunks: true,
    }),
    new ClosureCompiler({
      options: {
        processCommonJsModules: true,
        languageIn: "ECMASCRIPT6",
        languageOut: "ECMASCRIPT5",
        compilationLevel: "SIMPLE",
        warningLevel: "VERBOSE"
      },
    })
  ],
};

When a use the following in my entry point:
const prism = require("./prism")

I get a require not defined error in the browser. Removing ClosureCompiler fixes the error.

The webpack plugin at https://github.com/webpack-contrib/closure-webpack-plugin supports this use case. The JS version now supports the needed flags.