Skip to content

Commit

Permalink
feat: add --clean flag for build to auto rimraf dist
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Apr 10, 2024
1 parent 3f4f66e commit 35b609a
Show file tree
Hide file tree
Showing 21 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion playground/babel-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion playground/browser-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"main": "./dist/index.cjs",
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
}
}
2 changes: 1 addition & 1 deletion playground/custom-dist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"module": "./lib/index.esm.js",
"types": "./lib/index.d.ts",
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf lib"
}
}
2 changes: 1 addition & 1 deletion playground/default-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"main": "./dist/index.cjs",
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
}
}
2 changes: 1 addition & 1 deletion playground/dummy-commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"src"
],
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
}
}
2 changes: 1 addition & 1 deletion playground/dummy-lodash-es/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"src"
],
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist",
"test": "node test.cjs && node test.mjs"
},
Expand Down
2 changes: 1 addition & 1 deletion playground/dummy-lodash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"src"
],
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist",
"test": "node test.cjs && node test.mjs"
},
Expand Down
2 changes: 1 addition & 1 deletion playground/dummy-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"src"
],
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
}
}
2 changes: 1 addition & 1 deletion playground/extra-bundles/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"main": "./dist/index.js",
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
}
}
2 changes: 1 addition & 1 deletion playground/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"main": "./dist/index.js",
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
}
}
2 changes: 1 addition & 1 deletion playground/multi-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
}
},
"scripts": {
"build": "run-s clean && pkg build --strict && svelte-kit sync && svelte-package -i ./src/svelte -o dist-svelte && pkg check --strict",
"build": "pkg build --strict --clean && svelte-kit sync && svelte-package -i ./src/svelte -o dist-svelte && pkg check --strict",
"clean": "rimraf dist"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion playground/multi-exports-commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
}
},
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist plugin.js"
}
}
2 changes: 1 addition & 1 deletion playground/reference-esm-only/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"src"
],
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion playground/styled-components-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"src"
],
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist",
"test": "node test.mjs"
},
Expand Down
2 changes: 1 addition & 1 deletion playground/ts-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
}
}
2 changes: 1 addition & 1 deletion playground/ts-node16/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
}
}
2 changes: 1 addition & 1 deletion playground/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"check:types": "tsc",
"clean": "rimraf dist"
}
Expand Down
2 changes: 1 addition & 1 deletion playground/use-client-directive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "run-s clean && pkg build --strict --check",
"build": "pkg build --strict --check --clean",
"clean": "rimraf dist"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src/cli/buildAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export async function buildAction(options: {
emitDeclarationOnly?: boolean
strict?: boolean
tsconfig?: string
clean?: boolean
}): Promise<void> {
try {
await build({
cwd: process.cwd(),
emitDeclarationOnly: options.emitDeclarationOnly,
strict: options.strict,
tsconfig: options.tsconfig,
clean: options.clean,
})
} catch (err) {
handleError(err)
Expand Down
1 change: 1 addition & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cli
.option('--strict', 'Strict mode')
.option('--tsconfig [tsconfig]', '[string] tsconfig.json')
.option('--check', 'Run the check command after build (same as running `pkg build && pkg check`)')
.option('--clean', 'Clean the dist directory before building')
.action(async (options) => {
const {check = false, ...buildOptions} = options
const {buildAction} = await import('./buildAction')
Expand Down
21 changes: 20 additions & 1 deletion src/node/build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import path from 'node:path'

import rimraf from 'rimraf'

import {loadConfig, loadPkgWithReporting} from './core'
import {createLogger} from './logger'
import {resolveBuildContext} from './resolveBuildContext'
Expand Down Expand Up @@ -29,11 +33,19 @@ export async function build(options: {
emitDeclarationOnly?: boolean
strict?: boolean
tsconfig?: string
clean?: boolean
}): Promise<void> {
const {cwd, emitDeclarationOnly, strict = false, tsconfig: tsconfigOption} = options
const {
cwd,
emitDeclarationOnly,
strict = false,
tsconfig: tsconfigOption,
clean = false,
} = options
const logger = createLogger()

const config = await loadConfig({cwd})

const legacyExports = config?.legacyExports ?? false
const pkg = await loadPkgWithReporting({cwd, logger, strict, legacyExports})

Expand All @@ -49,6 +61,13 @@ export async function build(options: {
tsconfig,
})

if (clean) {
logger.log(
`Cleaning the contents of the \`dist\` folder: './${path.relative(cwd, ctx.distPath)}'`,
)
await rimraf(ctx.distPath)
}

const buildTasks = resolveBuildTasks(ctx)

for (const task of buildTasks) {
Expand Down

0 comments on commit 35b609a

Please sign in to comment.