From 2c90164a35411abcc0471f6ffd797e2900bbb3f7 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 16 Jun 2018 14:34:38 +0930 Subject: [PATCH] Add useExtensions option --- js-packages/webpack-config/README.md | 27 ++++++++++++++++++++++++++- js-packages/webpack-config/index.js | 14 ++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/js-packages/webpack-config/README.md b/js-packages/webpack-config/README.md index 9bd3939f1..5d5716c5f 100755 --- a/js-packages/webpack-config/README.md +++ b/js-packages/webpack-config/README.md @@ -9,7 +9,32 @@ This package generates a [Webpack](https://webpack.js.org) config object that wi ```js var config = require('flarum-webpack-config'); -module.exports = config(); +module.exports = config(options); ``` To merge in custom Webpack config options, use [webpack-merge](https://www.npmjs.com/package/webpack-merge). + +## Options + +### `useExtensions` + +`Array`, defaults to `[]`. + +An array of extensions whose modules should be made available. This is a shortcut to add [`externals`](https://webpack.js.org/configuration/externals/) configuration for extension modules. Imported extension modules will not be bundled, but will instead refer to the extension's exports included in the Flarum runtime (ie. `flarum.extensions["vendor/package"]`). + +For example, to access the Tags extension module within your extension: + +**forum.js** + +```js +import { Tag } from '@flarum/flarum-ext-tags/forum'; +``` + +**webpack.config.js** + +```js +module.exports = config({ + useExtensions: ['flarum/flarum-ext-tags'] +}); +``` + diff --git a/js-packages/webpack-config/index.js b/js-packages/webpack-config/index.js index 969254dfe..7688699c5 100644 --- a/js-packages/webpack-config/index.js +++ b/js-packages/webpack-config/index.js @@ -55,6 +55,20 @@ module.exports = function(options = {}) { 'jquery': 'jQuery', }, + function() { + const externals = {}; + + if (options.useExtensions) { + for (const extension of options.useExtensions) { + externals['@'+extension] = + externals['@'+extension+'/forum'] = + externals['@'+extension+'/admin'] = "flarum.extensions['"+extension+"']"; + } + } + + return externals; + }(), + // Support importing old-style core modules. function(context, request, callback) { let matches;