Skip to content

Commit

Permalink
Merge pull request #3 from Beardless/build-format-and-bump-version
Browse files Browse the repository at this point in the history
Builded formated and bumped version
  • Loading branch information
reod authored Apr 25, 2019
2 parents 5937fd4 + 74d445e commit 065314e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-head",
"version": "1.1.1",
"version": "1.2.0",
"description": "A document head manager middleware for koa",
"main": "src/index",
"scripts": {
Expand Down
44 changes: 22 additions & 22 deletions src/add-script/add-script.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ import config from "./../config";
import { createCtxWithKoaHead } from "./../../test/test-helpers";

test("addScript (as string)", async t => {
const ctx = await createCtxWithKoaHead();
const addScript = createAddScript(config, ctx);
const jsText = "console.log('test log')";
const expectedScript = {
type: "text/javascript",
jsText
};
const ctx = await createCtxWithKoaHead();
const addScript = createAddScript(config, ctx);
const jsText = "console.log('test log')";
const expectedScript = {
type: "text/javascript",
jsText
};

addScript("console.log('test log')");
addScript("console.log('test log')");

t.deepEqual(ctx.state[config.stateNamespace].scripts, [expectedScript]);
t.end();
t.deepEqual(ctx.state[config.stateNamespace].scripts, [expectedScript]);
t.end();
});

test("addScript (as object)", async t => {
const ctx = await createCtxWithKoaHead();
const addScript = createAddScript(config, ctx);
const jsText = "console.log('test log')";
const fixtureObj = "{ 'fixture': 'test fixture' }";
const expectedScript = {
fixtureObj,
jsText,
type: "text/javascript"
};
const ctx = await createCtxWithKoaHead();
const addScript = createAddScript(config, ctx);
const jsText = "console.log('test log')";
const fixtureObj = "{ 'fixture': 'test fixture' }";
const expectedScript = {
fixtureObj,
jsText,
type: "text/javascript"
};

addScript({ fixtureObj, jsText });
addScript({ fixtureObj, jsText });

t.deepEqual(ctx.state[config.stateNamespace].scripts, [expectedScript]);
t.end();
t.deepEqual(ctx.state[config.stateNamespace].scripts, [expectedScript]);
t.end();
});
12 changes: 6 additions & 6 deletions src/add-script/index.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default (config, ctx) => script => {
const scriptObj =
typeof script === "string" ? { jsText: script } : { ...script };
const scriptObj =
typeof script === "string" ? { jsText: script } : { ...script };

if (!scriptObj.type) {
scriptObj.type = "text/javascript";
}
if (!scriptObj.type) {
scriptObj.type = "text/javascript";
}

ctx.state[config.stateNamespace].scripts.push(scriptObj);
ctx.state[config.stateNamespace].scripts.push(scriptObj);
};
38 changes: 37 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ __webpack_require__.r(__webpack_exports__);

ctx.state[config.stateNamespace].styles.push(styleObj);
});
// CONCATENATED MODULE: ./src/add-script/index.mjs
/* harmony default export */ var add_script = ((config, ctx) => script => {
const scriptObj = typeof script === "string" ? {
jsText: script
} : { ...script
};

if (!scriptObj.type) {
scriptObj.type = "text/javascript";
}

ctx.state[config.stateNamespace].scripts.push(scriptObj);
});
// CONCATENATED MODULE: ./src/to-html/render-title/index.mjs
/* harmony default export */ var render_title = ((config, ctx) => title => {
return `<title>${title}</title>`;
Expand Down Expand Up @@ -170,13 +183,33 @@ __webpack_require__.r(__webpack_exports__);
html += "</style>";
return html;
});
// CONCATENATED MODULE: ./src/to-html/render-script/index.mjs
/* harmony default export */ var render_script = (script => {
let html = "<script";
Object.keys(script).filter(key => key !== "jsText").forEach((key, i, all) => {
if (i === 0) {
html += " ";
}

html += `${key}="${script[key]}"`;

if (i < all.length - 1) {
html += " ";
}
});
html += ">";
html += script.jsText;
html += "</script>";
return html;
});
// CONCATENATED MODULE: ./src/to-html/index.mjs





