From ced0be930dd86bc204cab5b4e3a1cd83aed68ccf Mon Sep 17 00:00:00 2001 From: Samy Pesse Date: Tue, 20 Sep 2016 19:19:01 +0200 Subject: [PATCH] Working loading of plugin --- packages/gitbook-core/package.json | 3 +- packages/gitbook-core/src/createPlugin.js | 2 + packages/gitbook-core/src/createStore.js | 6 +-- packages/gitbook-plugin-sharing/package.json | 42 +++++-------------- .../gitbook-plugin-theme-default/package.json | 13 +++++- packages/gitbook-plugin/README.md | 3 ++ packages/gitbook-plugin/package.json | 6 ++- packages/gitbook-plugin/src/cli.js | 27 ++++++++---- packages/gitbook-plugin/src/compile.js | 39 +++++++++++++++++ packages/gitbook/package.json | 4 +- packages/gitbook/src/browser/loadPlugins.js | 8 ++-- packages/gitbook/src/output/website/onPage.js | 4 +- 12 files changed, 104 insertions(+), 53 deletions(-) create mode 100644 packages/gitbook-plugin/README.md create mode 100644 packages/gitbook-plugin/src/compile.js diff --git a/packages/gitbook-core/package.json b/packages/gitbook-core/package.json index c75f05d95..387a6109b 100644 --- a/packages/gitbook-core/package.json +++ b/packages/gitbook-core/package.json @@ -24,7 +24,8 @@ "scripts": { "dist-lib": "rm -rf lib/ && babel -d lib/ src/", "dist-standalone": "browserify -r ./lib/index.js:gitbook-core -r react -r react-dom ./lib/index.js | uglifyjs -c > gitbook.core.min.js", - "prepublish": "npm run dist-lib && npm run dist-standalone" + "dist": "npm run dist-lib && npm run dist-standalone", + "prepublish": "npm run dist" }, "repository": { "type": "git", diff --git a/packages/gitbook-core/src/createPlugin.js b/packages/gitbook-core/src/createPlugin.js index c26d7452c..d00c99dae 100644 --- a/packages/gitbook-core/src/createPlugin.js +++ b/packages/gitbook-core/src/createPlugin.js @@ -7,6 +7,8 @@ * @return {Plugin} */ function createPlugin(onInitialState, onReduceState) { + onReduceState = onReduceState || ((state, action) => state); + const plugin = { onInitialState, onReduceState diff --git a/packages/gitbook-core/src/createStore.js b/packages/gitbook-core/src/createStore.js index c13f21923..487319087 100644 --- a/packages/gitbook-core/src/createStore.js +++ b/packages/gitbook-core/src/createStore.js @@ -13,12 +13,12 @@ const reducers = require('./reducers'); */ function createStore(plugins, initialState) { const pluginReducers = plugins.map(plugin => plugin.onReduceState); + console.log(pluginReducers); const reducer = Redux.compose(reducers, ...pluginReducers); - const store = Redux.createStore( - reducer, + reducers, initialState, - Redux.applyMiddleware(ReduxThunk) + Redux.compose(Redux.applyMiddleware(ReduxThunk)) ); // Initialize the plugins diff --git a/packages/gitbook-plugin-sharing/package.json b/packages/gitbook-plugin-sharing/package.json index 0ca8dc5ea..134acb825 100644 --- a/packages/gitbook-plugin-sharing/package.json +++ b/packages/gitbook-plugin-sharing/package.json @@ -4,7 +4,11 @@ "main": "index.js", "version": "1.0.2", "dependencies": { - + "gitbook-core": "^0.0.0", + "react": "^15.3.1" + }, + "devDependencies": { + "gitbook-plugin": "*" }, "engines": { "gitbook": ">=3.0.0" @@ -20,36 +24,12 @@ }, "gitbook": { "properties": { - "facebook": { - "type": "boolean", - "default": true, - "title": "Facebook" - }, - "twitter": { - "type": "boolean", - "default": true, - "title": "Twitter" - }, - "google": { - "type": "boolean", - "default": false, - "title": "Google" - }, - "weibo": { - "type": "boolean", - "default": false, - "description": "Weibo" - }, - "instapaper": { - "type": "boolean", - "default": false, - "description": "Instapaper" - }, - "vk": { - "type": "boolean", - "default": false, - "description": "VK" - }, + "facebook": { "type": "boolean", "default": true, "title": "Facebook" }, + "twitter": { "type": "boolean", "default": true, "title": "Twitter" }, + "google": { "type": "boolean", "default": false, "title": "Google" }, + "weibo": { "type": "boolean", "default": false, "description": "Weibo" }, + "instapaper": { "type": "boolean", "default": false, "description": "Instapaper" }, + "vk": { "type": "boolean", "default": false, "description": "VK" }, "all": { "type": "array", "items": { diff --git a/packages/gitbook-plugin-theme-default/package.json b/packages/gitbook-plugin-theme-default/package.json index 5ee288b19..42ff1b6d0 100644 --- a/packages/gitbook-plugin-theme-default/package.json +++ b/packages/gitbook-plugin-theme-default/package.json @@ -2,12 +2,21 @@ "name": "gitbook-plugin-theme-default", "description": "Default theme for GitBook", "main": "./index.js", - "browser": "./src/index.js", + "browser": "./assets/theme.js", "version": "1.0.5", "engines": { "gitbook": ">=3.0.0" }, - "dependencies": {}, + "dependencies": { + "gitbook-core": "^0.0.0", + "react": "^15.3.1" + }, + "devDependencies": { + "gitbook-plugin": "*" + }, + "scripts": { + "prepublish": "gitbook-plugin build ./src/index.js ./assets/theme.js" + }, "repository": { "type": "git", "url": "https://github.com/GitbookIO/gitbook.git" diff --git a/packages/gitbook-plugin/README.md b/packages/gitbook-plugin/README.md new file mode 100644 index 000000000..a6ded9e08 --- /dev/null +++ b/packages/gitbook-plugin/README.md @@ -0,0 +1,3 @@ +# `gitbook-plugin` + +CLI utility to build and test plugins. diff --git a/packages/gitbook-plugin/package.json b/packages/gitbook-plugin/package.json index e8b6983ba..2dcadbb04 100644 --- a/packages/gitbook-plugin/package.json +++ b/packages/gitbook-plugin/package.json @@ -4,9 +4,13 @@ "description": "CLI for compiling and testing plugins", "main": "./lib/index.js", "dependencies": { + "babel-preset-es2015": "^6.14.0", + "babel-preset-react": "^6.11.1", "babelify": "^7.3.0", "browserify": "^13.1.0", - "commander": "^2.9.0" + "commander": "^2.9.0", + "q": "^1.4.1", + "winston": "^2.2.0" }, "devDependencies": { "babel-cli": "^6.14.0", diff --git a/packages/gitbook-plugin/src/cli.js b/packages/gitbook-plugin/src/cli.js index e1f201b96..20ceed59a 100644 --- a/packages/gitbook-plugin/src/cli.js +++ b/packages/gitbook-plugin/src/cli.js @@ -1,13 +1,26 @@ -const program = require('commander'); -const pkg = require('../package.json'); +#! /usr/bin/env node -program.version(pkg.version) +const program = require('commander'); +const path = require('path'); +const winston = require('winston'); + +const pkg = require('../package.json'); +const compile = require('./compile'); + +const resolve = (input => path.resolve(process.cwd(), input)); + +program.version(pkg.version); +winston.cli(); program - .command('build [plugin]') - .description('build a plugin') - .action(function(plugin, options) { - + .command('build [input] [output]') + .description('build a browser plugin') + .action(function(input, output, options) { + compile(resolve(input), resolve(output)) + .then( + () => winston.info('Plugin compiled successfully'), + (err) => winston.error('Error: ', err) + ); }); diff --git a/packages/gitbook-plugin/src/compile.js b/packages/gitbook-plugin/src/compile.js new file mode 100644 index 000000000..e7179e59c --- /dev/null +++ b/packages/gitbook-plugin/src/compile.js @@ -0,0 +1,39 @@ +const fs = require('fs'); +const Promise = require('q'); +const browserify = require('browserify'); +const babelify = require('babelify'); + +/** + * Compile a plugin to work with "gitbook-core" in the browser. + * @param {String} inputFile + * @param {String} outputFile + * @return {Promise} + */ +function compilePlugin(inputFile, outputFile) { + const d = Promise.defer(); + const b = browserify({ + standalone: 'GitBookPlugin' + }); + + b.add(inputFile); + b.external('react'); + b.external('react-dom'); + b.external('gitbook-core'); + b.transform(babelify, { + presets: [ + require('babel-preset-es2015'), + require('babel-preset-react') + ] + }); + + const output = fs.createWriteStream(outputFile); + + b.bundle() + .pipe(output) + .on('error', (err) => d.reject(err)) + .on('end', () => d.resolve()); + + return d.promise; +} + +module.exports = compilePlugin; diff --git a/packages/gitbook/package.json b/packages/gitbook/package.json index 0c30285a3..8b6551edf 100644 --- a/packages/gitbook/package.json +++ b/packages/gitbook/package.json @@ -51,8 +51,8 @@ "omit-keys": "^0.1.0", "open": "0.0.5", "q": "1.4.1", - "react": "^15.3.1", - "react-dom": "^15.3.1", + "react": "^15.3.2", + "react-dom": "^15.3.2", "react-redux": "^4.4.5", "read-installed": "^4.0.3", "redux": "^3.5.2", diff --git a/packages/gitbook/src/browser/loadPlugins.js b/packages/gitbook/src/browser/loadPlugins.js index ba5381c76..0f26aae6a 100644 --- a/packages/gitbook/src/browser/loadPlugins.js +++ b/packages/gitbook/src/browser/loadPlugins.js @@ -2,11 +2,12 @@ const path = require('path'); /** * Load all browser plugins - * @param {List} plugins - * @return {List} + * @param {OrderedMap} plugins + * @return {Array} */ function loadPlugins(plugins) { return plugins + .valueSeq() .filter(plugin => plugin.getPackage().has('browser')) .map(plugin => { const browserFile = path.resolve( @@ -15,7 +16,8 @@ function loadPlugins(plugins) { ); return require(browserFile); - }); + }) + .toArray(); } module.exports = loadPlugins; diff --git a/packages/gitbook/src/output/website/onPage.js b/packages/gitbook/src/output/website/onPage.js index 27bc0b21a..7e93080e0 100644 --- a/packages/gitbook/src/output/website/onPage.js +++ b/packages/gitbook/src/output/website/onPage.js @@ -37,10 +37,8 @@ function onPage(output, page) { // We should probabbly move it to "template" or a "site" namespace // context.basePath = basePath; - console.log('render page'); - // Load the plugins - const browserPlugins = []; //loadBrowserPlugins(plugins); + const browserPlugins = loadBrowserPlugins(plugins); // Render the theme const html = render(browserPlugins, initialState);