-
Notifications
You must be signed in to change notification settings - Fork 320
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
Fix tree-shaking when importing govuk-frontend
#4961
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9deaf52
Mark GOV.UK Frontend as free of side effects
romaricpascal 69e8362
Add bundler integrations script for Rollup
romaricpascal 2b5444d
Add bundler integration for Webpack
romaricpascal 462b57b
Add vite bundler integration script
romaricpascal 37d73e8
Add GitHub workflow for checking integration with bundlers
romaricpascal f660509
Add clean script to bundler integrations workspace
romaricpascal 80b0329
Add script to run all bundler integrations at once
romaricpascal 604182a
Document npm tasks for bundler integrations
romaricpascal 775a3a7
Amend bundle tests to also test against `initAll` importing
owenatgov 9ed92a5
Add changelog entry
owenatgov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: Bundler integrations | ||
|
||
on: | ||
workflow_call: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
test-tree-shaking: | ||
name: Test tree shaking | ||
runs-on: ubuntu-latest | ||
|
||
env: | ||
PUPPETEER_SKIP_DOWNLOAD: true | ||
|
||
strategy: | ||
fail-fast: false | ||
|
||
matrix: | ||
bundler: | ||
- rollup | ||
- webpack | ||
- vite | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4.1.4 | ||
|
||
- name: Restore dependencies | ||
uses: ./.github/workflows/actions/install-node | ||
|
||
- name: Build GOV.UK Frontend | ||
uses: ./.github/workflows/actions/build | ||
|
||
- name: Build with bundler | ||
run: npm run ${{matrix.bundler}} -w @govuk-frontend/bundler-integrations | ||
|
||
# Check output for modules that should not be included | ||
- name: Check output | ||
working-directory: ./.github/workflows/bundler-integrations | ||
run: | | ||
! grep "Accordion" dist/${{matrix.bundler}}/single-component.js -q | ||
grep "Accordion" dist/${{matrix.bundler}}/initAll.js -q |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@govuk-frontend/bundler-integrations", | ||
"description": "Boilerplate to verify that GOV.UK Frontend works OK with main bundlers", | ||
"private": true, | ||
"scripts": { | ||
"rollup": "npm run rollup:single-component && npm run rollup:initAll", | ||
"rollup:single-component": "npm run rollup:cli -- -o dist/rollup/single-component.js ./src/single-component.mjs", | ||
"rollup:initAll": "npm run rollup:cli -- -o dist/rollup/initAll.js ./src/initAll.mjs", | ||
"rollup:cli": "rollup -c rollup.config.mjs", | ||
"webpack": "webpack --mode production -o dist/webpack", | ||
"vite": "vite build", | ||
"clean": "del-cli dist", | ||
"build:all": "concurrently \"npm run rollup\" \"npm run webpack\" \"npm run vite\" --names \"rollup,webpack,vite\" --prefix-colors \"red.dim,blue.dim,yellow.dim\"" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"concurrently": "^8.2.2", | ||
"del-cli": "^5.1.0", | ||
"govuk-frontend": "*", | ||
"rollup": "^4.17.2", | ||
"vite": "^5.2.10", | ||
"webpack": "^5.91.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import resolve from '@rollup/plugin-node-resolve' | ||
|
||
/** @type {import('rollup').RollupOptions} */ | ||
export default { | ||
plugins: [resolve()] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// An example of importing and initialising allcomponents via initAll | ||
import { initAll } from 'govuk-frontend' | ||
|
||
initAll() |
9 changes: 9 additions & 0 deletions
9
.github/workflows/bundler-integrations/src/single-component.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// An example of importing a single component from govuk-frontend and initialising it | ||
import { Button } from 'govuk-frontend' | ||
|
||
const $buttons = document.querySelectorAll('[data-module="govuk-button"]') | ||
|
||
$buttons.forEach(($button) => { | ||
/* eslint-disable-next-line no-new */ | ||
new Button($button) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** @type {import('vite').UserConfig} */ | ||
// the default vite config used by the different test case configs | ||
|
||
export default { | ||
build: { | ||
// Align output with other bundlers to facilitate testing | ||
outDir: 'dist/vite', | ||
assetsDir: '.', | ||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment By default, Vite would output in |
||
// Prevent minification so we can see actual class/function names | ||
minify: false, | ||
rollupOptions: { | ||
input: ['./src/single-component.mjs', './src/initAll.mjs'], | ||
output: { | ||
entryFileNames: '[name].js' | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** @type {import('webpack').Configuration} */ | ||
module.exports = { | ||
optimization: { | ||
// Configure WebPack to drop exports that are not used from the code | ||
// so we can disable Terser and still check what's being included | ||
usedExports: true, | ||
// Prevent minification so we can see what's going on in the built file | ||
minimize: false | ||
}, | ||
target: 'web', | ||
entry: { | ||
'single-component': './src/single-component.mjs', | ||
initAll: './src/initAll.mjs' | ||
}, | ||
output: { | ||
filename: '[name].js' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment Rollup needs a plugin to resolve package from
node_modules
.