@rollup-extras/plugin-html 1.3.3 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +40 -14
- package/asset-factories/package.json +5 -0
- package/dist/asset-factories.cjs +61 -0
- package/dist/asset-factories.d.ts +6 -0
- package/dist/asset-factories.mjs +56 -0
- package/dist/index.cjs +269 -285
- package/dist/index.d.ts +7 -7
- package/dist/index.mjs +269 -285
- package/dist/shared.cjs +31 -0
- package/dist/shared.d.ts +5 -0
- package/dist/shared.mjs +26 -0
- package/dist/types.d.ts +37 -34
- package/package.json +20 -29
package/dist/index.mjs
CHANGED
|
@@ -4,292 +4,276 @@ import path from 'path';
|
|
|
4
4
|
import { getOptionsObject } from '@rollup-extras/utils/options';
|
|
5
5
|
import logger from '@rollup-extras/utils/logger';
|
|
6
6
|
import { multiConfigPluginBase } from '@rollup-extras/utils/mutli-config-plugin-base';
|
|
7
|
+
import { g as getLinkElement, a as getNonModuleScriptElement, b as getModuleScriptElement, t as toAssetPredicate } from './shared.mjs';
|
|
7
8
|
|
|
8
|
-
const defaultTemplate = '<!DOCTYPE html><html><head></head><body></body></html>';
|
|
9
|
-
const headClosingElement = '</head>';
|
|
10
|
-
const bodyClosingElement = '</body>';
|
|
11
|
-
const cssExtention = '.css';
|
|
12
|
-
const defaults = {
|
|
13
|
-
pluginName: '@rollup-extras/plugin-html',
|
|
14
|
-
outputFile: 'index.html',
|
|
15
|
-
watch: true,
|
|
16
|
-
useWriteBundle: false,
|
|
17
|
-
emitFile: 'auto',
|
|
18
|
-
injectIntoHead: (fileName) => fileName.endsWith(cssExtention),
|
|
19
|
-
ignore: toAssetPredicate(false),
|
|
20
|
-
templateFactory: ((template, assets, defaultTemplateFactory) => defaultTemplateFactory(template, assets))
|
|
21
|
-
};
|
|
22
|
-
const factories = { logger, injectIntoHead: predicateFactory, ignore: predicateFactory };
|
|
23
|
-
function index (options = {}) {
|
|
24
|
-
const { pluginName, outputFile, template, watch, emitFile, conditionalLoading, logger, useWriteBundle, logLevel, useEmittedTemplate, injectIntoHead, ignore, assetsFactory, templateFactory } = getOptionsObject(options,
|
|
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
|
-
if (
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
return
|
|
276
|
-
}
|
|
277
|
-
function defaultTemplateFactory(template, assets) {
|
|
278
|
-
const assetsAsArray = assets.asset
|
|
279
|
-
.concat((assets.iife.length ? assets.iife : assets.umd))
|
|
280
|
-
.concat(assets.es)
|
|
281
|
-
.map((asset) => {
|
|
282
|
-
if (typeof asset.html === 'function') {
|
|
283
|
-
asset.html = asset.html(assets);
|
|
284
|
-
}
|
|
285
|
-
return asset;
|
|
286
|
-
});
|
|
287
|
-
const headElements = assetsAsArray.filter(asset => asset.head).map(asset => asset.html);
|
|
288
|
-
const bodyElements = assetsAsArray.filter(asset => !asset.head).map(asset => asset.html);
|
|
289
|
-
const headIndex = template.toLowerCase().indexOf(headClosingElement);
|
|
290
|
-
template = `${template.substring(0, headIndex)}${headElements.join('')}${template.substring(headIndex)}`;
|
|
291
|
-
const bodyIndex = template.toLowerCase().indexOf(bodyClosingElement);
|
|
292
|
-
return `${template.substring(0, bodyIndex)}${bodyElements.join('')}${template.substring(bodyIndex)}`;
|
|
9
|
+
const defaultTemplate = '<!DOCTYPE html><html><head></head><body></body></html>';
|
|
10
|
+
const headClosingElement = '</head>';
|
|
11
|
+
const bodyClosingElement = '</body>';
|
|
12
|
+
const cssExtention = '.css';
|
|
13
|
+
const defaults = {
|
|
14
|
+
pluginName: '@rollup-extras/plugin-html',
|
|
15
|
+
outputFile: 'index.html',
|
|
16
|
+
watch: true,
|
|
17
|
+
useWriteBundle: false,
|
|
18
|
+
emitFile: 'auto',
|
|
19
|
+
injectIntoHead: (fileName) => fileName.endsWith(cssExtention),
|
|
20
|
+
ignore: toAssetPredicate(false),
|
|
21
|
+
templateFactory: ((template, assets, defaultTemplateFactory) => defaultTemplateFactory(template, assets))
|
|
22
|
+
};
|
|
23
|
+
const factories = { logger, injectIntoHead: predicateFactory, ignore: predicateFactory };
|
|
24
|
+
function index (options = {}) {
|
|
25
|
+
const { pluginName, outputFile, template, watch, emitFile, conditionalLoading, logger, useWriteBundle, logLevel, useEmittedTemplate, injectIntoHead, ignore, assetsFactory, templateFactory } = getOptionsObject(options, {
|
|
26
|
+
...defaults,
|
|
27
|
+
useEmittedTemplate: !('template' in options),
|
|
28
|
+
logLevel: options.verbose ? 1 /* LogLevel.info */ : 0 /* LogLevel.verbose */
|
|
29
|
+
}, factories), hasCustomTemplateFactory = 'templateFactory' in options;
|
|
30
|
+
let templateString = defaultTemplate, hasTemplateFile = false;
|
|
31
|
+
if (template) {
|
|
32
|
+
if (isUsableByDefaultTemplateFactory(template)) {
|
|
33
|
+
templateString = template;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
if (watch) {
|
|
37
|
+
try {
|
|
38
|
+
// using sync fs call because we need to return plugin object immidiately
|
|
39
|
+
useNewTemplate(oldStyleFs.readFileSync(template, { encoding: 'utf8' }));
|
|
40
|
+
hasTemplateFile = true;
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
handleTemplateReadError(e);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
templateString = new Promise(resolve => {
|
|
48
|
+
fs.readFile(template, { encoding: 'utf8' })
|
|
49
|
+
.then((newTemplateString) => {
|
|
50
|
+
if (!isUsableByDefaultTemplateFactory(newTemplateString) && !hasCustomTemplateFactory) {
|
|
51
|
+
warnAboutUsingDefaultTemplate();
|
|
52
|
+
resolve(defaultTemplate);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
resolve(newTemplateString);
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
.catch(handleTemplateReadError);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
let initialDir = '', fileNameInInitialDir, assets = freshAssets();
|
|
64
|
+
const processedFiles = new Set();
|
|
65
|
+
const instance = multiConfigPluginBase(useWriteBundle, pluginName, generateBundle, updateAssets);
|
|
66
|
+
const baseRenderStart = instance.renderStart;
|
|
67
|
+
const baseAddInstance = instance.api.addInstance;
|
|
68
|
+
instance.renderStart = function (outputOptions, inputOptions) {
|
|
69
|
+
logger('started collecting information', 0 /* LogLevel.verbose */);
|
|
70
|
+
initialDir = outputOptions.dir || '';
|
|
71
|
+
fileNameInInitialDir = path.join(initialDir, outputFile);
|
|
72
|
+
return baseRenderStart.call(this, outputOptions, inputOptions);
|
|
73
|
+
};
|
|
74
|
+
instance.api.addInstance = () => {
|
|
75
|
+
const instance = baseAddInstance();
|
|
76
|
+
if (hasTemplateFile && watch) {
|
|
77
|
+
instance.buildStart = buildStart;
|
|
78
|
+
}
|
|
79
|
+
return instance;
|
|
80
|
+
};
|
|
81
|
+
if (hasTemplateFile && watch) {
|
|
82
|
+
instance.buildStart = async function () {
|
|
83
|
+
try {
|
|
84
|
+
useNewTemplate(await fs.readFile(template, { encoding: 'utf8' }));
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
handleTemplateReadError(e);
|
|
88
|
+
}
|
|
89
|
+
await buildStart.apply(this);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return instance;
|
|
93
|
+
async function updateAssets(options, bundle, remainingConfigsCount, remainingOutputsCount) {
|
|
94
|
+
await getAssets(options, bundle);
|
|
95
|
+
const statistics = Object.keys(assets)
|
|
96
|
+
.map(key => ({ key, count: assets[key].length }))
|
|
97
|
+
.filter(item => item.count)
|
|
98
|
+
.map(({ key, count }) => `${key}: ${count}`)
|
|
99
|
+
.join(', ');
|
|
100
|
+
logger(`assets collected: [${statistics}], remaining: ${remainingOutputsCount} outputs, ${remainingConfigsCount} configs`, 0 /* LogLevel.verbose */);
|
|
101
|
+
const dir = options.dir || '', fileName = path.relative(dir, fileNameInInitialDir);
|
|
102
|
+
if (fileName in bundle) {
|
|
103
|
+
if (useEmittedTemplate) {
|
|
104
|
+
logger(`using exiting emitted ${fileName} as an input for out templateFactory`, 0 /* LogLevel.verbose */);
|
|
105
|
+
const source = bundle[fileName].type === 'asset' ? bundle[fileName].source.toString() : bundle[fileName].code;
|
|
106
|
+
useNewTemplate(source);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
logger(`removing exiting emitted ${fileName}`, 0 /* LogLevel.verbose */);
|
|
110
|
+
}
|
|
111
|
+
if (remainingConfigsCount === 0 && emitFile && !useWriteBundle) {
|
|
112
|
+
delete bundle[fileName];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function freshAssets() {
|
|
117
|
+
return {
|
|
118
|
+
asset: [],
|
|
119
|
+
es: [],
|
|
120
|
+
iife: [],
|
|
121
|
+
umd: []
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function useNewTemplate(newTemplateString) {
|
|
125
|
+
if (!isUsableByDefaultTemplateFactory(newTemplateString) && !hasCustomTemplateFactory) {
|
|
126
|
+
warnAboutUsingDefaultTemplate();
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
templateString = newTemplateString;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function handleTemplateReadError(e) {
|
|
133
|
+
if (e?.code === 'ENOENT') {
|
|
134
|
+
logger('template nor a file or string', 2 /* LogLevel.warn */, e);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
logger('error reading template', 2 /* LogLevel.warn */, e);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function warnAboutUsingDefaultTemplate() {
|
|
141
|
+
logger('template is unusable by default template factory, using default one', 2 /* LogLevel.warn */);
|
|
142
|
+
}
|
|
143
|
+
async function buildStart() {
|
|
144
|
+
this.addWatchFile(template);
|
|
145
|
+
}
|
|
146
|
+
async function generateBundle(options) {
|
|
147
|
+
logger.start('generating html', logLevel);
|
|
148
|
+
try {
|
|
149
|
+
const dir = options.dir || '', fileName = path.relative(dir, fileNameInInitialDir);
|
|
150
|
+
const depromisifiedTemplateString = await templateString, source = await templateFactory(depromisifiedTemplateString, assets, defaultTemplateFactory);
|
|
151
|
+
if (!emitFile || fileName.startsWith('..') || useWriteBundle) {
|
|
152
|
+
if (emitFile && emitFile !== 'auto') {
|
|
153
|
+
logger('cannot emitFile because it is outside of current output.dir, using writeFile instead', 0 /* LogLevel.verbose */);
|
|
154
|
+
}
|
|
155
|
+
await fs.mkdir(path.dirname(fileNameInInitialDir), { recursive: true });
|
|
156
|
+
await fs.writeFile(fileNameInInitialDir, source);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
this.emitFile({
|
|
160
|
+
type: 'asset',
|
|
161
|
+
fileName,
|
|
162
|
+
source
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
// reset assets and configs for the next iteration
|
|
166
|
+
assets = freshAssets();
|
|
167
|
+
processedFiles.clear();
|
|
168
|
+
logger.finish('html file generated');
|
|
169
|
+
}
|
|
170
|
+
catch (e) {
|
|
171
|
+
logger.finish('html generation failed', 3 /* LogLevel.error */, e);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
async function getAssets(options, bundle) {
|
|
175
|
+
const dir = options.dir || '';
|
|
176
|
+
for (const fileName of Object.keys(bundle)) {
|
|
177
|
+
const relativeToRootAssetPath = path.join(dir, fileName);
|
|
178
|
+
if (ignore(relativeToRootAssetPath) || processedFiles.has(relativeToRootAssetPath)) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
processedFiles.add(relativeToRootAssetPath);
|
|
182
|
+
if (bundle[fileName].type === 'asset') {
|
|
183
|
+
if (await useAssetFactory(fileName, relativeToRootAssetPath, bundle[fileName].source, 'asset')) {
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (fileName.endsWith(cssExtention)) {
|
|
187
|
+
const assetPath = path.relative(initialDir, relativeToRootAssetPath);
|
|
188
|
+
assets.asset.push({
|
|
189
|
+
html: getLinkElement(assetPath),
|
|
190
|
+
head: injectIntoHead(relativeToRootAssetPath),
|
|
191
|
+
type: 'asset'
|
|
192
|
+
});
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else if (bundle[fileName].type === 'chunk') {
|
|
197
|
+
const chunk = bundle[fileName];
|
|
198
|
+
if (chunk.isEntry) {
|
|
199
|
+
if (await useAssetFactory(fileName, relativeToRootAssetPath, chunk.code, options.format)) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (options.format === 'es' || options.format === 'iife' || options.format === 'umd') {
|
|
203
|
+
const assetPath = path.relative(initialDir, relativeToRootAssetPath);
|
|
204
|
+
assets[options.format].push({
|
|
205
|
+
html: (assets) => {
|
|
206
|
+
let useConditionalLoading = conditionalLoading;
|
|
207
|
+
useConditionalLoading ??= !!((assets.iife.length
|
|
208
|
+
|| assets.umd.length)
|
|
209
|
+
&& assets.es.length);
|
|
210
|
+
return (options.format === 'iife' || options.format === 'umd') ? getNonModuleScriptElement(assetPath, !!useConditionalLoading) : getModuleScriptElement(assetPath);
|
|
211
|
+
},
|
|
212
|
+
head: injectIntoHead(relativeToRootAssetPath),
|
|
213
|
+
type: 'asset'
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
async function useAssetFactory(fileName, relativeToRootAssetPath, source, format) {
|
|
221
|
+
if (assetsFactory) {
|
|
222
|
+
try {
|
|
223
|
+
let asset = await assetsFactory(fileName, source, format);
|
|
224
|
+
if (asset) {
|
|
225
|
+
if (typeof asset === 'string') {
|
|
226
|
+
asset = {
|
|
227
|
+
html: asset,
|
|
228
|
+
head: injectIntoHead(relativeToRootAssetPath),
|
|
229
|
+
type: format
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
if (!assets[asset.type]) {
|
|
233
|
+
assets[asset.type] = [];
|
|
234
|
+
}
|
|
235
|
+
assets[asset.type].push(asset);
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
catch (e) {
|
|
240
|
+
logger('exception in assetFactory', 2 /* LogLevel.warn */, e);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function isUsableByDefaultTemplateFactory(template) {
|
|
247
|
+
return template.indexOf(headClosingElement) >= 0 && template.indexOf(bodyClosingElement) >= 0;
|
|
248
|
+
}
|
|
249
|
+
function predicateFactory(options, field) {
|
|
250
|
+
if (options[field] != null) {
|
|
251
|
+
const predicate = toAssetPredicate(options[field]);
|
|
252
|
+
if (predicate) {
|
|
253
|
+
return predicate;
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
options.logger && options.logger(`${field} option ignored because it is not a function, RegExp, string or boolean`, 2 /* LogLevel.warn */);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return defaults[field];
|
|
260
|
+
}
|
|
261
|
+
function defaultTemplateFactory(template, assets) {
|
|
262
|
+
const assetsAsArray = assets.asset
|
|
263
|
+
.concat((assets.iife.length ? assets.iife : assets.umd))
|
|
264
|
+
.concat(assets.es)
|
|
265
|
+
.map((asset) => {
|
|
266
|
+
if (typeof asset.html === 'function') {
|
|
267
|
+
asset.html = asset.html(assets);
|
|
268
|
+
}
|
|
269
|
+
return asset;
|
|
270
|
+
});
|
|
271
|
+
const headElements = assetsAsArray.filter(asset => asset.head).map(asset => asset.html);
|
|
272
|
+
const bodyElements = assetsAsArray.filter(asset => !asset.head).map(asset => asset.html);
|
|
273
|
+
const headIndex = template.toLowerCase().indexOf(headClosingElement);
|
|
274
|
+
template = `${template.substring(0, headIndex)}${headElements.join('')}${template.substring(headIndex)}`;
|
|
275
|
+
const bodyIndex = template.toLowerCase().indexOf(bodyClosingElement);
|
|
276
|
+
return `${template.substring(0, bodyIndex)}${bodyElements.join('')}${template.substring(bodyIndex)}`;
|
|
293
277
|
}
|
|
294
278
|
|
|
295
279
|
export { index as default };
|
package/dist/shared.cjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function getLinkElement(fileName) {
|
|
4
|
+
return `<link rel="stylesheet" href="${fileName}" type="text/css">`;
|
|
5
|
+
}
|
|
6
|
+
function getNonModuleScriptElement(fileName, conditionalLoading) {
|
|
7
|
+
return `<script src="${fileName}" type="text/javascript"${conditionalLoading ? ' nomodule' : ''}></script>`;
|
|
8
|
+
}
|
|
9
|
+
function getModuleScriptElement(fileName) {
|
|
10
|
+
return `<script src="${fileName}" type="module"></script>`;
|
|
11
|
+
}
|
|
12
|
+
function toAssetPredicate(sourceOption) {
|
|
13
|
+
if (typeof sourceOption === 'boolean') {
|
|
14
|
+
return () => sourceOption;
|
|
15
|
+
}
|
|
16
|
+
else if (typeof sourceOption === 'function') {
|
|
17
|
+
return sourceOption;
|
|
18
|
+
}
|
|
19
|
+
else if (sourceOption instanceof RegExp) {
|
|
20
|
+
return (fileName) => sourceOption.test(fileName);
|
|
21
|
+
}
|
|
22
|
+
else if (typeof sourceOption === 'string') {
|
|
23
|
+
return (fileName) => fileName.endsWith(sourceOption);
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
exports.getLinkElement = getLinkElement;
|
|
29
|
+
exports.getModuleScriptElement = getModuleScriptElement;
|
|
30
|
+
exports.getNonModuleScriptElement = getNonModuleScriptElement;
|
|
31
|
+
exports.toAssetPredicate = toAssetPredicate;
|
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AssetPredicate, PredicateSource } from './types';
|
|
2
|
+
export declare function getLinkElement(fileName: string): string;
|
|
3
|
+
export declare function getNonModuleScriptElement(fileName: string, conditionalLoading: boolean): string;
|
|
4
|
+
export declare function getModuleScriptElement(fileName: string): string;
|
|
5
|
+
export declare function toAssetPredicate(sourceOption: PredicateSource): AssetPredicate | undefined;
|
package/dist/shared.mjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
function getLinkElement(fileName) {
|
|
2
|
+
return `<link rel="stylesheet" href="${fileName}" type="text/css">`;
|
|
3
|
+
}
|
|
4
|
+
function getNonModuleScriptElement(fileName, conditionalLoading) {
|
|
5
|
+
return `<script src="${fileName}" type="text/javascript"${conditionalLoading ? ' nomodule' : ''}></script>`;
|
|
6
|
+
}
|
|
7
|
+
function getModuleScriptElement(fileName) {
|
|
8
|
+
return `<script src="${fileName}" type="module"></script>`;
|
|
9
|
+
}
|
|
10
|
+
function toAssetPredicate(sourceOption) {
|
|
11
|
+
if (typeof sourceOption === 'boolean') {
|
|
12
|
+
return () => sourceOption;
|
|
13
|
+
}
|
|
14
|
+
else if (typeof sourceOption === 'function') {
|
|
15
|
+
return sourceOption;
|
|
16
|
+
}
|
|
17
|
+
else if (sourceOption instanceof RegExp) {
|
|
18
|
+
return (fileName) => sourceOption.test(fileName);
|
|
19
|
+
}
|
|
20
|
+
else if (typeof sourceOption === 'string') {
|
|
21
|
+
return (fileName) => fileName.endsWith(sourceOption);
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { getNonModuleScriptElement as a, getModuleScriptElement as b, getLinkElement as g, toAssetPredicate as t };
|