-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.ts
192 lines (171 loc) · 7.59 KB
/
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import {
buildCSSInjectionCode,
buildJsCssMap,
clearImportedCssViteMetadataFromBundle,
globalCssInjection,
isCSSRequest,
relativeCssInjection,
removeLinkStyleSheets,
resolveInjectionCode,
warnLog,
} from './utils.js';
import type { OutputAsset } from 'rollup';
import type { Plugin, ResolvedConfig } from 'vite';
import type { DevOptions, PluginConfiguration } from './interface';
/**
* Inject the CSS compiled with JS.
*
* @return {Plugin}
*/
export default function cssInjectedByJsPlugin({
cssAssetsFilterFunction,
dev: { enableDev, removeStyleCode, removeStyleCodeFunction } = {} as DevOptions,
injectCode,
injectCodeFunction,
injectionCodeFormat,
jsAssetsFilterFunction,
preRenderCSSCode,
relativeCSSInjection,
styleId,
suppressUnusedCssWarning,
topExecutionPriority,
useStrictCSP,
}: PluginConfiguration | undefined = {}): Plugin[] {
let config: ResolvedConfig;
const topExecutionPriorityFlag = typeof topExecutionPriority == 'boolean' ? topExecutionPriority : true;
const plugins: Plugin[] = [
{
apply: 'build',
enforce: 'post',
name: 'vite-plugin-css-injected-by-js',
config(config, env) {
if (env.command === 'build') {
if (!config.build) {
config.build = {};
}
if (relativeCSSInjection == true) {
if (!config.build.cssCodeSplit) {
config.build.cssCodeSplit = true;
warnLog(
`[vite-plugin-css-injected-by-js] Override of 'build.cssCodeSplit' option to true, it must be true when 'relativeCSSInjection' is enabled.`
);
}
}
}
},
configResolved(_config) {
config = _config;
},
async generateBundle(opts, bundle) {
if (config.build.ssr) {
return;
}
const buildCssCode = (cssToInject: string) =>
buildCSSInjectionCode({
buildOptions: config.build,
cssToInject:
typeof preRenderCSSCode == 'function' ? preRenderCSSCode(cssToInject) : cssToInject,
injectCode,
injectCodeFunction,
injectionCodeFormat,
styleId,
useStrictCSP,
});
const cssAssetsFilter = (asset: OutputAsset): boolean => {
return typeof cssAssetsFilterFunction == 'function' ? cssAssetsFilterFunction(asset) : true;
};
const cssAssets = Object.keys(bundle).filter(
(i) =>
bundle[i].type == 'asset' &&
bundle[i].fileName.endsWith('.css') &&
cssAssetsFilter(bundle[i] as OutputAsset)
);
let unusedCssAssets: string[] = [];
if (relativeCSSInjection) {
const assetsWithCss = buildJsCssMap(bundle, jsAssetsFilterFunction);
await relativeCssInjection(bundle, assetsWithCss, buildCssCode, topExecutionPriorityFlag);
unusedCssAssets = cssAssets.filter((cssAsset) => !!bundle[cssAsset]);
if (!suppressUnusedCssWarning) {
// With all used CSS assets now being removed from the bundle, navigate any that have not been linked and output
const unusedCssAssetsString = unusedCssAssets.join(',');
unusedCssAssetsString.length > 0 &&
warnLog(
`[vite-plugin-css-injected-by-js] Some CSS assets were not included in any known JS: ${unusedCssAssetsString}`
);
}
} else {
const allCssAssets = Object.keys(bundle).filter(
(i) =>
bundle[i].type == 'asset' &&
bundle[i].fileName.endsWith('.css')
);
unusedCssAssets = allCssAssets.filter(cssAsset => !cssAssets.includes(cssAsset));
await globalCssInjection(
bundle,
cssAssets,
buildCssCode,
jsAssetsFilterFunction,
topExecutionPriorityFlag
);
}
clearImportedCssViteMetadataFromBundle(bundle, unusedCssAssets);
const htmlFiles = Object.keys(bundle).filter((i) => i.endsWith('.html'));
for (const name of htmlFiles) {
const htmlChunk = bundle[name] as OutputAsset;
let replacedHtml =
htmlChunk.source instanceof Uint8Array
? new TextDecoder().decode(htmlChunk.source)
: `${htmlChunk.source}`;
cssAssets.forEach(function replaceLinkedStylesheetsHtml(cssName) {
if (!unusedCssAssets.includes(cssName)) {
replacedHtml = removeLinkStyleSheets(replacedHtml, cssName);
htmlChunk.source = replacedHtml;
}
});
}
},
},
];
if (enableDev) {
warnLog(
'[vite-plugin-css-injected-by-js] Experimental dev mode activated! Please, for any error open a issue.'
);
plugins.push({
name: 'vite-plugin-css-injected-by-js-dev',
apply: 'serve',
enforce: 'post',
transform(src, id) {
if (isCSSRequest(id)) {
const defaultRemoveStyleCode = (devId: string) => `{
(function removeStyleInjected() {
const elementsToRemove = document.querySelectorAll("style[data-vite-dev-id='${devId}']");
elementsToRemove.forEach(element => {
element.remove();
});
})()
}`;
let removeStyleFunction: (id: string) => string = removeStyleCode || defaultRemoveStyleCode;
if (removeStyleCodeFunction) {
removeStyleFunction = (id) => `(${removeStyleCodeFunction})("${id}")`;
}
// removeStyleFunction is called before since the function that inject the CSS doesn't handle the update case required in dev mode.
let injectionCode = src.replace(
'__vite__updateStyle(__vite__id, __vite__css)',
';\n' +
removeStyleFunction(id) +
';\n' +
resolveInjectionCode('__vite__css', injectCode, injectCodeFunction, {
attributes: { type: 'text/css', ['data-vite-dev-id']: id },
})
);
injectionCode = injectionCode.replace('__vite__removeStyle(__vite__id)', removeStyleFunction(id));
return {
code: injectionCode,
map: null,
};
}
},
});
}
return plugins;
}