Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add a plugin system
  • Loading branch information
lucasterra committed May 5, 2018
commit 5ac85e76c47e7a124091d424621d12c88a5cb77e
1 change: 1 addition & 0 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"packages/babel-preset-razzle",
"packages/create-razzle-app",
"packages/razzle-dev-utils",
"packages/razzle-plugin-*",
"examples/*"
],
"useWorkspaces": true,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"packages/babel-preset-razzle",
"packages/create-razzle-app",
"packages/razzle-dev-utils",
"packages/razzle-plugin-*",
"examples/*"
]
}
22 changes: 21 additions & 1 deletion packages/razzle/config/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const eslintFormatter = require('react-dev-utils/eslintFormatter');
const autoprefixer = require('autoprefixer');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const paths = require('./paths');
const runPlugin = require('./runPlugin');
const getClientEnv = require('./env').getClientEnv;
const nodePath = require('./env').nodePath;
const errorOverlayMiddleware = require('react-dev-utils/errorOverlayMiddleware');
Expand All @@ -36,7 +37,8 @@ const postCssOptions = {
module.exports = (
target = 'web',
env = 'dev',
{ clearConsole = true, host = 'localhost', port = 3000 }
{ clearConsole = true, host = 'localhost', port = 3000, modify, plugins },
webpackObject
) => {
// First we check to see if the user has a custom .babelrc file, otherwise
// we just use babel-preset-razzle.
Expand Down Expand Up @@ -571,5 +573,23 @@ module.exports = (
];
}

// Apply razzle plugins, if they are present in razzle.config.js
if (Array.isArray(plugins)) {
plugins.forEach(plugin => {
config = runPlugin(
plugin,
config,
{ target, dev: IS_DEV },
webpackObject
);
});
}

// Check if razzle.config has a modify function. If it does, call it on the
// configs we created.
if (modify) {
config = modify(config, { target, dev: IS_DEV }, webpackObject);
}

return config;
};
25 changes: 25 additions & 0 deletions packages/razzle/config/runPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

function runPlugin(plugin, config, { target, dev }, webpack) {
if (typeof plugin === 'string') {
// Apply the plugin with default options if passing only a string
return runPlugin({ name: plugin }, config, { target, dev }, webpack);
}

if (typeof plugin.func === 'function') {
// Used for writing plugin tests
return plugin.func(config, { target, dev }, webpack, plugin.options);
}

const completePluginName = `razzle-plugin-${plugin.name}`;

// Try to find the plugin in node_modules
const razzlePlugin = require(completePluginName);
if (!razzlePlugin) {
throw new Error(`Unable to find '${completePluginName}`);
}

return razzlePlugin(config, { target, dev }, webpack, plugin.options);
}

module.exports = runPlugin;
15 changes: 0 additions & 15 deletions packages/razzle/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,6 @@ ${razzle.port !== '3000' && `PORT=${razzle.port}`}
let clientConfig = createConfig('web', 'prod', razzle);
let serverConfig = createConfig('node', 'prod', razzle);

// Check if razzle.config has a modify function. If it does, call it on the
// configs we just created.
if (razzle.modify) {
clientConfig = razzle.modify(
clientConfig,
{ target: 'web', dev: false },
webpack
);
serverConfig = razzle.modify(
serverConfig,
{ target: 'node', dev: false },
webpack
);
}

process.noDeprecation = true; // turns off that loadQuery clutter.

console.log('Creating an optimized production build...');
Expand Down
19 changes: 2 additions & 17 deletions packages/razzle/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,8 @@ function main() {

// Create dev configs using our config factory, passing in razzle file as
// options.
let clientConfig = createConfig('web', 'dev', razzle);
let serverConfig = createConfig('node', 'dev', razzle);

// Check if razzle.config has a modify function. If it does, call it on the
// configs we just created.
if (razzle.modify) {
clientConfig = razzle.modify(
clientConfig,
{ target: 'web', dev: true },
webpack
);
serverConfig = razzle.modify(
serverConfig,
{ target: 'node', dev: true },
webpack
);
}
let clientConfig = createConfig('web', 'dev', razzle, webpack);
let serverConfig = createConfig('node', 'dev', razzle, webpack);

// Compile our assets with webpack
const clientCompiler = compile(clientConfig);
Expand Down