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

Migrate to use <script type="module"> #3769

Merged
merged 6 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/examples/webpack/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion docs/examples/webpack/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
</main>
</div>

<script src="/assets/javascripts/main.min.js"></script>
<script type="module" src="/assets/javascripts/main.min.js"></script>
</body>
</html>
5 changes: 2 additions & 3 deletions docs/examples/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ module.exports = ({ WEBPACK_SERVE }, { mode }) => ({
format: { comments: false },

// Compatibility workarounds
ecma: 5,
safari10: true
}
})]
Expand All @@ -85,7 +84,7 @@ module.exports = ({ WEBPACK_SERVE }, { mode }) => ({
output: {
clean: true,
filename: 'assets/javascripts/[name].min.js',
library: { type: 'umd' },
iife: true,
path: destPath,
publicPath: '/'
},
Expand All @@ -108,5 +107,5 @@ module.exports = ({ WEBPACK_SERVE }, { mode }) => ({
errorDetails: true
},

target: ['web', 'es5']
target: ['web', 'es2015']
})
1 change: 0 additions & 1 deletion packages/govuk-frontend-review/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export default defineConfig(({ i: input }) => ({
},

// Compatibility workarounds
ecma: 5,
safari10: true
})
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
{% endblock %}

{% block scripts %}
<script src="/vendor/iframe-resizer/iframeResizer.contentWindow.min.js"></script>
<script src="/vendor/iframe-resizer/iframeResizer.contentWindow.min.js" defer></script>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
{% endblock %}

{% block bodyEnd %}
<script src="/javascripts/all.min.js"></script>
<script>
<script type="module" src="/javascripts/all.min.js"></script>
<script type="module">
var $scope = document.getElementById('scoped')
window.GOVUKFrontend.initAll({
scope: $scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@

{% block bodyEnd %}
<!-- block:bodyEnd -->
<script src="/javascripts/all.min.js"></script>
<script>window.GOVUKFrontend.initAll()</script>
<script type="module" src="/javascripts/all.min.js"></script>
<script type="module">window.GOVUKFrontend.initAll()</script>
<!-- endblock:bodyEnd -->
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
<!-- endblock:content -->
{% endblock %}


{% block bodyEnd %}
<script src="/javascripts/all.min.js"></script>
<script>window.GOVUKFrontend.initAll()</script>
<!-- block:bodyEnd -->
<script type="module" src="/javascripts/all.min.js"></script>
<script type="module">window.GOVUKFrontend.initAll()</script>
<!-- endblock:bodyEnd -->
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -937,8 +937,8 @@
{% endblock %}

{% block bodyEnd %}
<script src="/javascripts/all.min.js"></script>
<script>
<script type="module" src="/javascripts/all.min.js"></script>
<script type="module">
window.GOVUKFrontend.initAll({
accordion: {
i18n: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ notes: >-
</div>
</div>

<script>
<script type="module">
var acceptButton = document.querySelector('.js-cookies-button-accept')
var rejectButton = document.querySelector('.js-cookies-button-reject')

Expand Down Expand Up @@ -226,11 +226,11 @@ notes: >-
event.preventDefault()
})

acceptedBanner.querySelector('.js-hide').addEventListener('click', function() {
acceptedBanner.querySelector('.js-hide').addEventListener('click', function () {
cookieBanner.setAttribute('hidden', 'hidden')
})

rejectedBanner.querySelector('.js-hide').addEventListener('click', function() {
rejectedBanner.querySelector('.js-hide').addEventListener('click', function () {
cookieBanner.setAttribute('hidden', 'hidden')
})
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ notes: >-
{# As the cookie banner component does not currently include JavaScript, we
have to take care of the 'Hide' button to make progressive enhancement in
the example functional #}
<script>
<script type="module">
var hideButton = document.querySelector('.js-hide')
var cookieBanner = document.querySelector(".govuk-cookie-banner")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ notes: >-
{# As the cookie banner component does not currently include JavaScript, we
have to take care of the 'Hide' button to make progressive enhancement in
the example functional #}
<script>
<script type="module">
var hideButton = document.querySelector('.js-hide')
var cookieBanner = document.querySelector(".govuk-cookie-banner")
var confirmationBanner = document.querySelector('.js-confirmation-banner')
Expand Down
4 changes: 2 additions & 2 deletions packages/govuk-frontend-review/src/views/layouts/_generic.njk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
{% set mainClasses = 'govuk-main-wrapper--auto-spacing' %}

{% block bodyEnd %}
<script src="/javascripts/all.min.js"></script>
<script>window.GOVUKFrontend.initAll()</script>
<script type="module" src="/javascripts/all.min.js"></script>
<script type="module">window.GOVUKFrontend.initAll()</script>
{% endblock %}
6 changes: 4 additions & 2 deletions packages/govuk-frontend-review/src/views/layouts/layout.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
{% block bodyEnd %}
{{ super() }}
{% block scripts %}
<script src="/vendor/iframe-resizer/iframeResizer.min.js"></script>
<script src="/vendor/iframe-resizer/iframeResizer.min.js" defer></script>
<script>
window.iFrameResize({}, '.js-component-preview')
document.addEventListener('DOMContentLoaded', function () {
window.iFrameResize({}, '.js-component-preview')
})
</script>
{% endblock %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
Used during testing to inject rendered components and test specific configurations
</p>
<div id="slot"></div>
<script src="/javascripts/all.min.js"></script>
<script type="module" src="/javascripts/all.min.js"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions packages/govuk-frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ initialising Javascript in your application to ensure that all users can use it
You can include Javascript for all components either by copying the `all.js` from `node_modules/govuk-frontend/dist/govuk/` into your application or referencing the file directly:

```html
<script src="<path-to-govuk-frontend-all-file>/all.js"></script>
<script type="module" src="<path-to-govuk-frontend-all-file>/all.js"></script>
```

Next you need to initialise the script by adding:

```html
<script>window.GOVUKFrontend.initAll()</script>
<script type="module">window.GOVUKFrontend.initAll()</script>
```

[More details on importing Javascript and advanced options](https://frontend.design-system.service.gov.uk/importing-css-assets-and-javascript/#javascript)
Expand Down
1 change: 0 additions & 1 deletion packages/govuk-frontend/rollup.release.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export default defineConfig(({ i: input }) => ({
},

// Compatibility workarounds
ecma: 5,
safari10: true
})
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ CharacterCount.prototype.init = function () {
this.bindChangeEvents()

// When the page is restored after navigating 'back' in some browsers the
// state of the character count is not restored until *after* the
// DOMContentLoaded event is fired, so we need to manually update it after the
// pageshow event.
// state of form controls is not restored until *after* the DOMContentLoaded
// event is fired, so we need to sync after the pageshow event.
window.addEventListener('pageshow', this.updateCountMessage.bind(this))

// Although we've set up handlers to sync state on the pageshow event, init
// could be called after those events have fired, for example if they are
// added to the page dynamically, so update now too.
this.updateCountMessage()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ Checkboxes.prototype.init = function () {
// event is fired, so we need to sync after the pageshow event.
window.addEventListener('pageshow', this.syncAllConditionalReveals.bind(this))

// Although we've set up handlers to sync state on the pageshow or
// DOMContentLoaded event, init could be called after those events have fired,
// for example if they are added to the page dynamically, so sync now too.
// Although we've set up handlers to sync state on the pageshow event, init
// could be called after those events have fired, for example if they are
// added to the page dynamically, so sync now too.
this.syncAllConditionalReveals()

// Handle events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ Radios.prototype.init = function () {
// event is fired, so we need to sync after the pageshow event.
window.addEventListener('pageshow', this.syncAllConditionalReveals.bind(this))

// Although we've set up handlers to sync state on the pageshow or
// DOMContentLoaded event, init could be called after those events have fired,
// for example if they are added to the page dynamically, so sync now too.
// Although we've set up handlers to sync state on the pageshow event, init
// could be called after those events have fired, for example if they are
// added to the page dynamically, so sync now too.
this.syncAllConditionalReveals()

// Handle events
Expand Down