-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4961 from alphagov/fix-tree-shaking
Fix tree-shaking when importing `govuk-frontend`
- Loading branch information
Showing
15 changed files
with
793 additions
and
120 deletions.
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: '.', | ||
// 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.