This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
cssResolveURL.ts
150 lines (132 loc) · 4.03 KB
/
cssResolveURL.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import * as path from 'path';
import { IStyleSheetProps } from '../config/IStylesheetProps';
import { FONT_EXTENSIONS, IMAGE_EXTENSIONS } from '../config/extensions';
import { Context } from '../core/context';
import { fastHash, fileExists, joinFuseBoxPath } from '../utils/utils';
import { replaceCSSMacros } from './cssResolveModule';
export interface ICSSResolveURLProps {
contents: string;
ctx: Context;
filePath: string;
options: IStyleSheetProps;
}
const Expression = /url\(([^\)]+)\)/gm;
function extractValue(input: string) {
const first = input.charCodeAt(0);
const last = input.charCodeAt(input.length - 1);
if (first === 39 || first === 34) {
input = input.slice(1);
}
if (last === 39 || last === 34) {
input = input.slice(0, -1);
}
if (/data:/.test(input)) {
return;
}
return input;
}
export interface IURLReplaced {
destination: string;
original: string;
publicPath: string;
}
export function defineResourceGroup(extension) {
if (IMAGE_EXTENSIONS.includes(extension)) {
return 'images';
}
if (FONT_EXTENSIONS.includes(extension)) {
return 'fonts';
}
if (extension === '.svg') {
return 'svg';
}
if (extension === '.ico') {
return 'ico';
}
if (extension === '.pdf') {
return 'documents';
}
}
export function resolveCSSResource(target, props: ICSSResolveURLProps): IURLReplaced {
const root = path.dirname(props.filePath);
// getting rid of url#something or url?time=12
target = target.replace(/[?\#].*$/, '');
if (props.options.macros) {
target = replaceCSSMacros(target, props.options.macros);
}
if (!path.isAbsolute(target)) {
target = path.join(root, target);
}
if (fileExists(target)) {
let fileGroup: string;
// setting file group
if (props.options.groupResourcesFilesByType) {
fileGroup = defineResourceGroup(path.extname(target).toLowerCase());
}
let name = fastHash(target) + path.extname(target);
if (fileGroup) {
name = fileGroup + '/' + name;
}
const resourceConfig = props.ctx.config.getResourceConfig(props.options);
const resourcePublicRoot = resourceConfig.resourcePublicRoot;
const publicPath = joinFuseBoxPath(resourcePublicRoot, name);
const destination = path.join(resourceConfig.resourceFolder, name);
// we don't want to copy a file if that was copied before
if (props.options.ignoreChecksForCopiedResources) {
props.ctx.taskManager.copyFile(target, destination);
} else {
if (!fileExists(destination)) {
props.ctx.taskManager.copyFile(target, destination);
}
}
return { destination, original: target, publicPath };
}
}
export interface ICSSResolveURLResult {
contents: string;
replaced: Array<IURLReplaced>;
}
export function mapErrorLine(contents: string, offset: number) {
let line = 1;
let leftOffset = 0;
for (let i = 0; i <= offset; i++) {
if (contents[i] === '\n') {
leftOffset = 0;
line++;
} else {
leftOffset++;
}
}
return `${line}:${leftOffset}`;
}
export function cssResolveURL(props: ICSSResolveURLProps): ICSSResolveURLResult {
let contents = props.contents;
const replaced: Array<IURLReplaced> = [];
contents = contents.replace(Expression, (match, data, offset, input_string) => {
let value = extractValue(data);
if (typeof value === 'undefined') {
return match;
}
// apply macros before checking for special words
if (props.options.macros) {
value = replaceCSSMacros(value, props.options.macros);
}
// preserving svg filters, http links, data:image (base64), sass and less variables
if (/^#|data:|http|\$|@/.test(value)) {
// return those as is
return match;
}
const result = resolveCSSResource(value, props);
if (result) {
replaced.push(result);
return `url("${result.publicPath}")`;
}
props.ctx.log.warn('Failed to resolve $value in $file:$line', {
file: props.filePath,
line: mapErrorLine(contents, offset),
value: value,
});
return match;
});
return { contents, replaced };
}