ericclemmons / terse-webpack

Simplified, fluent Webpack API with presets. Describe *what* your app needs, not *how*.
MIT License
215 stars 15 forks source link

Server externals are complicated. Abstract away with features? #29

Open ericclemmons opened 8 years ago

ericclemmons commented 8 years ago

To summarize:

This solves any problems with server-side externals and alias.

diff --git a/webpack.config.test.js b/webpack.config.test.js
index 87af92b..094abbb 100644
--- a/webpack.config.test.js
+++ b/webpack.config.test.js
@@ -1,3 +1,17 @@
+/* eslint-disable no-var */
+var fs = require("fs");
+var path = require("path");
+
+function nodeModuleExists(request) {
+  try {
+    fs.accessSync(path.resolve(process.cwd(), "node_modules", request));
+  } catch (e) {
+    return false;
+  }
+
+  return true;
+}
+
 module.exports = require("./webpack.config.defaults")
   .externals(
     /^@?\w[a-z\-0-9\./]+$/,
@@ -6,7 +20,24 @@ module.exports = require("./webpack.config.defaults")
     "react/lib/ReactContext"
   )
   .loader("babel", ".js", {
-    exclude: /node_modules/,
+    function(context, request, callback) {
+      if (
+        /node_modules/.test(context)
+        || (
+          /^@?\w[a-z\-0-9\./]+$/.test(request)
+          &&
+          (
+            nodeModuleExists(request)
+            ||
+            nodeModuleExists(`${request}.js`)
+          )
+        )
+      ) {
+        return callback(null, `commonjs ${request}`);
+      }
+
+      callback();
+    },
     query: { cacheDirectory: true },
   })
   .loader("null", ".css")
ericclemmons commented 7 years ago

Also need to confirm this with our internal work project. There was an issue with lodash.map and lodash/something that wasn't registering correctly as an external.