Skip to content

Commit

Permalink
chore: handle jQuery via Rollup external
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Feb 24, 2025
1 parent 640897c commit 4f67eb5
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 15 deletions.
4 changes: 0 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
16 changes: 10 additions & 6 deletions gulp/build-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const concat = require('gulp-concat')
const rename = require('gulp-rename')
const uglify = require('gulp-uglify')
const { rollup } = require('rollup')
const externalGlobals = require('rollup-plugin-external-globals')

gulp.task('build:javascript', async () => {
const modulePaths = await glob('moj/components/**/*.{cjs,js,mjs}', {
Expand Down Expand Up @@ -36,7 +37,14 @@ gulp.task('build:javascript', async () => {
name: 'MOJFrontend'
}
],
plugins: [nodeResolve(), commonjs()]
external: ['jquery'],
plugins: [
externalGlobals({
jquery: 'window.jQuery'
}),
nodeResolve(),
commonjs()
]
})

// Create bundle
Expand All @@ -59,11 +67,7 @@ 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/'))
Expand Down
10 changes: 9 additions & 1 deletion gulp/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const rename = require('gulp-rename')
const gulpSass = require('gulp-sass')
const uglify = require('gulp-uglify')
const { rollup } = require('rollup')
const externalGlobals = require('rollup-plugin-external-globals')
const dartSass = require('sass-embedded')

const sass = gulpSass(dartSass)
Expand Down Expand Up @@ -71,7 +72,14 @@ gulp.task('docs:scripts', async () => {

const bundle = await rollup({
input,
plugins: [nodeResolve(), commonjs()]
external: ['jquery'],
plugins: [
externalGlobals({
jquery: 'window.jQuery'
}),
nodeResolve(),
commonjs()
]
})

await bundle.write({
Expand Down
3 changes: 0 additions & 3 deletions gulp/jquery/scope.js

This file was deleted.

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)
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"prettier": "^3.4.2",
"require-dir": "^1.2.0",
"rollup": "^4.34.4",
"rollup-plugin-external-globals": "^0.13.0",
"sass-embedded": "^1.83.4",
"semantic-release": "^23.0.0",
"semantic-release-plugin-update-version-in-files": "^1.1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/moj/all.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable no-new */

const $ = require('jquery')

const { AddAnother } = require('./components/add-another/add-another.js')
const { Alert } = require('./components/alert/alert.js')
const { ButtonMenu } = require('./components/button-menu/button-menu.js')
Expand Down
2 changes: 2 additions & 0 deletions src/moj/components/add-another/add-another.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const $ = require('jquery')

function AddAnother(container) {
this.container = $(container)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const $ = require('jquery')

function FilterToggleButton(options) {
this.options = options
this.container = $(this.options.toggleButton.container)
Expand Down
2 changes: 2 additions & 0 deletions src/moj/components/form-validator/form-validator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const $ = require('jquery')

const { addAttributeValue, removeAttributeValue } = require('../../helpers.js')

function FormValidator(form, options) {
Expand Down
2 changes: 2 additions & 0 deletions src/moj/components/multi-file-upload/multi-file-upload.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const $ = require('jquery')

const {
dragAndDropSupported,
fileApiSupported,
Expand Down
2 changes: 2 additions & 0 deletions src/moj/components/multi-select/multi-select.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const $ = require('jquery')

function MultiSelect(options) {
this.container = $(options.container)

Expand Down
2 changes: 2 additions & 0 deletions src/moj/components/password-reveal/password-reveal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const $ = require('jquery')

function PasswordReveal(element) {
this.el = element
const $el = $(this.el)
Expand Down
2 changes: 2 additions & 0 deletions src/moj/components/rich-text-editor/rich-text-editor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const $ = require('jquery')

function RichTextEditor(options) {
if (!('contentEditable' in document.documentElement)) {
return
Expand Down
2 changes: 2 additions & 0 deletions src/moj/components/search-toggle/search-toggle.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const $ = require('jquery')

function SearchToggle(options) {
this.options = options
this.container = $(this.options.search.container)
Expand Down
2 changes: 2 additions & 0 deletions src/moj/components/sortable-table/sortable-table.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const $ = require('jquery')

function SortableTable(params) {
this.table = $(params.table)

Expand Down

0 comments on commit 4f67eb5

Please # to comment.