diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-09-29 16:03:16 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-09-29 16:03:16 +0200 |
commit | 7f283791544195fa70c51003db9be3f029c014ef (patch) | |
tree | 4a5ac11ed2a3f1b6e6e0a8d3e7bedd641c0f186f | |
parent | 6f98ee18468e88af01a066eb6e203e4d6ef0a52c (diff) | |
download | gitbook-7f283791544195fa70c51003db9be3f029c014ef.zip gitbook-7f283791544195fa70c51003db9be3f029c014ef.tar.gz gitbook-7f283791544195fa70c51003db9be3f029c014ef.tar.bz2 |
Fix dist command
28 files changed, 264 insertions, 88 deletions
diff --git a/package.json b/package.json index f0ce91c..c7296cb 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "lint": "eslint .", "test": "", "bootstrap": "lerna bootstrap", - "dist": "lerna run dist" + "dist": "lerna run --concurrency 1 prepublish", + "clean": "lerna clean" }, "repository": { "type": "git", diff --git a/packages/gitbook-core/src/actions/TYPES.js b/packages/gitbook-core/src/actions/TYPES.js index 510c1d9..e758a44 100644 --- a/packages/gitbook-core/src/actions/TYPES.js +++ b/packages/gitbook-core/src/actions/TYPES.js @@ -1,10 +1,12 @@ module.exports = { // Components - REGISTER_COMPONENT: 'components:register', - UNREGISTER_COMPONENT: 'components:unregister', - //Navigation - PAGE_FETCH_START: 'navigation:fetch:start', - PAGE_FETCH_END: 'navigation:fetch:end', - PAGE_FETCH_ERROR: 'navigation:fetch:error' + REGISTER_COMPONENT: 'components/register', + UNREGISTER_COMPONENT: 'components/unregister', + // Navigation + PAGE_FETCH_START: 'navigation/fetch:start', + PAGE_FETCH_END: 'navigation/fetch:end', + PAGE_FETCH_ERROR: 'navigation/fetch:error', + // i18n + I18N_REGISTER_LOCALE: 'i18n/register:locale' }; diff --git a/packages/gitbook-core/src/actions/i18n.js b/packages/gitbook-core/src/actions/i18n.js new file mode 100644 index 0000000..e0a2d69 --- /dev/null +++ b/packages/gitbook-core/src/actions/i18n.js @@ -0,0 +1,15 @@ +const ACTION_TYPES = require('./TYPES'); + +/** + * Register messages for a locale + * @param {String} locale + * @param {Map<String:String>} messages + * @return {Action} + */ +function registerLocale(locale, messages) { + return { type: ACTION_TYPES.I18N_REGISTER_LOCALE, locale, messages }; +} + +module.exports = { + registerLocale +}; diff --git a/packages/gitbook-core/src/createStore.js b/packages/gitbook-core/src/createStore.js deleted file mode 100644 index 201a647..0000000 --- a/packages/gitbook-core/src/createStore.js +++ /dev/null @@ -1,36 +0,0 @@ -const Redux = require('redux'); -const ReduxThunk = require('redux-thunk').default; - -const coreReducers = require('./reducers'); -const composeReducer = require('./composeReducer'); - -/** - * Create a new redux store from an initial state and a list of plugins. - * Each plugin entry is the result of {createPlugin}. - * - * @param {Array<Plugin>} plugins - * @param {Object} initialState - * @return {ReduxStore} store - */ -function createStore(plugins, initialState) { - const pluginReducers = plugins.map(plugin => plugin.onReduceState); - const reducer = composeReducer(...[coreReducers].concat(pluginReducers)); - - const store = Redux.createStore( - (state, action) => { - console.log('[store]', action.type); - return reducer(state, action); - }, - initialState, - Redux.compose(Redux.applyMiddleware(ReduxThunk)) - ); - - // Initialize the plugins - plugins.forEach(plugin => { - plugin.onInitialState(store.dispatch, store.getState); - }); - - return store; -} - -module.exports = createStore; diff --git a/packages/gitbook-core/src/index.js b/packages/gitbook-core/src/index.js index cb107c8..c83fc3a 100644 --- a/packages/gitbook-core/src/index.js +++ b/packages/gitbook-core/src/index.js @@ -17,25 +17,26 @@ const { registerComponent } = require('./actions/components'); const ACTIONS = require('./actions/TYPES'); const Shapes = require('./shapes'); -const connect = require('./connect'); -const createPlugin = require('./createPlugin'); -const createReducer = require('./createReducer'); -const createStore = require('./createStore'); -const composeReducer = require('./composeReducer'); -const bootstrap = require('./bootstrap'); -const renderWithStore = require('./renderWithStore'); +const connect = require('./lib/connect'); +const createPlugin = require('./lib/createPlugin'); +const createReducer = require('./lib/createReducer'); +const createContext = require('./lib/createContext'); +const composeReducer = require('./lib/composeReducer'); +const bootstrap = require('./lib/bootstrap'); +const renderWithContext = require('./lib/renderWithContext'); module.exports = { ACTIONS, bootstrap, - renderWithStore, + renderWithContext, connect, createPlugin, createReducer, - createStore, + createContext, composeReducer, registerComponent, // React Components + IntlProvider, InjectedComponent, InjectedComponentSet, HTMLContent, diff --git a/packages/gitbook-core/src/bootstrap.js b/packages/gitbook-core/src/lib/bootstrap.js index 0a2e981..f5183d1 100644 --- a/packages/gitbook-core/src/bootstrap.js +++ b/packages/gitbook-core/src/lib/bootstrap.js @@ -1,8 +1,8 @@ const ReactDOM = require('react-dom'); -const getPayload = require('./lib/getPayload'); -const createStore = require('./createStore'); -const renderWithStore = require('./renderWithStore'); +const getPayload = require('./getPayload'); +const createContext = require('./createContext'); +const renderWithContext = require('./renderWithContext'); /** * Bootstrap GitBook on the browser (this function should not be called on the server side) @@ -11,17 +11,15 @@ function bootstrap() { const initialState = getPayload(window.document); const plugins = window.gitbookPlugins; - console.log(initialState); - const mountNode = document.getElementById('content'); // Create the redux store - const store = createStore(plugins, initialState); + const context = createContext(plugins, initialState); - window.appStore = store; + window.gitbookContext = context; // Render with the store - const el = renderWithStore(store); + const el = renderWithContext(context); ReactDOM.render(el, mountNode); } diff --git a/packages/gitbook-core/src/composeReducer.js b/packages/gitbook-core/src/lib/composeReducer.js index fa2a589..fa2a589 100644 --- a/packages/gitbook-core/src/composeReducer.js +++ b/packages/gitbook-core/src/lib/composeReducer.js diff --git a/packages/gitbook-core/src/connect.js b/packages/gitbook-core/src/lib/connect.js index e18158e..e18158e 100644 --- a/packages/gitbook-core/src/connect.js +++ b/packages/gitbook-core/src/lib/connect.js diff --git a/packages/gitbook-core/src/lib/createContext.js b/packages/gitbook-core/src/lib/createContext.js new file mode 100644 index 0000000..516688e --- /dev/null +++ b/packages/gitbook-core/src/lib/createContext.js @@ -0,0 +1,52 @@ +/* eslint-disable no-console */ +const { Record } = require('immutable'); +const Redux = require('redux'); +const ReduxThunk = require('redux-thunk').default; + +const coreReducers = require('../reducers'); +const composeReducer = require('./composeReducer'); + +const GitBookContext = Record({ + store: null, + actions: {} +}, 'GitBookContext'); + +/** + * Create a new context containing redux store from an initial state and a list of plugins. + * Each plugin entry is the result of {createPlugin}. + * + * @param {Array<Plugin>} plugins + * @param {Object} initialState + * @return {GitBookContext} context + */ +function createContext(plugins, initialState) { + // Compose the reducer from core with plugins + const pluginReducers = plugins.map(plugin => plugin.reduce); + const reducer = composeReducer(...[coreReducers].concat(pluginReducers)); + + // Get actions from all plugins + const actions = plugins.reduce((accu, plugin) => { + return { ...accu, ...plugin.actions }; + }); + + const store = Redux.createStore( + (state, action) => { + console.log('[store]', action.type); + return reducer(state, action); + }, + initialState, + Redux.compose(Redux.applyMiddleware(ReduxThunk)) + ); + + // Initialize the plugins + plugins.forEach(plugin => { + plugin.init(store.dispatch, store.getState); + }); + + return GitBookContext({ + store, + actions + }); +} + +module.exports = createContext; diff --git a/packages/gitbook-core/src/createPlugin.js b/packages/gitbook-core/src/lib/createPlugin.js index d00c99d..7a49053 100644 --- a/packages/gitbook-core/src/createPlugin.js +++ b/packages/gitbook-core/src/lib/createPlugin.js @@ -1,4 +1,7 @@ +const defaultInit = ((dispatch, getState) => {}); +const defaultReduce = ((state, action) => state); + /** * Create a plugin to extend the state and the views. * @@ -6,12 +9,15 @@ * @param {Funciton(state, action)} onReduceState * @return {Plugin} */ -function createPlugin(onInitialState, onReduceState) { - onReduceState = onReduceState || ((state, action) => state); +function createPlugin({ init, reduce, actions }) { + init = init || defaultInit; + reduce = reduce || defaultReduce; + actions = actions || {}; const plugin = { - onInitialState, - onReduceState + init, + reduce, + actions }; if (typeof window !== 'undefined') { diff --git a/packages/gitbook-core/src/createReducer.js b/packages/gitbook-core/src/lib/createReducer.js index 2ebecfb..2ebecfb 100644 --- a/packages/gitbook-core/src/createReducer.js +++ b/packages/gitbook-core/src/lib/createReducer.js diff --git a/packages/gitbook-core/src/renderWithStore.js b/packages/gitbook-core/src/lib/renderWithContext.js index 55594c4..b9e2cfd 100644 --- a/packages/gitbook-core/src/renderWithStore.js +++ b/packages/gitbook-core/src/lib/renderWithContext.js @@ -1,18 +1,18 @@ const React = require('react'); const { Provider } = require('react-redux'); -const { InjectedComponent } = require('./components/InjectedComponent'); -const PJAXWrapper = require('./components/PJAXWrapper'); -const IntlProvider = require('./components/IntlProvider'); +const { InjectedComponent } = require('../components/InjectedComponent'); +const PJAXWrapper = require('../components/PJAXWrapper'); +const IntlProvider = require('../components/IntlProvider'); /** * Render the application for a store - * @param {ReduxStore} store + * @param {GitBookContext} context * @return {React.Element} element */ -function renderWithStore(store) { +function renderWithContext(context) { return ( - <Provider store={store}> + <Provider store={context.store}> <PJAXWrapper> <IntlProvider> <InjectedComponent matching={{ role: 'Body' }} /> @@ -22,4 +22,4 @@ function renderWithStore(store) { ); } -module.exports = renderWithStore; +module.exports = renderWithContext; diff --git a/packages/gitbook-core/src/reducers/i18n.js b/packages/gitbook-core/src/reducers/i18n.js new file mode 100644 index 0000000..46df8fc --- /dev/null +++ b/packages/gitbook-core/src/reducers/i18n.js @@ -0,0 +1,25 @@ +const { Record, Map } = require('immutable'); +const ACTION_TYPES = require('../actions/TYPES'); + +const I18nState = Record({ + locale: 'en', + // Map of locale -> Map<String:String> + messages: Map() +}); + +function reduceI18n(state, action) { + state = state || I18nState(); + switch (action.type) { + + case ACTION_TYPES.I18N_REGISTER_LOCALE: + return state.merge({ + messages: state.messages.set(action.locale, Map(action.messages)) + }); + + default: + return state; + + } +} + +module.exports = reduceI18n; diff --git a/packages/gitbook-core/src/reducers/index.js b/packages/gitbook-core/src/reducers/index.js index 4785c1c..956172b 100644 --- a/packages/gitbook-core/src/reducers/index.js +++ b/packages/gitbook-core/src/reducers/index.js @@ -1,11 +1,10 @@ -const ACTION_TYPES = require('../actions/TYPES'); - -const composeReducer = require('../composeReducer'); -const createReducer = require('../createReducer'); +const composeReducer = require('../lib/composeReducer'); +const createReducer = require('../lib/createReducer'); module.exports = composeReducer( createReducer('components', require('./components')), createReducer('navigation', require('./navigation')), + createReducer('i18n', require('./i18n')), // GitBook JSON createReducer('page', require('./page')), createReducer('summary', require('./summary')), diff --git a/packages/gitbook-plugin-search/.gitignore b/packages/gitbook-plugin-search/.gitignore new file mode 100644 index 0000000..dfd90dc --- /dev/null +++ b/packages/gitbook-plugin-search/.gitignore @@ -0,0 +1 @@ +_assets diff --git a/packages/gitbook-plugin-search/.npmignore b/packages/gitbook-plugin-search/.npmignore new file mode 100644 index 0000000..75e0923 --- /dev/null +++ b/packages/gitbook-plugin-search/.npmignore @@ -0,0 +1 @@ +!_assets diff --git a/packages/gitbook-plugin-search/package.json b/packages/gitbook-plugin-search/package.json index df84726..d6741c9 100644 --- a/packages/gitbook-plugin-search/package.json +++ b/packages/gitbook-plugin-search/package.json @@ -2,7 +2,8 @@ "name": "gitbook-plugin-search", "description": "Search integration in GitBook", "main": "index.js", - "version": "2.0.0", + "browser": "./_assets/theme.js", + "version": "2.2.1", "dependencies": { "gitbook-core": "^0.0.0" }, @@ -12,6 +13,10 @@ "engines": { "gitbook": ">=3.0.0" }, + "scripts": { + "build-js": "gitbook-plugin build ./src/index.js ./_assets/theme.js", + "prepublish": "npm run build-js" + }, "homepage": "https://github.com/GitbookIO/gitbook", "repository": { "type": "git", diff --git a/packages/gitbook-plugin-search/src/components/Results.js b/packages/gitbook-plugin-search/src/components/Results.js index 99bc63d..3f53741 100644 --- a/packages/gitbook-plugin-search/src/components/Results.js +++ b/packages/gitbook-plugin-search/src/components/Results.js @@ -1,6 +1,23 @@ const GitBook = require('gitbook-core'); const { React } = GitBook; +const Result = React.createClass({ + propTypes: { + result: React.PropTypes.object + }, + + render() { + const { result } = this.props; + + return ( + <div className="Search/Result"> + <h3>{result.title}</h3> + <p>{result.body}</p> + </div> + ); + } +}); + const SearchResults = React.createClass({ propTypes: { results: GitBook.Shapes.list @@ -11,8 +28,8 @@ const SearchResults = React.createClass({ return ( <div className="Search/Results"> - {results.map(result => { - + {results.map((result, i) => { + return <Result key={i} result={result} />; })} </div> ); diff --git a/packages/gitbook-plugin-search/src/index.js b/packages/gitbook-plugin-search/src/index.js index 94a5d35..c3b0253 100644 --- a/packages/gitbook-plugin-search/src/index.js +++ b/packages/gitbook-plugin-search/src/index.js @@ -3,11 +3,15 @@ const GitBook = require('gitbook-core'); const SearchInput = require('./components/Input'); const SearchResults = require('./components/Results'); const reducers = require('./reducers'); +const search = require('./actions/search'); -module.exports = GitBook.createPlugin( - (dispatch, state) => { +module.exports = GitBook.createPlugin({ + init: (dispatch, getState) => { dispatch(GitBook.registerComponent(SearchInput, { role: 'search:input' })); dispatch(GitBook.registerComponent(SearchResults, { role: 'search:results' })); }, - reducers -); + reduce: reducers, + actions: { + search + } +}); diff --git a/packages/gitbook-plugin-sharing/.gitignore b/packages/gitbook-plugin-sharing/.gitignore new file mode 100644 index 0000000..7c6f0eb --- /dev/null +++ b/packages/gitbook-plugin-sharing/.gitignore @@ -0,0 +1,31 @@ +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# Deployed apps should consider commenting this line out: +# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git +node_modules + +# vim swapfile +*.swp + +# Plugin assets +_assets diff --git a/packages/gitbook-plugin-sharing/.npmignore b/packages/gitbook-plugin-sharing/.npmignore new file mode 100644 index 0000000..7bc36b7 --- /dev/null +++ b/packages/gitbook-plugin-sharing/.npmignore @@ -0,0 +1,2 @@ +# Publish assets on NPM +!_assets diff --git a/packages/gitbook-plugin-sharing/index.js b/packages/gitbook-plugin-sharing/index.js new file mode 100644 index 0000000..e542ae8 --- /dev/null +++ b/packages/gitbook-plugin-sharing/index.js @@ -0,0 +1,10 @@ + +module.exports = { + blocks: { + + }, + + hooks: { + + } +}; diff --git a/packages/gitbook-plugin-sharing/package.json b/packages/gitbook-plugin-sharing/package.json new file mode 100644 index 0000000..1519136 --- /dev/null +++ b/packages/gitbook-plugin-sharing/package.json @@ -0,0 +1,28 @@ +{ + "name": "gitbook-plugin-sharing", + "description": "Sharing button in the toolbar", + "main": "index.js", + "browser": "./_assets/theme.js", + "version": "0.0.0", + "dependencies": { + "gitbook-core": "^0.0.0" + }, + "devDependencies": { + "gitbook-plugin": "*" + }, + "engines": { + "gitbook": ">=3.0.0" + }, + "scripts": { + "build-js": "gitbook-plugin build ./src/index.js ./_assets/theme.js", + "prepublish": "npm run build-js" + }, + "homepage": "https://github.com/GitbookIO/gitbook", + "repository": { + "type": "git", + "url": "https://github.com/GitbookIO/gitbook.git" + }, + "bugs": { + "url": "https://github.com/GitbookIO/gitbook/issues" + } +} diff --git a/packages/gitbook-plugin-sharing/src/index.js b/packages/gitbook-plugin-sharing/src/index.js new file mode 100644 index 0000000..d8e52bf --- /dev/null +++ b/packages/gitbook-plugin-sharing/src/index.js @@ -0,0 +1,8 @@ +const GitBook = require('gitbook-core'); + +module.exports = GitBook.createPlugin({ + init: (dispatch, getState) => { + + }, + reduce: (state, action) => state +}); diff --git a/packages/gitbook-plugin-theme-default/src/index.js b/packages/gitbook-plugin-theme-default/src/index.js index ce678e1..9d328f2 100644 --- a/packages/gitbook-plugin-theme-default/src/index.js +++ b/packages/gitbook-plugin-theme-default/src/index.js @@ -48,6 +48,9 @@ ThemeBody = GitBook.connect(ThemeBody, ({page, summary, sidebar}) => { return { page, summary, sidebar }; }); -module.exports = GitBook.createPlugin((dispatch, state) => { - dispatch(GitBook.registerComponent(ThemeBody, { role: 'Body' })); -}, reduceState); +module.exports = GitBook.createPlugin({ + init: (dispatch, state) => { + dispatch(GitBook.registerComponent(ThemeBody, { role: 'Body' })); + }, + reduce: reduceState +}); diff --git a/packages/gitbook-plugin/src/compile.js b/packages/gitbook-plugin/src/compile.js index e7179e5..61c8777 100644 --- a/packages/gitbook-plugin/src/compile.js +++ b/packages/gitbook-plugin/src/compile.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const fs = require('fs-extra'); const Promise = require('q'); const browserify = require('browserify'); const babelify = require('babelify'); @@ -26,6 +26,8 @@ function compilePlugin(inputFile, outputFile) { ] }); + fs.ensureFileSync(outputFile); + const output = fs.createWriteStream(outputFile); b.bundle() diff --git a/packages/gitbook-plugin/src/create.js b/packages/gitbook-plugin/src/create.js index 5b218c2..2e50532 100644 --- a/packages/gitbook-plugin/src/create.js +++ b/packages/gitbook-plugin/src/create.js @@ -14,6 +14,7 @@ function create(outputDir, spec) { 'name': `gitbook-plugin-${spec.name}`, 'description': `${spec.desc}`, 'main': 'index.js', + 'browser': './_assets/theme.js', 'version': '0.0.0', 'dependencies': { 'gitbook-core': '^0.0.0' diff --git a/packages/gitbook/src/browser/render.js b/packages/gitbook/src/browser/render.js index 9338ce2..e976765 100644 --- a/packages/gitbook/src/browser/render.js +++ b/packages/gitbook/src/browser/render.js @@ -46,14 +46,14 @@ function render(plugins, initialState) { // Load the plugins const browserPlugins = loadPlugins(plugins); const payload = JSON.stringify(initialState); - const store = GitBook.createStore(browserPlugins, initialState); + const context = GitBook.createContext(browserPlugins, initialState); const scripts = plugins.toList() .filter(plugin => plugin.getPackage().has('browser')) .map(plugin => 'gitbook/plugins/' + plugin.getName() + '.js') .toArray(); - const el = GitBook.renderWithStore(store); + const el = GitBook.renderWithContext(context); // Render inner body const innerHTML = ReactDOMServer.renderToString(el); |