Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass through props in Js and Styles #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions __tests__/__snapshots__/createApiWithCss.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,38 @@ Object {
}
`;

exports[`unit tests passes additional props through the created component APIs 1`] = `
<span>
<script
defer={true}
foo="bar"
src="/static/0.js"
type="text/javascript"
/>
<script
defer={true}
foo="bar"
src="/static/main.js"
type="text/javascript"
/>
</span>
`;

exports[`unit tests passes additional props through the created component APIs 2`] = `
<span>
<link
foo="bar"
href="/static/main.css"
rel="stylesheet"
/>
<link
foo="bar"
href="/static/0.css"
rel="stylesheet"
/>
</span>
`;

exports[`unit tests stylesAsString() 1`] = `
"/Users/jamesgillmore/App/build/main.css- the css!

Expand Down
9 changes: 9 additions & 0 deletions __tests__/createApiWithCss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,13 @@ describe('unit tests', () => {
const hash = createCssHash(stats) /*? $ */
expect(hash).toMatchSnapshot()
})

it('passes additional props through the created component APIs', () => {
const files = ['0.js', '0.css', 'main.js', 'main.css']
const filesForCss = ['main.js', 'main.css', '0.js', '0.css']
const api = createApiWithCss(files, filesForCss, stats, outputPath) /*? $ */

expect(api.Js({ foo: 'bar' }) /*? $.props.children */).toMatchSnapshot()
expect(api.Styles({ foo: 'bar' }) /*? $.props.children */).toMatchSnapshot()
})
})
36 changes: 19 additions & 17 deletions src/createApiWithCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,28 @@ export default (
const api = {
// 1) Use as React components using ReactDOM.renderToStaticMarkup, eg:
// <html><Styles /><Js /><html>
Js: () => (
Js: props => (
<span>
{scripts.map((file, key) => (
{scripts.map(file => (
<script
type='text/javascript'
src={`${publicPath}/${file}`}
key={key}
key={file}
defer
{...props}
/>
))}
</span>
),
Styles: () => (
Styles: props => (
<span>
{stylesheets.map((file, key) => (
<link rel='stylesheet' href={`${publicPath}/${file}`} key={key} />
{stylesheets.map(file => (
<link
rel='stylesheet'
href={`${publicPath}/${file}`}
key={file}
{...props}
/>
))}
</span>
),
Expand Down Expand Up @@ -91,19 +97,17 @@ export default (
// Use as a React component (<Css />) or a string (`${css}`):
// NOTE: during development, HMR requires stylesheets.
Css: () =>
DEV ? (
api.Styles()
) : (
<span>
(DEV
? api.Styles()
: <span>
<style>{stylesAsString(stylesheets, outputPath)}</style>
</span>
),
</span>),
css: {
toString: () =>
// lazy-loaded in case not used
DEV
(DEV
? api.styles.toString()
: `<style>${stylesAsString(stylesheets, outputPath)}</style>`
: `<style>${stylesAsString(stylesheets, outputPath)}</style>`)
},

// 4) names of files without publicPath or outputPath prefixed:
Expand All @@ -126,9 +130,7 @@ export default (
),
cssHash: {
toString: () =>
`<script type='text/javascript'>window.__CSS_CHUNKS__= ${JSON.stringify(
cssHashRaw
)}</script>`
`<script type='text/javascript'>window.__CSS_CHUNKS__= ${JSON.stringify(cssHashRaw)}</script>`
}
}

Expand Down