From a9c42fcf73e5f9e83e567f9fa11051d467eb0b2f Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Mon, 31 Jul 2023 11:11:56 +0300 Subject: [PATCH] Convert build scripts to ESM --- .eslintrc.json | 2 +- build/banner.js | 15 ------------ build/banner.mjs | 22 +++++++++++++++++ build/{build-plugins.js => build-plugins.mjs} | 16 +++++++------ .../{change-version.js => change-version.mjs} | 9 +++---- build/{generate-sri.js => generate-sri.mjs} | 17 ++++++------- .../{postcss.config.js => postcss.config.mjs} | 4 +--- build/{rollup.config.js => rollup.config.mjs} | 15 ++++++------ build/{vnu-jar.js => vnu-jar.mjs} | 6 ++--- build/{zip-examples.js => zip-examples.mjs} | 13 ++++++---- package.json | 24 +++++++++---------- 11 files changed, 78 insertions(+), 65 deletions(-) delete mode 100644 build/banner.js create mode 100644 build/banner.mjs rename build/{build-plugins.js => build-plugins.mjs} (87%) rename build/{change-version.js => change-version.mjs} (92%) rename build/{generate-sri.js => generate-sri.mjs} (78%) rename build/{postcss.config.js => postcss.config.mjs} (86%) rename build/{rollup.config.js => rollup.config.mjs} (75%) rename build/{vnu-jar.js => vnu-jar.mjs} (94%) rename build/{zip-examples.js => zip-examples.mjs} (88%) diff --git a/.eslintrc.json b/.eslintrc.json index 055acc706488..6c9c24d46e59 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -108,7 +108,7 @@ "node": true }, "parserOptions": { - "sourceType": "script" + "sourceType": "module" }, "rules": { "no-console": "off", diff --git a/build/banner.js b/build/banner.js deleted file mode 100644 index a022f1c48570..000000000000 --- a/build/banner.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict' - -const pkg = require('../package.json') - -const year = new Date().getFullYear() - -function getBanner(pluginFilename) { - return `/*! - * Bootstrap${pluginFilename ? ` ${pluginFilename}` : ''} v${pkg.version} (${pkg.homepage}) - * Copyright 2011-${year} ${pkg.author} - * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) - */` -} - -module.exports = getBanner diff --git a/build/banner.mjs b/build/banner.mjs new file mode 100644 index 000000000000..9952fec055fa --- /dev/null +++ b/build/banner.mjs @@ -0,0 +1,22 @@ +import fs from 'node:fs/promises' +import path from 'node:path' +import { fileURLToPath } from 'node:url' + +const year = new Date().getFullYear() + +const __dirname = path.dirname(fileURLToPath(import.meta.url)) + +const pkgJson = path.join(__dirname, '../package.json') +const pkgJsonString = await fs.readFile(pkgJson, 'utf8') + +const pkg = JSON.parse(pkgJsonString) + +function getBanner(pluginFilename) { + return `/*! + * Bootstrap${pluginFilename ? ` ${pluginFilename}` : ''} v${pkg.version} (${pkg.homepage}) + * Copyright 2011-${year} ${pkg.author} + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) + */` +} + +export default getBanner diff --git a/build/build-plugins.js b/build/build-plugins.mjs similarity index 87% rename from build/build-plugins.js rename to build/build-plugins.mjs index b2833a3fb8ed..77e63070c550 100644 --- a/build/build-plugins.js +++ b/build/build-plugins.mjs @@ -6,13 +6,15 @@ * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) */ -'use strict' +import path from 'node:path' +import { fileURLToPath } from 'node:url' +import { babel } from '@rollup/plugin-babel' +import globby from 'globby' +import { rollup } from 'rollup' +import banner from './banner.mjs' -const path = require('node:path') -const rollup = require('rollup') -const globby = require('globby') -const { babel } = require('@rollup/plugin-babel') -const banner = require('./banner.js') +const __filename = fileURLToPath(import.meta.url) +const __dirname = path.dirname(fileURLToPath(import.meta.url)) const sourcePath = path.resolve(__dirname, '../js/src/').replace(/\\/g, '/') const jsFiles = globby.sync(`${sourcePath}/**/*.js`) @@ -37,7 +39,7 @@ for (const file of jsFiles) { const build = async plugin => { const globals = {} - const bundle = await rollup.rollup({ + const bundle = await rollup({ input: plugin.src, plugins: [ babel({ diff --git a/build/change-version.js b/build/change-version.mjs similarity index 92% rename from build/change-version.js rename to build/change-version.mjs index 9685df589644..2a16f2f21088 100644 --- a/build/change-version.js +++ b/build/change-version.mjs @@ -6,11 +6,12 @@ * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) */ -'use strict' +import fs from 'node:fs/promises' +import path from 'node:path' +import { fileURLToPath } from 'node:url' +import globby from 'globby' -const fs = require('node:fs').promises -const path = require('node:path') -const globby = require('globby') +const __dirname = path.dirname(fileURLToPath(import.meta.url)) const VERBOSE = process.argv.includes('--verbose') const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run') diff --git a/build/generate-sri.js b/build/generate-sri.mjs similarity index 78% rename from build/generate-sri.js rename to build/generate-sri.mjs index 2e2292475589..e2b1554f144e 100644 --- a/build/generate-sri.js +++ b/build/generate-sri.mjs @@ -9,12 +9,13 @@ * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) */ -'use strict' +import crypto from 'node:crypto' +import fs from 'node:fs' +import path from 'node:path' +import { fileURLToPath } from 'node:url' +import sh from 'shelljs' -const crypto = require('node:crypto') -const fs = require('node:fs') -const path = require('node:path') -const sh = require('shelljs') +const __dirname = path.dirname(fileURLToPath(import.meta.url)) sh.config.fatal = true @@ -52,9 +53,9 @@ for (const { file, configPropertyName } of files) { throw error } - const algo = 'sha384' - const hash = crypto.createHash(algo).update(data, 'utf8').digest('base64') - const integrity = `${algo}-${hash}` + const algorithm = 'sha384' + const hash = crypto.createHash(algorithm).update(data, 'utf8').digest('base64') + const integrity = `${algorithm}-${hash}` console.log(`${configPropertyName}: ${integrity}`) diff --git a/build/postcss.config.js b/build/postcss.config.mjs similarity index 86% rename from build/postcss.config.js rename to build/postcss.config.mjs index 7f8186d10314..7717cfc3f1f6 100644 --- a/build/postcss.config.js +++ b/build/postcss.config.mjs @@ -1,12 +1,10 @@ -'use strict' - const mapConfig = { inline: false, annotation: true, sourcesContent: true } -module.exports = context => { +export default context => { return { map: context.file.dirname.includes('examples') ? false : mapConfig, plugins: { diff --git a/build/rollup.config.js b/build/rollup.config.mjs similarity index 75% rename from build/rollup.config.js rename to build/rollup.config.mjs index f01918ebf24d..1c47d7593c5e 100644 --- a/build/rollup.config.js +++ b/build/rollup.config.mjs @@ -1,10 +1,11 @@ -'use strict' +import path from 'node:path' +import { fileURLToPath } from 'node:url' +import { babel } from '@rollup/plugin-babel' +import { nodeResolve } from '@rollup/plugin-node-resolve' +import replace from '@rollup/plugin-replace' +import banner from './banner.mjs' -const path = require('node:path') -const { babel } = require('@rollup/plugin-babel') -const { nodeResolve } = require('@rollup/plugin-node-resolve') -const replace = require('@rollup/plugin-replace') -const banner = require('./banner.js') +const __dirname = path.dirname(fileURLToPath(import.meta.url)) const BUNDLE = process.env.BUNDLE === 'true' const ESM = process.env.ESM === 'true' @@ -54,4 +55,4 @@ if (!ESM) { rollupConfig.output.name = 'bootstrap' } -module.exports = rollupConfig +export default rollupConfig diff --git a/build/vnu-jar.js b/build/vnu-jar.mjs similarity index 94% rename from build/vnu-jar.js rename to build/vnu-jar.mjs index 22956cb7e9a2..b66356032618 100644 --- a/build/vnu-jar.js +++ b/build/vnu-jar.mjs @@ -6,10 +6,8 @@ * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) */ -'use strict' - -const { execFile, spawn } = require('node:child_process') -const vnu = require('vnu-jar') +import { execFile, spawn } from 'node:child_process' +import vnu from 'vnu-jar' execFile('java', ['-version'], (error, stdout, stderr) => { if (error) { diff --git a/build/zip-examples.js b/build/zip-examples.mjs similarity index 88% rename from build/zip-examples.js rename to build/zip-examples.mjs index 7378c33c1769..b586e530d51b 100644 --- a/build/zip-examples.js +++ b/build/zip-examples.mjs @@ -7,12 +7,17 @@ * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) */ -'use strict' +import fs from 'node:fs/promises' +import path from 'node:path' +import { fileURLToPath } from 'node:url' +import sh from 'shelljs' -const path = require('node:path') -const sh = require('shelljs') +const __dirname = path.dirname(fileURLToPath(import.meta.url)) -const pkg = require('../package.json') +const pkgJson = path.join(__dirname, '../package.json') +const pkgJsonString = await fs.readFile(pkgJson, 'utf8') + +const pkg = JSON.parse(pkgJsonString) const versionShort = pkg.config.version_short const distFolder = `bootstrap-${pkg.version}-examples` diff --git a/package.json b/package.json index f0d087e3be08..a253528a9493 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "bundlewatch": "bundlewatch --config .bundlewatch.config.json", "css": "npm-run-all css-compile css-prefix css-rtl css-minify", "css-compile": "sass --style expanded --source-map --embed-sources --no-error-css scss/:dist/css/", - "css-rtl": "cross-env NODE_ENV=RTL postcss --config build/postcss.config.js --dir \"dist/css\" --ext \".rtl.css\" \"dist/css/*.css\" \"!dist/css/*.min.css\" \"!dist/css/*.rtl.css\"", + "css-rtl": "cross-env NODE_ENV=RTL postcss --config build/postcss.config.mjs --dir \"dist/css\" --ext \".rtl.css\" \"dist/css/*.css\" \"!dist/css/*.min.css\" \"!dist/css/*.rtl.css\"", "css-lint": "npm-run-all --aggregate-output --continue-on-error --parallel css-lint-*", "css-lint-stylelint": "stylelint \"**/*.{css,scss}\" --cache --cache-location .cache/.stylelintcache", "css-lint-vars": "fusv scss/ site/assets/scss/", @@ -51,17 +51,17 @@ "css-minify-main": "cleancss -O1 --format breakWith=lf --with-rebase --source-map --source-map-inline-sources --output dist/css/ --batch --batch-suffix \".min\" \"dist/css/*.css\" \"!dist/css/*.min.css\" \"!dist/css/*rtl*.css\"", "css-minify-rtl": "cleancss -O1 --format breakWith=lf --with-rebase --source-map --source-map-inline-sources --output dist/css/ --batch --batch-suffix \".min\" \"dist/css/*rtl.css\" \"!dist/css/*.min.css\"", "css-prefix": "npm-run-all --aggregate-output --parallel css-prefix-*", - "css-prefix-main": "postcss --config build/postcss.config.js --replace \"dist/css/*.css\" \"!dist/css/*.rtl*.css\" \"!dist/css/*.min.css\"", - "css-prefix-examples": "postcss --config build/postcss.config.js --replace \"site/content/**/*.css\"", - "css-prefix-examples-rtl": "cross-env-shell NODE_ENV=RTL postcss --config build/postcss.config.js --dir \"site/content/docs/$npm_package_config_version_short/examples/\" --ext \".rtl.css\" --base \"site/content/docs/$npm_package_config_version_short/examples/\" \"site/content/docs/$npm_package_config_version_short/examples/{blog,carousel,dashboard,cheatsheet}/*.css\" \"!site/content/docs/$npm_package_config_version_short/examples/{blog,carousel,dashboard,cheatsheet}/*.rtl.css\"", + "css-prefix-main": "postcss --config build/postcss.config.mjs --replace \"dist/css/*.css\" \"!dist/css/*.rtl*.css\" \"!dist/css/*.min.css\"", + "css-prefix-examples": "postcss --config build/postcss.config.mjs --replace \"site/content/**/*.css\"", + "css-prefix-examples-rtl": "cross-env-shell NODE_ENV=RTL postcss --config build/postcss.config.mjs --dir \"site/content/docs/$npm_package_config_version_short/examples/\" --ext \".rtl.css\" --base \"site/content/docs/$npm_package_config_version_short/examples/\" \"site/content/docs/$npm_package_config_version_short/examples/{blog,carousel,dashboard,cheatsheet}/*.css\" \"!site/content/docs/$npm_package_config_version_short/examples/{blog,carousel,dashboard,cheatsheet}/*.rtl.css\"", "css-test": "jasmine --config=scss/tests/jasmine.js", "js": "npm-run-all js-compile js-minify", "js-compile": "npm-run-all --aggregate-output --parallel js-compile-*", - "js-compile-standalone": "rollup --environment BUNDLE:false --config build/rollup.config.js --sourcemap", - "js-compile-standalone-esm": "rollup --environment ESM:true,BUNDLE:false --config build/rollup.config.js --sourcemap", - "js-compile-bundle": "rollup --environment BUNDLE:true --config build/rollup.config.js --sourcemap", - "js-compile-plugins": "node build/build-plugins.js", - "js-lint": "eslint --cache --cache-location .cache/.eslintcache --report-unused-disable-directives --ext .html,.js,.md .", + "js-compile-standalone": "rollup --environment BUNDLE:false --config build/rollup.config.mjs --sourcemap", + "js-compile-standalone-esm": "rollup --environment ESM:true,BUNDLE:false --config build/rollup.config.mjs --sourcemap", + "js-compile-bundle": "rollup --environment BUNDLE:true --config build/rollup.config.mjs --sourcemap", + "js-compile-plugins": "node build/build-plugins.mjs", + "js-lint": "eslint --cache --cache-location .cache/.eslintcache --report-unused-disable-directives --ext .html,.js,.mjs,.md .", "js-minify": "npm-run-all --aggregate-output --parallel js-minify-*", "js-minify-standalone": "terser --compress passes=2 --mangle --comments \"/^!/\" --source-map \"content=dist/js/bootstrap.js.map,includeSources,url=bootstrap.min.js.map\" --output dist/js/bootstrap.min.js dist/js/bootstrap.js", "js-minify-standalone-esm": "terser --compress passes=2 --mangle --comments \"/^!/\" --source-map \"content=dist/js/bootstrap.esm.js.map,includeSources,url=bootstrap.esm.min.js.map\" --output dist/js/bootstrap.esm.min.js dist/js/bootstrap.esm.js", @@ -84,10 +84,10 @@ "lockfile-lint": "lockfile-lint --allowed-hosts npm --allowed-schemes https: --empty-hostname false --type npm --path package-lock.json", "update-deps": "ncu -u -x globby,jasmine,karma-browserstack-launcher,karma-rollup-preprocessor && echo Manually update site/assets/js/vendor", "release": "npm-run-all dist release-sri docs-build release-zip*", - "release-sri": "node build/generate-sri.js", - "release-version": "node build/change-version.js", + "release-sri": "node build/generate-sri.mjs", + "release-version": "node build/change-version.mjs", "release-zip": "cross-env-shell \"rm -rf bootstrap-$npm_package_version-dist bootstrap-$npm_package_version-dist.zip && cp -r dist/ bootstrap-$npm_package_version-dist && zip -qr9 bootstrap-$npm_package_version-dist.zip bootstrap-$npm_package_version-dist && rm -rf bootstrap-$npm_package_version-dist\"", - "release-zip-examples": "node build/zip-examples.js", + "release-zip-examples": "node build/zip-examples.mjs", "dist": "npm-run-all --aggregate-output --parallel css js", "test": "npm-run-all lint dist js-test docs-build docs-lint", "netlify": "cross-env-shell HUGO_BASEURL=$DEPLOY_PRIME_URL npm-run-all dist release-sri docs-build",