@rollup-extras/plugin-html 1.3.4 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 Konstantin Shutkin
3
+ Copyright (c) 2023 Konstantin Shutkin
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -7,12 +7,12 @@ Points:
7
7
  - Inject file names with hashes
8
8
  - Watch on a template file and trigger rebuild if it is changed
9
9
  - Provides minimalistic template by default, so you are ready to start without configuration
10
- - Support mutliple rollup configs (will trigger only when last output generated)
10
+ - Supports multiple rollup configs (will trigger only when last output generated)
11
11
  - Extensible through API so you can plug in something for html processing or generate new types of html elements
12
12
 
13
13
  Uses [`@niceties/logger`](https://github.com/kshutkin/niceties/blob/main/logger/README.md) to log messages, can be configured through `@niceties/logger` API.
14
14
 
15
- [Changlelog](./CHANGELOG.md)
15
+ [Changelog](./CHANGELOG.md)
16
16
 
17
17
  ## Installation
18
18
 
@@ -54,13 +54,13 @@ For additional plugin instances (in case of multiple configs) please use: `first
54
54
 
55
55
  Optional, string.
56
56
 
57
- For debugging purposes, so many instances of a plugin can be differenciated in debugging output.
57
+ For debugging purposes, so many instances of a plugin can be differentiated in debugging output.
58
58
 
59
59
  ### outputFile
60
60
 
61
61
  Optional, string, default: `'index.html'`.
62
62
 
63
- Use to override output file name. If file name with the same name exits in pipeline it will be removed or overwritten in the process but its content by default will be used as an input for this plugin. In the following example file emitted by copy plugin will be used as an input for this plugin:
63
+ Used to override output file name. If a filename with the same name exists in the pipeline it will be removed or overwritten in the process but its content by default will be used as an input for this plugin. In the following example file emitted by copy plugin will be used as an input for this plugin:
64
64
 
65
65
  ```javascript
66
66
  import copy from '@rollup-extras/plugin-copy';
@@ -88,43 +88,43 @@ Default template string or template file name. If it is a template string it sho
88
88
 
89
89
  Optional, boolean, default: true.
90
90
 
91
- If plugin found a template file name this option defines if plugin need to watch it or not.
91
+ If the plugin found a template file name this option defines if the plugin needs to watch it or not.
92
92
 
93
93
  ### emitFile
94
94
 
95
95
  Optional, boolean, default: true.
96
96
 
97
- Defines if plugin should use this.emitFile or should just write it to disk. Option can be ignored in setup with mutliple rollup configs.
97
+ Defines if the plugin should use this.emitFile or should just write it to disk. Option can be ignored in setup with multiple rollup configs.
98
98
 
99
99
  ### useEmittedTemplate
100
100
 
101
101
  Optional, boolean, default: true.
102
102
 
103
- Defines what plugin does if it finds a file with expected file name in bundle. By default it will be used as a template. Template provided through template option should be of higher priority to the plugin.
103
+ Defines what the plugin does if it finds a file with the expected file name in the bundle. By default it will be used as a template. Templates provided through the template option should be of higher priority to the plugin.
104
104
 
105
105
  ### conditionalLoading
106
106
 
107
107
  Optional, boolean, default: undefined.
108
108
 
109
- Defines if plugin adds `nomodule` attribute for non modular js chunks. By default it is done only if we have `es` outputs in one of the bundles processed by plugin. Also plugin values `iife` outputs higher than `umd` and if we have both filters out `umd` ones. This can be changes only by providing custom `assetFactory`.
109
+ Defines if plugin adds `nomodule` attribute for non modular js chunks. By default it is done only if we have `es` outputs in one of the bundles processed by the plugin. Also plugin values `iife` outputs higher than `umd` and if we have both filters out `umd` ones. This can be changed only by providing custom `assetFactory`.
110
110
 
111
111
  ### injectIntoHead
112
112
 
113
113
  Optional, RegExp | function | boolean, default: `(fileName: string) => fileName.endsWith(cssExtention)`.
114
114
 
115
- Option to customize what assets should be injected into head element of a template.
115
+ Option to customize what assets should be injected into the head element of a template.
116
116
 
117
117
  ### ignore
118
118
 
119
119
  Optional, RegExp | function | boolean, default: false.
120
120
 
121
- Option to customize what assets should be ignored in process.
121
+ Option to customize what assets should be ignored in the process.
122
122
 
123
123
  ### verbose
124
124
 
125
125
  Optional, boolean, default: false.
126
126
 
127
- Option to print more debug information into console (with default appender).
127
+ Option to print more debug information into the console (with default appender).
128
128
 
129
129
  ### useWriteBundle
130
130
 
@@ -136,7 +136,7 @@ Option to use `writeBundle` hook instead of `generateBundle`.
136
136
 
137
137
  Optional, function (please check type in configuration section).
138
138
 
139
- To process additional types of assets / enchance default behavior. If known asset processed by factory (it returned an object, string or promise) plugin skips default processing for this asset.
139
+ To process additional types of assets / enhance default behavior. If a known asset is processed by a factory (it returns an object, string or promise), the plugin skips default processing for this asset.
140
140
 
141
141
  Example (adds integrity attribute to a css file):
142
142
  ```javascript
@@ -170,7 +170,7 @@ export default {
170
170
 
171
171
  Optional, function (please check type in configuration section).
172
172
 
173
- Use to customize template with external libraries.
173
+ Used to customize templates with external libraries.
174
174
 
175
175
  Example (pretty print html):
176
176
 
@@ -192,6 +192,32 @@ export default {
192
192
  };
193
193
  ```
194
194
 
195
+ ## Asset Factories (/asset-factories export)
196
+
197
+ ### simpleES5Script
198
+
199
+ `(PredicateSource) => AssetFactory`
200
+
201
+ Creates a simple script element.
202
+
203
+ ### simpleES5FallbackScript
204
+
205
+ `(PredicateSource) => AssetFactory`
206
+
207
+ Creates a simple script element with `nomodule` attribute.
208
+
209
+ ### simpleModuleScript
210
+
211
+ `(PredicateSource) => AssetFactory`
212
+
213
+ Creates a simple module script element.
214
+
215
+ ### combineAssetFactories
216
+
217
+ `(...factories: AssetFactory[]) => AssetFactory`
218
+
219
+ Combines several factories, calls them in order, the first factory created asset wins.
220
+
195
221
  ## Configuration
196
222
 
197
223
  [Definition of config typings in typescript](./src/types.ts)
@@ -205,4 +231,4 @@ export default {
205
231
 
206
232
  # License
207
233
 
208
- [MIT](./LICENSE)
234
+ [MIT](https://github.com/kshutkin/rollup-extras/blob/main/LICENSE)
@@ -0,0 +1,5 @@
1
+ {
2
+ "type": "module",
3
+ "types": "../dist/asset-factories.d.ts",
4
+ "main": "../dist/asset-factories.mjs"
5
+ }
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ var logger$1 = require('@niceties/logger');
4
+ var shared = require('./shared.cjs');
5
+
6
+ const noopPredicate = () => false;
7
+ let logger;
8
+ function simpleES5Script(predicateSource) {
9
+ const predicate = getPredicate(predicateSource);
10
+ return (fileName) => {
11
+ if (predicate(fileName)) {
12
+ return shared.getNonModuleScriptElement(fileName, false);
13
+ }
14
+ };
15
+ }
16
+ function simpleES5FallbackScript(predicateSource) {
17
+ const predicate = getPredicate(predicateSource);
18
+ return (fileName) => {
19
+ if (predicate(fileName)) {
20
+ return shared.getNonModuleScriptElement(fileName, true);
21
+ }
22
+ };
23
+ }
24
+ function simpleModuleScript(predicateSource) {
25
+ const predicate = getPredicate(predicateSource);
26
+ return (fileName) => {
27
+ if (predicate(fileName)) {
28
+ return shared.getModuleScriptElement(fileName);
29
+ }
30
+ };
31
+ }
32
+ function combineAssetFactories(...factories) {
33
+ return (fileName, content, type) => {
34
+ for (const factory of factories) {
35
+ const result = factory(fileName, content, type);
36
+ if (result) {
37
+ return result;
38
+ }
39
+ }
40
+ };
41
+ }
42
+ function getPredicate(predicateSource) {
43
+ let predicate = shared.toAssetPredicate(predicateSource);
44
+ if (!predicate) {
45
+ const logger = getLogger();
46
+ logger(`${predicateSource} is not valid, using noopPredicate`, 2 /* LogLevel.warn */);
47
+ predicate = noopPredicate;
48
+ }
49
+ return predicate;
50
+ }
51
+ function getLogger() {
52
+ if (!logger) {
53
+ logger = logger$1.createLogger('@rollup-extras/plugin-html/asset-factories');
54
+ }
55
+ return logger;
56
+ }
57
+
58
+ exports.combineAssetFactories = combineAssetFactories;
59
+ exports.simpleES5FallbackScript = simpleES5FallbackScript;
60
+ exports.simpleES5Script = simpleES5Script;
61
+ exports.simpleModuleScript = simpleModuleScript;
@@ -0,0 +1,6 @@
1
+ import type { AssetFactory, PredicateSource } from './types';
2
+ import type { InternalModuleFormat } from 'rollup';
3
+ export declare function simpleES5Script(predicateSource: PredicateSource): (fileName: string) => string | undefined;
4
+ export declare function simpleES5FallbackScript(predicateSource: PredicateSource): (fileName: string) => string | undefined;
5
+ export declare function simpleModuleScript(predicateSource: PredicateSource): (fileName: string) => string | undefined;
6
+ export declare function combineAssetFactories(...factories: AssetFactory[]): (fileName: string, content: string | Uint8Array, type: 'asset' | InternalModuleFormat) => string | import("./types").AssetDescriptor | Promise<string | import("./types").AssetDescriptor | undefined> | undefined;
@@ -0,0 +1,56 @@
1
+ import { createLogger } from '@niceties/logger';
2
+ import { a as getNonModuleScriptElement, b as getModuleScriptElement, t as toAssetPredicate } from './shared.mjs';
3
+
4
+ const noopPredicate = () => false;
5
+ let logger;
6
+ function simpleES5Script(predicateSource) {
7
+ const predicate = getPredicate(predicateSource);
8
+ return (fileName) => {
9
+ if (predicate(fileName)) {
10
+ return getNonModuleScriptElement(fileName, false);
11
+ }
12
+ };
13
+ }
14
+ function simpleES5FallbackScript(predicateSource) {
15
+ const predicate = getPredicate(predicateSource);
16
+ return (fileName) => {
17
+ if (predicate(fileName)) {
18
+ return getNonModuleScriptElement(fileName, true);
19
+ }
20
+ };
21
+ }
22
+ function simpleModuleScript(predicateSource) {
23
+ const predicate = getPredicate(predicateSource);
24
+ return (fileName) => {
25
+ if (predicate(fileName)) {
26
+ return getModuleScriptElement(fileName);
27
+ }
28
+ };
29
+ }
30
+ function combineAssetFactories(...factories) {
31
+ return (fileName, content, type) => {
32
+ for (const factory of factories) {
33
+ const result = factory(fileName, content, type);
34
+ if (result) {
35
+ return result;
36
+ }
37
+ }
38
+ };
39
+ }
40
+ function getPredicate(predicateSource) {
41
+ let predicate = toAssetPredicate(predicateSource);
42
+ if (!predicate) {
43
+ const logger = getLogger();
44
+ logger(`${predicateSource} is not valid, using noopPredicate`, 2 /* LogLevel.warn */);
45
+ predicate = noopPredicate;
46
+ }
47
+ return predicate;
48
+ }
49
+ function getLogger() {
50
+ if (!logger) {
51
+ logger = createLogger('@rollup-extras/plugin-html/asset-factories');
52
+ }
53
+ return logger;
54
+ }
55
+
56
+ export { combineAssetFactories, simpleES5FallbackScript, simpleES5Script, simpleModuleScript };