@homebound/truss 2.4.0 → 2.5.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/build/plugin/index.d.ts +47 -1
- package/build/plugin/index.js +58 -11
- package/build/plugin/index.js.map +1 -1
- package/package.json +1 -1
package/build/plugin/index.d.ts
CHANGED
|
@@ -34,6 +34,52 @@ type TrussMappingEntry =
|
|
|
34
34
|
chain: string[];
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
+
interface TrussEsbuildPluginOptions {
|
|
38
|
+
/** Path to the Css.json mapping file (relative to cwd or absolute). */
|
|
39
|
+
mapping: string;
|
|
40
|
+
/** Output path for the generated truss.css (relative to outDir or absolute). Defaults to `truss.css`. */
|
|
41
|
+
outputCss?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* esbuild plugin that transforms `Css.*.$` expressions and emits a `truss.css` file.
|
|
45
|
+
*
|
|
46
|
+
* Designed for library builds using tsup/esbuild. Transforms source files
|
|
47
|
+
* during the build and writes an annotated `truss.css` alongside the output
|
|
48
|
+
* that consuming applications can merge via the Vite plugin's `libraries` option.
|
|
49
|
+
*
|
|
50
|
+
* Usage with tsup:
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { trussEsbuildPlugin } from "@homebound/truss/plugin";
|
|
53
|
+
*
|
|
54
|
+
* export default defineConfig({
|
|
55
|
+
* esbuildPlugins: [trussEsbuildPlugin({ mapping: "./src/Css.json" })],
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
declare function trussEsbuildPlugin(opts: TrussEsbuildPluginOptions): {
|
|
60
|
+
name: string;
|
|
61
|
+
setup(build: EsbuildPluginBuild): void;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Minimal esbuild plugin types so we don't need esbuild as a dependency.
|
|
65
|
+
*
|
|
66
|
+
* These match the subset of the esbuild Plugin API that we use.
|
|
67
|
+
*/
|
|
68
|
+
interface EsbuildPluginBuild {
|
|
69
|
+
initialOptions: {
|
|
70
|
+
outdir?: string;
|
|
71
|
+
};
|
|
72
|
+
onLoad(options: {
|
|
73
|
+
filter: RegExp;
|
|
74
|
+
}, callback: (args: {
|
|
75
|
+
path: string;
|
|
76
|
+
}) => {
|
|
77
|
+
contents: string;
|
|
78
|
+
loader: string;
|
|
79
|
+
} | undefined): void;
|
|
80
|
+
onEnd(callback: () => void): void;
|
|
81
|
+
}
|
|
82
|
+
|
|
37
83
|
interface TrussPluginOptions {
|
|
38
84
|
/** Path to the Css.json mapping file used for transforming files (relative to project root or absolute). */
|
|
39
85
|
mapping: string;
|
|
@@ -72,4 +118,4 @@ declare function trussPlugin(opts: TrussPluginOptions): TrussVitePlugin;
|
|
|
72
118
|
/** Load a truss mapping file synchronously (for tests). */
|
|
73
119
|
declare function loadMapping(path: string): TrussMapping;
|
|
74
120
|
|
|
75
|
-
export { type TrussMapping, type TrussMappingEntry, type TrussPluginOptions, type TrussVitePlugin, loadMapping, trussPlugin };
|
|
121
|
+
export { type TrussEsbuildPluginOptions, type TrussMapping, type TrussMappingEntry, type TrussPluginOptions, type TrussVitePlugin, loadMapping, trussEsbuildPlugin, trussPlugin };
|
package/build/plugin/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/plugin/index.ts
|
|
2
|
-
import { readFileSync as
|
|
3
|
-
import { resolve, dirname, isAbsolute, join } from "path";
|
|
2
|
+
import { readFileSync as readFileSync3, writeFileSync as writeFileSync2, existsSync } from "fs";
|
|
3
|
+
import { resolve as resolve2, dirname, isAbsolute, join as join2 } from "path";
|
|
4
4
|
|
|
5
5
|
// src/plugin/emit-truss.ts
|
|
6
6
|
import * as t from "@babel/types";
|
|
@@ -2877,6 +2877,52 @@ function mergeTrussCss(sources) {
|
|
|
2877
2877
|
return lines.join("\n");
|
|
2878
2878
|
}
|
|
2879
2879
|
|
|
2880
|
+
// src/plugin/esbuild-plugin.ts
|
|
2881
|
+
import { readFileSync as readFileSync2, writeFileSync, mkdirSync } from "fs";
|
|
2882
|
+
import { resolve, join } from "path";
|
|
2883
|
+
function trussEsbuildPlugin(opts) {
|
|
2884
|
+
const cssRegistry = /* @__PURE__ */ new Map();
|
|
2885
|
+
let mapping = null;
|
|
2886
|
+
let outDir;
|
|
2887
|
+
return {
|
|
2888
|
+
name: "truss",
|
|
2889
|
+
setup(build) {
|
|
2890
|
+
outDir = build.initialOptions.outdir ?? build.initialOptions.outdir;
|
|
2891
|
+
build.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, function(args) {
|
|
2892
|
+
const code = readFileSync2(args.path, "utf8");
|
|
2893
|
+
if (!code.includes("Css")) return void 0;
|
|
2894
|
+
if (!mapping) {
|
|
2895
|
+
mapping = loadMapping(resolve(process.cwd(), opts.mapping));
|
|
2896
|
+
}
|
|
2897
|
+
const result = transformTruss(code, args.path, mapping);
|
|
2898
|
+
if (!result) return void 0;
|
|
2899
|
+
if (result.rules) {
|
|
2900
|
+
for (const [className, rule] of result.rules) {
|
|
2901
|
+
if (!cssRegistry.has(className)) {
|
|
2902
|
+
cssRegistry.set(className, rule);
|
|
2903
|
+
}
|
|
2904
|
+
}
|
|
2905
|
+
}
|
|
2906
|
+
return { contents: result.code, loader: loaderForPath(args.path) };
|
|
2907
|
+
});
|
|
2908
|
+
build.onEnd(function() {
|
|
2909
|
+
if (cssRegistry.size === 0) return;
|
|
2910
|
+
const css = generateCssText(cssRegistry);
|
|
2911
|
+
const cssFileName = opts.outputCss ?? "truss.css";
|
|
2912
|
+
const cssPath = resolve(outDir ?? join(process.cwd(), "dist"), cssFileName);
|
|
2913
|
+
mkdirSync(resolve(cssPath, ".."), { recursive: true });
|
|
2914
|
+
writeFileSync(cssPath, css, "utf8");
|
|
2915
|
+
});
|
|
2916
|
+
}
|
|
2917
|
+
};
|
|
2918
|
+
}
|
|
2919
|
+
function loaderForPath(filePath) {
|
|
2920
|
+
if (filePath.endsWith(".tsx")) return "tsx";
|
|
2921
|
+
if (filePath.endsWith(".ts")) return "ts";
|
|
2922
|
+
if (filePath.endsWith(".jsx")) return "jsx";
|
|
2923
|
+
return "js";
|
|
2924
|
+
}
|
|
2925
|
+
|
|
2880
2926
|
// src/plugin/index.ts
|
|
2881
2927
|
var VIRTUAL_CSS_PREFIX = "\0truss-css:";
|
|
2882
2928
|
var CSS_TS_QUERY = "?truss-css";
|
|
@@ -2894,7 +2940,7 @@ function trussPlugin(opts) {
|
|
|
2894
2940
|
let cssVersion = 0;
|
|
2895
2941
|
let lastSentVersion = 0;
|
|
2896
2942
|
function mappingPath() {
|
|
2897
|
-
return
|
|
2943
|
+
return resolve2(projectRoot || process.cwd(), opts.mapping);
|
|
2898
2944
|
}
|
|
2899
2945
|
function ensureMapping() {
|
|
2900
2946
|
if (!mapping) {
|
|
@@ -2906,7 +2952,7 @@ function trussPlugin(opts) {
|
|
|
2906
2952
|
function loadLibraries() {
|
|
2907
2953
|
if (!libraryCache) {
|
|
2908
2954
|
libraryCache = libraryPaths.map(function(libPath) {
|
|
2909
|
-
const resolved =
|
|
2955
|
+
const resolved = resolve2(projectRoot || process.cwd(), libPath);
|
|
2910
2956
|
return readTrussCss(resolved);
|
|
2911
2957
|
});
|
|
2912
2958
|
}
|
|
@@ -3008,7 +3054,7 @@ function trussPlugin(opts) {
|
|
|
3008
3054
|
}
|
|
3009
3055
|
if (!id.startsWith(VIRTUAL_CSS_PREFIX)) return null;
|
|
3010
3056
|
const sourcePath = id.slice(VIRTUAL_CSS_PREFIX.length) + ".ts";
|
|
3011
|
-
const sourceCode =
|
|
3057
|
+
const sourceCode = readFileSync3(sourcePath, "utf8");
|
|
3012
3058
|
return transformCssTs(sourceCode, sourcePath, ensureMapping());
|
|
3013
3059
|
},
|
|
3014
3060
|
transform(code, id) {
|
|
@@ -3072,15 +3118,15 @@ function trussPlugin(opts) {
|
|
|
3072
3118
|
if (!isBuild) return;
|
|
3073
3119
|
const css = collectCss();
|
|
3074
3120
|
if (!css) return;
|
|
3075
|
-
const outDir = options.dir ||
|
|
3076
|
-
const trussPath =
|
|
3121
|
+
const outDir = options.dir || join2(projectRoot, "dist");
|
|
3122
|
+
const trussPath = join2(outDir, "truss.css");
|
|
3077
3123
|
if (!existsSync(trussPath)) {
|
|
3078
3124
|
const alreadyEmitted = Object.keys(bundle).some(function(key) {
|
|
3079
3125
|
const asset = bundle[key];
|
|
3080
3126
|
return asset.type === "asset" && key.endsWith(".css") && typeof asset.source === "string" && asset.source.includes(css);
|
|
3081
3127
|
});
|
|
3082
3128
|
if (!alreadyEmitted) {
|
|
3083
|
-
|
|
3129
|
+
writeFileSync2(trussPath, css, "utf8");
|
|
3084
3130
|
}
|
|
3085
3131
|
}
|
|
3086
3132
|
}
|
|
@@ -3091,9 +3137,9 @@ function resolveImportPath(source, importer, projectRoot) {
|
|
|
3091
3137
|
return source;
|
|
3092
3138
|
}
|
|
3093
3139
|
if (importer) {
|
|
3094
|
-
return
|
|
3140
|
+
return resolve2(dirname(importer), source);
|
|
3095
3141
|
}
|
|
3096
|
-
return
|
|
3142
|
+
return resolve2(projectRoot || process.cwd(), source);
|
|
3097
3143
|
}
|
|
3098
3144
|
function stripQueryAndHash(id) {
|
|
3099
3145
|
const queryIndex = id.indexOf("?");
|
|
@@ -3111,11 +3157,12 @@ function isNodeModulesFile(filePath) {
|
|
|
3111
3157
|
return filePath.replace(/\\/g, "/").includes("/node_modules/");
|
|
3112
3158
|
}
|
|
3113
3159
|
function loadMapping(path) {
|
|
3114
|
-
const raw =
|
|
3160
|
+
const raw = readFileSync3(path, "utf8");
|
|
3115
3161
|
return JSON.parse(raw);
|
|
3116
3162
|
}
|
|
3117
3163
|
export {
|
|
3118
3164
|
loadMapping,
|
|
3165
|
+
trussEsbuildPlugin,
|
|
3119
3166
|
trussPlugin
|
|
3120
3167
|
};
|
|
3121
3168
|
//# sourceMappingURL=index.js.map
|