Skip to content

Commit 50c410e

Browse files
committed
fix(rollup-plugin-html): properly exclude paths stating with ./
1 parent 1815649 commit 50c410e

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

packages/rollup-plugin-html/src/assets/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Document, Element } from 'parse5';
22
import path from 'path';
3+
import picomatch from 'picomatch';
34
import { findElements, getTagName, getAttribute } from '@web/parse5-utils';
45
import { createError } from '../utils.js';
56
import { serialize } from 'v8';
@@ -143,3 +144,11 @@ export function getSourcePaths(node: Element) {
143144
export function findAssets(document: Document) {
144145
return findElements(document, isAsset);
145146
}
147+
148+
// picomatch follows glob spec and requires "./" to be removed for the matcher to work
149+
// it is safe, because with or without it resolves to the same file
150+
// read more: https://github.com/micromatch/picomatch/issues/77
151+
const removeLeadingSlash = (str: string) => (str.startsWith('./') ? str.slice(2) : str);
152+
export function createAssetPicomatchMatcher(glob?: string | string[]) {
153+
return picomatch(glob || [], { format: removeLeadingSlash });
154+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Document, serialize } from 'parse5';
22
import fs from 'fs';
33
import path from 'path';
4-
import picomatch from 'picomatch';
54
import { InputAsset } from '../InputData.js';
65
import {
76
findAssets,
87
getSourcePaths,
98
isHashedAsset,
109
resolveAssetFilePath,
10+
createAssetPicomatchMatcher,
1111
} from '../../assets/utils.js';
1212

1313
export interface ExtractAssetsParams {
@@ -22,7 +22,7 @@ export interface ExtractAssetsParams {
2222
export function extractAssets(params: ExtractAssetsParams): InputAsset[] {
2323
const assetNodes = findAssets(params.document);
2424
const allAssets: InputAsset[] = [];
25-
const isExternal = picomatch(params.externalAssets || []);
25+
const isExternal = createAssetPicomatchMatcher(params.externalAssets);
2626

2727
for (const node of assetNodes) {
2828
const sourcePaths = getSourcePaths(node);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { getAttribute, setAttribute } from '@web/parse5-utils';
22
import { Document } from 'parse5';
33
import path from 'path';
4-
import picomatch from 'picomatch';
54

65
import {
76
findAssets,
87
getSourceAttribute,
98
getSourcePaths,
109
isHashedAsset,
1110
resolveAssetFilePath,
11+
createAssetPicomatchMatcher,
1212
} from '../assets/utils.js';
1313
import { InputData } from '../input/InputData.js';
1414
import { createError } from '../utils.js';
@@ -49,7 +49,7 @@ export function injectedUpdatedAssetPaths(args: InjectUpdatedAssetPathsArgs) {
4949
absolutePathPrefix,
5050
} = args;
5151
const assetNodes = findAssets(document);
52-
const isExternal = picomatch(externalAssets || []);
52+
const isExternal = createAssetPicomatchMatcher(externalAssets);
5353

5454
for (const node of assetNodes) {
5555
const sourcePaths = getSourcePaths(node);

0 commit comments

Comments
 (0)