|
| 1 | +/** |
| 2 | + * Post-build step: lay out dist/ so a deploy is both stable and identifiable. |
| 3 | + * |
| 4 | + * Produces, from the single bundle Parcel emits: |
| 5 | + * |
| 6 | + * ar-button.js stable entry, what new embeds should use |
| 7 | + * ar-button.min.js identical copy under the legacy filename |
| 8 | + * ar-button-<version>-<hash>.js immutable, content-addressed build |
| 9 | + * version.json what is actually deployed here |
| 10 | + * .htaccess per-environment Cache-Control |
| 11 | + * |
| 12 | + * Why a copy and not the old `mv`: production URLs have shipped as |
| 13 | + * ar-button.min.js since the Vue CLI days and are embedded on customer sites, |
| 14 | + * so that name can never disappear. Emitting both lets both environments move |
| 15 | + * to ar-button.js without breaking anything already out there. |
| 16 | + * |
| 17 | + * The hashed copy exists because the stable names cannot be hashed - customers |
| 18 | + * control those references. It gives something immutable to pin, and makes |
| 19 | + * "which build is live?" answerable with one request instead of an |
| 20 | + * investigation into CDN headers. |
| 21 | + */ |
| 22 | +import { readFileSync, writeFileSync, copyFileSync, existsSync, mkdirSync } from 'node:fs'; |
| 23 | +import { createHash } from 'node:crypto'; |
| 24 | +import { execSync } from 'node:child_process'; |
| 25 | +import { join } from 'node:path'; |
| 26 | + |
| 27 | +const env = process.argv[2]; |
| 28 | +if (env !== 'dev' && env !== 'prod') { |
| 29 | + console.error('usage: node scripts/postbuild.mjs <dev|prod>'); |
| 30 | + process.exit(1); |
| 31 | +} |
| 32 | + |
| 33 | +const DIST = 'dist'; |
| 34 | +const ENTRY = join(DIST, 'ar-button.js'); |
| 35 | + |
| 36 | +if (!existsSync(ENTRY)) { |
| 37 | + console.error(`✗ ${ENTRY} not found - did the parcel build run?`); |
| 38 | + process.exit(1); |
| 39 | +} |
| 40 | + |
| 41 | +const bundle = readFileSync(ENTRY); |
| 42 | +const hash = createHash('sha256').update(bundle).digest('hex').slice(0, 8); |
| 43 | +const pkg = JSON.parse(readFileSync('package.json', 'utf8')); |
| 44 | +const immutable = `ar-button-${pkg.version}-${hash}.js`; |
| 45 | + |
| 46 | +// Legacy filename, kept forever for embeds already in the wild. |
| 47 | +copyFileSync(ENTRY, join(DIST, 'ar-button.min.js')); |
| 48 | +copyFileSync(ENTRY, join(DIST, immutable)); |
| 49 | + |
| 50 | +const htaccess = join('deploy', `htaccess.${env}`); |
| 51 | +if (!existsSync(htaccess)) { |
| 52 | + console.error(`✗ ${htaccess} not found`); |
| 53 | + process.exit(1); |
| 54 | +} |
| 55 | +copyFileSync(htaccess, join(DIST, '.htaccess')); |
| 56 | + |
| 57 | +let commit = 'unknown'; |
| 58 | +try { |
| 59 | + commit = execSync('git rev-parse --short HEAD', { stdio: ['ignore', 'pipe', 'ignore'] }) |
| 60 | + .toString() |
| 61 | + .trim(); |
| 62 | +} catch { |
| 63 | + // building outside a git checkout is fine, just less traceable |
| 64 | +} |
| 65 | + |
| 66 | +const info = { |
| 67 | + version: pkg.version, |
| 68 | + hash, |
| 69 | + commit, |
| 70 | + env, |
| 71 | + built: new Date().toISOString(), |
| 72 | + files: { latest: 'ar-button.js', legacy: 'ar-button.min.js', immutable }, |
| 73 | +}; |
| 74 | +writeFileSync(join(DIST, 'version.json'), JSON.stringify(info, null, 2) + '\n'); |
| 75 | + |
| 76 | +console.log(`postbuild (${env}): ${immutable} commit ${commit}`); |
0 commit comments