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

Allow components to be imported without dependencies #1804

Merged
merged 7 commits into from
May 12, 2020

Conversation

36degrees
Copy link
Contributor

@36degrees 36degrees commented May 6, 2020

What

Add a new file called _index.scss to each component, which contains the component’s styles but does not import the settings, helpers and tools layers.

Update the existing _[component].scss files to import the settings, helpers and tools layers and import _index.scss. This will preserve the existing behaviour, and continue to provide a single entry point for individual components that can be used in contexts such as React.

Update components/_all.scss to include every component’s _index.scss file, avoiding the unnecessary imports of the settings, helpers and tools layers within each component.

Where components import other dependents, update to import their _index.scss file, avoiding the unnecessary imports of the settings, helpers and tools layers within the dependent components.

Why

As proposed in alphagov/govuk-design-system-architecture#22, we’re making changes to the way that the base layers (settings, tools and helpers) are imported within different parts of GOV.UK Frontend’s Sass, to reduce the time it takes to compile to CSS.

Closes #1797

Add a new file called _index.scss to each component, which contains the component’s styles but does not import the settings, helpers and tools layers.

Update the existing _[component].scss files to import the settings, helpers and tools layers and import _index.scss.

This will preserve the existing behaviour, and continue to provide a single entry point for individual components that can be used in contexts such as React.

We already have a test in src/govuk/components/all.test.js which ensures that the _[component].scss file renders without errors.

We're doing this so that we can reduce the number of imports within the codebase – by moving everything except the imports into a separate file (`_index.scss`) and including that in the original _[component].scss file, we can then import just the _index.scss file when importing all components, shortcutting all of the base layer imports.
We already import `helpers/all`, so we don't need to then import the typography helpers separately.
This avoids importing the base layers again for every component.

We need to explicitly import `accordion/index` file, rather than just importing `accordion`, as index file support has only been in LibSass since 3.6.0 and Ruby Sass since 3.6.0[1], and we support 3.3.0 and 1.0.0 respectively.

Add an import of the base layers, so that this isn't a breaking change for anyone currently importing `components/all` without also importing the base layers.

[1]: https://sass-lang.com/documentation/at-rules/import#index-files
Provide a single import for the settings, tools and helpers layers, which all of the other layers rely on.
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1804 May 6, 2020 10:21 Inactive
@36degrees
Copy link
Contributor Author

RubySass

sass --version
Sass 3.4.25 (Selective Steve)

Before: ~26s

➜ time sass -C src/govuk/all.scss > /dev/null
sass -C src/govuk/all.scss > /dev/null  25.05s user 0.85s system 99% cpu 26.045 total

➜ time sass -C src/govuk/all.scss > /dev/null
sass -C src/govuk/all.scss > /dev/null  26.29s user 0.99s system 97% cpu 27.839 total

➜ time sass -C src/govuk/all.scss > /dev/null
sass -C src/govuk/all.scss > /dev/null  26.28s user 0.95s system 99% cpu 27.416 total

After: ~16s

➜ time sass -C src/govuk/all.scss > /dev/null
sass -C src/govuk/all.scss > /dev/null  16.59s user 0.59s system 96% cpu 17.770 total

➜ time sass -C src/govuk/all.scss > /dev/null
sass -C src/govuk/all.scss > /dev/null  16.61s user 0.57s system 99% cpu 17.257 total

➜ time sass -C src/govuk/all.scss > /dev/null
sass -C src/govuk/all.scss > /dev/null  16.91s user 0.59s system 99% cpu 17.565 total

LibSass

➜ sassc -v
sassc: 3.6.1
libsass: 3.6.3
sass2scss: 1.1.1
sass: 3.5

Before: ~0.20s

➜ time sassc src/govuk/all.scss > /dev/null
sassc src/govuk/all.scss > /dev/null  0.22s user 0.03s system 94% cpu 0.259 total

➜ time sassc src/govuk/all.scss > /dev/null
sassc src/govuk/all.scss > /dev/null  0.20s user 0.02s system 98% cpu 0.226 total

➜ time sassc src/govuk/all.scss > /dev/null
sassc src/govuk/all.scss > /dev/null  0.20s user 0.02s system 98% cpu 0.228 total

After: ~0.18s

govuk-frontend on  import-components-without-dependencies [$?] via ⬢ v12.13.1 
➜ time sassc src/govuk/all.scss > /dev/null
sassc src/govuk/all.scss > /dev/null  0.20s user 0.02s system 94% cpu 0.238 total

govuk-frontend on  import-components-without-dependencies [$?] via ⬢ v12.13.1 
➜ time sassc src/govuk/all.scss > /dev/null
sassc src/govuk/all.scss > /dev/null  0.18s user 0.02s system 98% cpu 0.204 total

govuk-frontend on  import-components-without-dependencies [$?] via ⬢ v12.13.1 
➜ time sassc src/govuk/all.scss > /dev/null
sassc src/govuk/all.scss > /dev/null  0.18s user 0.02s system 98% cpu 0.203 total

DartSass

/usr/local/Cellar/sass/1.23.7/bin/sass --version
1.23.7

Before: 0.5s

