From 74d445e90a0b1f46f87499e8feb6348e7532a8e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Miko=C5=82ajczuk?= Date: Thu, 25 Apr 2019 09:29:28 +0200 Subject: [PATCH] Builded formated and bumped version --- package.json | 2 +- src/add-script/add-script.spec.js | 44 +++++++++---------- src/add-script/index.mjs | 12 ++--- src/index.js | 38 +++++++++++++++- src/to-html/render-script/index.mjs | 32 +++++++------- .../render-script/render-script.spec.js | 28 ++++++------ 6 files changed, 96 insertions(+), 60 deletions(-) diff --git a/package.json b/package.json index ac0b52c..ed10fb6 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/add-script/add-script.spec.js b/src/add-script/add-script.spec.js index 6490ce4..f7fa23b 100644 --- a/src/add-script/add-script.spec.js +++ b/src/add-script/add-script.spec.js @@ -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(); }); diff --git a/src/add-script/index.mjs b/src/add-script/index.mjs index bd810ae..2320b88 100644 --- a/src/add-script/index.mjs +++ b/src/add-script/index.mjs @@ -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); }; diff --git a/src/index.js b/src/index.js index 86c26a8..f00bcb9 100644 --- a/src/index.js +++ b/src/index.js @@ -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}`; @@ -170,13 +183,33 @@ __webpack_require__.r(__webpack_exports__); html += ""; return html; }); +// CONCATENATED MODULE: ./src/to-html/render-script/index.mjs +/* harmony default export */ var render_script = (script => { + let html = " 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 += ""; + 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 => { @@ -205,6 +238,7 @@ function renderGroup(renderItem, config) { + /* harmony default export */ var src = __webpack_exports__["default"] = (function (opts) { const config = { ...src_config, ...opts @@ -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; diff --git a/src/to-html/render-script/index.mjs b/src/to-html/render-script/index.mjs index 4c74b7d..38da989 100644 --- a/src/to-html/render-script/index.mjs +++ b/src/to-html/render-script/index.mjs @@ -1,23 +1,23 @@ export default script => { - let html = " 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 += ""; + html += ">"; + html += script.jsText; + html += ""; - return html; + return html; }; diff --git a/src/to-html/render-script/render-script.spec.js b/src/to-html/render-script/render-script.spec.js index 95cccc9..e74b861 100644 --- a/src/to-html/render-script/render-script.spec.js +++ b/src/to-html/render-script/render-script.spec.js @@ -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 = ``; + const renderedScript = renderScript({ + jsText: "{ fixture: 'test fixture' }" + }); + const expectedRenderedScript = ``; - 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 = ``; + const renderedScript = renderScript({ + jsText: "{ fixture: 'test fixture' }", + src: "javascript.js", + type: "module" + }); + const expectedRenderedScript = ``; - t.equal(renderedScript, expectedRenderedScript); - t.end(); + t.equal(renderedScript, expectedRenderedScript); + t.end(); });