Skip to content

Commit

Permalink
Merge pull request #2627 from rgbkrk/webpack-for-all-next
Browse files Browse the repository at this point in the history
Unify webpack configuration across next.js apps
  • Loading branch information
theengineear committed Mar 5, 2018
2 parents fc035e5 + cd5841d commit 373596f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 89 deletions.
5 changes: 5 additions & 0 deletions applications/notebook-on-next/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const configurator = require("@nteract/webpack-configurator");

module.exports = {
webpack: configurator.nextWebpack
};
5 changes: 5 additions & 0 deletions applications/play/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const configurator = require("@nteract/webpack-configurator");

module.exports = {
webpack: configurator.nextWebpack
};
14 changes: 1 addition & 13 deletions applications/showcase/next.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
const configurator = require("@nteract/webpack-configurator");

module.exports = {
webpack: (config, { buildId, dev }) => {
config.module.rules.push({
test: /\.js$/,
exclude: configurator.exclude,
loader: "babel-loader"
});

config.resolve = Object.assign({}, config.resolve, {
mainFields: ["nteractDesktop", "es2015", "jsnext:main", "module", "main"],
alias: configurator.mergeDefaultAliases(config.resolve.alias)
});
return config;
}
webpack: configurator.nextWebpack
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@
"eslint-plugin-flowtype": "^2.46.1",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-prettier": "^2.6.0",
"eventsource": "^1.0.5",
"express": "^4.15.4",
"file-loader": "^1.1.10",
"file-saver": "^1.3.3",
Expand Down
5 changes: 5 additions & 0 deletions packages/commuter-frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const configurator = require("@nteract/webpack-configurator");

module.exports = {
webpack: configurator.nextWebpack
};
1 change: 0 additions & 1 deletion packages/rx-binder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"nteract"
],
"dependencies": {
"eventsource": "^1.0.5",
"rxjs": "^5.5.6"
},
"author": "Kyle Kelley <rgbkrk@gmail.com>",
Expand Down
19 changes: 16 additions & 3 deletions packages/rx-binder/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @flow
const { Observable } = require("rxjs/Observable");

const EventSourcePolyfill = require("eventsource");

const mybinderURL = "https://mybinder.org";

function cleanRepo(repo) {
Expand Down Expand Up @@ -36,11 +34,26 @@ function formBinderURL({
return url;
}

const eventSourceFallback =
window && window.EventSource
? window.EventSource
: function(url) {
throw new Error(
"Event Source not supported on this platform -- please polyfill"
);
};

function binder(
options /*: BinderOptions */,
/** Allow overriding EventSource for testing and ponyfilling **/
EventSourceDI /* :* */ = EventSourcePolyfill
EventSourceDI /* :* */ = eventSourceFallback
) /*: Observable<*> */ {
if (!EventSourceDI) {
throw new Error(
"Event Source not supported on this platform -- please polyfill"
);
}

const url = formBinderURL(options);

return Observable.create(observer => {
Expand Down
82 changes: 11 additions & 71 deletions packages/webpack-configurator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,90 +52,30 @@ type WebpackConfig = {
}>
}
};
*/
*/

function configure(
function nextWebpack(
config /*: WebpackConfig */,
options /*: ?NextWebPackOptions */
) /*: WebpackConfig */ {
// non-next.js app assumptions, that we're in dev mode and not server side
// we're not using these yet, I'd like defaults set to keep a convention
if (!options) {
options = {
dev: true,
isServer: false
};
}

const { dev, isServer } = options;

if (!config.module) {
config.module = {
rules: []
};
}

if (!config.module.rules) {
config.module.rules = [];
}

let hasBabelLoader = false;

// If, for example, the webpack config was set up for hot reload, we override
// it to accept nteract packages
config.module.rules = config.module.rules.map(rule => {
if (
rule.loader === "babel-loader" ||
(rule.use && rule.use.loader === "babel-loader")
) {
hasBabelLoader = true;
}

if (String(rule.exclude) === String(/node_modules/)) {
rule.exclude = exclude;
}

return rule;
});

// ** Enforce transpilation **
// At least for next.js apps, it seems like we still have to add this on.
// We do know, based on hasBabelLoader if it already was configured in the
// suite of rules. I hope this isn't adding a second step.
config.module.rules.push({
test: /\.js$/,
exclude,
loader: "babel-loader?cacheDirectory=true"
exclude: exclude,
loader: "babel-loader"
});

if (!config.resolve) {
config.resolve = {};
}

config.resolve.alias = {
// Whatever came in before
...config.resolve.alias,
// Alias nteract packages
...aliases,
// Alias RxJS modules
...rxAliases
};

config.resolve.mainFields = [
"nteractDesktop",
"es2015",
"jsnext:main",
"module",
"main"
];
config.resolve.extensions = [".js", ".jsx", ".json"];

config.resolve = Object.assign({}, config.resolve, {
mainFields: ["nteractDesktop", "es2015", "jsnext:main", "module", "main"],
alias: mergeDefaultAliases(
config.resolve ? config.resolve.alias : undefined
)
});
return config;
}

module.exports = {
exclude,
aliases,
mergeDefaultAliases,
configure
nextWebpack
};

0 comments on commit 373596f

Please sign in to comment.