/* harmony default export */ var to_html = ((config, ctx) => () => {
const renderFunctions = new Map([["title", render_title(config, ctx)], ["metaTags", renderGroup(render_meta_tag, config)], ["links", renderGroup(render_link, config)], ["styles", renderGroup(render_style, config)]]);
const renderFunctions = new Map([["title", render_title(config, ctx)], ["metaTags", renderGroup(render_meta_tag, config)], ["links", renderGroup(render_link, config)], ["styles", renderGroup(render_style, config)], ["scripts", renderGroup(render_script, config)]]);
let headHtml = "";
const documentHead = ctx.state[config.stateNamespace];
Object.keys(documentHead).forEach(headPart => {
Expand Down Expand Up @@ -205,6 +238,7 @@ function renderGroup(renderItem, config) {




/* harmony default export */ var src = __webpack_exports__["default"] = (function (opts) {
const config = { ...src_config,
...opts
Expand All @@ -215,11 +249,13 @@ function renderGroup(renderItem, config) {
ctx.state[config.stateNamespace].metaTags = [];
ctx.state[config.stateNamespace].links = [];
ctx.state[config.stateNamespace].styles = [];
ctx.state[config.stateNamespace].scripts = [];
const middlewareApi = {
setTitle: set_title(config, ctx),
addMetaTag: add_meta_tag(config, ctx),
addLink: add_link(config, ctx),
addStyle: add_style(config, ctx),
addScript: add_script(config, ctx),
toHtml: to_html(config, ctx)
};
ctx[config.ctxNamespace] = middlewareApi;
Expand Down
32 changes: 16 additions & 16 deletions src/to-html/render-script/index.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
export default script => {
let html = "<script";
let html = "<script";

Object.keys(script)
.filter(key => key !== "jsText")
.forEach((key, i, all) => {
if (i === 0) {
html += " ";
}
Object.keys(script)
.filter(key => key !== "jsText")
.forEach((key, i, all) => {
if (i === 0) {
html += " ";
}

html += `${key}="${script[key]}"`;
html += `${key}="${script[key]}"`;

if (i < all.length - 1) {
html += " ";
}
});
if (i < all.length - 1) {
html += " ";
}
});

html += ">";
html += script.jsText;
html += "</script>";
html += ">";
html += script.jsText;
html += "</script>";

return html;
return html;
};
28 changes: 14 additions & 14 deletions src/to-html/render-script/render-script.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import renderScript from "./index";
import test from "tape";

test("renderScript (without attrs)", async t => {
const renderedScript = renderScript({
jsText: "{ fixture: 'test fixture' }"
});
const expectedRenderedScript = `<script>{ fixture: 'test fixture' }</script>`;
const renderedScript = renderScript({
jsText: "{ fixture: 'test fixture' }"
});
const expectedRenderedScript = `<script>{ fixture: 'test fixture' }</script>`;

t.equal(renderedScript, expectedRenderedScript);
t.end();
t.equal(renderedScript, expectedRenderedScript);
t.end();
});

test("renderScript (with attrs)", async t => {
const renderedScript = renderScript({
jsText: "{ fixture: 'test fixture' }",
src: "javascript.js",
type: "module"
});
const expectedRenderedScript = `<script src="javascript.js" type="module">{ fixture: 'test fixture' }</script>`;
const renderedScript = renderScript({
jsText: "{ fixture: 'test fixture' }",
src: "javascript.js",
type: "module"
});
const expectedRenderedScript = `<script src="javascript.js" type="module">{ fixture: 'test fixture' }</script>`;

t.equal(renderedScript, expectedRenderedScript);
t.end();
t.equal(renderedScript, expectedRenderedScript);
t.end();
});

0 comments on commit 065314e

Please sign in to comment.