Skip to content

Commit

Permalink
feat(runtime): consolidate all runtime in a single module
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypopp committed Apr 23, 2016
1 parent bfc827e commit 36a8922
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
11 changes: 1 addition & 10 deletions runtime.js
@@ -1,10 +1 @@
/**
* @copyright 2016-present, Reactdown team
*/

// All modules used by compiled markdown files.
//
// When using webpack, you will probably want to import 'reactdown/runtime' in a
// vendor bundle to cut down on per-file weight.
module.exports.components = require('./components');
module.exports.DocumentContext = require('./DocumentContext').default;
module.exports = require('./lib/runtime');
6 changes: 6 additions & 0 deletions src/directives/index.js
@@ -0,0 +1,6 @@
/**
* @copyright 2016-present, Reactdown team
*/

export ref from './ref';
export meta from './meta';
44 changes: 26 additions & 18 deletions src/render/index.js
Expand Up @@ -34,7 +34,7 @@ export type ModelConfig = {

type CompleteRenderConfig = {
build: JSASTFactory;
components: string;
components: ?string;
directives: ComponentMapping;
roles: ComponentMapping;
model: ModelConfig;
Expand All @@ -48,17 +48,10 @@ const defaultRendererConfig: RendererConfig = {
roles: {},
};

function directive(name: string): ComponentRef {
return {source: `reactdown/lib/directives/${name}`, name: 'default'};
}

const defaultRenderConfig: CompleteRenderConfig = {
build: build,
components: 'reactdown/lib/components',
directives: {
'meta': directive('meta'),
'ref': directive('ref'),
},
components: null,
directives: {},
roles: {},
model: {toc, title},
};
Expand Down Expand Up @@ -100,7 +93,11 @@ export function renderToProgram(
let {build, components, directives, roles} = config;
let rendererConfig = {
build,
directives: keyMirrorToJSAST(build, directives),
directives: {
...keyMirrorToJSAST(build, directives),
meta: expr`defaultDirectives.meta`,
ref: expr`defaultDirectives.ref`,
},
roles: keyMirrorToJSAST(build, roles),
};

Expand Down Expand Up @@ -151,16 +148,27 @@ export function renderToProgram(
}
});

statements = stmt`
let prelude = stmt`
import React from "react";
import DocumentContext from "reactdown/lib/DocumentContext";
import * as defaultComponents from "reactdown/lib/components";
import * as customComponents from "${build.stringLiteral(components)}";
import {
DocumentContext,
directives as defaultDirectives,
components as defaultComponents
} from "reactdown/runtime";
`;

let components = {...defaultComponents, ...customComponents};
`.concat(statements);
if (components) {
prelude = prelude.concat(stmt`
import * as customComponents from "${build.stringLiteral(components)}";
let components = {...defaultComponents, ...customComponents};
`);
} else {
prelude = prelude.concat(stmt`
let components = defaultComponents;
`);
}

return build.program(statements);
return build.program(prelude.concat(statements));
}

export function renderToParts(
Expand Down
13 changes: 13 additions & 0 deletions src/runtime.js
@@ -0,0 +1,13 @@
/**
* Reactdown runtime.
*
* Runtime contains all modules which are by default imported by compiled
* Reactdown documents. This allows easier configuration when you need bundle
* them separately.
*
* @copyright 2016-present, Reactdown team
*/

export * as components from './components';
export * as directives from './directives';
export DocumentContext from './DocumentContext';

0 comments on commit 36a8922

Please sign in to comment.