Skip to content

Commit

Permalink
fix: Improve file selector not to include unnecessary files into Webp…
Browse files Browse the repository at this point in the history
…ub/EPUB

fix #461
  • Loading branch information
spring-raining committed Jan 17, 2024
1 parent e26985f commit a1921e2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 27 deletions.
36 changes: 21 additions & 15 deletions src/output/webbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import fs from 'node:fs';
import MIMEType from 'whatwg-mimetype';
import { MANIFEST_FILENAME } from '../const.js';
import { MergedConfig, WebbookEntryConfig } from '../input/config.js';
import { generateManifest, globAssetFiles } from '../processor/compile.js';
import {
generateManifest,
getAutoGeneratedAssetPatterns,
getDefaultIgnorePatterns,
globAssetFiles,
} from '../processor/compile.js';
import {
ResourceLoader,
fetchLinkedPublicationManifest,
Expand All @@ -18,7 +23,6 @@ import {
debug,
logError,
logUpdate,
pathContains,
pathEquals,
remove,
safeGlob,
Expand Down Expand Up @@ -247,10 +251,14 @@ export async function copyWebPublicationAssets({
exportAliases,
outputs,
copyAsset,
themesDir,
manifestPath,
input,
outputDir,
}: Pick<MergedConfig, 'exportAliases' | 'outputs' | 'copyAsset'> & {
}: Pick<
MergedConfig,
'exportAliases' | 'outputs' | 'copyAsset' | 'themesDir'
> & {
input: string;
outputDir: string;
manifestPath: string;
Expand All @@ -261,21 +269,12 @@ export async function copyWebPublicationAssets({
target: upath.relative(input, target),
}))
.filter(({ source }) => !source.startsWith('..'));
const ignore = [
// don't copy auto-generated assets
...outputs.flatMap(({ format, path: p }) =>
!pathContains(input, p)
? []
: format === 'webpub'
? upath.join(upath.relative(input, p), '**')
: upath.relative(input, p),
),
];
const allFiles = new Set([
...(await globAssetFiles({
copyAsset,
cwd: input,
ignore,
outputs,
themesDir,
})),
...(await safeGlob(
[
Expand All @@ -285,7 +284,14 @@ export async function copyWebPublicationAssets({
{
cwd: input,
ignore: [
...ignore,
...getAutoGeneratedAssetPatterns({
cwd: input,
outputs,
}),
...getDefaultIgnorePatterns({
cwd: input,
themesDir,
}),
// only include dotfiles starting with `.vs-`
'**/.!(vs-*)/**',
],
Expand Down
65 changes: 53 additions & 12 deletions src/processor/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,22 +362,68 @@ export async function compile({
}
}

export function getDefaultIgnorePatterns({
themesDir,
cwd,
}: Pick<MergedConfig, 'themesDir'> & {
cwd: string;
}): string[] {
const ignorePatterns = [
// ignore node_modules directory
'**/node_modules',
];
if (pathContains(cwd, themesDir)) {
// ignore example files of theme packages
ignorePatterns.push(
`${upath.relative(cwd, themesDir)}/packages/*/example`,
`${upath.relative(cwd, themesDir)}/packages/*/*/example`,
);
}
return ignorePatterns;
}

export function getAutoGeneratedAssetPatterns({
outputs,
cwd,
}: Pick<MergedConfig, 'outputs'> & {
cwd: string;
}): string[] {
return outputs.flatMap(({ format, path: p }) =>
!pathContains(cwd, p)
? []
: format === 'webpub'
? upath.join(upath.relative(cwd, p), '**')
: upath.relative(cwd, p),
);
}

export async function globAssetFiles({
copyAsset: { fileExtensions, includes, excludes },
outputs,
themesDir,
cwd,
ignore = [],
}: Pick<MergedConfig, 'copyAsset'> & {
}: Pick<MergedConfig, 'copyAsset' | 'outputs' | 'themesDir'> & {
cwd: string;
ignore?: string[];
}): Promise<Set<string>> {
const ignorePatterns = [
...ignore,
...excludes,
...getAutoGeneratedAssetPatterns({ outputs, cwd }),
];
const weakIgnorePatterns = getDefaultIgnorePatterns({ themesDir, cwd });
debug('globAssetFiles > ignorePatterns', ignorePatterns);
debug('globAssetFiles > weakIgnorePatterns', weakIgnorePatterns);

const assets = new Set([
// Step 1: Glob files with an extension in `fileExtension`
// Ignore files in node_modules directory and files matched `excludes`
// Ignore files in node_modules directory, theme example files and files matched `excludes`
...(await safeGlob(
fileExtensions.map((ext) => `**/*.${ext}`),
{
cwd,
ignore: [...ignore, ...excludes, '**/node_modules'],
ignore: [...ignorePatterns, ...weakIgnorePatterns],
followSymbolicLinks: true,
gitignore: false,
},
Expand All @@ -386,7 +432,7 @@ export async function globAssetFiles({
// Ignore only files matched `excludes`
...(await safeGlob(includes, {
cwd,
ignore: [...ignore, ...excludes],
ignore: ignorePatterns,
followSymbolicLinks: true,
gitignore: false,
})),
Expand All @@ -399,6 +445,7 @@ export async function copyAssets({
workspaceDir,
copyAsset,
outputs,
themesDir,
}: MergedConfig): Promise<void> {
if (pathEquals(entryContextDir, workspaceDir)) {
return;
Expand All @@ -407,15 +454,9 @@ export async function copyAssets({
const assets = await globAssetFiles({
copyAsset,
cwd: entryContextDir,
outputs,
themesDir,
ignore: [
// don't copy auto-generated assets
...outputs.flatMap(({ format, path: p }) =>
!pathContains(entryContextDir, p)
? []
: format === 'webpub'
? upath.join(upath.relative(entryContextDir, p), '**')
: upath.relative(entryContextDir, p),
),
// don't copy workspace itself
...(relWorkspaceDir ? [upath.join(relWorkspaceDir, '**')] : []),
],
Expand Down

0 comments on commit a1921e2

Please sign in to comment.