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

feat: add new lint:types npm script #1128

Draft
wants to merge 18 commits into
base: esmodules
Choose a base branch
from
Draft
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
10 changes: 6 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module.exports = {
extends: ['standard', 'prettier'],
globals: {
$: 'readonly',
jQuery: 'readonly'
},
ignorePatterns: [
'**/vendor/**',
'package/**',
Expand Down Expand Up @@ -90,6 +86,12 @@ module.exports = {
}
],

// Skip rules handled by TypeScript compiler
'n/no-extraneous-require': 'off',
'n/no-extraneous-import': 'off',
'n/no-missing-require': 'off',
'n/no-missing-import': 'off',

// Automatically use template strings
'no-useless-concat': 'error',
'prefer-template': 'error',
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ package/*
.env
.port.tmp
.vscode/
*.tsbuildinfo
npm-debug.log
sass-cache
settings.json
tmp-uploads


2 changes: 1 addition & 1 deletion .releaserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
[
"semantic-release-plugin-update-version-in-files",
{
"files": ["package/moj/all.js"],
"files": ["package/moj/all.js", "package/moj/all.mjs"],
"placeholder": "0.0.0-development"
}
]
Expand Down
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['@babel/preset-env']
}
2 changes: 1 addition & 1 deletion docs/assets/javascript/application.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as GOVUKFrontend from 'govuk-frontend'

import * as MOJFrontend from '../../../src/moj/all.js'
import * as MOJFrontend from '../../../src/moj/all.mjs'

import CollapsibleNav from './collapsible-nav.mjs'
import Cookies from './cookies.mjs'
Expand Down
6 changes: 3 additions & 3 deletions docs/assets/javascript/collapsible-nav.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class CollapsibleNav extends HTMLElement {
}

$button.setAttribute('aria-controls', $list.id)
$button.setAttribute('aria-expanded', true)
$button.setAttribute('aria-expanded', 'true')
$button.classList.add('app-vertical-nav__toggle')

$button.insertAdjacentHTML('afterbegin', $link.innerHTML)
Expand Down Expand Up @@ -60,7 +60,7 @@ export default class CollapsibleNav extends HTMLElement {

$list.hidden = false
$item.classList.add(this.openClass)
$button.setAttribute('aria-expanded', true)
$button.setAttribute('aria-expanded', 'true')
}

close($item) {
Expand All @@ -71,7 +71,7 @@ export default class CollapsibleNav extends HTMLElement {

$list.hidden = true
$item.classList.remove(this.openClass)
$button.setAttribute('aria-expanded', false)
$button.setAttribute('aria-expanded', 'false')
}

closeOpenItems() {
Expand Down
4 changes: 2 additions & 2 deletions docs/assets/javascript/menu-toggle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ export default class MenuToggle extends HTMLElement {

hideMenu() {
this.$menu.hidden = true
this.$button.setAttribute('aria-expanded', false)
this.$button.setAttribute('aria-expanded', 'false')
}

showMenu() {
this.$menu.hidden = false
this.$button.setAttribute('aria-expanded', true)
this.$button.setAttribute('aria-expanded', 'true')
}

get breakpoint() {
Expand Down
26 changes: 11 additions & 15 deletions docs/assets/javascript/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const Tabs = function (container) {
this.cssHide = 'app-tabs__panel--hidden'
this.tabs = container.find('.app-tabs__tab')
this.panels = container.find('.app-tabs__panel')
this.container.on('click', '[role=tab]', $.proxy(this, 'onTabClick'))
this.container.on('keydown', '[role=tab]', $.proxy(this, 'onTabKeydown'))
this.container.on('click', '[role=tab]', this.onTabClick.bind(this))
this.container.on('keydown', '[role=tab]', this.onTabKeydown.bind(this))
this.container.on(
'click',
'.app-tabs__close',
$.proxy(this, 'onCloseButtonClick')
this.onCloseButtonClick.bind(this)
)
this.setupHtml()
}
Expand Down Expand Up @@ -37,18 +37,14 @@ Tabs.prototype.setupHtml = function () {
this.container.find('.app-tabs__list-item').attr('role', 'presentation')
this.tabs.attr('role', 'tab')
this.panels.attr('role', 'tabpanel')
this.tabs.each(
$.proxy(function (i, tab) {
const panelId = this.getHref($(tab)).slice(1)
tab.id = `tab_${panelId}`
$(tab).attr('aria-controls', panelId)
}, this)
)
this.panels.each(
$.proxy(function (i, panel) {
$(panel).attr('aria-labelledby', this.tabs[i].id)
}, this)
)
this.tabs.each((i, tab) => {
const panelId = this.getHref($(tab)).slice(1)
tab.id = `tab_${panelId}`
$(tab).attr('aria-controls', panelId)
})
this.panels.each((i, panel) => {
$(panel).attr('aria-labelledby', this.tabs[i].id)
})

// setup state
// this.tabs.attr('tabindex', '-1');
Expand Down
4 changes: 4 additions & 0 deletions docs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.base.json",
"include": ["**/*.js", "**/*.mjs"]
}
84 changes: 59 additions & 25 deletions gulp/build-javascript.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,64 @@
const { join } = require('path')

const commonjs = require('@rollup/plugin-commonjs')
const nodeResolve = require('@rollup/plugin-node-resolve')
const { glob } = require('glob')
const gulp = require('gulp')
const concat = require('gulp-concat')
const rename = require('gulp-rename')
const uglify = require('gulp-uglify')
const umd = require('gulp-umd')
const { rollup } = require('rollup')
const externalGlobals = require('rollup-plugin-external-globals')

gulp.task('build:javascript', () => {
return gulp
.src([
'src/moj/namespace.js',
'src/moj/helpers.js',
'src/moj/all.js',
'src/moj/version.js',
'src/moj/components/**/!(*.spec).js'
])
.pipe(concat('all.js'))
.pipe(
umd({
exports: function () {
return 'MOJFrontend'
gulp.task('build:javascript', async () => {
const modulePaths = await glob('moj/**/*.{cjs,js,mjs}', {
cwd: 'src',
ignore: [
'**/*.spec.{cjs,js,mjs}',
'**/filters/**',
'**/vendor/**',
'**/init.js'
],
nodir: true
})

// Create Rollup bundle(s)
for (const modulePath of modulePaths) {
const options = /** @satisfies {RollupOptions} */ ({
input: join('src', modulePath),
output: [
{
dir: 'package',
entryFileNames: '[name].mjs',
format: 'es',
preserveModules: true,
preserveModulesRoot: 'src'
},
namespace: function () {
return 'MOJFrontend'
{
extend: true,
file: join('package', modulePath.replace('.mjs', '.js')),
format: 'umd',
name: 'MOJFrontend'
}
})
)
.pipe(gulp.dest('package/moj/'))
],
external: ['jquery'],
plugins: [
externalGlobals({
jquery: 'window.jQuery'
}),
nodeResolve(),
commonjs()
]
})

// Create bundle
const bundle = await rollup(options)

// Write to output format(s)
for (const output of options.output) {
await bundle.write(output)
}
}
})

gulp.task('build:javascript-minified', () => {
Expand All @@ -37,12 +71,12 @@ gulp.task('build:javascript-minified', () => {

gulp.task('build:javascript-minified-with-jquery', () => {
return gulp
.src([
'node_modules/jquery/dist/jquery.js',
'gulp/jquery/scope.js',
'package/moj/all.js'
])
.src(['node_modules/jquery/dist/jquery.js', 'package/moj/all.js'])
.pipe(concat('all.jquery.min.js'))
.pipe(uglify())
.pipe(gulp.dest('package/moj/'))
})

/**
* @import { RollupOptions } from 'rollup'
*/
42 changes: 28 additions & 14 deletions gulp/docs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const commonjs = require('@rollup/plugin-commonjs')
const nodeResolve = require('@rollup/plugin-node-resolve')
const gulp = require('gulp')
const gulpEsbuild = require('gulp-esbuild')
const gulpSass = require('gulp-sass')
const { rollup } = require('rollup')
const externalGlobals = require('rollup-plugin-external-globals')
const dartSass = require('sass-embedded')

const sass = gulpSass(dartSass)
Expand Down Expand Up @@ -59,19 +62,26 @@ gulp.task('docs:styles', (done) => {
})

// Bundle the docs site javascript
gulp.task('docs:scripts', (done) => {
return gulp
.src('docs/assets/javascript/application.mjs')
.pipe(
gulpEsbuild({
bundle: true,
loader: { '.mjs': 'js' },
minify: process.env.ENV !== 'dev',
outfile: 'application.js',
target: 'es6'
}).on('error', done)
)
.pipe(gulp.dest('public/assets/javascript'))
gulp.task('docs:scripts', async () => {
const options = /** @satisfies {RollupOptions} */ ({
input: 'docs/assets/javascript/application.mjs',
output: {
dir: 'public/assets/javascript',
format: 'es',
name: 'MOJFrontend'
},
external: ['jquery'],
plugins: [
externalGlobals({
jquery: 'window.jQuery'
}),
nodeResolve(),
commonjs()
]
})

const bundle = await rollup(options)
await bundle.write(options.output)
})

gulp.task('docs:revision', async () => {
Expand All @@ -94,3 +104,7 @@ gulp.task('docs:revision', async () => {
.pipe(rev.manifest())
.pipe(gulp.dest('public/assets/'))
})

/**
* @import { RollupOptions } from 'rollup'
*/
3 changes: 0 additions & 3 deletions gulp/jquery/scope.js

This file was deleted.

4 changes: 4 additions & 0 deletions gulp/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.base.json",
"include": ["**/*.js", "**/*.mjs"]
}
8 changes: 6 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ gulp.task('watch:styles', () => {

// Watch all the component js files and build the package
gulp.task('watch:package-js', () => {
gulp.watch(['src/moj/components/**/*.js'], gulp.series('build:javascript'))
gulp.watch(
['src/moj/components/**/*.mjs'],
{ ignored: ['**/*.spec.*'] },
gulp.series('build:javascript')
)
})

// Watch the docs js files and the bundled package js and rebuild
gulp.task('watch:docs-js', () => {
gulp.watch(
['docs/assets/**/*.js', 'package/moj/all.js'],
['docs/assets/**/*.mjs', 'package/moj/all.js'],
gulp.series(['docs:scripts'])
)
})
Expand Down
23 changes: 15 additions & 8 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
const config = {
testEnvironment: 'jsdom',
testEnvironmentOptions: {},
module.exports = {
setupFilesAfterEnv: ['./jest.setup.js', 'jest-sinon'],
// See: https://github.com/sinonjs/sinon/issues/2522#issuecomment-1612555284
moduleNameMapper: {
sinon: '<rootDir>/node_modules/sinon/pkg/sinon.js'
}
testEnvironment: 'jsdom',
testMatch: ['<rootDir>/**/*.spec.{js,mjs}'],

// Enable Babel transforms until Jest supports ESM and `import()`
// See: https://jestjs.io/docs/ecmascript-modules
transform: {
'^.+\\.(js|mjs)$': ['babel-jest', { rootMode: 'upward' }]
},

// Enable Babel transforms for ESM-only node_modules
// See: https://jestjs.io/docs/ecmascript-modules
transformIgnorePatterns: [
`<rootDir>/node_modules/(?!${['sinon'].join('|')}/)`
]
}
module.exports = config
2 changes: 1 addition & 1 deletion jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require('@testing-library/jest-dom')
require('./src/moj/vendor/jquery')
require('mock-match-media/jest-setup')

const { toHaveNoViolations } = require('jest-axe')

expect.extend(toHaveNoViolations)
Loading