carbon-preprocess-svelte 0.9.1 → 0.11.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 +162 -348
- package/dist/component-index.d.ts +4 -0
- package/dist/component-index.js +1924 -0
- package/dist/constants.d.ts +8 -14
- package/dist/constants.js +7 -0
- package/dist/index.d.ts +3 -4
- package/dist/index.js +12 -83169
- package/dist/plugins/OptimizeCssPlugin.d.ts +8 -0
- package/dist/plugins/OptimizeCssPlugin.js +49 -0
- package/dist/plugins/compare-diff.d.ts +6 -0
- package/dist/plugins/compare-diff.js +36 -0
- package/dist/plugins/create-optimized-css.d.ts +25 -0
- package/dist/plugins/create-optimized-css.js +89 -0
- package/dist/plugins/optimize-css.d.ts +3 -10
- package/dist/plugins/optimize-css.js +49 -0
- package/dist/preprocessors/optimize-imports.d.ts +3 -3
- package/dist/preprocessors/optimize-imports.js +65 -0
- package/dist/utils.d.ts +3 -11
- package/dist/utils.js +16 -0
- package/package.json +21 -49
- package/CHANGELOG.md +0 -126
- package/dist/build/build-components.d.ts +0 -9
- package/dist/build/build-elements.d.ts +0 -10
- package/dist/build/build-icons.d.ts +0 -15
- package/dist/build/build-pictograms.d.ts +0 -54
- package/dist/build/index.d.ts +0 -19
- package/dist/build/type.d.ts +0 -11
- package/dist/carbon-components-svelte.d.ts +0 -681
- package/dist/carbon-elements.d.ts +0 -833
- package/dist/carbon-icons.d.ts +0 -84420
- package/dist/carbon-pictograms.d.ts +0 -8839
- package/dist/extractors/extract-selectors.d.ts +0 -14
- package/dist/extractors/index.d.ts +0 -1
- package/dist/index.cjs.d.ts +0 -4
- package/dist/index.mjs +0 -83184
- package/dist/plugins/index.d.ts +0 -1
- package/dist/plugins/optimize-css.cjs.d.ts +0 -10
- package/dist/preprocessors/collect-headings.d.ts +0 -27
- package/dist/preprocessors/elements.d.ts +0 -22
- package/dist/preprocessors/icons.d.ts +0 -2
- package/dist/preprocessors/include.d.ts +0 -49
- package/dist/preprocessors/index.d.ts +0 -6
- package/dist/preprocessors/pictograms.d.ts +0 -2
- package/dist/presets/index.d.ts +0 -1
- package/dist/presets/preset-carbon.d.ts +0 -7
- package/dist/walk-and-replace.d.ts +0 -117
- package/src/carbon-components-svelte.js +0 -513
package/LICENSE
CHANGED
|
@@ -198,4 +198,4 @@
|
|
|
198
198
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
199
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
200
|
See the License for the specific language governing permissions and
|
|
201
|
-
limitations under the License.
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -4,42 +4,31 @@
|
|
|
4
4
|

|
|
5
5
|

