Skip to content

Commit f230789

Browse files
committed
Improve clean command
1 parent f7306ca commit f230789

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

bin/clean.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,62 @@ import { dirname } from 'path'
77
* Argv helpers.
88
*/
99

10-
const argument = (name, fallback) => {
10+
const argument = (name) => {
1111
const index = process.argv.findIndex(argument => argument.startsWith(`--${name}=`))
1212

1313
return index === -1
14-
? fallback()
14+
? undefined
1515
: process.argv[index].substring(`--${name}=`.length)
1616
}
1717

1818
const option = (name) => process.argv.includes(`--${name}`)
1919

20-
/*
21-
* Configuration.
22-
*/
23-
24-
const dryRun = option(`dry-run`)
25-
const quiet = option(`quiet`)
26-
const wantsSsr = option('ssr')
27-
const manifestPath = argument(`manifest`, () => {
28-
if (! wantsSsr) {
29-
return `./public/build/manifest.json`
30-
}
31-
32-
return existsSync(`./bootstrap/ssr/ssr-manifest.json`) ? `./bootstrap/ssr/ssr-manifest.json` : `./public/build/manifest.json`
33-
})
34-
const assetsDirectory = argument(`assets`, () => `${dirname(manifestPath)}/assets`)
35-
3620
/*
3721
* Helpers.
3822
*/
39-
const info = quiet ? (() => undefined) : console.log
23+
const info = option(`quiet`) ? (() => undefined) : console.log
24+
const error = option(`quiet`) ? (() => undefined) : console.error
4025

4126
/*
4227
* Clean.
4328
*/
4429

4530
const main = () => {
46-
info(`Reading manifest [${manifestPath}].`)
31+
const manifestPaths = argument(`manifest`) ? [argument(`manifest`)] : (option(`ssr`)
32+
? [`./bootstrap/ssr/ssr-manifest.json`, `./bootstrap/ssr/manifest.json`]
33+
: [`./public/build/manifest.json`])
4734

48-
const manifest = JSON.parse(readFileSync(manifestPath).toString())
35+
const foundManifestPath = manifestPaths.find(existsSync)
4936

50-
const manifestKeys = Object.keys(manifest)
37+
if (! foundManifestPath) {
38+
error(`Unable to find manifest file.`)
5139

52-
const isSsr = Array.isArray(manifest[manifestKeys[0]])
40+
process.exit(1)
41+
}
42+
43+
info(`Reading manifest [${foundManifestPath}].`)
44+
45+
const manifest = JSON.parse(readFileSync(foundManifestPath).toString())
46+
47+
const manifestFiles = Object.keys(manifest)
48+
49+
const isSsr = Array.isArray(manifest[manifestFiles[0]])
5350

5451
isSsr
5552
? info(`SSR manifest found.`)
5653
: info(`Non-SSR manifest found.`)
5754

5855
const manifestAssets = isSsr
59-
? manifestKeys.flatMap(key => manifest[key])
60-
: manifestKeys.map(key => manifest[key].file)
56+
? manifestFiles.flatMap(key => manifest[key])
57+
: manifestFiles.map(key => manifest[key].file)
58+
59+
const assetsPath = argument('assets') ?? dirname(foundManifestPath)+'/assets'
6160

62-
info(`Verify assets in [${assetsDirectory}].`)
61+
info(`Verify assets in [${assetsPath}].`)
6362

64-
const allAssets = readdirSync(assetsDirectory, { withFileTypes: true })
63+
const existingAssets = readdirSync(assetsPath, { withFileTypes: true })
6564

66-
const orphanedAssets = allAssets.filter(file => file.isFile())
65+
const orphanedAssets = existingAssets.filter(file => file.isFile())
6766
.filter(file => manifestAssets.findIndex(asset => asset.endsWith(`/${file.name}`)) === -1)
6867

6968
if (orphanedAssets.length === 0) {
@@ -74,13 +73,13 @@ const main = () => {
7473
: info(`[${orphanedAssets.length}] orphaned assets found.`)
7574

7675
orphanedAssets.forEach(asset => {
77-
const path = `${assetsDirectory}/${asset.name}`
76+
const path = `${assetsPath}/${asset.name}`
7877

79-
dryRun
78+
option(`dry-run`)
8079
? info(`Orphaned asset [${path}] would be removed.`)
8180
: info(`Removing orphaned asset [${path}].`)
8281

83-
if (! dryRun) {
82+
if (! option(`dry-run`)) {
8483
unlinkSync(path)
8584
}
8685
})

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
141141
base: userConfig.base ?? (command === 'build' ? resolveBase(pluginConfig, assetUrl) : ''),
142142
publicDir: userConfig.publicDir ?? false,
143143
build: {
144-
manifest: userConfig.build?.manifest ?? (!ssr ? 'manifest.json' : false),
144+
manifest: userConfig.build?.manifest ?? (ssr ? false : 'manifest.json'),
145+
ssrManifest: userConfig.build?.ssrManifest ?? 'ssr-manifest.json',
145146
outDir: userConfig.build?.outDir ?? resolveOutDir(pluginConfig, ssr),
146147
rollupOptions: {
147148
input: userConfig.build?.rollupOptions?.input ?? resolveInput(pluginConfig, ssr)

0 commit comments

Comments
 (0)