Convert build scripts to ESM (#38984)

This commit is contained in:
XhmikosR 2023-08-06 07:37:24 +03:00 committed by GitHub
parent 0fe3dd93f2
commit 56664e0caa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 67 additions and 57 deletions

View File

@ -108,7 +108,7 @@
"node": true "node": true
}, },
"parserOptions": { "parserOptions": {
"sourceType": "script" "sourceType": "module"
}, },
"rules": { "rules": {
"no-console": "off", "no-console": "off",

View File

@ -1,6 +1,11 @@
'use strict' import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const pkg = require('../package.json') const __dirname = path.dirname(fileURLToPath(import.meta.url))
const pkgJson = path.join(__dirname, '../package.json')
const pkg = JSON.parse(await fs.readFile(pkgJson, 'utf8'))
const year = new Date().getFullYear() const year = new Date().getFullYear()
@ -12,4 +17,4 @@ function getBanner(pluginFilename) {
*/` */`
} }
module.exports = getBanner export default getBanner

View File

@ -6,13 +6,15 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * 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 __filename = fileURLToPath(import.meta.url)
const rollup = require('rollup') const __dirname = path.dirname(fileURLToPath(import.meta.url))
const globby = require('globby')
const { babel } = require('@rollup/plugin-babel')
const banner = require('./banner.js')
const sourcePath = path.resolve(__dirname, '../js/src/').replace(/\\/g, '/') const sourcePath = path.resolve(__dirname, '../js/src/').replace(/\\/g, '/')
const jsFiles = globby.sync(`${sourcePath}/**/*.js`) const jsFiles = globby.sync(`${sourcePath}/**/*.js`)
@ -37,7 +39,7 @@ for (const file of jsFiles) {
const build = async plugin => { const build = async plugin => {
const globals = {} const globals = {}
const bundle = await rollup.rollup({ const bundle = await rollup({
input: plugin.src, input: plugin.src,
plugins: [ plugins: [
babel({ babel({

View File

@ -6,11 +6,12 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * 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 __dirname = path.dirname(fileURLToPath(import.meta.url))
const path = require('node:path')
const globby = require('globby')
const VERBOSE = process.argv.includes('--verbose') const VERBOSE = process.argv.includes('--verbose')
const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run') const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run')

View File

@ -9,12 +9,13 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * 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 __dirname = path.dirname(fileURLToPath(import.meta.url))
const fs = require('node:fs')
const path = require('node:path')
const sh = require('shelljs')
sh.config.fatal = true sh.config.fatal = true
@ -52,9 +53,9 @@ for (const { file, configPropertyName } of files) {
throw error throw error
} }
const algo = 'sha384' const algorithm = 'sha384'
const hash = crypto.createHash(algo).update(data, 'utf8').digest('base64') const hash = crypto.createHash(algorithm).update(data, 'utf8').digest('base64')
const integrity = `${algo}-${hash}` const integrity = `${algorithm}-${hash}`
console.log(`${configPropertyName}: ${integrity}`) console.log(`${configPropertyName}: ${integrity}`)

View File

@ -1,12 +1,10 @@
'use strict'
const mapConfig = { const mapConfig = {
inline: false, inline: false,
annotation: true, annotation: true,
sourcesContent: true sourcesContent: true
} }
module.exports = context => { export default context => {
return { return {
map: context.file.dirname.includes('examples') ? false : mapConfig, map: context.file.dirname.includes('examples') ? false : mapConfig,
plugins: { plugins: {

View File

@ -1,15 +1,17 @@
'use strict' import path from 'node:path'
import process from 'node:process'
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 __dirname = path.dirname(fileURLToPath(import.meta.url))
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 BUNDLE = process.env.BUNDLE === 'true' const BUNDLE = process.env.BUNDLE === 'true'
const ESM = process.env.ESM === 'true' const ESM = process.env.ESM === 'true'
let fileDestination = `bootstrap${ESM ? '.esm' : ''}` let destinationFile = `bootstrap${ESM ? '.esm' : ''}`
const external = ['@popperjs/core'] const external = ['@popperjs/core']
const plugins = [ const plugins = [
babel({ babel({
@ -24,7 +26,7 @@ const globals = {
} }
if (BUNDLE) { if (BUNDLE) {
fileDestination += '.bundle' destinationFile += '.bundle'
// Remove last entry in external array to bundle Popper // Remove last entry in external array to bundle Popper
external.pop() external.pop()
delete globals['@popperjs/core'] delete globals['@popperjs/core']
@ -41,7 +43,7 @@ const rollupConfig = {
input: path.resolve(__dirname, `../js/index.${ESM ? 'esm' : 'umd'}.js`), input: path.resolve(__dirname, `../js/index.${ESM ? 'esm' : 'umd'}.js`),
output: { output: {
banner: banner(), banner: banner(),
file: path.resolve(__dirname, `../dist/js/${fileDestination}.js`), file: path.resolve(__dirname, `../dist/js/${destinationFile}.js`),
format: ESM ? 'esm' : 'umd', format: ESM ? 'esm' : 'umd',
globals, globals,
generatedCode: 'es2015' generatedCode: 'es2015'
@ -54,4 +56,4 @@ if (!ESM) {
rollupConfig.output.name = 'bootstrap' rollupConfig.output.name = 'bootstrap'
} }
module.exports = rollupConfig export default rollupConfig

View File

@ -6,10 +6,8 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
*/ */
'use strict' import { execFile, spawn } from 'node:child_process'
import vnu from 'vnu-jar'
const { execFile, spawn } = require('node:child_process')
const vnu = require('vnu-jar')
execFile('java', ['-version'], (error, stdout, stderr) => { execFile('java', ['-version'], (error, stdout, stderr) => {
if (error) { if (error) {

View File

@ -7,12 +7,15 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * 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 __dirname = path.dirname(fileURLToPath(import.meta.url))
const sh = require('shelljs')
const pkg = require('../package.json') const pkgJson = path.join(__dirname, '../package.json')
const pkg = JSON.parse(await fs.readFile(pkgJson, 'utf8'))
const versionShort = pkg.config.version_short const versionShort = pkg.config.version_short
const distFolder = `bootstrap-${pkg.version}-examples` const distFolder = `bootstrap-${pkg.version}-examples`

View File

@ -43,7 +43,7 @@
"bundlewatch": "bundlewatch --config .bundlewatch.config.json", "bundlewatch": "bundlewatch --config .bundlewatch.config.json",
"css": "npm-run-all css-compile css-prefix css-rtl css-minify", "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-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": "npm-run-all --aggregate-output --continue-on-error --parallel css-lint-*",
"css-lint-stylelint": "stylelint \"**/*.{css,scss}\" --cache --cache-location .cache/.stylelintcache", "css-lint-stylelint": "stylelint \"**/*.{css,scss}\" --cache --cache-location .cache/.stylelintcache",
"css-lint-vars": "fusv scss/ site/assets/scss/", "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-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-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": "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-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.js --replace \"site/content/**/*.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.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-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", "css-test": "jasmine --config=scss/tests/jasmine.js",
"js": "npm-run-all js-compile js-minify", "js": "npm-run-all js-compile js-minify",
"js-compile": "npm-run-all --aggregate-output --parallel js-compile-*", "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": "rollup --environment BUNDLE:false --config build/rollup.config.mjs --sourcemap",
"js-compile-standalone-esm": "rollup --environment ESM:true,BUNDLE:false --config build/rollup.config.js --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.js --sourcemap", "js-compile-bundle": "rollup --environment BUNDLE:true --config build/rollup.config.mjs --sourcemap",
"js-compile-plugins": "node build/build-plugins.js", "js-compile-plugins": "node build/build-plugins.mjs",
"js-lint": "eslint --cache --cache-location .cache/.eslintcache --report-unused-disable-directives --ext .html,.js,.md .", "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": "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": "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", "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",
@ -77,17 +77,17 @@
"docs": "npm-run-all docs-build docs-lint", "docs": "npm-run-all docs-build docs-lint",
"docs-build": "hugo --cleanDestinationDir --printUnusedTemplates", "docs-build": "hugo --cleanDestinationDir --printUnusedTemplates",
"docs-compile": "npm run docs-build", "docs-compile": "npm run docs-build",
"docs-vnu": "node build/vnu-jar.js", "docs-vnu": "node build/vnu-jar.mjs",
"docs-lint": "npm run docs-vnu", "docs-lint": "npm run docs-vnu",
"docs-serve": "hugo server --port 9001 --disableFastRender --printUnusedTemplates", "docs-serve": "hugo server --port 9001 --disableFastRender --printUnusedTemplates",
"docs-serve-only": "npx sirv-cli _site --port 9001", "docs-serve-only": "npx sirv-cli _site --port 9001",
"lockfile-lint": "lockfile-lint --allowed-hosts npm --allowed-schemes https: --empty-hostname false --type npm --path package-lock.json", "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", "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": "npm-run-all dist release-sri docs-build release-zip*",
"release-sri": "node build/generate-sri.js", "release-sri": "node build/generate-sri.mjs",
"release-version": "node build/change-version.js", "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": "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", "dist": "npm-run-all --aggregate-output --parallel css js",
"test": "npm-run-all lint dist js-test docs-build docs-lint", "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", "netlify": "cross-env-shell HUGO_BASEURL=$DEPLOY_PRIME_URL npm-run-all dist release-sri docs-build",