Skip to content

Commit

Permalink
examples: improve minimal example (#688)
Browse files Browse the repository at this point in the history
* examples: remove node_modules from minimal server bundle

* examples: simplify minimal example webpack build configuration
  • Loading branch information
pablopalacios authored May 12, 2021
1 parent 479a9b1 commit 881084c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 deletions.
14 changes: 14 additions & 0 deletions examples/minimal/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const isNodeTarget = (api) =>
api.caller((caller) => caller && caller.target === "node");

module.exports = (api) => ({
presets: [
[
"@babel/preset-env",
{
targets: isNodeTarget(api) ? { node: "current" } : "defaults",
},
],
"@babel/preset-react",
],
});
25 changes: 0 additions & 25 deletions examples/minimal/babel.config.json

This file was deleted.

11 changes: 5 additions & 6 deletions examples/minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
"author": "Pablo Palacios <pablo.palacios@holidaycheck.com>",
"private": true,
"scripts": {
"build": "npm run build:browser && npm run build:server",
"build:browser": "BABEL_ENV=browser webpack",
"build:server": "BABEL_ENV=server webpack",
"build": "NODE_ENV=production webpack",
"dev": "npm run dev:browser & npm run dev:server",
"dev:browser": "NODE_ENV=development BABEL_ENV=browser webpack --watch",
"dev:server": "NODE_ENV=development BABEL_ENV=server webpack --watch & nodemon",
"dev:browser": "NODE_ENV=development webpack --watch --no-stats",
"dev:server": "NODE_ENV=development nodemon",
"start": "node ."
},
"dependencies": {
Expand All @@ -28,6 +26,7 @@
"babel-loader": "^8.2.2",
"nodemon": "^2.0.7",
"webpack": "^5.36.1",
"webpack-cli": "^4.6.0"
"webpack-cli": "^4.6.0",
"webpack-node-externals": "^3.0.0"
}
}
40 changes: 27 additions & 13 deletions examples/minimal/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
const path = require("path");
const nodeExternals = require("webpack-node-externals");

const isServer = process.env.BABEL_ENV === "server";
const isProduction = process.env.NODE_ENV !== "development";

module.exports = {
const commonConfig = {
mode: isProduction ? "production" : "development",
target: isServer ? "node" : "web",
entry: isServer ? "./src/server.js" : "./src/browser.js",
output: isServer
? {
filename: "server.js",
path: path.resolve(__dirname, "dist"),
}
: {
filename: "browser.js",
path: path.resolve(__dirname, "dist/assets"),
},
module: {
rules: [
{
Expand All @@ -26,3 +15,28 @@ module.exports = {
],
},
};

const browserConfig = {
...commonConfig,
target: "web",
entry: "./src/browser.js",
output: {
filename: "browser.js",
path: path.resolve(__dirname, "dist/assets"),
publicPath: "/assets/",
},
};

const serverConfig = {
...commonConfig,
target: "node",
entry: "./src/server.js",
output: {
filename: "server.js",
path: path.resolve(__dirname, "dist"),
},
externals: [nodeExternals()],
externalsPresets: { node: true },
};

module.exports = [browserConfig, serverConfig];

0 comments on commit 881084c

Please sign in to comment.