Skip to content

Commit

Permalink
feat: add --publicDir [dir] flag
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 14, 2022
1 parent 002cb99 commit 3da1c00
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,12 @@ export default defineConfig({
- When building the cjs bundle, it will compile `import.meta.url` as `typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href`
- When building the esm bundle, it will compile `__dirname` as `path.dirname(fileURLToPath(import.meta.url))`

### Copy files to output directory

Use `--publicDir` flag to copy files inside `./public` folder to the output directory.

You can also specify a custom directory using `--publicDir another-directory`.

## Troubleshooting

### error: No matching export in "xxx.ts" for import "xxx"
Expand Down
1 change: 1 addition & 0 deletions src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export async function main(options: Options = {}) {
'--treeshake [strategy]',
'Using Rollup for treeshaking instead, "recommended" or "smallest" or "safest"'
)
.option('--publicDir [dir]', 'Copy public directory to output directory')
.action(async (files: string[], flags) => {
const { build } = await import('.')
Object.assign(options, {
Expand Down
19 changes: 19 additions & 0 deletions src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,22 @@ export const outputFile = async (
await fs.promises.mkdir(path.dirname(filepath), { recursive: true })
await fs.promises.writeFile(filepath, data, options)
}

export function copyDirSync(srcDir: string, destDir: string): void {
if (!fs.existsSync(srcDir)) return

fs.mkdirSync(destDir, { recursive: true })
for (const file of fs.readdirSync(srcDir)) {
const srcFile = path.resolve(srcDir, file)
if (srcFile === destDir) {
continue
}
const destFile = path.resolve(destDir, file)
const stat = fs.statSync(srcFile)
if (stat.isDirectory()) {
copyDirSync(srcFile, destFile)
} else {
fs.copyFileSync(srcFile, destFile)
}
}
}
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { PluginContainer } from './plugin'
import { es5 } from './plugins/es5'
import { sizeReporter } from './plugins/size-reporter'
import { treeShakingPlugin } from './plugins/tree-shaking'
import { copyPublicDir, isInPublicDir } from './lib/public-dir'

export type { Format, Options, NormalizedOptions }

Expand Down Expand Up @@ -122,7 +123,7 @@ const normalizeOptions = async (
if (!options.target) {
options.target = 'node14'
}

return options as NormalizedOptions
}

Expand Down Expand Up @@ -328,6 +329,16 @@ export async function build(_options: Options) {
})
watcher.on('all', async (type, file) => {
file = slash(file)

if (
options.publicDir &&
isInPublicDir(options.publicDir, file)
) {
logger.info('CLI', `Change in public dir: ${file}`)
copyPublicDir(options.publicDir, options.outDir)
return
}

// By default we only rebuild when imported files change
// If you specify custom `watch`, a string or multiple strings
// We rebuild when those files change
Expand Down Expand Up @@ -355,6 +366,7 @@ export async function build(_options: Options) {
logger.info('CLI', `Target: ${options.target}`)

await buildAll()
copyPublicDir(options.publicDir, options.outDir)

startWatcher()
}
Expand Down
22 changes: 22 additions & 0 deletions src/lib/public-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import path from 'path'
import { copyDirSync } from '../fs'
import { slash } from '../utils'

export const copyPublicDir = (
publicDir: string | boolean | undefined,
outDir: string
) => {
if (!publicDir) return
copyDirSync(path.resolve(publicDir === true ? 'public' : publicDir), outDir)
}

export const isInPublicDir = (
publicDir: string | boolean | undefined,
filePath: string
) => {
if (!publicDir) return false
const publicPath = slash(
path.resolve(publicDir === true ? 'public' : publicDir)
)
return slash(path.resolve(filePath)).startsWith(`${publicPath}/`)
}
4 changes: 4 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ export type Options = {
* This can result in smaller bundle size
*/
treeshake?: TreeshakingStrategy
/**
* Copy the files inside `publicDir` to output directory
*/
publicDir?: string | boolean
}

export type NormalizedOptions = Omit<
Expand Down

0 comments on commit 3da1c00

Please sign in to comment.