Skip to content

Commit b7e9548

Browse files
committed
feat: Add —versionFilePathPrefix option
Scenario: I have 2 React apps hosted at app.com/subdirectory1 and app.com/subdirectory2. I need the version.json for each app to be served from the correct subdirectory, but served from the normal build directory. In Vite this is known as the ‘base’ and now this option allows for specification of the correct subdirectory (e.g. —versionFilePathPrefix=subdirectory1/) so that the version.json path is output as `subdirectory1/version.json` in the HTML.
1 parent 15ce23f commit b7e9548

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ In your `package.json`, add call to `generate-version` after the build is create
4242
| `-b`, `--buildPath` | Set custom build path. This should be the root of the public directory that is served. | `build` |
4343
| `-i`, `--indexFile` | Path to index.html relative to build path. | `index.html` |
4444
| `-v`, `--versionFile` | Version file target path relative to build path. | `version.json` |
45+
| `-p`, `--versionFilePathPrefix` | A prefix to add before the versionFile option above (useful for more control over the deployed version file target path e.g. when using subdirectories to host your React app).| |
4546

4647
### 2. Using a React hook
4748

bin/generate-version.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function generate(argv) {
5353

5454
const loadedIndex = cheerio.load(indexFile.toString());
5555
loadedIndex('head').append(
56-
`<script>window.__APP_VERSION__ = "${appVersion}"; window.__APP_VERSION_FILE__ = "${argv.v}"</script>`
56+
`<script>window.__APP_VERSION__ = "${appVersion}"; window.__APP_VERSION_FILE__ = "${argv.p}${argv.v}"</script>`
5757
);
5858
fs.writeFileSync(indexPath, loadedIndex.html());
5959
}
@@ -85,6 +85,12 @@ const argv = yargs
8585
nargs: 1,
8686
default: 'version.json',
8787
})
88+
.option('versionFilePathPrefix', {
89+
alias: 'p',
90+
description: 'A prefix to add before the versionFile option',
91+
nargs: 1,
92+
default: '',
93+
})
8894
.example('$0 -b build/my-custom-build-root')
8995
.example('$0 -i index.html')
9096
.example('$0 -i index.html -v version.json')

0 commit comments

Comments
 (0)