Skip to content

Commit 3dc76e5

Browse files
JulianCataldobashmish
authored andcommitted
feat: glob patterns exclusion for external assets
Fixes #811
1 parent 2f04ee7 commit 3dc76e5

7 files changed

+27
-5
lines changed

packages/rollup-plugin-html/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@
4848
"glob": "^10.0.0",
4949
"html-minifier-terser": "^7.1.0",
5050
"lightningcss": "^1.24.0",
51-
"parse5": "^6.0.1"
51+
"parse5": "^6.0.1",
52+
"picomatch": "^4.0.1"
5253
},
5354
"devDependencies": {
5455
"@types/html-minifier-terser": "^7.0.0",
55-
"rollup": "^4.4.0"
56+
"rollup": "^4.4.0",
57+
"@types/picomatch": "^2.3.3"
5658
}
5759
}

packages/rollup-plugin-html/src/RollupPluginHTMLOptions.ts

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export interface RollupPluginHTMLOptions {
2929
transformHtml?: TransformHtmlFunction | TransformHtmlFunction[];
3030
/** Whether to extract and bundle assets referenced in HTML. Defaults to true. */
3131
extractAssets?: boolean;
32+
/** Whether to ignore assets referenced in HTML with glob patterns. */
33+
external?: string[];
3234
/** Define a full absolute url to your site (e.g. https://domain.com) */
3335
absoluteBaseUrl?: string;
3436
/** Whether to set full absolute urls for ['meta[property=og:image]', 'link[rel=canonical]', 'meta[property=og:url]'] or not. Requires a absoluteBaseUrl to be set. Default to true. */

packages/rollup-plugin-html/src/input/extract/extractAssets.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Document, serialize } from 'parse5';
22
import fs from 'fs';
33
import path from 'path';
4+
import picomatch from 'picomatch';
45
import { InputAsset } from '../InputData.js';
56
import {
67
findAssets,
@@ -15,6 +16,7 @@ export interface ExtractAssetsParams {
1516
htmlDir: string;
1617
rootDir: string;
1718
absolutePathPrefix?: string;
19+
external: string[];
1820
}
1921

2022
export function extractAssets(params: ExtractAssetsParams): InputAsset[] {
@@ -24,6 +26,9 @@ export function extractAssets(params: ExtractAssetsParams): InputAsset[] {
2426
for (const node of assetNodes) {
2527
const sourcePaths = getSourcePaths(node);
2628
for (const sourcePath of sourcePaths) {
29+
const isExternal = picomatch(params.external);
30+
if (isExternal(sourcePath)) continue;
31+
2732
const filePath = resolveAssetFilePath(
2833
sourcePath,
2934
params.htmlDir,

packages/rollup-plugin-html/src/input/extract/extractModulesAndAssets.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ export interface ExtractParams {
88
htmlFilePath: string;
99
rootDir: string;
1010
extractAssets: boolean;
11+
external: string[];
1112
absolutePathPrefix?: string;
1213
}
1314

1415
export function extractModulesAndAssets(params: ExtractParams) {
15-
const { html, htmlFilePath, rootDir, absolutePathPrefix } = params;
16+
const { html, htmlFilePath, rootDir, absolutePathPrefix, external } = params;
1617
const htmlDir = path.dirname(htmlFilePath);
1718
const document = parse(html);
1819

@@ -24,7 +25,7 @@ export function extractModulesAndAssets(params: ExtractParams) {
2425
absolutePathPrefix,
2526
});
2627
const assets = params.extractAssets
27-
? extractAssets({ document, htmlDir, htmlFilePath, rootDir, absolutePathPrefix })
28+
? extractAssets({ document, htmlDir, htmlFilePath, rootDir, absolutePathPrefix, external })
2829
: [];
2930

3031
// turn mutated AST back to a string

packages/rollup-plugin-html/src/input/getInputData.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,20 @@ export interface CreateInputDataParams {
3131
rootDir: string;
3232
filePath?: string;
3333
extractAssets: boolean;
34+
external: string[];
3435
absolutePathPrefix?: string;
3536
}
3637

3738
function createInputData(params: CreateInputDataParams): InputData {
38-
const { name, html, rootDir, filePath, extractAssets, absolutePathPrefix } = params;
39+
const { name, html, rootDir, filePath, extractAssets, absolutePathPrefix, external } = params;
3940
const htmlFilePath = filePath ? filePath : path.resolve(rootDir, name);
4041
const result = extractModulesAndAssets({
4142
html,
4243
htmlFilePath,
4344
rootDir,
4445
extractAssets,
4546
absolutePathPrefix,
47+
external,
4648
});
4749

4850
return {
@@ -65,6 +67,7 @@ export function getInputData(
6567
extractAssets = true,
6668
absolutePathPrefix,
6769
exclude: ignore,
70+
external = [],
6871
} = pluginOptions;
6972
const allInputs = normalizeInputOptions(pluginOptions, rollupInput);
7073

@@ -78,6 +81,7 @@ export function getInputData(
7881
rootDir,
7982
extractAssets,
8083
absolutePathPrefix,
84+
external,
8185
});
8286
result.push(data);
8387
} else if (typeof input.path === 'string') {
@@ -98,6 +102,7 @@ export function getInputData(
98102
filePath,
99103
extractAssets,
100104
absolutePathPrefix,
105+
external,
101106
});
102107
result.push(data);
103108
}

packages/rollup-plugin-html/src/output/getOutputHTML.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export async function getOutputHTML(params: GetOutputHTMLParams) {
5555
emittedAssets,
5656
absolutePathPrefix,
5757
publicPath: pluginOptions.publicPath,
58+
external: pluginOptions.external,
5859
});
5960
}
6061
if (!defaultInjectDisabled) {

packages/rollup-plugin-html/src/output/injectedUpdatedAssetPaths.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getAttribute, setAttribute } from '@web/parse5-utils';
22
import { Document } from 'parse5';
33
import path from 'path';
4+
import picomatch from 'picomatch';
45

56
import {
67
findAssets,
@@ -20,6 +21,7 @@ export interface InjectUpdatedAssetPathsArgs {
2021
outputDir: string;
2122
rootDir: string;
2223
emittedAssets: EmittedAssets;
24+
external: string[];
2325
publicPath?: string;
2426
absolutePathPrefix?: string;
2527
}
@@ -44,12 +46,16 @@ export function injectedUpdatedAssetPaths(args: InjectUpdatedAssetPathsArgs) {
4446
emittedAssets,
4547
publicPath = './',
4648
absolutePathPrefix,
49+
external,
4750
} = args;
4851
const assetNodes = findAssets(document);
4952

5053
for (const node of assetNodes) {
5154
const sourcePaths = getSourcePaths(node);
5255
for (const sourcePath of sourcePaths) {
56+
const isExternal = picomatch(external);
57+
if (isExternal(sourcePath)) continue;
58+
5359
const htmlFilePath = input.filePath ? input.filePath : path.join(rootDir, input.name);
5460
const htmlDir = path.dirname(htmlFilePath);
5561
const filePath = resolveAssetFilePath(sourcePath, htmlDir, rootDir, absolutePathPrefix);

0 commit comments

Comments
 (0)