Skip to content

Commit 35b609a

Browse files
committedApr 10, 2024··
feat: add --clean flag for build to auto rimraf dist
1 parent 3f4f66e commit 35b609a

File tree

21 files changed

+41
-19
lines changed

21 files changed

+41
-19
lines changed
 

‎playground/babel-plugin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"main": "./dist/index.cjs",
1818
"types": "./dist/index.d.ts",
1919
"scripts": {
20-
"build": "run-s clean && pkg build --strict --check",
20+
"build": "pkg build --strict --check --clean",
2121
"clean": "rimraf dist"
2222
},
2323
"devDependencies": {

‎playground/browser-bundle/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"main": "./dist/index.cjs",
1818
"scripts": {
19-
"build": "run-s clean && pkg build --strict --check",
19+
"build": "pkg build --strict --check --clean",
2020
"clean": "rimraf dist"
2121
}
2222
}

‎playground/custom-dist/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"module": "./lib/index.esm.js",
1919
"types": "./lib/index.d.ts",
2020
"scripts": {
21-
"build": "run-s clean && pkg build --strict --check",
21+
"build": "pkg build --strict --check --clean",
2222
"clean": "rimraf lib"
2323
}
2424
}

‎playground/default-export/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"main": "./dist/index.cjs",
1818
"scripts": {
19-
"build": "run-s clean && pkg build --strict --check",
19+
"build": "pkg build --strict --check --clean",
2020
"clean": "rimraf dist"
2121
}
2222
}

‎playground/dummy-commonjs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"src"
4545
],
4646
"scripts": {
47-
"build": "run-s clean && pkg build --strict --check",
47+
"build": "pkg build --strict --check --clean",
4848
"clean": "rimraf dist"
4949
}
5050
}

‎playground/dummy-lodash-es/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"src"
2323
],
2424
"scripts": {
25-
"build": "run-s clean && pkg build --strict --check",
25+
"build": "pkg build --strict --check --clean",
2626
"clean": "rimraf dist",
2727
"test": "node test.cjs && node test.mjs"
2828
},

‎playground/dummy-lodash/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"src"
2323
],
2424
"scripts": {
25-
"build": "run-s clean && pkg build --strict --check",
25+
"build": "pkg build --strict --check --clean",
2626
"clean": "rimraf dist",
2727
"test": "node test.cjs && node test.mjs"
2828
},

‎playground/dummy-module/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"src"
5252
],
5353
"scripts": {
54-
"build": "run-s clean && pkg build --strict --check",
54+
"build": "pkg build --strict --check --clean",
5555
"clean": "rimraf dist"
5656
}
5757
}

‎playground/extra-bundles/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"main": "./dist/index.js",
1717
"scripts": {
18-
"build": "run-s clean && pkg build --strict --check",
18+
"build": "pkg build --strict --check --clean",
1919
"clean": "rimraf dist"
2020
}
2121
}

‎playground/js/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"main": "./dist/index.js",
1717
"scripts": {
18-
"build": "run-s clean && pkg build --strict --check",
18+
"build": "pkg build --strict --check --clean",
1919
"clean": "rimraf dist"
2020
}
2121
}

‎playground/multi-export/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
}
6464
},
6565
"scripts": {
66-
"build": "run-s clean && pkg build --strict && svelte-kit sync && svelte-package -i ./src/svelte -o dist-svelte && pkg check --strict",
66+
"build": "pkg build --strict --clean && svelte-kit sync && svelte-package -i ./src/svelte -o dist-svelte && pkg check --strict",
6767
"clean": "rimraf dist"
6868
},
6969
"devDependencies": {

‎playground/multi-exports-commonjs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
}
3636
},
3737
"scripts": {
38-
"build": "run-s clean && pkg build --strict --check",
38+
"build": "pkg build --strict --check --clean",
3939
"clean": "rimraf dist plugin.js"
4040
}
4141
}

