Skip to content

Added namespace option to allow for namespacing, when running multiple instances of typescript-plugin-styled-components #395

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

Merged
merged 4 commits into from
Jun 14, 2021

Conversation

TomGrundy
Copy link

Added namespace option to allow for namespacing, when running multiple instances of typescript-plugin-styled-components

…e instances of typescript-plugin-styled-components
@thomasgrundydiscovery
Copy link
Contributor

@Igorbek Anything I can do to help get this merged into master?

@Igorbek
Copy link
Owner

Igorbek commented Jun 1, 2021

@TomGrundy sorry for very late response, didn't see the PR until you mentioned me directly.
Let me review it quickly. I got the idea, but have some questions.

Copy link
Owner

@Igorbek Igorbek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So having said that, I think the following changes needed:

  • rename namespace to componentIdPrefix and have it default as 'sc-' which is currently the effective value
  • do not generate component id if only componentIdPrefix present, use it with ssr only

Let me know if you think there's scenarios where you need component id with ssr.

if ((isVariableDeclaration(node) && isIdentifier(node.name)) || isExportAssignment(node)) {
const fileName = node.getSourceFile().fileName;
const filePath = sourceRoot ? path.relative(sourceRoot, fileName).replace(path.sep, path.posix.sep) : fileName;
return 'sc-' + hash(`${getDisplayNameFromNode(node)}${filePath}${position}`);
return `${namespace ? namespace : 'sc'}-` + hash(`${getDisplayNameFromNode(node)}${filePath}${position}`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the namespace is effectively the prefix to use in the component id?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's a user defined prefix, to avoid two instances of typescript-plugin-styled-components from generating the same display name and then clashing, e.g. a span with sc-LqBRf, so instead instance one would be say instance-one-LqBRf, and the second instance-two-LqBRf.

}

if (ssr) {
if (namespace) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is unclear why specified namespace itself would make component ids generated, that is only for ssr mode. I think the condition should remain the same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's simply doing the same work as ssr, but then ensuring you can make them unique if more than one instance of the library is run. babel-plugin-styled-components does basically the same thing, taking it's ssr and a namespace value and throwing them together: styled-components/babel-plugin-styled-components#101

Copy link
Contributor

@thomasgrundydiscovery thomasgrundydiscovery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I initially misunderstood your notes, will make it so the componentIdPrefix prefixes separately from ssr

if ((isVariableDeclaration(node) && isIdentifier(node.name)) || isExportAssignment(node)) {
const fileName = node.getSourceFile().fileName;
const filePath = sourceRoot ? path.relative(sourceRoot, fileName).replace(path.sep, path.posix.sep) : fileName;
return 'sc-' + hash(`${getDisplayNameFromNode(node)}${filePath}${position}`);
return `${namespace ? namespace : 'sc'}-` + hash(`${getDisplayNameFromNode(node)}${filePath}${position}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's a user defined prefix, to avoid two instances of typescript-plugin-styled-components from generating the same display name and then clashing, e.g. a span with sc-LqBRf, so instead instance one would be say instance-one-LqBRf, and the second instance-two-LqBRf.

}

if (ssr) {
if (namespace) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's simply doing the same work as ssr, but then ensuring you can make them unique if more than one instance of the library is run. babel-plugin-styled-components does basically the same thing, taking it's ssr and a namespace value and throwing them together: styled-components/babel-plugin-styled-components#101

@thomasgrundydiscovery
Copy link
Contributor

@Igorbek Sorry for the confusing questions above, hopefully I've properly addressed the issues, let me know if something is still wrong. Thank you so much!

@thomasgrundydiscovery
Copy link
Contributor

@Igorbek Anything else I need to do?

@Igorbek
Copy link
Owner

Igorbek commented Jun 9, 2021

If you don't mind, please add an entry to the changelog.md file - and I'll be releasing it shortly after merging another pr in the queue.

@Igorbek Igorbek merged commit 9cb7aae into Igorbek:master Jun 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants