Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

/**
* @import { DomStackOpts, Results } from './lib/builder.js'
* @import { CopyEntry } from './lib/helpers/copy-entry.js'
* @import { TestBuildResult } from './types.js'
* @import { WatchOptions } from './lib/watch/index.js'

*/
import { mkdtemp, readFile, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { basename, isAbsolute, join, relative, resolve } from 'node:path'
import { isAbsolute, join, relative, resolve } from 'node:path'

import { builder } from './lib/builder.js'
import { normalizeCopyEntry, parseCopyEntry } from './lib/helpers/copy-entry.js'
Expand Down Expand Up @@ -152,23 +151,22 @@ export async function testBuild (src, opts = {}) {
}

/**
* Derive anchored `src`-relative ignore patterns for `--copy` directories that
* live inside `src` so they are copied verbatim without being processed as
* pages or static assets, and without ignoring same-named directories elsewhere
* in `src`.
* Derive anchored `src`-relative ignore patterns for directories (such as `dest`
* or `--copy` sources) that live inside `src` so they are skipped without
* ignoring same-named directories elsewhere in `src`.
* @param {string} src
* @param {CopyEntry[]} copyDirs
* @param {string[]} dirs
* @returns {string[]}
*/
function copyDirIgnorePatterns (src, copyDirs) {
function containedDirIgnorePatterns (src, dirs) {
const absSrc = resolve(src)
/** @type {string[]} */
const patterns = []
for (const entry of copyDirs) {
const dir = parseCopyEntry(entry).from
const rel = relative(absSrc, dir)
if (rel !== '' && !rel.startsWith('..') && !isAbsolute(rel)) {
patterns.push(`/${toPosix(rel)}`)
for (const dir of dirs) {
const rel = relative(absSrc, resolve(dir))
const posixRel = toPosix(rel)
if (posixRel !== '' && posixRel !== '..' && !posixRel.startsWith('../') && !isAbsolute(rel)) {
patterns.push(`/${posixRel}`)
}
}
return patterns
Expand All @@ -190,8 +188,7 @@ function normalizeDomStackOpts (opts, src, dest) {
copy: copyDirs,
ignore: [
...DEFAULT_IGNORES,
basename(dest),
...copyDirIgnorePatterns(src, copyDirs),
...containedDirIgnorePatterns(src, [dest, ...copyDirs.map(entry => parseCopyEntry(entry).from)]),
...[buildOpts.ignore ?? []].flat(),
],
}
Expand Down
32 changes: 31 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @import { DomStackOpts } from './lib/builder.js' */
import { test } from 'node:test'
import assert from 'node:assert'
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { isAbsolute, resolve, join } from 'node:path'
import { tmpdir } from 'node:os'
import { DomStack, fingerprint, testBuild } from './index.js'
Expand Down Expand Up @@ -130,3 +130,33 @@ test('copy entries support destination subpath mappings via string and object sy
assert.strictEqual(await build.readOutput('images/2026/logo.txt'), 'logo-bytes')
assert.strictEqual(await build.readOutput('assets/media/clip.txt'), 'clip-bytes')
})

test('dest only adds an anchored ignore pattern when inside src', async (t) => {
const outsideDs = new DomStack(tmpSrc, tmpDest)
assert.ok(!outsideDs.opts.ignore?.includes('domstack-test-dest'), 'external dest basename is not added to ignore')

const nestedDest = join(tmpSrc, 'build', 'public')
const nestedDs = new DomStack(tmpSrc, nestedDest)
assert.ok(!nestedDs.opts.ignore?.includes('public'), 'nested dest bare basename is not added to ignore')
assert.ok(nestedDs.opts.ignore?.includes('/build/public'), 'nested dest is ignored via anchored src-relative path')

// End-to-end verification: external `dest = public` does not hide `src/docs/public/page.md`,
// and nested `dest = src/public` does not hide `src/docs/public/page.md`.
const root = await mkdtemp(join(import.meta.dirname, 'tmp-dest-ignore-'))
t.after(() => rm(root, { recursive: true, force: true }))
const src = join(root, 'src')
const externalDest = join(root, 'public')
await mkdir(join(src, 'docs/public'), { recursive: true })
await writeFile(join(src, 'root.layout.js'), 'export default ({ children }) => children\n')
await writeFile(join(src, 'page.md'), '# Home\n')
await writeFile(join(src, 'docs/public/page.md'), '# Docs public\n')

const externalBuild = new DomStack(src, externalDest)
await externalBuild.build()
assert.match(await readFile(join(externalDest, 'docs/public/index.html'), 'utf8'), /Docs public/)

const nestedBuildDest = join(src, 'public')
const nestedBuild = new DomStack(src, nestedBuildDest)
await nestedBuild.build()
assert.match(await readFile(join(nestedBuildDest, 'docs/public/index.html'), 'utf8'), /Docs public/)
})
Loading