Skip to content

Commit

Permalink
Make all JavaScript files .mjs by default, and transform to .js using…
Browse files Browse the repository at this point in the history
… rollup

Allows us to add file extensions to the end of our JavaScript imports, which means
our ESM code has "fully specified" import paths as required:

A file extension must be provided when using the import keyword to resolve relative or absolute specifiers. Directory indexes (e.g. './startup/index.js') must also be fully specified.
https://nodejs.org/api/esm.html#mandatory-file-extensions
  • Loading branch information
Vanita Barrett-Smith committed Jun 14, 2022
1 parent e094dc1 commit 4ef5bb2
Show file tree
Hide file tree
Showing 30 changed files with 73 additions and 76 deletions.
1 change: 1 addition & 0 deletions config/paths.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"sassdoc": "sassdoc/",
"src": "src/govuk/",
"components": "src/govuk/components/",
"vendor": "src/govuk/vendor",
"ports": {
"app": 3000,
"test": 8888
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"/dist/**/*.js",
"/package/**/*.js",
"/package/**/*.mjs",
"/src/govuk/vendor/polyfills/**/*.js"
"/src/govuk/vendor/polyfills/**/*.mjs"
]
},
"jest": {
Expand Down
24 changes: 12 additions & 12 deletions src/govuk/all.js → src/govuk/all.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { nodeListForEach } from './common'
import Accordion from './components/accordion/accordion'
import Button from './components/button/button'
import Details from './components/details/details'
import CharacterCount from './components/character-count/character-count'
import Checkboxes from './components/checkboxes/checkboxes'
import ErrorSummary from './components/error-summary/error-summary'
import NotificationBanner from './components/notification-banner/notification-banner'
import Header from './components/header/header'
import Radios from './components/radios/radios'
import SkipLink from './components/skip-link/skip-link'
import Tabs from './components/tabs/tabs'
import { nodeListForEach } from './common.mjs'
import Accordion from './components/accordion/accordion.mjs'
import Button from './components/button/button.mjs'
import Details from './components/details/details.mjs'
import CharacterCount from './components/character-count/character-count.mjs'
import Checkboxes from './components/checkboxes/checkboxes.mjs'
import ErrorSummary from './components/error-summary/error-summary.mjs'
import NotificationBanner from './components/notification-banner/notification-banner.mjs'
import Header from './components/header/header.mjs'
import Radios from './components/radios/radios.mjs'
import SkipLink from './components/skip-link/skip-link.mjs'
import Tabs from './components/tabs/tabs.mjs'

function initAll (options) {
// Set the options to an empty object by default if no options are passed.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import { nodeListForEach } from '../../common'
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Element/prototype/classList'
import { nodeListForEach } from '../../common.mjs'
import '../../vendor/polyfills/Function/prototype/bind.mjs'
import '../../vendor/polyfills/Element/prototype/classList.mjs'

function Accordion ($module) {
this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../../vendor/polyfills/Event' // addEventListener and event.target normaliziation
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Event.mjs' // addEventListener and event.target normaliziation
import '../../vendor/polyfills/Function/prototype/bind.mjs'

var KEY_SPACE = 32
var DEBOUNCE_TIMEOUT_IN_SECONDS = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Event' // addEventListener and event.target normaliziation
import '../../vendor/polyfills/Element/prototype/classList'
import '../../vendor/polyfills/Function/prototype/bind.mjs'
import '../../vendor/polyfills/Event.mjs' // addEventListener and event.target normaliziation
import '../../vendor/polyfills/Element/prototype/classList.mjs'

function CharacterCount ($module) {
this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Function/prototype/bind.mjs'
// addEventListener, event.target normalization and DOMContentLoaded
import '../../vendor/polyfills/Event'
import '../../vendor/polyfills/Element/prototype/classList'
import { nodeListForEach } from '../../common'
import '../../vendor/polyfills/Event.mjs'
import '../../vendor/polyfills/Element/prototype/classList.mjs'
import { nodeListForEach } from '../../common.mjs'

function Checkboxes ($module) {
this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*
* http://caniuse.com/#feat=details
*/
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Event' // addEventListener and event.target normaliziation
import { generateUniqueID } from '../../common'
import '../../vendor/polyfills/Function/prototype/bind.mjs'
import '../../vendor/polyfills/Event.mjs' // addEventListener and event.target normaliziation
import { generateUniqueID } from '../../common.mjs'

var KEY_ENTER = 13
var KEY_SPACE = 32
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Event' // addEventListener
import '../../vendor/polyfills/Element/prototype/closest'
import '../../vendor/polyfills/Function/prototype/bind.mjs'
import '../../vendor/polyfills/Event.mjs' // addEventListener
import '../../vendor/polyfills/Element/prototype/closest.mjs'

function ErrorSummary ($module) {
this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '../../vendor/polyfills/Event'
import '../../vendor/polyfills/Element/prototype/classList'
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Event.mjs'
import '../../vendor/polyfills/Element/prototype/classList.mjs'
import '../../vendor/polyfills/Function/prototype/bind.mjs'

function Header ($module) {
this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '../../vendor/polyfills/Event' // addEventListener
import '../../vendor/polyfills/Event.mjs' // addEventListener

function NotificationBanner ($module) {
this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Function/prototype/bind.mjs'
// addEventListener, event.target normalization and DOMContentLoaded
import '../../vendor/polyfills/Event'
import '../../vendor/polyfills/Element/prototype/classList'
import { nodeListForEach } from '../../common'
import '../../vendor/polyfills/Event.mjs'
import '../../vendor/polyfills/Element/prototype/classList.mjs'
import { nodeListForEach } from '../../common.mjs'

function Radios ($module) {
this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Element/prototype/classList'
import '../../vendor/polyfills/Event' // addEventListener and event.target normalization
import '../../vendor/polyfills/Function/prototype/bind.mjs'
import '../../vendor/polyfills/Element/prototype/classList.mjs'
import '../../vendor/polyfills/Event.mjs' // addEventListener and event.target normalization

function SkipLink ($module) {
this.$module = $module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Element/prototype/classList'
import '../../vendor/polyfills/Element/prototype/nextElementSibling'
import '../../vendor/polyfills/Element/prototype/previousElementSibling'
import '../../vendor/polyfills/Event' // addEventListener and event.target normaliziation
import { nodeListForEach } from '../../common'
import '../../vendor/polyfills/Function/prototype/bind.mjs'
import '../../vendor/polyfills/Element/prototype/classList.mjs'
import '../../vendor/polyfills/Element/prototype/nextElementSibling.mjs'
import '../../vendor/polyfills/Element/prototype/previousElementSibling.mjs'
import '../../vendor/polyfills/Event.mjs' // addEventListener and event.target normaliziation
import { nodeListForEach } from '../../common.mjs'

function Tabs ($module) {
this.$module = $module
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import './Document'
import './Document.mjs'

(function(undefined) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '../../Object/defineProperty'
import '../../DOMTokenList'
import '../../Element'
import '../../Object/defineProperty.mjs'
import '../../DOMTokenList.mjs'
import '../../Element.mjs'

(function(undefined) {

Expand Down Expand Up @@ -90,4 +90,4 @@ import '../../Element'
addProp(global.HTMLAreaElement, "relList", "rel");
}(this));

}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import './matches'
import './matches.mjs'

(function(undefined) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../../Object/defineProperty'
import '../../Element'
import '../../Object/defineProperty.mjs'
import '../../Element.mjs'

(function(undefined) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../../Object/defineProperty'
import '../../Element'
import '../../Object/defineProperty.mjs'
import '../../Element.mjs'

(function(undefined) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './Window'
import './Element'
import './Object/defineProperty'
import './Window.mjs'
import './Element.mjs'
import './Object/defineProperty.mjs'

(function(undefined) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '../../Object/defineProperty'
import '../../Object/defineProperty.mjs'

(function(undefined) {
// Detection from https://github.com/Financial-Times/polyfill-service/blob/master/packages/polyfill-library/polyfills/Function/prototype/bind/detect.js
Expand Down
File renamed without changes.
9 changes: 3 additions & 6 deletions tasks/gulp/__tests__/after-build-package.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,11 @@ describe('package/', () => {
var fileWithoutSrc = file.replace(/^src\//, '')

// Account for govuk-esm folder
if (fileWithoutSrc.split('.').pop() === 'js') {
if (fileWithoutSrc.split('.').pop() === 'mjs') {
var esmFile = fileWithoutSrc.replace('govuk/', 'govuk-esm/')
var umdFile = fileWithoutSrc.replace('.mjs', '.js')

if (!esmFile.includes('vendor/')) {
esmFile = esmFile.replace('.js', '.mjs')
}

return [fileWithoutSrc, esmFile]
return [umdFile, esmFile]
} else {
return fileWithoutSrc
}
Expand Down
5 changes: 4 additions & 1 deletion tasks/gulp/compile-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ gulp.task('scss:compile', function (done) {
// --------------------------------------
gulp.task('js:compile', (done) => {
// For dist/ folder we only want compiled 'all.js'
const fileLookup = isDist ? configPaths.src + 'all.js' : configPaths.src + '**/!(*.test).js'
const fileLookup = isDist ? configPaths.src + 'all.mjs' : configPaths.src + '**/!(*.test).mjs'

// Perform a synchronous search and return an array of matching file names
const srcFiles = glob.sync(fileLookup)
Expand Down Expand Up @@ -225,6 +225,9 @@ gulp.task('js:compile', (done) => {
extname: '.min.js'
})
))
.pipe(rename({
extname: '.js'
}))
.pipe(eol())
.pipe(gulp.dest(destinationPath() + newDirectoryPath))
})
Expand Down
10 changes: 3 additions & 7 deletions tasks/gulp/copy-to-destination.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ gulp.task('copy-files', () => {
return gulp.src([
configPaths.src + '**/*',
'!**/.DS_Store',
'!**/*.test.js',
'!**/*.mjs',
'!**/*.test.*',
'!' + configPaths.src + 'README.md', // Don't override the existing README in /package
'!' + configPaths.components + '**/__snapshots__/**',
'!' + configPaths.components + '**/__snapshots__/'
Expand Down Expand Up @@ -86,12 +87,7 @@ function generateFixtures (file) {
}

gulp.task('js:copy-esm', () => {
return gulp.src([configPaths.src + '**/*.js', '!' + configPaths.src + '/**/*.test.js'])
.pipe(rename(function (path) {
if (!path.dirname.includes('vendor')) {
path.extname = '.mjs'
}
}))
return gulp.src([configPaths.src + '**/*.mjs', configPaths.src + '**/*.js', '!' + configPaths.src + '/**/*.test.js'])
.pipe(gulp.dest(taskArguments.destination + '/govuk-esm/'))
})

Expand Down

0 comments on commit 4ef5bb2

Please sign in to comment.