Skip to content

Commit

Permalink
Restrict ESM syntax to *.mjs files only
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Mar 1, 2023
1 parent ff38f6d commit 2b7a382
Show file tree
Hide file tree
Showing 30 changed files with 60 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* @jest-environment jsdom
*/

import * as CookieHelpers from '../src/javascripts/components/cookie-functions'
import * as Analytics from '../src/javascripts/components/analytics'
import * as CookieHelpers from '../src/javascripts/components/cookie-functions.mjs'
import * as Analytics from '../src/javascripts/components/analytics.mjs'

jest.mock('../src/javascripts/components/analytics')

describe('Cookie settings', () => {
Expand Down
8 changes: 4 additions & 4 deletions lib/metalsmith.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ module.exports = metalsmith(path.resolve(__dirname, '../'))
.use(hashAssets({
pattern: [
'**/*.css?(.map)',
'javascripts/application?(-ie8).js?(.map)',
'javascripts/example.js?(.map)',
'javascripts/govuk-frontend.js?(.map)',
'javascripts/head-ie8.js?(.map)',
'javascripts/application?(-ie8).mjs?(.map)',
'javascripts/example.mjs?(.map)',
'javascripts/govuk-frontend.mjs?(.map)',
'javascripts/head-ie8.mjs?(.map)',
'javascripts/vendor/*'
]
}))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import example from './example.js'
import example from './example.mjs'

export default () => {
return example
Expand Down
10 changes: 5 additions & 5 deletions lib/rollup/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Rollup plugin', () => {
.destination(destination)

// Rollup example
.use(rollup('javascripts/entry.js', 'entry'))
.use(rollup('javascripts/entry.mjs', 'entry'))

// Build
.build((err, files) => {
Expand All @@ -33,18 +33,18 @@ describe('Rollup plugin', () => {
})

it('compiles JavaScript to Metalsmith files', () => {
expect(output[normalize('javascripts/entry.js')])
expect(output[normalize('javascripts/entry.mjs')])
.toMatchObject({ contents: expect.any(Buffer) })

expect(output[normalize('javascripts/example.js')])
expect(output[normalize('javascripts/example.mjs')])
.toMatchObject({ contents: expect.any(Buffer) })
})

it('bundles ESM modules to single IIFE file (minified)', () => {
expect(output[normalize('javascripts/entry.js')].contents.toString())
expect(output[normalize('javascripts/entry.mjs')].contents.toString())
.toEqual(outdent`
var entry=function(){"use strict";return()=>"example"}();
//# sourceMappingURL=entry.js.map
//# sourceMappingURL=entry.mjs.map
`)
})
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { nodeListForEach } from './components/helpers.js'
import Example from './components/example.js'
import AppTabs from './components/tabs.js'
import OptionsTable from './components/options-table.js'
import { nodeListForEach } from './components/helpers.mjs'
import Example from './components/example.mjs'
import AppTabs from './components/tabs.mjs'
import OptionsTable from './components/options-table.mjs'

// Initialise example frames
var $examples = document.querySelectorAll('[data-module="app-example-frame"]')
Expand Down
24 changes: 12 additions & 12 deletions src/javascripts/application.js → src/javascripts/application.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import CookieBanner from './components/cookie-banner.js'
import BackToTop from './components/back-to-top.js'
import { nodeListForEach } from './components/helpers.js'
import Example from './components/example.js'
import AppTabs from './components/tabs.js'
import Copy from './components/copy.js'
import Navigation from './components/navigation.js'
import Search from './components/search.js'
import OptionsTable from './components/options-table.js'
import { getConsentCookie, isValidConsentCookie } from './components/cookie-functions.js'
import Analytics from './components/analytics.js'
import CookiesPage from './components/cookies-page.js'
import CookieBanner from './components/cookie-banner.mjs'
import BackToTop from './components/back-to-top.mjs'
import { nodeListForEach } from './components/helpers.mjs'
import Example from './components/example.mjs'
import AppTabs from './components/tabs.mjs'
import Copy from './components/copy.mjs'
import Navigation from './components/navigation.mjs'
import Search from './components/search.mjs'
import OptionsTable from './components/options-table.mjs'
import { getConsentCookie, isValidConsentCookie } from './components/cookie-functions.mjs'
import Analytics from './components/analytics.mjs'
import CookiesPage from './components/cookies-page.mjs'

// Initialise cookie banner
var $cookieBanner = document.querySelector('[data-module="govuk-cookie-banner"]')
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as CookieFunctions from './cookie-functions.js'
import * as CookieFunctions from './cookie-functions.mjs'
import 'govuk-frontend/govuk/vendor/polyfills/Function/prototype/bind'
import 'govuk-frontend/govuk/vendor/polyfills/Event'
import { nodeListForEach } from './helpers.js'
import { nodeListForEach } from './helpers.mjs'

var cookieBannerAcceptSelector = '.js-cookie-banner-accept'
var cookieBannerRejectSelector = '.js-cookie-banner-reject'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* The consent cookie version is defined in cookie-banner.njk
*/

import Analytics from './analytics.js'
import Analytics from './analytics.mjs'

/* Name of the cookie to save users cookie preferences to. */
var CONSENT_COOKIE_NAME = 'design_system_cookies_policy'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getConsentCookie, setConsentCookie } from './cookie-functions.js'
import { nodeListForEach } from './helpers.js'
import { getConsentCookie, setConsentCookie } from './cookie-functions.mjs'
import { nodeListForEach } from './helpers.mjs'

function CookiesPage ($module) {
this.$module = $module
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
* This seems to fail in IE8, requires more investigation.
* See: https://github.com/imagitama/nodelist-foreach-polyfill
*/
function nodeListForEach (nodes, callback) {
export function nodeListForEach (nodes, callback) {
if (window.NodeList.prototype.forEach) {
return nodes.forEach(callback)
}
for (var i = 0; i < nodes.length; i++) {
callback.call(window, nodes[i], i, nodes)
}
}

export {
nodeListForEach
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'govuk-frontend/govuk/vendor/polyfills/Element/prototype/classList'
import { nodeListForEach } from './helpers.js'
import { nodeListForEach } from './helpers.mjs'

var navActiveClass = 'app-navigation--active'
var navMenuButtonActiveClass = 'govuk-header__menu-button--open'
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import accessibleAutocomplete from 'accessible-autocomplete'
import lunr from 'lunr'

import { trackSearchResults, trackConfirm } from './search.tracking.js'
import { trackSearchResults, trackConfirm } from './search.tracking.mjs'

// CONSTANTS
var TIMEOUT = 10 // Time to wait before giving up fetching the search index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function stripPossiblePII (string) {
return string
}

function trackConfirm (searchQuery, searchResults, result) {
export function trackConfirm (searchQuery, searchResults, result) {
if (window.DO_NOT_TRACK_ENABLED) {
return
}
Expand Down Expand Up @@ -51,7 +51,7 @@ function trackConfirm (searchQuery, searchResults, result) {
})
}

function trackSearchResults (searchQuery, searchResults) {
export function trackSearchResults (searchQuery, searchResults) {
if (window.DO_NOT_TRACK_ENABLED) {
return
}
Expand Down Expand Up @@ -81,5 +81,3 @@ function trackSearchResults (searchQuery, searchResults) {
}
})
}

export { trackConfirm, trackSearchResults }
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'govuk-frontend/govuk/vendor/polyfills/Function/prototype/bind'
import 'govuk-frontend/govuk/vendor/polyfills/Element/prototype/classList'
import 'govuk-frontend/govuk/vendor/polyfills/Event'
import { nodeListForEach } from './helpers.js'
import { nodeListForEach } from './helpers.mjs'

/**
* The naming of things is a little complicated in here.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { initAll } from 'govuk-frontend'

initAll({
// auto focusing the error summary and notification banner is not useful
// when used in examples, and causes the viewport to scroll
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/styles/page-template/block-areas/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ stylesheets:
<!--<![endif]-->
<!--[if lt IE 9]>
<link href="{{ getFingerprint('stylesheets/main-ie8.css') }}" rel="stylesheet" media="all">
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.mjs') }}"></script>
<![endif]-->
<script src="{{ getFingerprint('javascripts/vendor/modernizr.js') }}"></script>
{% for stylesheet in stylesheets %}
Expand Down Expand Up @@ -88,7 +88,7 @@ stylesheets:
<span class="app-annotate-block__label">block: bodyEnd</span>
{{ super() }}
{# Since we’re not extending the Design System layout we need to add this manually #}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.mjs') }}"></script>

<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/vendor/ie8.polyfils.min.js') }}"></script>
Expand Down
4 changes: 2 additions & 2 deletions src/styles/page-template/custom/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ignore_in_sitemap: true
<!--<![endif]-->
<!--[if lt IE 9]>
<link href="{{ getFingerprint('stylesheets/main-ie8.css') }}" rel="stylesheet" media="all">
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.mjs') }}"></script>
<![endif]-->
<script src="{{ getFingerprint('javascripts/vendor/modernizr.js') }}"></script>
{% endblock %}
Expand Down Expand Up @@ -156,7 +156,7 @@ ignore_in_sitemap: true
{% endblock %}

{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.mjs') }}"></script>

<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/vendor/ie8.polyfils.min.js') }}"></script>
Expand Down
4 changes: 2 additions & 2 deletions src/styles/page-template/default/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ layout: false
<!--<![endif]-->
<!--[if lt IE 9]>
<link href="{{ getFingerprint('stylesheets/main-ie8.css') }}" rel="stylesheet" media="all">
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.mjs') }}"></script>
<![endif]-->
<script src="{{ getFingerprint('javascripts/vendor/modernizr.js') }}"></script>
{% endblock %}
Expand All @@ -21,7 +21,7 @@ layout: false
{% endblock %}

{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.mjs') }}"></script>

<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/vendor/ie8.polyfils.min.js') }}"></script>
Expand Down
8 changes: 4 additions & 4 deletions views/layouts/_generic.njk
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<link rel="canonical" href="{{ canonical }}">
<!--[if lt IE 9]>
<link href="{{ getFingerprint('stylesheets/main-ie8.css') }}" rel="stylesheet" media="all">
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.mjs') }}"></script>
<![endif]-->
<!--[if !IE 8]><!-->
<link href="{{ getFingerprint('stylesheets/main.css') }}" rel="stylesheet" media="all">
Expand Down Expand Up @@ -46,12 +46,12 @@
{% block footer %}{% endblock %}

{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.mjs') }}"></script>
<!--[if !IE 8]><!-->
<script src="{{ getFingerprint('javascripts/application.js') }}"></script>
<script src="{{ getFingerprint('javascripts/application.mjs') }}"></script>
<!--<![endif]-->
<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/vendor/ie8.polyfils.min.js') }}"></script>
<script src="{{ getFingerprint('javascripts/application-ie8.js') }}"></script>
<script src="{{ getFingerprint('javascripts/application-ie8.mjs') }}"></script>
<![endif]-->
{% endblock %}
6 changes: 3 additions & 3 deletions views/layouts/layout-example-full-page-govuk.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<link href="{{ getFingerprint(stylesheet) }}" rel="stylesheet" media="all">
{%- endfor %}
<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.mjs') }}"></script>
<![endif]-->
<script src="{{ getFingerprint('javascripts/vendor/modernizr.js') }}"></script>
{% endblock %}
Expand All @@ -26,8 +26,8 @@
{{ contents | safe }}
{% endblock %}
{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/example.js') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/example.mjs') }}"></script>

<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/vendor/ie8.polyfils.min.js') }}"></script>
Expand Down
6 changes: 3 additions & 3 deletions views/layouts/layout-example-full-page.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<link href="{{ getFingerprint(stylesheet) }}" rel="stylesheet" media="all">
{%- endfor %}
<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.mjs') }}"></script>
<![endif]-->
<script src="{{ getFingerprint('javascripts/vendor/modernizr.js') }}"></script>
{% endblock %}
Expand All @@ -28,8 +28,8 @@
{{ contents | safe }}
{% endblock %}
{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/example.js') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/example.mjs') }}"></script>

<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/vendor/ie8.polyfils.min.js') }}"></script>
Expand Down
6 changes: 3 additions & 3 deletions views/layouts/layout-example.njk
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{%- endfor %}

<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.mjs') }}"></script>
<![endif]-->
<script src="{{ getFingerprint('javascripts/vendor/modernizr.js') }}"></script>
{% endblock %}
Expand All @@ -40,8 +40,8 @@
{% endblock %}

{% block bodyEnd %}
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/example.js') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/example.mjs') }}"></script>

<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/vendor/ie8.polyfils.min.js') }}"></script>
Expand Down

0 comments on commit 2b7a382

Please sign in to comment.