|
|
6
6
|
|
|
7
|
-
>
|
|
7
|
+
> [Svelte preprocessors](https://svelte.dev/docs/svelte-compiler#types-preprocessor) for the Carbon Design System
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Install `carbon-preprocess-svelte` as a development dependency.
|
|
12
12
|
|
|
13
13
|
```sh
|
|
14
|
+
# Yarn
|
|
14
15
|
yarn add -D carbon-preprocess-svelte
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
**NPM**
|
|
18
16
|
|
|
19
|
-
|
|
17
|
+
# npm
|
|
20
18
|
npm i -D carbon-preprocess-svelte
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
**pnpm**
|
|
24
19
|
|
|
25
|
-
|
|
20
|
+
# pnpm
|
|
26
21
|
pnpm i -D carbon-preprocess-svelte
|
|
27
22
|
```
|
|
28
23
|
|
|
29
24
|
## Usage
|
|
30
25
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
- **optimizeImports**: rewrites Carbon Svelte imports to their source path in the `script` block
|
|
34
|
-
- **optimizeCss**: Rollup plugin that removes unused CSS using [PurgeCSS](https://github.com/FullHuman/purgecss)
|
|
35
|
-
- **elements**: computes [Carbon tokens](https://www.carbondesignsystem.com/guidelines/themes/overview#tokens) in the `style` block
|
|
36
|
-
- **icons**: inlines [Carbon icons](https://www.carbondesignsystem.com/guidelines/icons/library/) in the `markup` block
|
|
37
|
-
- **pictograms**: inlines [Carbon pictograms](https://www.carbondesignsystem.com/guidelines/pictograms/library/) in the `markup` block
|
|
38
|
-
- **collectHeadings**: extracts heading elements (e.g., `<h1>`, `<h2>`) from the `markup`
|
|
26
|
+
- [**optimizeImports**](#optimizeimports): Svelte preprocessor that rewrites Carbon Svelte imports to their source path in the `script` block, making development compile times dramatically faster.
|
|
27
|
+
- [**optimizeCss**](#optimizecss): Vite/Rollup plugin that removes unused Carbon styles, resulting in smaller CSS bundles.
|
|
39
28
|
|
|
40
29
|
### `optimizeImports`
|
|
41
30
|
|
|
42
|
-
`optimizeImports` is a Svelte
|
|
31
|
+
`optimizeImports` is a Svelte preprocessor that rewrites barrel imports from Carbon components/icons/pictograms packages to their source Svelte code paths. This can significantly speed up development and production build compile times while preserving typeahead and autocompletion offered by integrated development environments (IDE) like VS Code.
|
|
43
32
|
|
|
44
33
|
The preprocessor optimizes imports from the following packages:
|
|
45
34
|
|
|
@@ -47,420 +36,245 @@ The preprocessor optimizes imports from the following packages:
|
|
|
47
36
|
- [carbon-icons-svelte](https://github.com/carbon-design-system/carbon-icons-svelte)
|
|
48
37
|
- [carbon-pictograms-svelte](https://github.com/carbon-design-system/carbon-pictograms-svelte)
|
|
49
38
|
|
|
50
|
-
**Example**
|
|
51
|
-
|
|
52
39
|
```diff
|
|
53
40
|
- import { Button } from "carbon-components-svelte";
|
|
54
|
-
|
|
55
|
-
- import { Airplane } from "carbon-pictograms-svelte";
|
|
56
|
-
+ import Button from "carbon-components-svelte/Button.svelte";
|
|
57
|
-
+ import Add16 from "carbon-icons-svelte/lib/Add16.svelte";
|
|
58
|
-
+ import Airplane from "carbon-pictograms-svelte/lib/Airplane.svelte";
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
**Note**
|
|
62
|
-
|
|
63
|
-
A minimum version of `carbon-preprocess-svelte@0.9.0` is required for `carbon-component-svelte@0.63.0`.
|
|
64
|
-
|
|
65
|
-
A minimum version of `carbon-preprocess-svelte@0.8.0` is required for `carbon-icons-svelte@11`.
|
|
41
|
+
+ import Button from "carbon-components-svelte/src/Button/Button.svelte";
|
|
66
42
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
```js
|
|
70
|
-
// svelte.config.js
|
|
71
|
-
import { optimizeImports } from "carbon-preprocess-svelte";
|
|
43
|
+
- import { Add } from "carbon-icons-svelte";
|
|
44
|
+
+ import Add from "carbon-icons-svelte/lib/Add.svelte";
|
|
72
45
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
};
|
|
46
|
+
- import { Airplane } from "carbon-pictograms-svelte";
|
|
47
|
+
+ import Airplane from "carbon-pictograms-svelte/lib/Airplane.svelte";
|
|
76
48
|
```
|
|
77
49
|
|
|
78
|
-
|
|
50
|
+
> [!NOTE]
|
|
51
|
+
> When this preprocessor was first created, there was no workaround to optimize slow cold start times with Vite in development.
|
|
52
|
+
> As of today, [@sveltejs/vite-plugin-svelte](https://github.com/sveltejs/vite-plugin-svelte) enables [`prebundleSvelteLibraries`](https://github.com/sveltejs/vite-plugin-svelte/blob/ba4ac32cf5c3e9c048d1ac430c1091ca08eaa130/docs/config.md#prebundlesveltelibraries), which pre-bundles Svelte libraries to improve cold start times for Vite-based set-ups.
|
|
53
|
+
> However, this preprocessor is still useful for non-Vite bundlers, like Rollup and Webpack.
|
|
79
54
|
|
|
80
|
-
|
|
55
|
+
#### SvelteKit
|
|
81
56
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
`optimizeCss` should be added to the list of `vite.plugins`. Take care to only run the plugin when building for production.
|
|
57
|
+
See [examples/sveltekit](examples/sveltekit).
|
|
85
58
|
|
|
86
59
|
```js
|
|
87
60
|
// svelte.config.js
|
|
88
61
|
import adapter from "@sveltejs/adapter-static";
|
|
89
|
-
import {
|
|
62
|
+
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
|
|
63
|
+
import { optimizeImports } from "carbon-preprocess-svelte";
|
|
90
64
|
|
|
91
|
-
|
|
65
|
+
/** @type {import('@sveltejs/kit').Config} */
|
|
66
|
+
const config = {
|
|
67
|
+
preprocess: [
|
|
68
|
+
// Preprocessors are run in sequence.
|
|
69
|
+
// If using TypeScript, the code must be transpiled first.
|
|
70
|
+
vitePreprocess(),
|
|
71
|
+
optimizeImports(),
|
|
72
|
+
],
|
|
92
73
|
kit: {
|
|
93
74
|
adapter: adapter(),
|
|
94
|
-
vite: {
|
|
95
|
-
plugins: [process.env.NODE_ENV === "production" && optimizeCss()],
|
|
96
|
-
},
|
|
97
75
|
},
|
|
98
76
|
};
|
|
99
|
-
```
|
|
100
77
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
```ts
|
|
104
|
-
interface OptimizeCssOptions {
|
|
105
|
-
safelist: {
|
|
106
|
-
standard?: Array<RegExp | string>;
|
|
107
|
-
deep?: RegExp[];
|
|
108
|
-
greedy?: RegExp[];
|
|
109
|
-
};
|
|
110
|
-
}
|
|
78
|
+
export default config;
|
|
111
79
|
```
|
|
112
80
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
`elements` is a Svelte style preprocessor that rewrites [Carbon Design System tokens](https://www.carbondesignsystem.com/guidelines/themes/overview#tokens) to their computed values. The purpose is to use design system token names (e.g., `"interactive-01"`) instead of hardcoded values (e.g., `"#0f62fe"`).
|
|
116
|
-
|
|
117
|
-
**Before**
|
|
81
|
+
#### Vite
|
|
118
82
|
|
|
119
|
-
|
|
120
|
-
<style>
|
|
121
|
-
h1,
|
|
122
|
-
h2 {
|
|
123
|
-
font-weight: "semibold";
|
|
124
|
-
background: "ui-01";
|
|
125
|
-
}
|
|
83
|
+
See [examples/vite](examples/vite).
|
|
126
84
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
85
|
+
```js
|
|
86
|
+
// vite.config.js
|
|
87
|
+
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
|
88
|
+
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
|
|
89
|
+
import { optimizeImports } from "carbon-preprocess-svelte";
|
|
131
90
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
91
|
+
/** @type {import('vite').UserConfig} */
|
|
92
|
+
export default {
|
|
93
|
+
plugins: [
|
|
94
|
+
svelte({
|
|
95
|
+
preprocess: [
|
|
96
|
+
// Preprocessors are run in sequence.
|
|
97
|
+
// If using TypeScript, the code must be transpiled first.
|
|
98
|
+
vitePreprocess(),
|
|
99
|
+
optimizeImports(),
|
|
100
|
+
],
|
|
101
|
+
}),
|
|
102
|
+
],
|
|
103
|
+
};
|
|
138
104
|
```
|
|
139
105
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
```svelte
|
|
143
|
-
<style>
|
|
144
|
-
h1,
|
|
145
|
-
h2 {
|
|
146
|
-
font-weight: 600;
|
|
147
|
-
background: #f4f4f4;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
div {
|
|
151
|
-
font-size: 0.875rem;
|
|
152
|
-
font-weight: 600;
|
|
153
|
-
line-height: 1.25;
|
|
154
|
-
letter-spacing: 0.16px;
|
|
155
|
-
transition: background cubic-bezier(0.2, 0, 0.38, 0.9);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
@media (between: 321px) and (md) {
|
|
159
|
-
div {
|
|
160
|
-
color: #0f62fe;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
</style>
|
|
164
|
-
```
|
|
106
|
+
#### Rollup
|
|
165
107
|
|
|
166
|
-
|
|
108
|
+
This code is abridged; see [examples/rollup](examples/rollup) for a full set-up.
|
|
167
109
|
|
|
168
110
|
```js
|
|
169
|
-
//
|
|
170
|
-
import
|
|
111
|
+
// rollup.config.js
|
|
112
|
+
import svelte from "rollup-plugin-svelte";
|
|
113
|
+
import { optimizeImports } from "carbon-preprocess-svelte";
|
|
171
114
|
|
|
172
115
|
export default {
|
|
173
|
-
|
|
116
|
+
plugins: [
|
|
117
|
+
svelte({
|
|
118
|
+
preprocess: [optimizeImports()],
|
|
119
|
+
}),
|
|
120
|
+
],
|
|
174
121
|
};
|
|
175
122
|
```
|
|
176
123
|
|
|
177
|
-
####
|
|
124
|
+
#### Webpack
|
|
178
125
|
|
|
179
|
-
|
|
180
|
-
interface ElementsOptions {
|
|
181
|
-
/**
|
|
182
|
-
* Specify the Carbon theme
|
|
183
|
-
* Setting to "all" will also enable `cssVars`
|
|
184
|
-
* @default "white"
|
|
185
|
-
*/
|
|
186
|
-
theme: "white" | "g10" | "g90" | "g100" | "all";
|
|
126
|
+
This code is abridged; see [examples/webpack](examples/webpack) for a full set-up.
|
|
187
127
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
128
|
+
```js
|
|
129
|
+
// webpack.config.js
|
|
130
|
+
const { optimizeImports } = require("carbon-preprocess-svelte");
|
|
131
|
+
|
|
132
|
+
module.exports = {
|
|
133
|
+
module: {
|
|
134
|
+
rules: [
|
|
135
|
+
{
|
|
136
|
+
test: /\.svelte$/,
|
|
137
|
+
use: {
|
|
138
|
+
loader: "svelte-loader",
|
|
139
|
+
options: {
|
|
140
|
+
hotReload: !PROD,
|
|
141
|
+
preprocess: [optimizeImports()],
|
|
142
|
+
compilerOptions: { dev: !PROD },
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
},
|
|
148
|
+
};
|
|
198
149
|
```
|
|
199
150
|
|
|
200
|
-
### `
|
|
201
|
-
|
|
202
|
-
`icons` is a Svelte markup preprocessor that inlines [Carbon SVG icons](https://www.carbondesignsystem.com/guidelines/icons/library/).
|
|
151
|
+
### `optimizeCss`
|
|
203
152
|
|
|
204
|
-
|
|
153
|
+
`optimizeCss` is a Vite plugin that removes unused Carbon styles at build time. The plugin is compatible with Rollup ([Vite](https://vitejs.dev/guide/api-plugin) extends the Rollup plugin API).
|
|
205
154
|
|
|
206
|
-
|
|
155
|
+
`carbon-components-svelte@0.85.0` or greater is required.
|
|
207
156
|
|
|
208
157
|
```diff
|
|
209
|
-
|
|
210
|
-
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" fill="currentColor" focusable="false" preserveAspectRatio="xMidYMid meet" width=16 height=16><path d="M17 15L17 8 15 8 15 15 8 15 8 17 15 17 15 24 17 24 17 17 24 17 24 15z"></path></svg>
|
|
211
|
-
```
|
|
158
|
+
$ vite build
|
|
212
159
|
|
|
213
|
-
|
|
160
|
+
Optimized index-CU4gbKFa.css
|
|
161
|
+
- Before: 606.26 kB
|
|
162
|
+
+ After: 53.22 kB (-91.22%)
|
|
214
163
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
export default {
|
|
220
|
-
preprocess: [icons()],
|
|
221
|
-
};
|
|
164
|
+
dist/index.html 0.34 kB │ gzip: 0.24 kB
|
|
165
|
+
dist/assets/index-CU4gbKFa.css 53.22 kB │ gzip: 6.91 kB
|
|
166
|
+
dist/assets/index-Ceijs3eO.js 53.65 kB │ gzip: 15.88 kB
|
|
222
167
|
```
|
|
223
168
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
`pictograms` is a Svelte markup preprocessor that inlines [Carbon SVG pictograms](https://www.carbondesignsystem.com/guidelines/pictograms/library/).
|
|
169
|
+
> [!NOTE]
|
|
170
|
+
> This is a plugin and not a Svelte preprocessor. It should be added to the list of `vite.plugins`. For Vite set-ups, this plugin is only run when building the app. For Rollup and Webpack, you should conditionally apply the plugin to only execute when building for production.
|
|
227
171
|
|
|
228
|
-
|
|
172
|
+
#### SvelteKit
|
|
229
173
|
|
|
230
|
-
|
|
174
|
+
See [examples/sveltekit](examples/sveltekit).
|
|
231
175
|
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
|
|
176
|
+
```js
|
|
177
|
+
// vite.config.js
|
|
178
|
+
import { sveltekit } from "@sveltejs/kit/vite";
|
|
179
|
+
import { optimizeCss } from "carbon-preprocess-svelte";
|
|
180
|
+
import { defineConfig } from "vite";
|
|
235
181
|
|
|
182
|
+
export default defineConfig({
|
|
183
|
+
plugins: [sveltekit(), optimizeCss()],
|
|
184
|
+
});
|
|
236
185
|
```
|
|
237
186
|
|
|
238
|
-
####
|
|
187
|
+
#### Vite
|
|
188
|
+
|
|
189
|
+
See [examples/vite](examples/vite).
|
|
239
190
|
|
|
240
191
|
```js
|
|
241
|
-
//
|
|
242
|
-
import {
|
|
192
|
+
// vite.config.js
|
|
193
|
+
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
|
194
|
+
import { optimizeCss } from "carbon-preprocess-svelte";
|
|
243
195
|
|
|
196
|
+
/** @type {import('vite').UserConfig} */
|
|
244
197
|
export default {
|
|
245
|
-
|
|
198
|
+
plugins: [svelte(), optimizeCss()],
|
|
246
199
|
};
|
|
247
200
|
```
|
|
248
201
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
`collectHeadings` extracts heading elements from markup with an optional callback to modify the source content. This can be used to create a table of contents.
|
|
252
|
-
|
|
253
|
-
**Example**
|
|
254
|
-
|
|
255
|
-
Markup:
|
|
256
|
-
|
|
257
|
-
```svelte
|
|
258
|
-
<main>
|
|
259
|
-
<!-- toc -->
|
|
260
|
-
<h1 id="h1">Heading 1</h1>
|
|
261
|
-
<h2 id="h2"><strong>Heading</strong> 2</h2>
|
|
262
|
-
<h3>Heading 3</h3>
|
|
263
|
-
</main>
|
|
264
|
-
```
|
|
202
|
+
#### Rollup
|
|
265
203
|
|
|
266
|
-
|
|
204
|
+
This code is abridged; see [examples/rollup](examples/rollup) for a full set-up.
|
|
267
205
|
|
|
268
206
|
```js
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
{ id: "h2-1", text: "Heading 2", level: 2 },
|
|
273
|
-
{ id: undefined, text: "Heading 3", level: 3 },
|
|
274
|
-
];
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
#### Usage
|
|
207
|
+
// rollup.config.js
|
|
208
|
+
import svelte from "rollup-plugin-svelte";
|
|
209
|
+
import { optimizeCss } from "carbon-preprocess-svelte";
|
|
278
210
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
```js
|
|
282
|
-
// svelte.config.js
|
|
283
|
-
import { collectHeadings } from "carbon-preprocess-svelte";
|
|
211
|
+
const production = !process.env.ROLLUP_WATCH;
|
|
284
212
|
|
|
285
213
|
export default {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
const toc = headings
|
|
290
|
-
.filter((heading) => heading.level === 2)
|
|
291
|
-
.map((item) => `<li><a href="#${item.id}">${item.text}</a></li>`)
|
|
292
|
-
.join("");
|
|
293
|
-
|
|
294
|
-
return content.replace("<!-- toc -->", `<ul>${toc}</ul>`);
|
|
295
|
-
},
|
|
214
|
+
plugins: [
|
|
215
|
+
svelte({
|
|
216
|
+
preprocess: [optimizeImports()],
|
|
296
217
|
}),
|
|
218
|
+
|
|
219
|
+
// Only apply the plugin when building for production.
|
|
220
|
+
production && optimizeCss(),
|
|
297
221
|
],
|
|
298
222
|
};
|
|
299
223
|
```
|
|
300
224
|
|
|
301
|
-
####
|
|
302
|
-
|
|
303
|
-
```js
|
|
304
|
-
interface CollectHeadingsOptions {
|
|
305
|
-
/**
|
|
306
|
-
* Specify the filename pattern to process
|
|
307
|
-
* Defaults to files ending with ".svelte"
|
|
308
|
-
* @default /\.(svelte)$/
|
|
309
|
-
*/
|
|
310
|
-
test: RegExp;
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Optional callback to transform the content after extracting all headings
|
|
314
|
-
*/
|
|
315
|
-
afterCollect: (
|
|
316
|
-
headings: Array<{
|
|
317
|
-
id?: string,
|
|
318
|
-
text: string,
|
|
319
|
-
level: 1 | 2 | 3 | 4 | 5 | 6,
|
|
320
|
-
}>,
|
|
321
|
-
content: string
|
|
322
|
-
) => string;
|
|
323
|
-
}
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
### `include`
|
|
327
|
-
|
|
328
|
-
`include` prepends or appends arbitrary content to the script and markup blocks.
|
|
329
|
-
|
|
330
|
-
**Example**
|
|
331
|
-
|
|
332
|
-
```diff
|
|
333
|
-
<script>
|
|
334
|
-
+ import { CodeSnippet } from "carbon-components-svelte";
|
|
335
|
-
import { onMount } from "svelte";
|
|
336
|
-
</script>
|
|
337
|
-
|
|
338
|
-
+ <!-- toc -->
|
|
339
|
-
|
|
340
|
-
<h1>Title</h1>
|
|
341
|
-
|
|
342
|
-
+ <p>Summary</p>
|
|
343
|
-
```
|
|
225
|
+
#### Webpack
|
|
344
226
|
|
|
345
|
-
|
|
227
|
+
Webpack users should use the `OptimizeCssPlugin`. The plugin API is identical to the Vite plugin.
|
|
346
228
|
|
|
347
|
-
|
|
229
|
+
This code is abridged; see [examples/webpack](examples/webpack) for a full set-up.
|
|
348
230
|
|
|
349
231
|
```js
|
|
350
|
-
//
|
|
351
|
-
|
|
232
|
+
// webpack.config.js
|
|
233
|
+
const { OptimizeCssPlugin } = require("carbon-preprocess-svelte");
|
|
352
234
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
},
|
|
360
|
-
],
|
|
361
|
-
markup: [
|
|
362
|
-
{ content: "<!-- toc -->" },
|
|
363
|
-
{
|
|
364
|
-
content: "<p>Summary</p>",
|
|
365
|
-
behavior: "append",
|
|
366
|
-
},
|
|
367
|
-
],
|
|
368
|
-
}),
|
|
235
|
+
const PROD = process.env.NODE_ENV === "production";
|
|
236
|
+
|
|
237
|
+
module.exports = {
|
|
238
|
+
plugins: [
|
|
239
|
+
// Only apply the plugin when building for production.
|
|
240
|
+
PROD && new OptimizeCssPlugin(),
|
|
369
241
|
],
|
|
370
242
|
};
|
|
371
243
|
```
|
|
372
244
|
|
|
373
|
-
#### API
|
|
245
|
+
#### `optimizeCss` API
|
|
374
246
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
```js
|
|
378
|
-
interface PreprocessIncludeOptions {
|
|
247
|
+
```ts
|
|
248
|
+
optimizeCss({
|
|
379
249
|
/**
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
250
|
+
* By default, the plugin will print the size
|
|
251
|
+
* difference between the original and optimized CSS.
|
|
252
|
+
*
|
|
253
|
+
* Set to `false` to disable verbose logging.
|
|
254
|
+
* @default true
|
|
383
255
|
*/
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
script: Array<{
|
|
387
|
-
/**
|
|
388
|
-
* Specify the content the prepend or append
|
|
389
|
-
* @example
|
|
390
|
-
* import { CodeSnippet } from "carbon-components-svelte";
|
|
391
|
-
*/
|
|
392
|
-
content: string,
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* Specify the filename pattern to process
|
|
396
|
-
* Defaults to files ending with ".svelte"
|
|
397
|
-
* @default /\.(svelte)$/
|
|
398
|
-
*/
|
|
399
|
-
test?: RegExp,
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Specify whether the content should be prepended or appended
|
|
403
|
-
* @default "prepend"
|
|
404
|
-
*/
|
|
405
|
-
behavior?: "prepend" | "append",
|
|
406
|
-
}>;
|
|
407
|
-
|
|
408
|
-
markup: Array<{
|
|
409
|
-
/**
|
|
410
|
-
* Specify the content the prepend or append
|
|
411
|
-
* @example
|
|
412
|
-
* <ul>Table of Contents</ul>
|
|
413
|
-
*/
|
|
414
|
-
content: string,
|
|
415
|
-
|
|
416
|
-
/**
|
|
417
|
-
* Specify the filename pattern to process
|
|
418
|
-
* Defaults to files ending with ".svelte"
|
|
419
|
-
* @default /\.(svelte)$/
|
|
420
|
-
*/
|
|
421
|
-
test?: RegExp,
|
|
422
|
-
|
|
423
|
-
/**
|
|
424
|
-
* Specify whether the content should be prepended or appended
|
|
425
|
-
* @default "prepend"
|
|
426
|
-
*/
|
|
427
|
-
behavior?: "prepend" | "append",
|
|
428
|
-
}>;
|
|
429
|
-
}
|
|
430
|
-
```
|
|
431
|
-
|
|
432
|
-
## Presets
|
|
433
|
-
|
|
434
|
-
```js
|
|
435
|
-
// svelte.config.js
|
|
436
|
-
import { presetCarbon } from "carbon-preprocess-svelte";
|
|
256
|
+
verbose: false,
|
|
437
257
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
258
|
+
/**
|
|
259
|
+
* By default, pre-compiled Carbon StyleSheets ship `@font-face` rules
|
|
260
|
+
* for all available IBM Plex fonts, many of which are not actually
|
|
261
|
+
* used in Carbon Svelte components.
|
|
262
|
+
*
|
|
263
|
+
* The default behavior is to preserve the following IBM Plex fonts:
|
|
264
|
+
* - IBM Plex Sans (300/400/600-weight and normal-font-style rules)
|
|
265
|
+
* - IBM Plex Mono (400-weight and normal-font-style rules)
|
|
266
|
+
*
|
|
267
|
+
* Set to `true` to disable this behavior and
|
|
268
|
+
* retain *all* IBM Plex `@font-face` rules.
|
|
269
|
+
* @default false
|
|
270
|
+
*/
|
|
271
|
+
preserveAllIBMFonts: false,
|
|
272
|
+
});
|
|
444
273
|
```
|
|
445
274
|
|
|
446
|
-
##
|
|
275
|
+
## Examples
|
|
447
276
|
|
|
448
|
-
|
|
449
|
-
// svelte.config.js
|
|
450
|
-
import adapter from "@sveltejs/adapter-static";
|
|
451
|
-
import { optimizeImports, optimizeCss } from "carbon-preprocess-svelte";
|
|
452
|
-
|
|
453
|
-
/** @type {import('@sveltejs/kit').Config} */
|
|
454
|
-
export default {
|
|
455
|
-
preprocess: [optimizeImports()],
|
|
456
|
-
kit: {
|
|
457
|
-
adapter: adapter(),
|
|
458
|
-
vite: {
|
|
459
|
-
plugins: [process.env.NODE_ENV === "production" && optimizeCss()],
|
|
460
|
-
},
|
|
461
|
-
},
|
|
462
|
-
};
|
|
463
|
-
```
|
|
277
|
+
Refer to [examples](examples) for common set-ups.
|
|
464
278
|
|
|
465
279
|
## Contributing
|
|
466
280
|
|