From e5b277b068dfac7a8e29e11c74bc5eaee466d892 Mon Sep 17 00:00:00 2001 From: Anduin Withers Date: Sun, 11 Sep 2016 12:32:10 -0400 Subject: [PATCH 1/3] fix this to run when it needs to --- src/reactTemplates.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/reactTemplates.js b/src/reactTemplates.js index 5cdf3473..0c099e6a 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -245,15 +245,13 @@ function generateProps(node, context) { props[propKey] = convertText(node, context, val.trim()); } - // Only run this in the client. + // There is no note as to why this would ever only be run on the client. + // What this change does do it break the only use case I care about. We + // cannot do client checks as the results of npm run build have + // templates expanded. if (node.name === 'img' && propKey === 'src') { - let evaluated = props[propKey]; - if (/^"\/?bhf-assets/.test(evaluated)){ - evaluated = `( typeof window !== 'undefined' ? require(${evaluated.replace(/^"\/?bhf-assets/, '"bhf-assets')}) : ${evaluated})`; - } - props[propKey] = evaluated; + props[propKey] = `require(${props[propKey]})`; } - }); _.assign(props, generateTemplateProps(node, context)); From 75528ea4e348b9a9e9b161cbec663769a6a235bf Mon Sep 17 00:00:00 2001 From: Anduin Withers Date: Sun, 11 Sep 2016 13:52:25 -0400 Subject: [PATCH 2/3] post build --- dist/reactTemplates.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dist/reactTemplates.js b/dist/reactTemplates.js index 5c3cae8a..c0727ebf 100644 --- a/dist/reactTemplates.js +++ b/dist/reactTemplates.js @@ -243,12 +243,12 @@ function generateProps(node, context) { props[propKey] = convertText(node, context, val.trim()); } + // There is no note as to why this would ever only be run on the client. + // What this change does do it break the only use case I care about. We + // cannot do client checks as the results of npm run build have + // templates expanded. if (node.name === 'img' && propKey === 'src') { - let evaluated = props[propKey]; - if (/^"\/?bhf-assets/.test(evaluated)) { - evaluated = 'require(' + evaluated.replace(/^"\/?bhf-assets/, '"bhf-assets') + ')'; - } - props[propKey] = evaluated; + props[propKey] = `require(${ props[propKey] })`; } }); _.assign(props, generateTemplateProps(node, context)); From d18079252d8c581ff4d06473211af3bb10be7fdb Mon Sep 17 00:00:00 2001 From: Anduin Withers Date: Tue, 4 Oct 2016 11:15:05 -0400 Subject: [PATCH 3/3] Final changes, this always emits require as changes were made to bh-frontend in the branch that uses this to run every react template through webpack before use. --- dist/reactTemplates.js | 14 +++++++++----- src/reactTemplates.js | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/dist/reactTemplates.js b/dist/reactTemplates.js index c0727ebf..e0eb9852 100644 --- a/dist/reactTemplates.js +++ b/dist/reactTemplates.js @@ -243,12 +243,16 @@ function generateProps(node, context) { props[propKey] = convertText(node, context, val.trim()); } - // There is no note as to why this would ever only be run on the client. - // What this change does do it break the only use case I care about. We - // cannot do client checks as the results of npm run build have - // templates expanded. + // Note: The correct way to handle require being output in generated + // templates is to ensure the output is processed by webpack. if (node.name === 'img' && propKey === 'src') { - props[propKey] = `require(${ props[propKey] })`; + let nv = val.trim(); + // Remove any leading / so module aliases are easier, the + // code should probably be updated but this is currently easier. + if (nv[0] === '/') { + nv = nv.slice(1); + } + props[propKey] = `require('${ nv }')`; } }); _.assign(props, generateTemplateProps(node, context)); diff --git a/src/reactTemplates.js b/src/reactTemplates.js index 0c099e6a..98dac29d 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -245,12 +245,16 @@ function generateProps(node, context) { props[propKey] = convertText(node, context, val.trim()); } - // There is no note as to why this would ever only be run on the client. - // What this change does do it break the only use case I care about. We - // cannot do client checks as the results of npm run build have - // templates expanded. + // Note: The correct way to handle require being output in generated + // templates is to ensure the output is processed by webpack. if (node.name === 'img' && propKey === 'src') { - props[propKey] = `require(${props[propKey]})`; + let nv = val.trim(); + // Remove any leading / so module aliases are easier, the + // code should probably be updated but this is currently easier. + if (nv[0] === '/') { + nv = nv.slice(1); + } + props[propKey] = `require('${nv}')`; } }); _.assign(props, generateTemplateProps(node, context));