From 8a3d06725ec97ccc246cfcd5fc65098d8410a0f6 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Tue, 22 Jan 2019 17:42:50 -0500 Subject: [PATCH] feat: allow to specify extra attributes in getScriptTags & others (#210) --- packages/server/src/ChunkExtractor.js | 65 +++++++++------- packages/server/src/ChunkExtractor.test.js | 89 ++++++++++++++++++++++ 2 files changed, 125 insertions(+), 29 deletions(-) diff --git a/packages/server/src/ChunkExtractor.js b/packages/server/src/ChunkExtractor.js index bf0cf47..a1cd755 100644 --- a/packages/server/src/ChunkExtractor.js +++ b/packages/server/src/ChunkExtractor.js @@ -20,15 +20,19 @@ function getAssets(chunks, getAsset) { return _.uniqBy(_.flatMap(chunks, chunk => getAsset(chunk)), 'url') } -function assetToScriptTag(asset) { - return `` +function extraPropsToString(extraProps) { + return Object.keys(extraProps).reduce((acc, key) => `${acc} ${key}="${extraProps[key]}"`, ''); } -function assetToScriptElement(asset) { +function assetToScriptTag(asset, extraProps) { + return `` +} + +function assetToScriptElement(asset, extraProps) { return ( - ` + getRequiredChunksScriptTag(extraProps) { + return `${this.getRequiredChunksScriptContent()}` } - getRequiredChunksScriptElement() { + getRequiredChunksScriptElement(extraProps) { return ( " +`) + }) + + it('should add extra props if specified', () => { + extractor.addChunk('letters-A') + expect(extractor.getScriptTags({ nonce: 'testnonce' })) + .toMatchInlineSnapshot(` +" + +" `) }) }) @@ -100,6 +110,35 @@ Array [ src="/dist/node/main.js" />, ] +`) + }) + + it('should add extra props if specified', () => { + extractor.addChunk('letters-A') + expect(extractor.getScriptElements({ nonce: 'testnonce' })) + .toMatchInlineSnapshot(` +Array [ +