@css-inline/css-inline 0.12.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 +21 -0
- package/README.md +156 -0
- package/index.d.ts +31 -0
- package/index.js +257 -0
- package/js-binding.d.ts +33 -0
- package/js-binding.js +287 -0
- package/package.json +108 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-2023 Dmitry Dygalo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# css-inline
|
|
2
|
+
|
|
3
|
+
[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/Stranger6667/css-inline/build.yml?style=flat-square&labelColor=555555&logo=github" height="20">](https://github.com/Stranger6667/css-inline/actions/workflows/build.yml)
|
|
4
|
+
[<img alt="npm" src="https://img.shields.io/npm/v/@css-inline/css-inline.svg?style=flat-square" height="20">](https://www.npmjs.com/package/@css-inline/css-inline)
|
|
5
|
+
[<img alt="codecov.io" src="https://img.shields.io/codecov/c/gh/Stranger6667/css-inline?logo=codecov&style=flat-square&token=tOzvV4kDY0" height="20">](https://app.codecov.io/github/Stranger6667/css-inline)
|
|
6
|
+
[<img alt="gitter" src="https://img.shields.io/gitter/room/Stranger6667/css-inline?style=flat-square" height="20">](https://gitter.im/Stranger6667/css-inline)
|
|
7
|
+
|
|
8
|
+
`css-inline` is a high-performance library for inlining CSS into HTML 'style' attributes.
|
|
9
|
+
|
|
10
|
+
This library is designed for scenarios such as preparing HTML emails or embedding HTML into third-party web pages.
|
|
11
|
+
|
|
12
|
+
For instance, the library transforms HTML like this:
|
|
13
|
+
|
|
14
|
+
```html
|
|
15
|
+
<html>
|
|
16
|
+
<head>
|
|
17
|
+
<style>h1 { color:blue; }</style>
|
|
18
|
+
</head>
|
|
19
|
+
<body>
|
|
20
|
+
<h1>Big Text</h1>
|
|
21
|
+
</body>
|
|
22
|
+
</html>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
into:
|
|
26
|
+
|
|
27
|
+
```html
|
|
28
|
+
<html>
|
|
29
|
+
<head></head>
|
|
30
|
+
<body>
|
|
31
|
+
<h1 style="color:blue;">Big Text</h1>
|
|
32
|
+
</body>
|
|
33
|
+
</html>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- Uses reliable components from Mozilla's Servo project
|
|
37
|
+
- Inlines CSS from `style` and `link` tags
|
|
38
|
+
- Removes `style` and `link` tags
|
|
39
|
+
- Resolves external stylesheets (including local files)
|
|
40
|
+
- Works on Linux, Windows, and macOS
|
|
41
|
+
- Supports HTML5 & CSS3
|
|
42
|
+
|
|
43
|
+
## Playground
|
|
44
|
+
|
|
45
|
+
If you'd like to try `css-inline`, you can check the WebAssembly-powered [playground](https://css-inline.org/) to see the results instantly.
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
### Node.js
|
|
50
|
+
|
|
51
|
+
Install with `npm`:
|
|
52
|
+
|
|
53
|
+
```shell
|
|
54
|
+
npm i @css-inline/css-inline
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { inline } from "css-inline";
|
|
61
|
+
|
|
62
|
+
var inlined = inline(
|
|
63
|
+
`
|
|
64
|
+
<html>
|
|
65
|
+
<head>
|
|
66
|
+
<style>h1 { color:red }</style>
|
|
67
|
+
</head>
|
|
68
|
+
<body>
|
|
69
|
+
<h1>Test</h1>
|
|
70
|
+
</body>
|
|
71
|
+
</html>
|
|
72
|
+
`,
|
|
73
|
+
);
|
|
74
|
+
// Do something with the inlined HTML, e.g. send an email
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Configuration
|
|
78
|
+
|
|
79
|
+
- `inlineStyleTags`. Specifies whether to inline CSS from "style" tags. Default: `true`
|
|
80
|
+
- `keepStyleTags`. Specifies whether to keep "style" tags after inlining. Default: `false`
|
|
81
|
+
- `keepLinkTags`. Specifies whether to keep "link" tags after inlining. Default: `false`
|
|
82
|
+
- `baseUrl`. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use the `file://` scheme. Default: `null`
|
|
83
|
+
- `loadRemoteStylesheets`. Specifies whether remote stylesheets should be loaded. Default: `true`
|
|
84
|
+
- `extraCss`. Extra CSS to be inlined. Default: `null`
|
|
85
|
+
- `preallocateNodeCapacity`. **Advanced**. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default: `32`
|
|
86
|
+
|
|
87
|
+
You can also skip CSS inlining for an HTML tag by adding the `data-css-inline="ignore"` attribute to it:
|
|
88
|
+
|
|
89
|
+
```html
|
|
90
|
+
<head>
|
|
91
|
+
<style>h1 { color:blue; }</style>
|
|
92
|
+
</head>
|
|
93
|
+
<body>
|
|
94
|
+
<!-- The tag below won't receive additional styles -->
|
|
95
|
+
<h1 data-css-inline="ignore">Big Text</h1>
|
|
96
|
+
</body>
|
|
97
|
+
</html>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The `data-css-inline="ignore"` attribute also allows you to skip `link` and `style` tags:
|
|
101
|
+
|
|
102
|
+
```html
|
|
103
|
+
<head>
|
|
104
|
+
<!-- Styles below are ignored -->
|
|
105
|
+
<style data-css-inline="ignore">h1 { color:blue; }</style>
|
|
106
|
+
</head>
|
|
107
|
+
<body>
|
|
108
|
+
<h1>Big Text</h1>
|
|
109
|
+
</body>
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## WebAssembly
|
|
113
|
+
|
|
114
|
+
`css-inline` also ships a WebAssembly module built with `wasm-bindgen` to run in browsers.
|
|
115
|
+
|
|
116
|
+
```html
|
|
117
|
+
<script src="https://unpkg.com/@css-inline/css-inline-wasm"></script>
|
|
118
|
+
<script>
|
|
119
|
+
// Initialize the WASM module first
|
|
120
|
+
cssInline.initWasm(fetch('https://unpkg.com/@css-inline/css-inline-wasm/index_bg.wasm'));
|
|
121
|
+
|
|
122
|
+
const inlinedHtml = cssInline.inline(`<html>
|
|
123
|
+
<head>
|
|
124
|
+
<style>h1 { color:blue; }</style>
|
|
125
|
+
</head>
|
|
126
|
+
<body>
|
|
127
|
+
<h1>Big Text</h1>
|
|
128
|
+
</body>
|
|
129
|
+
</html>`);
|
|
130
|
+
|
|
131
|
+
document.getElementById('output').src = inlinedHtml
|
|
132
|
+
</script>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
**NOTE**: WASM module currently lacks support for fetching stylesheets from network or filesystem.
|
|
136
|
+
|
|
137
|
+
## Performance
|
|
138
|
+
|
|
139
|
+
`css-inline` is powered by efficient tooling from Mozilla's Servo project and significantly outperforms other JavaScript alternatives in terms of speed.
|
|
140
|
+
Most of the time it achieves over a **4x** speed advantage compared to the next fastest alternative.
|
|
141
|
+
|
|
142
|
+
Here is the performance comparison:
|
|
143
|
+
|
|
144
|
+
| | Size | `css-inline 0.12.0` | `css-inline-wasm 0.12.0` | `juice 10.0.0` | `inline-css 4.0.2` |
|
|
145
|
+
|-------------|---------|---------------------|--------------------------|-----------------------|------------------------|
|
|
146
|
+
| Basic | 230 B | 15.06 µs | 29.33 µs (**1.94x**) | 142.53 µs (**9.46x**) | 163.77 µs (**10.87x**) |
|
|
147
|
+
| Realistic-1 | 8.58 KB | 320.71 µs | 638.97 µs (**1.99x**) | 1.28 ms (**4.01x**) | 1.87 ms (**5.85x**) |
|
|
148
|
+
| Realistic-2 | 4.3 KB | 205.21 µs | 385.20 µs (**1.87x**) | 1.72 ms (**8.42x**) | 1.56 ms (**7.63x**) |
|
|
149
|
+
|
|
150
|
+
The "Basic" case was obtained from benchmarking the example from the Usage section.
|
|
151
|
+
|
|
152
|
+
The benchmarking code is available in the `benches/bench.ts` file. The benchmarks were conducted using the stable `rustc 1.74.1` on Node.js `v21.4.0`.
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
This project is licensed under the terms of the [MIT license](https://opensource.org/licenses/MIT).
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/* auto-generated by NAPI-RS */
|
|
5
|
+
|
|
6
|
+
export interface Options {
|
|
7
|
+
/**
|
|
8
|
+
* Whether to inline CSS from "style" tags.
|
|
9
|
+
*
|
|
10
|
+
* Sometimes HTML may include a lot of boilerplate styles, that are not applicable in every
|
|
11
|
+
* scenario, and it is useful to ignore them and use `extra_css` instead.
|
|
12
|
+
*/
|
|
13
|
+
inlineStyleTags?: boolean
|
|
14
|
+
/** Keep "style" tags after inlining. */
|
|
15
|
+
keepStyleTags?: boolean
|
|
16
|
+
/** Keep "link" tags after inlining. */
|
|
17
|
+
keepLinkTags?: boolean
|
|
18
|
+
/** Used for loading external stylesheets via relative URLs. */
|
|
19
|
+
baseUrl?: string
|
|
20
|
+
/** Whether remote stylesheets should be loaded or not. */
|
|
21
|
+
loadRemoteStylesheets?: boolean
|
|
22
|
+
/** Additional CSS to inline. */
|
|
23
|
+
extraCss?: string
|
|
24
|
+
/**
|
|
25
|
+
* Pre-allocate capacity for HTML nodes during parsing.
|
|
26
|
+
* It can improve performance when you have an estimate of the number of nodes in your HTML document.
|
|
27
|
+
*/
|
|
28
|
+
preallocateNodeCapacity?: number
|
|
29
|
+
}
|
|
30
|
+
/** Inline CSS styles from <style> tags to matching elements in the HTML tree and return a string. */
|
|
31
|
+
export function inline(html: string, options?: Options | undefined | null): string
|
package/index.js
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/* prettier-ignore */
|
|
4
|
+
|
|
5
|
+
/* auto-generated by NAPI-RS */
|
|
6
|
+
|
|
7
|
+
const { existsSync, readFileSync } = require('fs')
|
|
8
|
+
const { join } = require('path')
|
|
9
|
+
|
|
10
|
+
const { platform, arch } = process
|
|
11
|
+
|
|
12
|
+
let nativeBinding = null
|
|
13
|
+
let localFileExisted = false
|
|
14
|
+
let loadError = null
|
|
15
|
+
|
|
16
|
+
function isMusl() {
|
|
17
|
+
// For Node 10
|
|
18
|
+
if (!process.report || typeof process.report.getReport !== 'function') {
|
|
19
|
+
try {
|
|
20
|
+
const lddPath = require('child_process').execSync('which ldd').toString().trim()
|
|
21
|
+
return readFileSync(lddPath, 'utf8').includes('musl')
|
|
22
|
+
} catch (e) {
|
|
23
|
+
return true
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
const { glibcVersionRuntime } = process.report.getReport().header
|
|
27
|
+
return !glibcVersionRuntime
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
switch (platform) {
|
|
32
|
+
case 'android':
|
|
33
|
+
switch (arch) {
|
|
34
|
+
case 'arm64':
|
|
35
|
+
localFileExisted = existsSync(join(__dirname, 'css-inline.android-arm64.node'))
|
|
36
|
+
try {
|
|
37
|
+
if (localFileExisted) {
|
|
38
|
+
nativeBinding = require('./css-inline.android-arm64.node')
|
|
39
|
+
} else {
|
|
40
|
+
nativeBinding = require('@css-inline/css-inline-android-arm64')
|
|
41
|
+
}
|
|
42
|
+
} catch (e) {
|
|
43
|
+
loadError = e
|
|
44
|
+
}
|
|
45
|
+
break
|
|
46
|
+
case 'arm':
|
|
47
|
+
localFileExisted = existsSync(join(__dirname, 'css-inline.android-arm-eabi.node'))
|
|
48
|
+
try {
|
|
49
|
+
if (localFileExisted) {
|
|
50
|
+
nativeBinding = require('./css-inline.android-arm-eabi.node')
|
|
51
|
+
} else {
|
|
52
|
+
nativeBinding = require('@css-inline/css-inline-android-arm-eabi')
|
|
53
|
+
}
|
|
54
|
+
} catch (e) {
|
|
55
|
+
loadError = e
|
|
56
|
+
}
|
|
57
|
+
break
|
|
58
|
+
default:
|
|
59
|
+
throw new Error(`Unsupported architecture on Android ${arch}`)
|
|
60
|
+
}
|
|
61
|
+
break
|
|
62
|
+
case 'win32':
|
|
63
|
+
switch (arch) {
|
|
64
|
+
case 'x64':
|
|
65
|
+
localFileExisted = existsSync(
|
|
66
|
+
join(__dirname, 'css-inline.win32-x64-msvc.node')
|
|
67
|
+
)
|
|
68
|
+
try {
|
|
69
|
+
if (localFileExisted) {
|
|
70
|
+
nativeBinding = require('./css-inline.win32-x64-msvc.node')
|
|
71
|
+
} else {
|
|
72
|
+
nativeBinding = require('@css-inline/css-inline-win32-x64-msvc')
|
|
73
|
+
}
|
|
74
|
+
} catch (e) {
|
|
75
|
+
loadError = e
|
|
76
|
+
}
|
|
77
|
+
break
|
|
78
|
+
case 'ia32':
|
|
79
|
+
localFileExisted = existsSync(
|
|
80
|
+
join(__dirname, 'css-inline.win32-ia32-msvc.node')
|
|
81
|
+
)
|
|
82
|
+
try {
|
|
83
|
+
if (localFileExisted) {
|
|
84
|
+
nativeBinding = require('./css-inline.win32-ia32-msvc.node')
|
|
85
|
+
} else {
|
|
86
|
+
nativeBinding = require('@css-inline/css-inline-win32-ia32-msvc')
|
|
87
|
+
}
|
|
88
|
+
} catch (e) {
|
|
89
|
+
loadError = e
|
|
90
|
+
}
|
|
91
|
+
break
|
|
92
|
+
case 'arm64':
|
|
93
|
+
localFileExisted = existsSync(
|
|
94
|
+
join(__dirname, 'css-inline.win32-arm64-msvc.node')
|
|
95
|
+
)
|
|
96
|
+
try {
|
|
97
|
+
if (localFileExisted) {
|
|
98
|
+
nativeBinding = require('./css-inline.win32-arm64-msvc.node')
|
|
99
|
+
} else {
|
|
100
|
+
nativeBinding = require('@css-inline/css-inline-win32-arm64-msvc')
|
|
101
|
+
}
|
|
102
|
+
} catch (e) {
|
|
103
|
+
loadError = e
|
|
104
|
+
}
|
|
105
|
+
break
|
|
106
|
+
default:
|
|
107
|
+
throw new Error(`Unsupported architecture on Windows: ${arch}`)
|
|
108
|
+
}
|
|
109
|
+
break
|
|
110
|
+
case 'darwin':
|
|
111
|
+
localFileExisted = existsSync(join(__dirname, 'css-inline.darwin-universal.node'))
|
|
112
|
+
try {
|
|
113
|
+
if (localFileExisted) {
|
|
114
|
+
nativeBinding = require('./css-inline.darwin-universal.node')
|
|
115
|
+
} else {
|
|
116
|
+
nativeBinding = require('@css-inline/css-inline-darwin-universal')
|
|
117
|
+
}
|
|
118
|
+
break
|
|
119
|
+
} catch {}
|
|
120
|
+
switch (arch) {
|
|
121
|
+
case 'x64':
|
|
122
|
+
localFileExisted = existsSync(join(__dirname, 'css-inline.darwin-x64.node'))
|
|
123
|
+
try {
|
|
124
|
+
if (localFileExisted) {
|
|
125
|
+
nativeBinding = require('./css-inline.darwin-x64.node')
|
|
126
|
+
} else {
|
|
127
|
+
nativeBinding = require('@css-inline/css-inline-darwin-x64')
|
|
128
|
+
}
|
|
129
|
+
} catch (e) {
|
|
130
|
+
loadError = e
|
|
131
|
+
}
|
|
132
|
+
break
|
|
133
|
+
case 'arm64':
|
|
134
|
+
localFileExisted = existsSync(
|
|
135
|
+
join(__dirname, 'css-inline.darwin-arm64.node')
|
|
136
|
+
)
|
|
137
|
+
try {
|
|
138
|
+
if (localFileExisted) {
|
|
139
|
+
nativeBinding = require('./css-inline.darwin-arm64.node')
|
|
140
|
+
} else {
|
|
141
|
+
nativeBinding = require('@css-inline/css-inline-darwin-arm64')
|
|
142
|
+
}
|
|
143
|
+
} catch (e) {
|
|
144
|
+
loadError = e
|
|
145
|
+
}
|
|
146
|
+
break
|
|
147
|
+
default:
|
|
148
|
+
throw new Error(`Unsupported architecture on macOS: ${arch}`)
|
|
149
|
+
}
|
|
150
|
+
break
|
|
151
|
+
case 'freebsd':
|
|
152
|
+
if (arch !== 'x64') {
|
|
153
|
+
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
|
|
154
|
+
}
|
|
155
|
+
localFileExisted = existsSync(join(__dirname, 'css-inline.freebsd-x64.node'))
|
|
156
|
+
try {
|
|
157
|
+
if (localFileExisted) {
|
|
158
|
+
nativeBinding = require('./css-inline.freebsd-x64.node')
|
|
159
|
+
} else {
|
|
160
|
+
nativeBinding = require('@css-inline/css-inline-freebsd-x64')
|
|
161
|
+
}
|
|
162
|
+
} catch (e) {
|
|
163
|
+
loadError = e
|
|
164
|
+
}
|
|
165
|
+
break
|
|
166
|
+
case 'linux':
|
|
167
|
+
switch (arch) {
|
|
168
|
+
case 'x64':
|
|
169
|
+
if (isMusl()) {
|
|
170
|
+
localFileExisted = existsSync(
|
|
171
|
+
join(__dirname, 'css-inline.linux-x64-musl.node')
|
|
172
|
+
)
|
|
173
|
+
try {
|
|
174
|
+
if (localFileExisted) {
|
|
175
|
+
nativeBinding = require('./css-inline.linux-x64-musl.node')
|
|
176
|
+
} else {
|
|
177
|
+
nativeBinding = require('@css-inline/css-inline-linux-x64-musl')
|
|
178
|
+
}
|
|
179
|
+
} catch (e) {
|
|
180
|
+
loadError = e
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
localFileExisted = existsSync(
|
|
184
|
+
join(__dirname, 'css-inline.linux-x64-gnu.node')
|
|
185
|
+
)
|
|
186
|
+
try {
|
|
187
|
+
if (localFileExisted) {
|
|
188
|
+
nativeBinding = require('./css-inline.linux-x64-gnu.node')
|
|
189
|
+
} else {
|
|
190
|
+
nativeBinding = require('@css-inline/css-inline-linux-x64-gnu')
|
|
191
|
+
}
|
|
192
|
+
} catch (e) {
|
|
193
|
+
loadError = e
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
break
|
|
197
|
+
case 'arm64':
|
|
198
|
+
if (isMusl()) {
|
|
199
|
+
localFileExisted = existsSync(
|
|
200
|
+
join(__dirname, 'css-inline.linux-arm64-musl.node')
|
|
201
|
+
)
|
|
202
|
+
try {
|
|
203
|
+
if (localFileExisted) {
|
|
204
|
+
nativeBinding = require('./css-inline.linux-arm64-musl.node')
|
|
205
|
+
} else {
|
|
206
|
+
nativeBinding = require('@css-inline/css-inline-linux-arm64-musl')
|
|
207
|
+
}
|
|
208
|
+
} catch (e) {
|
|
209
|
+
loadError = e
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
localFileExisted = existsSync(
|
|
213
|
+
join(__dirname, 'css-inline.linux-arm64-gnu.node')
|
|
214
|
+
)
|
|
215
|
+
try {
|
|
216
|
+
if (localFileExisted) {
|
|
217
|
+
nativeBinding = require('./css-inline.linux-arm64-gnu.node')
|
|
218
|
+
} else {
|
|
219
|
+
nativeBinding = require('@css-inline/css-inline-linux-arm64-gnu')
|
|
220
|
+
}
|
|
221
|
+
} catch (e) {
|
|
222
|
+
loadError = e
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
break
|
|
226
|
+
case 'arm':
|
|
227
|
+
localFileExisted = existsSync(
|
|
228
|
+
join(__dirname, 'css-inline.linux-arm-gnueabihf.node')
|
|
229
|
+
)
|
|
230
|
+
try {
|
|
231
|
+
if (localFileExisted) {
|
|
232
|
+
nativeBinding = require('./css-inline.linux-arm-gnueabihf.node')
|
|
233
|
+
} else {
|
|
234
|
+
nativeBinding = require('@css-inline/css-inline-linux-arm-gnueabihf')
|
|
235
|
+
}
|
|
236
|
+
} catch (e) {
|
|
237
|
+
loadError = e
|
|
238
|
+
}
|
|
239
|
+
break
|
|
240
|
+
default:
|
|
241
|
+
throw new Error(`Unsupported architecture on Linux: ${arch}`)
|
|
242
|
+
}
|
|
243
|
+
break
|
|
244
|
+
default:
|
|
245
|
+
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (!nativeBinding) {
|
|
249
|
+
if (loadError) {
|
|
250
|
+
throw loadError
|
|
251
|
+
}
|
|
252
|
+
throw new Error(`Failed to load native binding`)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const { inline } = nativeBinding
|
|
256
|
+
|
|
257
|
+
module.exports.inline = inline
|
package/js-binding.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/* auto-generated by NAPI-RS */
|
|
5
|
+
|
|
6
|
+
export interface Options {
|
|
7
|
+
/**
|
|
8
|
+
* Whether to inline CSS from "style" tags.
|
|
9
|
+
*
|
|
10
|
+
* Sometimes HTML may include a lot of boilerplate styles, that are not applicable in every
|
|
11
|
+
* scenario, and it is useful to ignore them and use `extra_css` instead.
|
|
12
|
+
*/
|
|
13
|
+
inlineStyleTags?: boolean
|
|
14
|
+
/** Keep "style" tags after inlining. */
|
|
15
|
+
keepStyleTags?: boolean
|
|
16
|
+
/** Keep "link" tags after inlining. */
|
|
17
|
+
keepLinkTags?: boolean
|
|
18
|
+
/** Used for loading external stylesheets via relative URLs. */
|
|
19
|
+
baseUrl?: string
|
|
20
|
+
/** Whether remote stylesheets should be loaded or not. */
|
|
21
|
+
loadRemoteStylesheets?: boolean
|
|
22
|
+
/** Additional CSS to inline. */
|
|
23
|
+
extraCss?: string
|
|
24
|
+
/**
|
|
25
|
+
* Pre-allocate capacity for HTML nodes during parsing.
|
|
26
|
+
* It can improve performance when you have an estimate of the number of nodes in your HTML document.
|
|
27
|
+
*/
|
|
28
|
+
preallocateNodeCapacity?: number
|
|
29
|
+
}
|
|
30
|
+
/** Inline CSS styles from <style> tags to matching elements in the HTML tree and return a string. */
|
|
31
|
+
export function inline(html: string, options?: Options | undefined | null): string
|
|
32
|
+
/** Get the package version. */
|
|
33
|
+
export function version(): string
|
package/js-binding.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/* prettier-ignore */
|
|
4
|
+
|
|
5
|
+
/* auto-generated by NAPI-RS */
|
|
6
|
+
|
|
7
|
+
const { existsSync, readFileSync } = require('fs')
|
|
8
|
+
const { join } = require('path')
|
|
9
|
+
|
|
10
|
+
const { platform, arch } = process
|
|
11
|
+
|
|
12
|
+
let nativeBinding = null
|
|
13
|
+
let localFileExisted = false
|
|
14
|
+
let loadError = null
|
|
15
|
+
|
|
16
|
+
function isMusl() {
|
|
17
|
+
// For Node 10
|
|
18
|
+
if (!process.report || typeof process.report.getReport !== 'function') {
|
|
19
|
+
try {
|
|
20
|
+
const lddPath = require('child_process').execSync('which ldd').toString().trim()
|
|
21
|
+
return readFileSync(lddPath, 'utf8').includes('musl')
|
|
22
|
+
} catch (e) {
|
|
23
|
+
return true
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
const { glibcVersionRuntime } = process.report.getReport().header
|
|
27
|
+
return !glibcVersionRuntime
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
switch (platform) {
|
|
32
|
+
case 'android':
|
|
33
|
+
switch (arch) {
|
|
34
|
+
case 'arm64':
|
|
35
|
+
localFileExisted = existsSync(join(__dirname, 'css-inline.android-arm64.node'))
|
|
36
|
+
try {
|
|
37
|
+
if (localFileExisted) {
|
|
38
|
+
nativeBinding = require('./css-inline.android-arm64.node')
|
|
39
|
+
} else {
|
|
40
|
+
nativeBinding = require('@css-inline/css-inline-android-arm64')
|
|
41
|
+
}
|
|
42
|
+
} catch (e) {
|
|
43
|
+
loadError = e
|
|
44
|
+
}
|
|
45
|
+
break
|
|
46
|
+
case 'arm':
|
|
47
|
+
localFileExisted = existsSync(join(__dirname, 'css-inline.android-arm-eabi.node'))
|
|
48
|
+
try {
|
|
49
|
+
if (localFileExisted) {
|
|
50
|
+
nativeBinding = require('./css-inline.android-arm-eabi.node')
|
|
51
|
+
} else {
|
|
52
|
+
nativeBinding = require('@css-inline/css-inline-android-arm-eabi')
|
|
53
|
+
}
|
|
54
|
+
} catch (e) {
|
|
55
|
+
loadError = e
|
|
56
|
+
}
|
|
57
|
+
break
|
|
58
|
+
default:
|
|
59
|
+
throw new Error(`Unsupported architecture on Android ${arch}`)
|
|
60
|
+
}
|
|
61
|
+
break
|
|
62
|
+
case 'win32':
|
|
63
|
+
switch (arch) {
|
|
64
|
+
case 'x64':
|
|
65
|
+
localFileExisted = existsSync(
|
|
66
|
+
join(__dirname, 'css-inline.win32-x64-msvc.node')
|
|
67
|
+
)
|
|
68
|
+
try {
|
|
69
|
+
if (localFileExisted) {
|
|
70
|
+
nativeBinding = require('./css-inline.win32-x64-msvc.node')
|
|
71
|
+
} else {
|
|
72
|
+
nativeBinding = require('@css-inline/css-inline-win32-x64-msvc')
|
|
73
|
+
}
|
|
74
|
+
} catch (e) {
|
|
75
|
+
loadError = e
|
|
76
|
+
}
|
|
77
|
+
break
|
|
78
|
+
case 'ia32':
|
|
79
|
+
localFileExisted = existsSync(
|
|
80
|
+
join(__dirname, 'css-inline.win32-ia32-msvc.node')
|
|
81
|
+
)
|
|
82
|
+
try {
|
|
83
|
+
if (localFileExisted) {
|
|
84
|
+
nativeBinding = require('./css-inline.win32-ia32-msvc.node')
|
|
85
|
+
} else {
|
|
86
|
+
nativeBinding = require('@css-inline/css-inline-win32-ia32-msvc')
|
|
87
|
+
}
|
|
88
|
+
} catch (e) {
|
|
89
|
+
loadError = e
|
|
90
|
+
}
|
|
91
|
+
break
|
|
92
|
+
case 'arm64':
|
|
93
|
+
localFileExisted = existsSync(
|
|
94
|
+
join(__dirname, 'css-inline.win32-arm64-msvc.node')
|
|
95
|
+
)
|
|
96
|
+
try {
|
|
97
|
+
if (localFileExisted) {
|
|
98
|
+
nativeBinding = require('./css-inline.win32-arm64-msvc.node')
|
|
99
|
+
} else {
|
|
100
|
+
nativeBinding = require('@css-inline/css-inline-win32-arm64-msvc')
|
|
101
|
+
}
|
|
102
|
+
} catch (e) {
|
|
103
|
+
loadError = e
|
|
104
|
+
}
|
|
105
|
+
break
|
|
106
|
+
default:
|
|
107
|
+
throw new Error(`Unsupported architecture on Windows: ${arch}`)
|
|
108
|
+
}
|
|
109
|
+
break
|
|
110
|
+
case 'darwin':
|
|
111
|
+
localFileExisted = existsSync(join(__dirname, 'css-inline.darwin-universal.node'))
|
|
112
|
+
try {
|
|
113
|
+
if (localFileExisted) {
|
|
114
|
+
nativeBinding = require('./css-inline.darwin-universal.node')
|
|
115
|
+
} else {
|
|
116
|
+
nativeBinding = require('@css-inline/css-inline-darwin-universal')
|
|
117
|
+
}
|
|
118
|
+
break
|
|
119
|
+
} catch {}
|
|
120
|
+
switch (arch) {
|
|
121
|
+
case 'x64':
|
|
122
|
+
localFileExisted = existsSync(join(__dirname, 'css-inline.darwin-x64.node'))
|
|
123
|
+
try {
|
|
124
|
+
if (localFileExisted) {
|
|
125
|
+
nativeBinding = require('./css-inline.darwin-x64.node')
|
|
126
|
+
} else {
|
|
127
|
+
nativeBinding = require('@css-inline/css-inline-darwin-x64')
|
|
128
|
+
}
|
|
129
|
+
} catch (e) {
|
|
130
|
+
loadError = e
|
|
131
|
+
}
|
|
132
|
+
break
|
|
133
|
+
case 'arm64':
|
|
134
|
+
localFileExisted = existsSync(
|
|
135
|
+
join(__dirname, 'css-inline.darwin-arm64.node')
|
|
136
|
+
)
|
|
137
|
+
try {
|
|
138
|
+
if (localFileExisted) {
|
|
139
|
+
nativeBinding = require('./css-inline.darwin-arm64.node')
|
|
140
|
+
} else {
|
|
141
|
+
nativeBinding = require('@css-inline/css-inline-darwin-arm64')
|
|
142
|
+
}
|
|
143
|
+
} catch (e) {
|
|
144
|
+
loadError = e
|
|
145
|
+
}
|
|
146
|
+
break
|
|
147
|
+
default:
|
|
148
|
+
throw new Error(`Unsupported architecture on macOS: ${arch}`)
|
|
149
|
+
}
|
|
150
|
+
break
|
|
151
|
+
case 'freebsd':
|
|
152
|
+
if (arch !== 'x64') {
|
|
153
|
+
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
|
|
154
|
+
}
|
|
155
|
+
localFileExisted = existsSync(join(__dirname, 'css-inline.freebsd-x64.node'))
|
|
156
|
+
try {
|
|
157
|
+
if (localFileExisted) {
|
|
158
|
+
nativeBinding = require('./css-inline.freebsd-x64.node')
|
|
159
|
+
} else {
|
|
160
|
+
nativeBinding = require('@css-inline/css-inline-freebsd-x64')
|
|
161
|
+
}
|
|
162
|
+
} catch (e) {
|
|
163
|
+
loadError = e
|
|
164
|
+
}
|
|
165
|
+
break
|
|
166
|
+
case 'linux':
|
|
167
|
+
switch (arch) {
|
|
168
|
+
case 'x64':
|
|
169
|
+
if (isMusl()) {
|
|
170
|
+
localFileExisted = existsSync(
|
|
171
|
+
join(__dirname, 'css-inline.linux-x64-musl.node')
|
|
172
|
+
)
|
|
173
|
+
try {
|
|
174
|
+
if (localFileExisted) {
|
|
175
|
+
nativeBinding = require('./css-inline.linux-x64-musl.node')
|
|
176
|
+
} else {
|
|
177
|
+
nativeBinding = require('@css-inline/css-inline-linux-x64-musl')
|
|
178
|
+
}
|
|
179
|
+
} catch (e) {
|
|
180
|
+
loadError = e
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
localFileExisted = existsSync(
|
|
184
|
+
join(__dirname, 'css-inline.linux-x64-gnu.node')
|
|
185
|
+
)
|
|
186
|
+
try {
|
|
187
|
+
if (localFileExisted) {
|
|
188
|
+
nativeBinding = require('./css-inline.linux-x64-gnu.node')
|
|
189
|
+
} else {
|
|
190
|
+
nativeBinding = require('@css-inline/css-inline-linux-x64-gnu')
|
|
191
|
+
}
|
|
192
|
+
} catch (e) {
|
|
193
|
+
loadError = e
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
break
|
|
197
|
+
case 'arm64':
|
|
198
|
+
if (isMusl()) {
|
|
199
|
+
localFileExisted = existsSync(
|
|
200
|
+
join(__dirname, 'css-inline.linux-arm64-musl.node')
|
|
201
|
+
)
|
|
202
|
+
try {
|
|
203
|
+
if (localFileExisted) {
|
|
204
|
+
nativeBinding = require('./css-inline.linux-arm64-musl.node')
|
|
205
|
+
} else {
|
|
206
|
+
nativeBinding = require('@css-inline/css-inline-linux-arm64-musl')
|
|
207
|
+
}
|
|
208
|
+
} catch (e) {
|
|
209
|
+
loadError = e
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
localFileExisted = existsSync(
|
|
213
|
+
join(__dirname, 'css-inline.linux-arm64-gnu.node')
|
|
214
|
+
)
|
|
215
|
+
try {
|
|
216
|
+
if (localFileExisted) {
|
|
217
|
+
nativeBinding = require('./css-inline.linux-arm64-gnu.node')
|
|
218
|
+
} else {
|
|
219
|
+
nativeBinding = require('@css-inline/css-inline-linux-arm64-gnu')
|
|
220
|
+
}
|
|
221
|
+
} catch (e) {
|
|
222
|
+
loadError = e
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
break
|
|
226
|
+
case 'arm':
|
|
227
|
+
localFileExisted = existsSync(
|
|
228
|
+
join(__dirname, 'css-inline.linux-arm-gnueabihf.node')
|
|
229
|
+
)
|
|
230
|
+
try {
|
|
231
|
+
if (localFileExisted) {
|
|
232
|
+
nativeBinding = require('./css-inline.linux-arm-gnueabihf.node')
|
|
233
|
+
} else {
|
|
234
|
+
nativeBinding = require('@css-inline/css-inline-linux-arm-gnueabihf')
|
|
235
|
+
}
|
|
236
|
+
} catch (e) {
|
|
237
|
+
loadError = e
|
|
238
|
+
}
|
|
239
|
+
break
|
|
240
|
+
case 'riscv64':
|
|
241
|
+
if (isMusl()) {
|
|
242
|
+
localFileExisted = existsSync(
|
|
243
|
+
join(__dirname, 'css-inline.linux-riscv64-musl.node')
|
|
244
|
+
)
|
|
245
|
+
try {
|
|
246
|
+
if (localFileExisted) {
|
|
247
|
+
nativeBinding = require('./css-inline.linux-riscv64-musl.node')
|
|
248
|
+
} else {
|
|
249
|
+
nativeBinding = require('@css-inline/css-inline-linux-riscv64-musl')
|
|
250
|
+
}
|
|
251
|
+
} catch (e) {
|
|
252
|
+
loadError = e
|
|
253
|
+
}
|
|
254
|
+
} else {
|
|
255
|
+
localFileExisted = existsSync(
|
|
256
|
+
join(__dirname, 'css-inline.linux-riscv64-gnu.node')
|
|
257
|
+
)
|
|
258
|
+
try {
|
|
259
|
+
if (localFileExisted) {
|
|
260
|
+
nativeBinding = require('./css-inline.linux-riscv64-gnu.node')
|
|
261
|
+
} else {
|
|
262
|
+
nativeBinding = require('@css-inline/css-inline-linux-riscv64-gnu')
|
|
263
|
+
}
|
|
264
|
+
} catch (e) {
|
|
265
|
+
loadError = e
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
break
|
|
269
|
+
default:
|
|
270
|
+
throw new Error(`Unsupported architecture on Linux: ${arch}`)
|
|
271
|
+
}
|
|
272
|
+
break
|
|
273
|
+
default:
|
|
274
|
+
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (!nativeBinding) {
|
|
278
|
+
if (loadError) {
|
|
279
|
+
throw loadError
|
|
280
|
+
}
|
|
281
|
+
throw new Error(`Failed to load native binding`)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const { inline, version } = nativeBinding
|
|
285
|
+
|
|
286
|
+
module.exports.inline = inline
|
|
287
|
+
module.exports.version = version
|
package/package.json
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@css-inline/css-inline",
|
|
3
|
+
"version": "0.12.0",
|
|
4
|
+
"description": "High-performance library for inlining CSS into HTML 'style' attributes",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"repository": "https://github.com/Stranger6667/css-inline",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"css",
|
|
9
|
+
"html",
|
|
10
|
+
"email",
|
|
11
|
+
"stylesheet",
|
|
12
|
+
"inlining"
|
|
13
|
+
],
|
|
14
|
+
"files": [
|
|
15
|
+
"index.d.ts",
|
|
16
|
+
"index.js",
|
|
17
|
+
"js-binding.js",
|
|
18
|
+
"js-binding.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"types": "index.d.ts",
|
|
21
|
+
"napi": {
|
|
22
|
+
"name": "css-inline",
|
|
23
|
+
"triples": {
|
|
24
|
+
"additional": [
|
|
25
|
+
"aarch64-apple-darwin",
|
|
26
|
+
"aarch64-unknown-linux-gnu",
|
|
27
|
+
"aarch64-unknown-linux-musl",
|
|
28
|
+
"armv7-unknown-linux-gnueabihf",
|
|
29
|
+
"x86_64-unknown-linux-musl"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@ava/typescript": "^4.1.0",
|
|
36
|
+
"@napi-rs/cli": "^2.17.0",
|
|
37
|
+
"@swc-node/register": "^1.6.8",
|
|
38
|
+
"@swc/core": "^1.3.101",
|
|
39
|
+
"@types/copyfiles": "^2",
|
|
40
|
+
"@types/eslint": "^8",
|
|
41
|
+
"@typescript-eslint/eslint-plugin": "^6.16.0",
|
|
42
|
+
"@typescript-eslint/parser": "^6.16.0",
|
|
43
|
+
"ava": "^6.0.1",
|
|
44
|
+
"benny": "^3.7.1",
|
|
45
|
+
"copyfiles": "^2.4.1",
|
|
46
|
+
"dts-bundle-generator": "^9.1.0",
|
|
47
|
+
"esbuild": "^0.19.10",
|
|
48
|
+
"eslint": "^8.56.0",
|
|
49
|
+
"eslint-config-prettier": "^9.1.0",
|
|
50
|
+
"eslint-plugin-import": "^2.29.1",
|
|
51
|
+
"eslint-plugin-prettier": "^5.1.2",
|
|
52
|
+
"eslint-plugin-sonarjs": "^0.23.0",
|
|
53
|
+
"inline-css": "^4.0.2",
|
|
54
|
+
"juice": "^10.0.0",
|
|
55
|
+
"npm-run-all2": "^6.1.1",
|
|
56
|
+
"prettier": "^3.1.1",
|
|
57
|
+
"typescript": "^5.3.3"
|
|
58
|
+
},
|
|
59
|
+
"ava": {
|
|
60
|
+
"require": [
|
|
61
|
+
"@swc-node/register"
|
|
62
|
+
],
|
|
63
|
+
"typescript": {
|
|
64
|
+
"rewritePaths": {
|
|
65
|
+
"src/": "build/"
|
|
66
|
+
},
|
|
67
|
+
"compile": false
|
|
68
|
+
},
|
|
69
|
+
"timeout": "3m"
|
|
70
|
+
},
|
|
71
|
+
"engines": {
|
|
72
|
+
"node": ">= 10"
|
|
73
|
+
},
|
|
74
|
+
"publishConfig": {
|
|
75
|
+
"registry": "https://registry.npmjs.org/",
|
|
76
|
+
"access": "public"
|
|
77
|
+
},
|
|
78
|
+
"scripts": {
|
|
79
|
+
"artifacts": "napi artifacts",
|
|
80
|
+
"bench": "node -r @swc-node/register benches/bench.ts",
|
|
81
|
+
"bundle": "run-p 'bundle:*'",
|
|
82
|
+
"bundle:js": "node bundle.js",
|
|
83
|
+
"bundle:dts": "dts-bundle-generator --external-types -o wasm/index.d.ts wasm-binding.ts",
|
|
84
|
+
"build": "napi build --platform --release --js js-binding.js --dts js-binding.d.ts",
|
|
85
|
+
"build:debug": "napi build --platform --js js-binding.js --dts js-binding.d.ts",
|
|
86
|
+
"build:wasm": "run-s build:wasm-web copy-wasm bundle",
|
|
87
|
+
"build:wasm-web": "wasm-pack build --target web --out-name index --out-dir wasm/dist --release",
|
|
88
|
+
"copy-wasm": "copyfiles -f wasm/dist/index_bg.wasm ./wasm",
|
|
89
|
+
"lint": "eslint . -c ./.eslintrc.yml './**/*.{ts,tsx,js}'",
|
|
90
|
+
"lint:fix": "eslint . -c ./.eslintrc.yml './**/*.{ts,tsx,js}' --fix",
|
|
91
|
+
"prepublishOnly": "napi prepublish -t npm",
|
|
92
|
+
"test": "ava __test__/index*.*",
|
|
93
|
+
"test:wasm": "ava __test__/**/wasm*.*",
|
|
94
|
+
"universal": "napi universal",
|
|
95
|
+
"version": "napi version"
|
|
96
|
+
},
|
|
97
|
+
"packageManager": "yarn@4.0.2",
|
|
98
|
+
"optionalDependencies": {
|
|
99
|
+
"@css-inline/css-inline-win32-x64-msvc": "0.12.0",
|
|
100
|
+
"@css-inline/css-inline-darwin-x64": "0.12.0",
|
|
101
|
+
"@css-inline/css-inline-linux-x64-gnu": "0.12.0",
|
|
102
|
+
"@css-inline/css-inline-darwin-arm64": "0.12.0",
|
|
103
|
+
"@css-inline/css-inline-linux-arm64-gnu": "0.12.0",
|
|
104
|
+
"@css-inline/css-inline-linux-arm64-musl": "0.12.0",
|
|
105
|
+
"@css-inline/css-inline-linux-arm-gnueabihf": "0.12.0",
|
|
106
|
+
"@css-inline/css-inline-linux-x64-musl": "0.12.0"
|
|
107
|
+
}
|
|
108
|
+
}
|