Skip to content

Commit

Permalink
Merge pull request #17 from craftamap/disable-finding-related-files
Browse files Browse the repository at this point in the history
related-files: add option to disable finding related files (#16)
  • Loading branch information
craftamap authored Mar 12, 2022
2 parents c9b9355 + 7c22d45 commit 1f4224d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ interface HtmlFileConfiguration {
// Decide if the script tag will be inserted as blocking script tag,
// with `defer=""` (default) or with `type="module"`
favicon?: string, // path to favicon.ico. If not specified, no favicon will be injected
findRelatedOutputFiles?: boolean,
// Find related output (*.css)-files and inject them into the html.
// Defaults to true.
}
```

Expand Down
1 change: 1 addition & 0 deletions lib/cjs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export interface HtmlFileConfiguration {
define?: Record<string, string>;
scriptLoading?: 'blocking' | 'defer' | 'module';
favicon?: string;
findRelatedOutputFiles?: boolean;
}
export declare const htmlPlugin: (configuration?: Configuration) => esbuild.Plugin;
11 changes: 10 additions & 1 deletion lib/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
const htmlPlugin = (configuration = { files: [], }) => {
configuration.files = configuration.files.map((htmlFileConfiguration) => {
return Object.assign({}, { 'findRelatedOutputFiles': true }, htmlFileConfiguration); // Set default values
});
let logInfo = false;
function collectEntrypoints(htmlFileConfiguration, metafile) {
const entryPoints = Object.entries((metafile === null || metafile === void 0 ? void 0 : metafile.outputs) || {}).filter(([, value]) => {
Expand Down Expand Up @@ -166,7 +169,13 @@ const htmlPlugin = (configuration = { files: [], }) => {
if (!entrypoint) {
throw new Error(`Found no match for ${htmlFileConfiguration.entryPoints}`);
}
const relatedOutputFiles = findRelatedOutputFiles(entrypoint, result.metafile, build.initialOptions.entryNames);
let relatedOutputFiles;
if (htmlFileConfiguration.findRelatedOutputFiles) {
relatedOutputFiles = findRelatedOutputFiles(entrypoint, result.metafile, build.initialOptions.entryNames);
}
else {
relatedOutputFiles = [entrypoint];
}
collectedOutputFiles = [...collectedOutputFiles, ...relatedOutputFiles];
}
// Note: we can safely disable this rule here, as we already asserted this in setup.onStart
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface HtmlFileConfiguration {
define?: Record<string, string>,
scriptLoading?: 'blocking' | 'defer' | 'module',
favicon?: string,
findRelatedOutputFiles?: boolean,
}

const defaultHtmlTemplate = `
Expand All @@ -41,6 +42,10 @@ function escapeRegExp(text: string): string {


export const htmlPlugin = (configuration: Configuration = { files: [], }): esbuild.Plugin => {
configuration.files = configuration.files.map((htmlFileConfiguration: HtmlFileConfiguration) => {
return Object.assign({}, { 'findRelatedOutputFiles': true }, htmlFileConfiguration) // Set default values
})

let logInfo = false

function collectEntrypoints(htmlFileConfiguration: HtmlFileConfiguration, metafile?: esbuild.Metafile) {
Expand Down Expand Up @@ -193,8 +198,12 @@ export const htmlPlugin = (configuration: Configuration = { files: [], }): esbui
if (!entrypoint) {
throw new Error(`Found no match for ${htmlFileConfiguration.entryPoints}`)
}

const relatedOutputFiles = findRelatedOutputFiles(entrypoint, result.metafile, build.initialOptions.entryNames)
let relatedOutputFiles
if (htmlFileConfiguration.findRelatedOutputFiles) {
relatedOutputFiles = findRelatedOutputFiles(entrypoint, result.metafile, build.initialOptions.entryNames)
} else {
relatedOutputFiles = [entrypoint]
}

collectedOutputFiles = [...collectedOutputFiles, ...relatedOutputFiles]
}
Expand Down

0 comments on commit 1f4224d

Please sign in to comment.