➜ time /usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null
/usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null  0.54s user 0.23s system 100% cpu 0.765 total

➜ time /usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null
/usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null  0.53s user 0.23s system 100% cpu 0.750 total

➜ time /usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null
/usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null  0.54s user 0.23s system 100% cpu 0.771 total

After: ~0.4s

➜ time /usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null
/usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null  0.42s user 0.18s system 95% cpu 0.627 total

➜ time /usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null
/usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null  0.41s user 0.17s system 100% cpu 0.580 total

➜ time /usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null
/usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null  0.41s user 0.17s system 100% cpu 0.580 total

@36degrees
Copy link
Contributor Author

This is very similar to #1752, which was closed. For context:

@36degrees
Copy link
Contributor Author

There's a pre-release of this available for testing:

npm install --save alphagov/govuk-frontend#1699f846

@36degrees 36degrees closed this May 6, 2020
@36degrees 36degrees reopened this May 6, 2020
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1804 May 6, 2020 12:54 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1804 May 7, 2020 13:41 Inactive
Copy link
Member

@hannalaakso hannalaakso left a comment

Choose a reason for hiding this comment

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

This looks great @36degrees 👏 Just added a couple of small suggestions for the changelog. (Also, I don't know if there's anything we want to call out in the PR description that wasn't captured in the issue.)

CHANGELOG.md Outdated
If you currently import a subset of GOV.UK Frontend:

- you can now import `node-modules/govuk-frontend/govuk/base` instead of importing `node-modules/govuk-frontend/govuk/settings/all`, `node-modules/govuk-frontend/govuk/helpers/all` and `node-modules/govuk-frontend/govuk/tools/all`
- you should import components using their `index` file, which will avoid importing the dependencies again
Copy link
Member

Choose a reason for hiding this comment

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

Should we say that you can also just continue to import accordion/accordion (but won't get the improvements if you do)? In case someone doesn't want to do this right now.

CHANGELOG.md Outdated
If you currently import a subset of GOV.UK Frontend:

- you can now import `node-modules/govuk-frontend/govuk/base` instead of importing `node-modules/govuk-frontend/govuk/settings/all`, `node-modules/govuk-frontend/govuk/helpers/all` and `node-modules/govuk-frontend/govuk/tools/all`
- you should import components using their `index` file, which will avoid importing the dependencies again
Copy link
Member

@hannalaakso hannalaakso May 7, 2020

Choose a reason for hiding this comment

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

Wondering if this should be the first bullet point on the list? It's the one where users will see the most benefit.

This avoids importing the base layers again when components import other components as dependencies.

We need to explicitly import `accordion/index` file, rather than just importing `accordion`, as index file support has only been in LibSass since 3.6.0 and Ruby Sass since 3.6.0[1], and we support 3.3.0 and 1.0.0 respectively.

[1]: https://sass-lang.com/documentation/at-rules/import#index-files
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1804 May 12, 2020 08:17 Inactive
@36degrees
Copy link
Contributor Author

Benchmarks after fixing imports of dependent components in 3e471f8:

RubySass: ~6.5s

➜ time sass -C src/govuk/all.scss > /dev/null
sass -C src/govuk/all.scss > /dev/null  6.38s user 0.39s system 94% cpu 7.167 total

➜ time sass -C src/govuk/all.scss > /dev/null
sass -C src/govuk/all.scss > /dev/null  6.37s user 0.35s system 99% cpu 6.736 total

➜ time sass -C src/govuk/all.scss > /dev/null
sass -C src/govuk/all.scss > /dev/null  6.65s user 0.36s system 99% cpu 7.029 total

LibSass: ~0.16s

➜ time sassc src/govuk/all.scss > /dev/null
sassc src/govuk/all.scss > /dev/null  0.15s user 0.02s system 94% cpu 0.185 total

➜ time sassc src/govuk/all.scss > /dev/null
sassc src/govuk/all.scss > /dev/null  0.16s user 0.02s system 98% cpu 0.175 total

➜ time sassc src/govuk/all.scss > /dev/null
sassc src/govuk/all.scss > /dev/null  0.16s user 0.02s system 98% cpu 0.176 total

DartSass: ~0.23s

➜ time /usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null
/usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null  0.23s user 0.11s system 93% cpu 0.370 total

➜ time /usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null
/usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null  0.23s user 0.11s system 101% cpu 0.333 total

➜ time /usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null
/usr/local/Cellar/sass/1.23.7/bin/sass src/govuk/all.scss > /dev/null  0.23s user 0.10s system 102% cpu 0.331 total

Copy link
Member

@hannalaakso hannalaakso left a comment

Choose a reason for hiding this comment

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

Even better @36degrees 😄

@36degrees 36degrees merged commit 22bbe47 into master May 12, 2020
@36degrees 36degrees deleted the import-components-without-dependencies branch May 12, 2020 15:26
36degrees added a commit that referenced this pull request May 20, 2020
36degrees added a commit that referenced this pull request May 20, 2020
Add deprecation in #1807 to changelog, and add missing PR link for #1804
@36degrees 36degrees mentioned this pull request Jun 1, 2020
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.

Allow components to be imported with or without dependencies
3 participants