carbon-preprocess-svelte 0.11.2 → 0.11.4

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/README.md CHANGED
@@ -49,7 +49,7 @@ The preprocessor optimizes imports from the following packages:
49
49
 
50
50
  > [!NOTE]
51
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.
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) by default, which greatly improves development times.
53
53
  > However, this preprocessor is still useful for non-Vite bundlers, like Rollup and Webpack.
54
54
 
55
55
  #### SvelteKit
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const utils_1 = require("../utils");
4
- const print_diff_1 = require("./print-diff");
5
4
  const create_optimized_css_1 = require("./create-optimized-css");
5
+ const print_diff_1 = require("./print-diff");
6
6
  // Webpack plugin to optimize CSS for Carbon Svelte components.
7
7
  class OptimizeCssPlugin {
8
8
  constructor(options) {
@@ -35,7 +35,7 @@ class OptimizeCssPlugin {
35
35
  for (const [id] of Object.entries(assets)) {
36
36
  if ((0, utils_1.isCssFile)(id)) {
37
37
  const original_css = assets[id].source();
38
- const optimized_css = (0, create_optimized_css_1.createOptimizedCss)(original_css, ids, this.options);
38
+ const optimized_css = (0, create_optimized_css_1.createOptimizedCss)(Object.assign(Object.assign({}, this.options), { source: original_css, ids }));
39
39
  compilation.updateAsset(id, new RawSource(optimized_css));
40
40
  if (this.options.verbose) {
41
41
  (0, print_diff_1.printDiff)({ original_css, optimized_css, id });
@@ -22,4 +22,9 @@ export type OptimizeCssOptions = {
22
22
  */
23
23
  preserveAllIBMFonts?: boolean;
24
24
  };
25
- export declare function createOptimizedCss(original_css: Uint8Array | string, ids: string[], options?: OptimizeCssOptions): string;
25
+ type CreateOptimizedCssOptions = OptimizeCssOptions & {
26
+ source: Uint8Array | string;
27
+ ids: string[];
28
+ };
29
+ export declare function createOptimizedCss(options: CreateOptimizedCssOptions): string;
30
+ export {};
@@ -3,13 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createOptimizedCss = void 0;
6
+ exports.createOptimizedCss = createOptimizedCss;
7
7
  const node_path_1 = __importDefault(require("node:path"));
8
8
  const postcss_1 = __importDefault(require("postcss"));
9
9
  const postcss_discard_empty_1 = __importDefault(require("postcss-discard-empty"));
10
10
  const component_index_1 = require("../component-index");
11
11
  const constants_1 = require("../constants");
12
- function createOptimizedCss(original_css, ids, options) {
12
+ function createOptimizedCss(options) {
13
+ const { source, ids } = options;
13
14
  const preserveAllIBMFonts = (options === null || options === void 0 ? void 0 : options.preserveAllIBMFonts) === true;
14
15
  // List of Carbon classes that must be preserved in the CSS
15
16
  // but that are not referenced in Carbon Svelte components.
@@ -62,16 +63,16 @@ function createOptimizedCss(original_css, ids, options) {
62
63
  node.walkDecls((decl) => {
63
64
  switch (decl.prop) {
64
65
  case "font-family":
65
- attributes["font-family"] = decl.value;
66
- break;
67
66
  case "font-style":
68
- attributes["font-style"] = decl.value;
69
- break;
70
67
  case "font-weight":
71
- attributes["font-weight"] = decl.value;
68
+ attributes[decl.prop] = decl.value;
72
69
  break;
73
70
  }
74
71
  });
72
+ // Do not proceed if font is not IBM Plex.
73
+ if (!attributes["font-family"].startsWith("IBM Plex")) {
74
+ return;
75
+ }
75
76
  const is_mono = attributes["font-style"] === "normal" &&
76
77
  attributes["font-family"] === "IBM Plex Mono" &&
77
78
  attributes["font-weight"] === "400";
@@ -85,6 +86,5 @@ function createOptimizedCss(original_css, ids, options) {
85
86
  },
86
87
  },
87
88
  (0, postcss_discard_empty_1.default)(),
88
- ]).process(original_css).css;
89
+ ]).process(source).css;
89
90
  }
90
- exports.createOptimizedCss = createOptimizedCss;
@@ -11,8 +11,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.optimizeCss = void 0;
13
13
  const utils_1 = require("../utils");
14
- const print_diff_1 = require("./print-diff");
15
14
  const create_optimized_css_1 = require("./create-optimized-css");
15
+ const print_diff_1 = require("./print-diff");
16
16
  // Vite plugin (Rollup-compatible) to optimize CSS for Carbon Svelte components.
17
17
  const optimizeCss = (options) => {
18
18
  const verbose = (options === null || options === void 0 ? void 0 : options.verbose) !== false;
@@ -35,7 +35,7 @@ const optimizeCss = (options) => {
35
35
  const file = bundle[id];
36
36
  if (file.type === "asset" && (0, utils_1.isCssFile)(id)) {
37
37
  const original_css = file.source;
38
- const optimized_css = (0, create_optimized_css_1.createOptimizedCss)(original_css, ids, options);
38
+ const optimized_css = (0, create_optimized_css_1.createOptimizedCss)(Object.assign(Object.assign({}, options), { source: original_css, ids }));
39
39
  file.source = optimized_css;
40
40
  if (verbose) {
41
41
  (0, print_diff_1.printDiff)({ original_css, optimized_css, id });
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  export declare function printDiff(props: {
3
2
  original_css: Uint8Array | Buffer | string;
4
3
  optimized_css: string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.printDiff = void 0;
3
+ exports.printDiff = printDiff;
4
4
  const constants_1 = require("../constants");
5
5
  const formatter = new Intl.NumberFormat("en-US", { maximumFractionDigits: 2 });
6
6
  function toHumanReadableSize(size_in_kb) {
@@ -36,4 +36,3 @@ function printDiff(props) {
36
36
  console.log("Before:", original_display);
37
37
  console.log("After: ", optimized_display, `(-${diff})\n`);
38
38
  }
39
- exports.printDiff = printDiff;
@@ -1,3 +1,2 @@
1
- /// <reference types="svelte" />
2
1
  import type { SveltePreprocessor } from "svelte/types/compiler/preprocess";
3
2
  export declare const optimizeImports: SveltePreprocessor<"script">;
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.optimizeImports = void 0;
7
+ const estree_walker_1 = require("estree-walker");
7
8
  const magic_string_1 = __importDefault(require("magic-string"));
8
9
  const compiler_1 = require("svelte/compiler");
9
10
  const component_index_1 = require("../component-index");
@@ -29,7 +30,7 @@ const optimizeImports = () => {
29
30
  // Wrap the content in a `<script>` tag to parse it with the Svelte parser.
30
31
  const content = `<script>${raw}</script>`;
31
32
  const s = new magic_string_1.default(content);
32
- (0, compiler_1.walk)((0, compiler_1.parse)(content), {
33
+ (0, estree_walker_1.walk)((0, compiler_1.parse)(content), {
33
34
  enter(node) {
34
35
  if (node.type === "ImportDeclaration") {
35
36
  const import_name = node.source.value;
package/dist/utils.js CHANGED
@@ -1,16 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isCarbonSvelteImport = exports.isCssFile = exports.isSvelteFile = void 0;
3
+ exports.isSvelteFile = isSvelteFile;
4
+ exports.isCssFile = isCssFile;
5
+ exports.isCarbonSvelteImport = isCarbonSvelteImport;
4
6
  const constants_1 = require("./constants");
5
7
  function isSvelteFile(id) {
6
8
  return constants_1.RE_EXT_SVELTE.test(id);
7
9
  }
8
- exports.isSvelteFile = isSvelteFile;
9
10
  function isCssFile(id) {
10
11
  return constants_1.RE_EXT_CSS.test(id);
11
12
  }
12
- exports.isCssFile = isCssFile;
13
13
  function isCarbonSvelteImport(id) {
14
14
  return isSvelteFile(id) && id.includes("carbon-components-svelte" /* CarbonSvelte.Components */);
15
15
  }
16
- exports.isCarbonSvelteImport = isCarbonSvelteImport;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carbon-preprocess-svelte",
3
- "version": "0.11.2",
3
+ "version": "0.11.4",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Svelte preprocessors for the Carbon Design System",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
@@ -8,17 +8,19 @@
8
8
  "types": "./dist/index.d.ts",
9
9
  "scripts": {
10
10
  "index:components": "bun scripts/index-components.ts",
11
- "prepack": "tsc -p tsconfig.build.json",
12
- "format": "bunx --bun prettier --write ."
11
+ "build": "bun --bun tsc -p tsconfig.build.json",
12
+ "test:e2e": "bun tests/test-e2e.ts",
13
+ "format": "bun --bun prettier --write . --cache"
13
14
  },
14
15
  "dependencies": {
15
- "magic-string": "^0.30.8",
16
- "postcss": "^8.4.36",
16
+ "estree-walker": "^2.0.2",
17
+ "magic-string": "^0.30.10",
18
+ "postcss": "^8.4.40",
17
19
  "postcss-discard-empty": "^6.0.3"
18
20
  },
19
21
  "devDependencies": {
20
22
  "@types/bun": "latest",
21
- "@types/jest": "^29.5.12",
23
+ "@types/jest": "latest",
22
24
  "carbon-components-svelte": "0.85.0",
23
25
  "prettier": "latest",
24
26
  "svelte": "latest",