Skip to content

Commit

Permalink
Ensure ESM *.mjs files are bundled to *.js
Browse files Browse the repository at this point in the history
This extension can be changed back to `*.mjs` once native ES modules are shipped
  • Loading branch information
colinrotherham committed Mar 3, 2023
1 parent 9e5ea37 commit c48bfc9
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 25 deletions.
9 changes: 8 additions & 1 deletion lib/rollup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const plugin = (modulePath, moduleName) => async (files, metalsmith, done) => {
modulePath = normalize(modulePath)

// Metalsmith file paths
const file = modulePath
const file = modulePath.replace(/\.mjs$/, '.js')
const map = `${file}.map`

try {
Expand All @@ -31,6 +31,7 @@ const plugin = (modulePath, moduleName) => async (files, metalsmith, done) => {

// Compile Rollup bundle
const result = await bundle.generate({
file: modulePath,
sourcemapFile: file,
sourcemap: true,

Expand Down Expand Up @@ -72,6 +73,12 @@ const plugin = (modulePath, moduleName) => async (files, metalsmith, done) => {
contents: Buffer.from(minified.map)
}

// Remove source module from output when
// file path changes (e.g. `.mjs` to `.js`)
if (modulePath !== file) {
delete files[modulePath]
}

done()
} catch (error) {
done(error)
Expand Down
15 changes: 10 additions & 5 deletions lib/rollup/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,23 @@ describe('Rollup plugin', () => {
})

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

expect(output[normalize('javascripts/example.mjs')])
.toMatchObject({ contents: expect.any(Buffer) })
it('deletes JavaScript source files', () => {
expect(output['javascripts/entry.mjs']).toBeUndefined()
expect(output['javascripts/example.mjs']).toBeUndefined()

// Only the entry point (bundled) is output
expect(output['javascripts/entry.js']).not.toBeUndefined()
})

it('bundles ESM modules to single IIFE file (minified)', () => {
expect(output[normalize('javascripts/entry.mjs')].contents.toString())
expect(output[normalize('javascripts/entry.js')].contents.toString())
.toEqual(outdent`
var entry=function(){"use strict";return()=>"example"}();
//# sourceMappingURL=entry.mjs.map
//# sourceMappingURL=entry.js.map
`)
})
})
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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<!--[if !IE 8]><!-->
<script src="{{ getFingerprint('javascripts/application.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/application.js') }}"></script>
<!--<![endif]-->
<!--[if lt IE 9]>
<script src="{{ getFingerprint('javascripts/vendor/ie8.polyfils.min.js') }}"></script>
<script src="{{ getFingerprint('javascripts/application-ie8.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/application-ie8.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/example.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/example.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/example.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/example.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/head-ie8.js') }}"></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.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/example.mjs') }}"></script>
<script src="{{ getFingerprint('javascripts/govuk-frontend.js') }}"></script>
<script src="{{ getFingerprint('javascripts/example.js') }}"></script>

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

0 comments on commit c48bfc9

Please sign in to comment.