Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
When using React in a variety of environments (client-side rendered, server-side rendered, React Native), your build tooling is going to vary from platform to platform. Often times it can be a heavy lift to introduce new rules, plugins, and loaders, as well as keep their options consistent between platforms. Allowing Linaria to inject some logic to add a
style
tag to the page during the babel transform allows us to check for the existence of thedocument
object, and if it exists, add it to the page. This lets you use libraries that rely on Linaria without setting up any bespoke build tooling, and styles the page if possible.Summary
I refactored
transform.ts
to split out any of the preprocessing logic, so it could be shared between it and the babel plugin. (preprocess.ts
)I added an additional option to the linaria configuration,
injectStyleTags
, that defaults to false.When
injectStyleTags
is true, the babel plugin constructs an AST for code to inject the CSS into thehead
of the DOM, appends that code to the end of the transformed file, so on module initialization, the file adds its' styles to the DOM.Test plan
All existing unit tests pass (I moved the tests for
transformUrl
fromtransform.ts
topreprocess.test.ts
, that function is now inpreprocess.test.ts
).In addition, a new snapshot test was added in
babel.test.ts
, to make sure that injecting the style works as expected.