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

[SPIKE] Configure Babel to add core-js polyfills #4557

Closed
wants to merge 8 commits into from
Closed
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: 2 additions & 8 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ module.exports = function (api) {
api.cache.forever()

return {
presets: [
[
'@babel/preset-env',
{
browserslistEnv: 'node'
}
]
]
browserslistEnv: 'node',
presets: ['@babel/preset-env']
}
}
2 changes: 1 addition & 1 deletion dist/govuk-frontend-5.1.0.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/govuk-frontend-5.1.0.min.css.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/govuk-frontend-5.1.0.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/govuk-frontend-5.1.0.min.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions docs/examples/webpack/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ module.exports = function (api) {
const browserslistEnv = isBrowser ? 'javascripts' : 'node'

return {
browserslistEnv,
presets: [
[
'@babel/preset-env',
{
browserslistEnv,

// Apply bug fixes to avoid transforms
bugfixes: true,

Expand Down
15 changes: 15 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 packages/govuk-frontend/.browserslistrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

[javascripts]
supports es6-module
not OperaMobile > 0

[stylesheets]
> 0.1% in GB and not dead
Expand Down
52 changes: 50 additions & 2 deletions packages/govuk-frontend/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const { pkg } = require('@govuk-frontend/config')

/**
* Babel config
*
* @type {import('@babel/core').ConfigFunction}
*/
module.exports = function (api) {
const isProduction = !api.env('development')

// Assume browser environment via Rollup plugin
const isBrowser = api.caller(
(caller) => caller?.name === '@rollup/plugin-babel'
Expand All @@ -14,6 +18,7 @@ module.exports = function (api) {
const browserslistEnv = isBrowser ? 'javascripts' : 'node'

return {
browserslistEnv,
generatorOpts: {
shouldPrintComment(comment) {
if (!isBrowser || comment.includes('* @preserve')) {
Expand All @@ -35,12 +40,55 @@ module.exports = function (api) {
return !isPrivate && isDocumentation
}
},
plugins: [
[
'polyfill-corejs3',
{
// Add logging for required polyfills
debug: isProduction,

// Browser support polyfills to exclude
exclude: [
// Ignore various fixes in supported features
// https://github.com/zloirock/core-js/blob/v3/packages/core-js-compat/src/data.mjs
'es.array.includes',
'es.array.iterator',
'es.array.push',
'es.regexp.exec',
'es.regexp.to-string',
'es.string.includes',
'es.string.match',
'es.string.replace',
'es.string.trim',

// ES2022 Error cause is unused
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
'es.error.cause',

// DOM collection iteration is unnecessary with `Array.from()`
// see https://github.com/zloirock/core-js/issues/1003
'web.dom-collections.iterator',

// WHATWG URL constructor `undefined` base fix is unnecessary
// https://bugs.webkit.org/show_bug.cgi?id=216841
// https://github.com/zloirock/core-js/commit/0ef4be7352e900dfa6f1eda27de22f7974576531
'web.url',
'web.url-search-params',
'web.url-search-params.delete',
'web.url-search-params.has',
'web.url-search-params.size'
],

// Replace unsupported code with polyfills
method: 'usage-global',
version: pkg.devDependencies['core-js']
}
]
],
presets: [
[
'@babel/preset-env',
{
browserslistEnv,

// Apply bug fixes to avoid transforms
bugfixes: true,

Expand Down
4 changes: 4 additions & 0 deletions packages/govuk-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@
"@govuk-frontend/lib": "*",
"@govuk-frontend/tasks": "*",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.4",
"autoprefixer": "^10.4.17",
"babel-plugin-polyfill-corejs3": "^0.9.0",
"core-js": "^3.36.0",
"cssnano": "^6.0.3",
"cssnano-preset-default": "^6.0.1",
"govuk-prototype-kit": "^13.16.0",
Expand Down
12 changes: 11 additions & 1 deletion packages/govuk-frontend/rollup.publish.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import config from '@govuk-frontend/config'
import { babel } from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import { defineConfig } from 'rollup'

Expand Down Expand Up @@ -54,15 +56,23 @@ export default defineConfig(({ i: input }) => ({
* Input plugins
*/
plugins: [
resolve({
browser: true
}),
replace({
include: '**/common/govuk-frontend-version.mjs',
preventAssignment: true,

// Add GOV.UK Frontend release version
development: config.version
}),
commonjs({
requireReturnsDefault: 'preferred',
defaultIsModuleExports: true
}),
babel({
babelHelpers: 'bundled'
babelHelpers: 'bundled',
exclude: '**/node_modules/**'
})
]
}))
12 changes: 11 additions & 1 deletion packages/govuk-frontend/rollup.release.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import config from '@govuk-frontend/config'
import { babel } from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import terser from '@rollup/plugin-terser'
import * as GOVUKFrontend from 'govuk-frontend/src/govuk/all.mjs'
Expand Down Expand Up @@ -54,15 +56,23 @@ export default defineConfig(({ i: input }) => ({
* Input plugins
*/
plugins: [
resolve({
browser: true
}),
replace({
include: '**/common/govuk-frontend-version.mjs',
preventAssignment: true,

// Add GOV.UK Frontend release version
development: config.version
}),
commonjs({
requireReturnsDefault: 'preferred',
defaultIsModuleExports: true
}),
babel({
babelHelpers: 'bundled'
babelHelpers: 'bundled',
exclude: '**/node_modules/**'
})
]
}))