‎playground/reference-esm-only/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"src"
4242
],
4343
"scripts": {
44-
"build": "run-s clean && pkg build --strict --check",
44+
"build": "pkg build --strict --check --clean",
4545
"clean": "rimraf dist"
4646
},
4747
"devDependencies": {

‎playground/styled-components-module/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"src"
2222
],
2323
"scripts": {
24-
"build": "run-s clean && pkg build --strict --check",
24+
"build": "pkg build --strict --check --clean",
2525
"clean": "rimraf dist",
2626
"test": "node test.mjs"
2727
},

‎playground/ts-bundler/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"main": "./dist/index.cjs",
1818
"types": "./dist/index.d.ts",
1919
"scripts": {
20-
"build": "run-s clean && pkg build --strict --check",
20+
"build": "pkg build --strict --check --clean",
2121
"clean": "rimraf dist"
2222
}
2323
}

‎playground/ts-node16/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"main": "./dist/index.cjs",
1818
"types": "./dist/index.d.ts",
1919
"scripts": {
20-
"build": "run-s clean && pkg build --strict --check",
20+
"build": "pkg build --strict --check --clean",
2121
"clean": "rimraf dist"
2222
}
2323
}

‎playground/ts/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"main": "./dist/index.cjs",
1818
"types": "./dist/index.d.ts",
1919
"scripts": {
20-
"build": "run-s clean && pkg build --strict --check",
20+
"build": "pkg build --strict --check --clean",
2121
"check:types": "tsc",
2222
"clean": "rimraf dist"
2323
}

‎playground/use-client-directive/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"main": "./dist/index.cjs",
1919
"types": "./dist/index.d.ts",
2020
"scripts": {
21-
"build": "run-s clean && pkg build --strict --check",
21+
"build": "pkg build --strict --check --clean",
2222
"clean": "rimraf dist"
2323
},
2424
"devDependencies": {

‎src/cli/buildAction.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ export async function buildAction(options: {
55
emitDeclarationOnly?: boolean
66
strict?: boolean
77
tsconfig?: string
8+
clean?: boolean
89
}): Promise<void> {
910
try {
1011
await build({
1112
cwd: process.cwd(),
1213
emitDeclarationOnly: options.emitDeclarationOnly,
1314
strict: options.strict,
1415
tsconfig: options.tsconfig,
16+
clean: options.clean,
1517
})
1618
} catch (err) {
1719
handleError(err)

‎src/cli/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ cli
2121
.option('--strict', 'Strict mode')
2222
.option('--tsconfig [tsconfig]', '[string] tsconfig.json')
2323
.option('--check', 'Run the check command after build (same as running `pkg build && pkg check`)')
24+
.option('--clean', 'Clean the dist directory before building')
2425
.action(async (options) => {
2526
const {check = false, ...buildOptions} = options
2627
const {buildAction} = await import('./buildAction')

‎src/node/build.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import path from 'node:path'
2+
3+
import rimraf from 'rimraf'
4+
15
import {loadConfig, loadPkgWithReporting} from './core'
26
import {createLogger} from './logger'
37
import {resolveBuildContext} from './resolveBuildContext'
@@ -29,11 +33,19 @@ export async function build(options: {
2933
emitDeclarationOnly?: boolean
3034
strict?: boolean
3135
tsconfig?: string
36+
clean?: boolean
3237
}): Promise<void> {
33-
const {cwd, emitDeclarationOnly, strict = false, tsconfig: tsconfigOption} = options
38+
const {
39+
cwd,
40+
emitDeclarationOnly,
41+
strict = false,
42+
tsconfig: tsconfigOption,
43+
clean = false,
44+
} = options
3445
const logger = createLogger()
3546

3647
const config = await loadConfig({cwd})
48+
3749
const legacyExports = config?.legacyExports ?? false
3850
const pkg = await loadPkgWithReporting({cwd, logger, strict, legacyExports})
3951

@@ -49,6 +61,13 @@ export async function build(options: {
4961
tsconfig,
5062
})
5163

64+
if (clean) {
65+
logger.log(
66+
`Cleaning the contents of the \`dist\` folder: './${path.relative(cwd, ctx.distPath)}'`,
67+
)
68+
await rimraf(ctx.distPath)
69+
}
70+
5271
const buildTasks = resolveBuildTasks(ctx)
5372

5473
for (const task of buildTasks) {

0 commit comments

Comments
 (0)
Please sign in to comment.