Skip to content

Commit

Permalink
Inject version identifiers in module builds
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixHenninger committed Jul 5, 2023
1 parent 12789d7 commit 887578d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
"build:js:coverage": "NODE_ENV=coverage webpack --mode=development",
"build:js:development": "webpack --mode=development",
"build:js:production": "webpack --mode=production",
"build:js:esm": "tsc -m es6 --outDir dist/es6",
"build:js:tsc": "tsc --outDir dist/es2022",
"build:js": "yarn run build:js:development && yarn run build:js:production && (yarn run build:js:esm || true) && (yarn run build:js:tsc || true)",
"build:js:esm": "(tsc -m es6 --outDir dist/es6 || true) && node scripts/postprocess_module_build.js",
"build:js:tsc": "(tsc --outDir dist/es2022 || true) && node scripts/postprocess_module_build.js",
"build:js": "yarn run build:js:development && yarn run build:js:production && yarn run build:js:esm && yarn run build:js:tsc",
"build:css": "postcss --local-plugins -u postcss-preset-env -o dist/css/lab.css src/starterkit/lib/lab.css",
"build:starterkit": "yarn run build:js && yarn run build:css && node scripts/bundle_starterkit.js",
"clean": "shx rm -rf dist && shx mkdir dist",
Expand Down
40 changes: 40 additions & 0 deletions packages/library/scripts/postprocess_module_build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Inject variables into distribution code
// (this was previously handled by webpack for the bundled library)
const fs = require('fs')
const shell = require('shelljs')

const files = [
'dist/es6/index.js',
'dist/es2022/index.js'
]

const build_flavor = JSON.stringify('module')
const build_commit = JSON.stringify(
shell.exec('git rev-list -1 HEAD -- .', { silent: true }).trim(),
)

files.map(f => {
fs.readFile(f, 'utf8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
console.warn('Could not patch file', f, `which doesn't seem to exist`)
return
} else {
throw err
}
}

let output = data.replace(
/BUILD_FLAVOR/g,
build_flavor
)
output = output.replace(
/BUILD_COMMIT/g,
build_commit
)

fs.writeFile(f, output, 'utf8', (err) => {
if (err) throw err
})
})
})
4 changes: 2 additions & 2 deletions packages/library/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const version = '22.0.0-beta10'
export const build = {
//@ts-ignore Injected by webpack
//@ts-ignore Injected during build
flavor: <string>BUILD_FLAVOR,
//@ts-ignore Injected by webpack
//@ts-ignore Injected during build
commit: <string>BUILD_COMMIT,
}

Expand Down

0 comments on commit 887578d

Please sign in to comment.