From 5a4441e3090143f52bda46605309b1757a16b37c Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 6 Apr 2017 17:41:18 -0600 Subject: [PATCH] Adjusted loaders, webpack --- .../coral-framework/loaders/plugins-loader.js | 18 +++++---- plugins.env.js | 1 + plugins.js | 8 ++-- webpack.config.js | 38 +++++++++++++++---- 4 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 plugins.env.js diff --git a/client/coral-framework/loaders/plugins-loader.js b/client/coral-framework/loaders/plugins-loader.js index 83bdae33b..c3534df32 100644 --- a/client/coral-framework/loaders/plugins-loader.js +++ b/client/coral-framework/loaders/plugins-loader.js @@ -10,22 +10,24 @@ const {stripIndent} = require('common-tags'); function getPluginList(config) { - return config.client.map(x => typeof x === 'string' ? x : Object.keys(x)[0]); + if (config && config.client) { + return config.client.map(x => typeof x === 'string' ? x : Object.keys(x)[0]); + } + + return []; } module.exports = function(source) { this.cacheable(); const config = this.exec(source, this.resourcePath); - const plugins = getPluginList(config); - - const list = []; - plugins.forEach(plugin => { - list.push(`{module: require('plugins/${plugin}/client/index.js'), plugin: '${plugin}'}`); - }); + const plugins = getPluginList(config).map((plugin) => `{ + module: require('${plugin}/client'), + plugin: '${plugin}' + }`); return stripIndent` module.exports = [ - ${list.join(',')} + ${plugins.join(',')} ]; `; }; diff --git a/plugins.env.js b/plugins.env.js new file mode 100644 index 000000000..7be520cc0 --- /dev/null +++ b/plugins.env.js @@ -0,0 +1 @@ +module.exports = JSON.parse(process.env.TALK_PLUGINS_JSON); diff --git a/plugins.js b/plugins.js index 42db996b8..2455e430f 100644 --- a/plugins.js +++ b/plugins.js @@ -13,13 +13,13 @@ let plugins = {}; // file isn't loaded, but continuing. Else, like a parsing error, throw it and // crash the program. try { - let defaultPlugins = path.join(__dirname, 'plugins.default.json'); + let envPlugins = path.join(__dirname, 'plugins.env.js'); let customPlugins = path.join(__dirname, 'plugins.json'); - let envPluginJSON = process.env.TALK_PLUGINS_JSON; + let defaultPlugins = path.join(__dirname, 'plugins.default.json'); - if (envPluginJSON && envPluginJSON.length > 0) { + if (process.env.TALK_PLUGINS_JSON && process.env.TALK_PLUGINS_JSON.length > 0) { debug('Now using TALK_PLUGINS_JSON environment variable for plugins'); - plugins = JSON.parse(envPluginJSON); + plugins = require(envPlugins); } else if (fs.existsSync(customPlugins)) { debug(`Now using ${customPlugins} for plugins`); plugins = JSON.parse(fs.readFileSync(customPlugins, 'utf8')); diff --git a/webpack.config.js b/webpack.config.js index fe407656c..3a54e85c0 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,10 +1,30 @@ const path = require('path'); +const fs = require('fs'); const autoprefixer = require('autoprefixer'); const precss = require('precss'); const Copy = require('copy-webpack-plugin'); const LicenseWebpackPlugin = require('license-webpack-plugin'); const webpack = require('webpack'); +// Possibly load the config from the .env file (if there is one). +require('dotenv').config(); + +let pluginsConfigPath; + +let envPlugins = path.join(__dirname, 'plugins.env.js'); +let customPlugins = path.join(__dirname, 'plugins.json'); +let defaultPlugins = path.join(__dirname, 'plugins.default.json'); + +if (process.env.TALK_PLUGINS_JSON && process.env.TALK_PLUGINS_JSON.length > 0) { + pluginsConfigPath = envPlugins; +} else if (fs.existsSync(customPlugins)) { + pluginsConfigPath = customPlugins; +} else { + pluginsConfigPath = defaultPlugins; +} + +console.log(`Using ${pluginsConfigPath} as the plugin configuration path`); + // Edit the build targets and embeds below. const buildTargets = [ @@ -53,6 +73,11 @@ module.exports = { }, module: { rules: [ + { + loader: 'plugins-loader', + test: /\.(json|js)$/, + include: pluginsConfigPath + }, { loader: 'babel-loader', exclude: /node_modules/, @@ -61,11 +86,6 @@ module.exports = { cacheDirectory: true } }, - { - loader: 'plugins-loader', - test: /\.json$/, - include: path.join(__dirname, 'plugins.json') - }, { loader: 'json-loader', test: /\.json$/, @@ -120,8 +140,11 @@ module.exports = { }), new webpack.DefinePlugin({ 'process.env': { - 'VERSION': `"${require('./package.json').version}"` + 'VERSION': `"${require('./package.json').version}"`, } + }), + new webpack.EnvironmentPlugin({ + 'TALK_PLUGINS_JSON': '{}' }) ], resolveLoader: { @@ -130,9 +153,10 @@ module.exports = { resolve: { alias: { plugins: path.resolve(__dirname, 'plugins/'), - pluginsConfig: path.resolve(__dirname, 'plugins.json') + pluginsConfig: pluginsConfigPath }, modules: [ + path.resolve(__dirname, 'plugins'), path.resolve(__dirname, 'client'), ...buildTargets.map(target => path.join(__dirname, 'client', target, 'src')), ...buildEmbeds.map(embed => path.join(__dirname, 'client', `coral-embed-${embed}`, 'src')),