@sweetener/unplugin 0.1.0-alpha.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.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +24 -0
  3. package/dist/src/bun.d.ts +5 -0
  4. package/dist/src/bun.d.ts.map +1 -0
  5. package/dist/src/bun.js +37 -0
  6. package/dist/src/bun.js.map +1 -0
  7. package/dist/src/esbuild.d.ts +2 -0
  8. package/dist/src/esbuild.d.ts.map +1 -0
  9. package/dist/src/esbuild.js +2 -0
  10. package/dist/src/esbuild.js.map +1 -0
  11. package/dist/src/farm.d.ts +4 -0
  12. package/dist/src/farm.d.ts.map +1 -0
  13. package/dist/src/farm.js +47 -0
  14. package/dist/src/farm.js.map +1 -0
  15. package/dist/src/index.d.ts +4 -0
  16. package/dist/src/index.d.ts.map +1 -0
  17. package/dist/src/index.js +3 -0
  18. package/dist/src/index.js.map +1 -0
  19. package/dist/src/plugin.d.ts +14 -0
  20. package/dist/src/plugin.d.ts.map +1 -0
  21. package/dist/src/plugin.js +105 -0
  22. package/dist/src/plugin.js.map +1 -0
  23. package/dist/src/rolldown.d.ts +2 -0
  24. package/dist/src/rolldown.d.ts.map +1 -0
  25. package/dist/src/rolldown.js +2 -0
  26. package/dist/src/rolldown.js.map +1 -0
  27. package/dist/src/rollup.d.ts +2 -0
  28. package/dist/src/rollup.d.ts.map +1 -0
  29. package/dist/src/rollup.js +2 -0
  30. package/dist/src/rollup.js.map +1 -0
  31. package/dist/src/rsbuild.d.ts +2 -0
  32. package/dist/src/rsbuild.d.ts.map +1 -0
  33. package/dist/src/rsbuild.js +2 -0
  34. package/dist/src/rsbuild.js.map +1 -0
  35. package/dist/src/rspack.d.ts +2 -0
  36. package/dist/src/rspack.d.ts.map +1 -0
  37. package/dist/src/rspack.js +2 -0
  38. package/dist/src/rspack.js.map +1 -0
  39. package/dist/src/vite.d.ts +5 -0
  40. package/dist/src/vite.d.ts.map +1 -0
  41. package/dist/src/vite.js +32 -0
  42. package/dist/src/vite.js.map +1 -0
  43. package/dist/src/webpack.d.ts +2 -0
  44. package/dist/src/webpack.d.ts.map +1 -0
  45. package/dist/src/webpack.js +2 -0
  46. package/dist/src/webpack.js.map +1 -0
  47. package/package.json +98 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jimmy Miller
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,24 @@
1
+ # @sweetener/unplugin
2
+
3
+ Sweetener for Vite, Rollup, Rolldown, webpack, Rspack, Rsbuild, esbuild, Farm, and Bun.
4
+
5
+ Part of [Sweetener](https://github.com/sweetener-ts/sweetener), hygienic
6
+ declarative macros for TypeScript. Alpha: the language version is 1 and the
7
+ package interfaces may still change.
8
+
9
+ ## Usage
10
+
11
+ ```ts
12
+ // vite.config.ts
13
+ import { resolve } from "node:path";
14
+ import sweetener from "@sweetener/unplugin/vite";
15
+
16
+ export default defineConfig({
17
+ plugins: [
18
+ ...sweetener({ configFile: resolve(import.meta.dirname, "sweetener.json") }),
19
+ ],
20
+ });
21
+ ```
22
+
23
+ Swap the entry point for the host you use: `/rollup`, `/rolldown`,
24
+ `/webpack`, `/rspack`, `/rsbuild`, `/esbuild`, `/farm`, `/bun`.
@@ -0,0 +1,5 @@
1
+ import type { BunPlugin } from "bun";
2
+ import type { SweetenerPluginOptions } from "./plugin.js";
3
+ /** A synchronous-setup plugin that works with both Bun.build and Bun.plugin. */
4
+ export default function sweetener(options?: SweetenerPluginOptions): BunPlugin;
5
+ //# sourceMappingURL=bun.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bun.d.ts","sourceRoot":"","sources":["../../src/bun.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAU,MAAM,KAAK,CAAC;AAE7C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAW1D,gFAAgF;AAChF,MAAM,CAAC,OAAO,UAAU,SAAS,CAC/B,OAAO,GAAE,sBAA2B,GACnC,SAAS,CA2BX"}
@@ -0,0 +1,37 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { createSweetenerSession } from "@sweetener/compiler";
3
+ const sweetExtension = /\.s(?:ts|js)x?$/u;
4
+ function loader(filename) {
5
+ if (filename.endsWith(".stsx"))
6
+ return "tsx";
7
+ if (filename.endsWith(".sts"))
8
+ return "ts";
9
+ if (filename.endsWith(".sjsx"))
10
+ return "jsx";
11
+ return "js";
12
+ }
13
+ /** A synchronous-setup plugin that works with both Bun.build and Bun.plugin. */
14
+ export default function sweetener(options = {}) {
15
+ const session = createSweetenerSession();
16
+ const include = options.include ?? sweetExtension;
17
+ return {
18
+ name: "sweetener",
19
+ setup(build) {
20
+ build.onLoad({ filter: include }, async ({ path }) => {
21
+ const code = await readFile(path, "utf8");
22
+ const result = await session.transform({
23
+ code,
24
+ filename: path,
25
+ configFile: options.configFile,
26
+ mode: "development",
27
+ });
28
+ if (result.diagnostics.length > 0)
29
+ throw new Error(result.diagnostics
30
+ .map(({ code: diagnosticCode, messageText }) => `SWR${String(diagnosticCode)} ${String(messageText)}`)
31
+ .join("\n"));
32
+ return { contents: result.code, loader: loader(path) };
33
+ });
34
+ },
35
+ };
36
+ }
37
+ //# sourceMappingURL=bun.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bun.js","sourceRoot":"","sources":["../../src/bun.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAG7D,MAAM,cAAc,GAAG,kBAAkB,CAAC;AAE1C,SAAS,MAAM,CAAC,QAAgB;IAC9B,IAAI,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC;QAAE,OAAO,KAAK,CAAC;IAC7C,IAAI,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAC;IAC3C,IAAI,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC;QAAE,OAAO,KAAK,CAAC;IAC7C,OAAO,IAAI,CAAC;AACd,CAAC;AAED,gFAAgF;AAChF,MAAM,CAAC,OAAO,UAAU,SAAS,CAC/B,UAAkC,EAAE;IAEpC,MAAM,OAAO,GAAG,sBAAsB,EAAE,CAAC;IACzC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,cAAc,CAAC;IAClD,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,KAAK,CAAC,KAAK;YACT,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE;gBACnD,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;gBAC1C,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,SAAS,CAAC;oBACrC,IAAI;oBACJ,QAAQ,EAAE,IAAI;oBACd,UAAU,EAAE,OAAO,CAAC,UAAU;oBAC9B,IAAI,EAAE,aAAa;iBACpB,CAAC,CAAC;gBACH,IAAI,MAAM,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC;oBAC/B,MAAM,IAAI,KAAK,CACb,MAAM,CAAC,WAAW;yBACf,GAAG,CACF,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,WAAW,EAAE,EAAE,EAAE,CACxC,MAAM,MAAM,CAAC,cAAc,CAAC,IAAI,MAAM,CAAC,WAAW,CAAC,EAAE,CACxD;yBACA,IAAI,CAAC,IAAI,CAAC,CACd,CAAC;gBACJ,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;YACzD,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { esbuild as default } from "./plugin.js";
2
+ //# sourceMappingURL=esbuild.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"esbuild.d.ts","sourceRoot":"","sources":["../../src/esbuild.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { esbuild as default } from "./plugin.js";
2
+ //# sourceMappingURL=esbuild.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"esbuild.js","sourceRoot":"","sources":["../../src/esbuild.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { JsPlugin } from "@farmfe/core";
2
+ import type { SweetenerPluginOptions } from "./plugin.js";
3
+ export default function sweetenerFarm(options?: SweetenerPluginOptions): JsPlugin;
4
+ //# sourceMappingURL=farm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"farm.d.ts","sourceRoot":"","sources":["../../src/farm.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEV,QAAQ,EAET,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAI1D,MAAM,CAAC,OAAO,UAAU,aAAa,CACnC,OAAO,GAAE,sBAA2B,GACnC,QAAQ,CAoDV"}
@@ -0,0 +1,47 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { createSweetenerSession } from "@sweetener/compiler";
3
+ const sweetExtension = /\.s(?:ts|js)x?$/u;
4
+ export default function sweetenerFarm(options = {}) {
5
+ const session = createSweetenerSession();
6
+ const include = options.include ?? sweetExtension;
7
+ const transform = async (code, filename, context) => {
8
+ if (!include.test(filename))
9
+ return undefined;
10
+ const result = await session.transform({
11
+ code,
12
+ filename,
13
+ configFile: options.configFile,
14
+ mode: "production",
15
+ });
16
+ if (result.diagnostics.length > 0)
17
+ context?.error(result.diagnostics
18
+ .map(({ messageText }) => String(messageText))
19
+ .join("\n"));
20
+ for (const dependency of result.dependencies)
21
+ context?.addWatchFile(filename, dependency);
22
+ return {
23
+ content: result.code,
24
+ moduleType: result.virtualFilename.endsWith("x") ? "tsx" : "ts",
25
+ sourceMap: JSON.stringify(result.map),
26
+ };
27
+ };
28
+ return {
29
+ name: "sweetener",
30
+ priority: 1_000,
31
+ load: {
32
+ filters: { resolvedPaths: [String.raw `\.s(?:ts|js)x?$`] },
33
+ async executor({ resolvedPath }, context) {
34
+ return transform(await readFile(resolvedPath, "utf8"), resolvedPath, context);
35
+ },
36
+ },
37
+ transform: {
38
+ filters: { moduleTypes: ["ts", "tsx", "js", "jsx"] },
39
+ executor({ content, resolvedPath }, context) {
40
+ if (sweetExtension.test(resolvedPath))
41
+ return undefined;
42
+ return transform(content, resolvedPath, context);
43
+ },
44
+ },
45
+ };
46
+ }
47
+ //# sourceMappingURL=farm.js.map
@@ -0,0 +1 @@
1
+ {"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,CAAC,MAAM,CAAC,GAAG,CAAC;SACtC,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,QAAQ,EAAE,KAAK;QACf,IAAI,EAAE;YACJ,OAAO,EAAE,EAAE,aAAa,EAAE,CAAC,MAAM,CAAC,GAAG,CAAA,iBAAiB,CAAC,EAAE;YACzD,KAAK,CAAC,QAAQ,CAAC,EAAE,YAAY,EAAE,EAAE,OAAO;gBACtC,OAAO,SAAS,CACd,MAAM,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC,EACpC,YAAY,EACZ,OAAO,CACR,CAAC;YACJ,CAAC;SACF;QACD,SAAS,EAAE;YACT,OAAO,EAAE,EAAE,WAAW,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC,EAAE;YACpD,QAAQ,CAAC,EAAE,OAAO,EAAE,YAAY,EAAE,EAAE,OAAO;gBACzC,IAAI,cAAc,CAAC,IAAI,CAAC,YAAY,CAAC;oBAAE,OAAO,SAAS,CAAC;gBACxD,OAAO,SAAS,CAAC,OAAO,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC;YACnD,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,4 @@
1
+ export declare const packageName: "@sweetener/unplugin";
2
+ export type { SweetenerPluginOptions } from "./plugin.js";
3
+ export { sweetenerUnplugin as default, sweetenerUnplugin } from "./plugin.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,EAAG,qBAA8B,CAAC;AAC1D,YAAY,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,3 @@
1
+ export const packageName = "@sweetener/unplugin";
2
+ export { sweetenerUnplugin as default, sweetenerUnplugin } from "./plugin.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,qBAA8B,CAAC;AAE1D,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,14 @@
1
+ export interface SweetenerPluginOptions {
2
+ readonly configFile?: string | undefined;
3
+ readonly include?: RegExp | undefined;
4
+ }
5
+ export declare const sweetenerUnplugin: import("unplugin").UnpluginInstance<SweetenerPluginOptions | undefined, boolean>;
6
+ export declare const vite: (options?: SweetenerPluginOptions | undefined) => import("vite").Plugin<any> | import("vite").Plugin<any>[];
7
+ export declare const rollup: (options?: SweetenerPluginOptions | undefined) => import("rollup").Plugin<any> | import("rollup").Plugin<any>[];
8
+ export declare const rolldown: (options?: SweetenerPluginOptions | undefined) => import("unplugin").RolldownPlugin<any> | import("unplugin").RolldownPlugin<any>[];
9
+ export declare const webpack: (options?: SweetenerPluginOptions | undefined) => import("webpack").WebpackPluginInstance;
10
+ export declare const rspack: (options?: SweetenerPluginOptions | undefined) => import("unplugin").RspackPluginInstance;
11
+ export declare const rsbuild: (options?: SweetenerPluginOptions | undefined) => import("unplugin").RsbuildPlugin | import("unplugin").RsbuildPlugin[];
12
+ export declare const esbuild: (options?: SweetenerPluginOptions | undefined) => import("esbuild").Plugin;
13
+ export declare const bun: (options?: SweetenerPluginOptions | undefined) => Bun.BunPlugin;
14
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACzC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACvC;AAiCD,eAAO,MAAM,iBAAiB,kFA+D5B,CAAC;AAEH,eAAO,MAAM,IAAI,6GAAyB,CAAC;AAC3C,eAAO,MAAM,MAAM,iHAA2B,CAAC;AAC/C,eAAO,MAAM,QAAQ,qIAA6B,CAAC;AACnD,eAAO,MAAM,OAAO,2FAA4B,CAAC;AACjD,eAAO,MAAM,MAAM,2FAA2B,CAAC;AAC/C,eAAO,MAAM,OAAO,yHAA4B,CAAC;AACjD,eAAO,MAAM,OAAO,4EAA4B,CAAC;AACjD,eAAO,MAAM,GAAG,iEAAwB,CAAC"}
@@ -0,0 +1,105 @@
1
+ import { createSweetenerSession, describeDiagnostics, stripTypes, } from "@sweetener/compiler";
2
+ import { createUnplugin } from "unplugin";
3
+ const defaultInclude = /\.s(?:ts|js)x?(?:\?.*)?$/u;
4
+ /** The loader name a host that understands TypeScript should read output as. */
5
+ function loaderFor(filename) {
6
+ if (/\.stsx$/u.test(filename))
7
+ return "tsx";
8
+ if (/\.sts$/u.test(filename))
9
+ return "ts";
10
+ if (/\.sjsx$/u.test(filename))
11
+ return "jsx";
12
+ return "js";
13
+ }
14
+ /**
15
+ * Hosts that cannot read the TypeScript expansion produces.
16
+ *
17
+ * Expansion emits TypeScript, and only some hosts can take it from here. Vite
18
+ * runs its own Oxc transform in the entry point beside this one; esbuild and
19
+ * Bun are told the loader to use and strip types themselves. The rest have no
20
+ * TypeScript at all, and were handed `export const x: T = ...` — which they
21
+ * reported as `'const' declarations must be initialized`, an error naming
22
+ * neither Sweetener nor types. Their fixtures were untyped, so nothing caught
23
+ * it. These hosts get the types stripped for them.
24
+ */
25
+ const stripsTypeScript = new Set([
26
+ "rollup",
27
+ "rolldown",
28
+ "webpack",
29
+ "rspack",
30
+ "rsbuild",
31
+ "farm",
32
+ "unloader",
33
+ ]);
34
+ export const sweetenerUnplugin = createUnplugin((options = {}, meta) => {
35
+ const session = createSweetenerSession();
36
+ const include = options.include ?? defaultInclude;
37
+ return {
38
+ name: "sweetener",
39
+ enforce: "pre",
40
+ bun: {
41
+ loader(_code, id) {
42
+ return loaderFor(id.replace(/[?#].*$/u, ""));
43
+ },
44
+ },
45
+ // esbuild reads what a plugin returns with the loader named here, so it
46
+ // strips the expansion's types itself rather than being handed JavaScript
47
+ // and losing its own target and JSX settings.
48
+ esbuild: {
49
+ loader(_code, id) {
50
+ return loaderFor(id.replace(/[?#].*$/u, ""));
51
+ },
52
+ },
53
+ async transform(code, id) {
54
+ const filename = id.replace(/[?#].*$/u, "");
55
+ if (!include.test(id))
56
+ return;
57
+ try {
58
+ const result = await session.transform({
59
+ code,
60
+ filename,
61
+ configFile: options.configFile,
62
+ mode: "development",
63
+ });
64
+ for (const dependency of result.dependencies)
65
+ this.addWatchFile(dependency);
66
+ if (result.diagnostics.length > 0)
67
+ this.error(describeDiagnostics(result.diagnostics));
68
+ const emitted = stripsTypeScript.has(meta.framework)
69
+ ? stripTypes(result, {
70
+ filename,
71
+ configFile: options.configFile,
72
+ })
73
+ : { code: result.code, map: undefined };
74
+ const map = emitted.map ?? result.map;
75
+ return {
76
+ code: emitted.code,
77
+ // Build tools commonly enrich maps in place, so do not expose the
78
+ // compiler session's immutable cached value directly.
79
+ map: {
80
+ ...map,
81
+ sources: [...map.sources],
82
+ sourcesContent: [...(map.sourcesContent ?? [])],
83
+ names: [...map.names],
84
+ },
85
+ };
86
+ }
87
+ catch (error) {
88
+ const normalized = error instanceof Error ? error : new Error(String(error));
89
+ this.error(meta.framework === "farm" ? normalized.message : normalized);
90
+ }
91
+ },
92
+ watchChange(id) {
93
+ session.invalidate([id]);
94
+ },
95
+ };
96
+ });
97
+ export const vite = sweetenerUnplugin.vite;
98
+ export const rollup = sweetenerUnplugin.rollup;
99
+ export const rolldown = sweetenerUnplugin.rolldown;
100
+ export const webpack = sweetenerUnplugin.webpack;
101
+ export const rspack = sweetenerUnplugin.rspack;
102
+ export const rsbuild = sweetenerUnplugin.rsbuild;
103
+ export const esbuild = sweetenerUnplugin.esbuild;
104
+ export const bun = sweetenerUnplugin.bun;
105
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,mBAAmB,EACnB,UAAU,GACX,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAO1C,MAAM,cAAc,GAAG,2BAA2B,CAAC;AAEnD,gFAAgF;AAChF,SAAS,SAAS,CAAC,QAAgB;IACjC,IAAI,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC;QAAE,OAAO,KAAK,CAAC;IAC5C,IAAI,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC;QAAE,OAAO,IAAI,CAAC;IAC1C,IAAI,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC;QAAE,OAAO,KAAK,CAAC;IAC5C,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC;IAC/B,QAAQ;IACR,UAAU;IACV,SAAS;IACT,QAAQ;IACR,SAAS;IACT,MAAM;IACN,UAAU;CACX,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG,cAAc,CAE7C,CAAC,OAAO,GAAG,EAAE,EAAE,IAAI,EAAE,EAAE;IACvB,MAAM,OAAO,GAAG,sBAAsB,EAAE,CAAC;IACzC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,cAAc,CAAC;IAClD,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,OAAO,EAAE,KAAK;QACd,GAAG,EAAE;YACH,MAAM,CAAC,KAAK,EAAE,EAAE;gBACd,OAAO,SAAS,CAAC,EAAE,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,CAAC;YAC/C,CAAC;SACF;QACD,wE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@ -0,0 +1,2 @@
1
+ export { rolldown as default } from "./plugin.js";
2
+ //# sourceMappingURL=rolldown.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rolldown.d.ts","sourceRoot":"","sources":["../../src/rolldown.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { rolldown as default } from "./plugin.js";
2
+ //# sourceMappingURL=rolldown.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rolldown.js","sourceRoot":"","sources":["../../src/rolldown.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { rollup as default } from "./plugin.js";
2
+ //# sourceMappingURL=rollup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rollup.d.ts","sourceRoot":"","sources":["../../src/rollup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { rollup as default } from "./plugin.js";
2
+ //# sourceMappingURL=rollup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rollup.js","sourceRoot":"","sources":["../../src/rollup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { rsbuild as default } from "./plugin.js";
2
+ //# sourceMappingURL=rsbuild.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rsbuild.d.ts","sourceRoot":"","sources":["../../src/rsbuild.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { rsbuild as default } from "./plugin.js";
2
+ //# sourceMappingURL=rsbuild.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rsbuild.js","sourceRoot":"","sources":["../../src/rsbuild.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { rspack as default } from "./plugin.js";
2
+ //# sourceMappingURL=rspack.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rspack.d.ts","sourceRoot":"","sources":["../../src/rspack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { rspack as default } from "./plugin.js";
2
+ //# sourceMappingURL=rspack.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rspack.js","sourceRoot":"","sources":["../../src/rspack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { type Plugin } from "vite";
2
+ import { type SweetenerPluginOptions } from "./plugin.js";
3
+ /** Vite cannot infer a TypeScript loader from Sweetener's custom extensions. */
4
+ export default function sweetener(options?: SweetenerPluginOptions): Plugin[];
5
+ //# sourceMappingURL=vite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../../src/vite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,MAAM,EAAE,MAAM,MAAM,CAAC;AACrD,OAAO,EAAQ,KAAK,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAIhE,gFAAgF;AAChF,MAAM,CAAC,OAAO,UAAU,SAAS,CAC/B,OAAO,GAAE,sBAA2B,GACnC,MAAM,EAAE,CA0BV"}
@@ -0,0 +1,32 @@
1
+ import { transformWithOxc } from "vite";
2
+ import { vite } from "./plugin.js";
3
+ const sweetExtension = /\.s(?:ts|js)x?(?:\?.*)?$/u;
4
+ /** Vite cannot infer a TypeScript loader from Sweetener's custom extensions. */
5
+ export default function sweetener(options = {}) {
6
+ const include = options.include ?? sweetExtension;
7
+ return [
8
+ vite(options),
9
+ {
10
+ name: "sweetener:vite-typescript",
11
+ enforce: "pre",
12
+ async transform(code, id) {
13
+ if (!include.test(id))
14
+ return;
15
+ const filename = id.replace(/[?#].*$/u, "");
16
+ const javascript = /\.sjs(x)?$/u.exec(filename);
17
+ const lang = javascript === null
18
+ ? filename.endsWith("x")
19
+ ? "tsx"
20
+ : "ts"
21
+ : javascript[1] === undefined
22
+ ? "js"
23
+ : "jsx";
24
+ return transformWithOxc(code, filename, {
25
+ lang,
26
+ sourcemap: true,
27
+ });
28
+ },
29
+ },
30
+ ];
31
+ }
32
+ //# sourceMappingURL=vite.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.js","sourceRoot":"","sources":["../../src/vite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAe,MAAM,MAAM,CAAC;AACrD,OAAO,EAAE,IAAI,EAA+B,MAAM,aAAa,CAAC;AAEhE,MAAM,cAAc,GAAG,2BAA2B,CAAC;AAEnD,gFAAgF;AAChF,MAAM,CAAC,OAAO,UAAU,SAAS,CAC/B,UAAkC,EAAE;IAEpC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,cAAc,CAAC;IAClD,OAAO;QACL,IAAI,CAAC,OAAO,CAAW;QACvB;YACE,IAAI,EAAE,2BAA2B;YACjC,OAAO,EAAE,KAAK;YACd,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE;gBACtB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;oBAAE,OAAO;gBAC9B,MAAM,QAAQ,GAAG,EAAE,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;gBAC5C,MAAM,UAAU,GAAG,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;gBAChD,MAAM,IAAI,GACR,UAAU,KAAK,IAAI;oBACjB,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC;wBACtB,CAAC,CAAC,KAAK;wBACP,CAAC,CAAC,IAAI;oBACR,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,SAAS;wBAC3B,CAAC,CAAC,IAAI;wBACN,CAAC,CAAC,KAAK,CAAC;gBACd,OAAO,gBAAgB,CAAC,IAAI,EAAE,QAAQ,EAAE;oBACtC,IAAI;oBACJ,SAAS,EAAE,IAAI;iBAChB,CAAC,CAAC;YACL,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { webpack as default } from "./plugin.js";
2
+ //# sourceMappingURL=webpack.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webpack.d.ts","sourceRoot":"","sources":["../../src/webpack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { webpack as default } from "./plugin.js";
2
+ //# sourceMappingURL=webpack.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webpack.js","sourceRoot":"","sources":["../../src/webpack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC"}
package/package.json ADDED
@@ -0,0 +1,98 @@
1
+ {
2
+ "name": "@sweetener/unplugin",
3
+ "version": "0.1.0-alpha.0",
4
+ "description": "Sweetener alpha package: unplugin",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/src/index.d.ts",
9
+ "import": "./dist/src/index.js"
10
+ },
11
+ "./vite": {
12
+ "types": "./dist/src/vite.d.ts",
13
+ "import": "./dist/src/vite.js"
14
+ },
15
+ "./rollup": {
16
+ "types": "./dist/src/rollup.d.ts",
17
+ "import": "./dist/src/rollup.js"
18
+ },
19
+ "./rolldown": {
20
+ "types": "./dist/src/rolldown.d.ts",
21
+ "import": "./dist/src/rolldown.js"
22
+ },
23
+ "./webpack": {
24
+ "types": "./dist/src/webpack.d.ts",
25
+ "import": "./dist/src/webpack.js"
26
+ },
27
+ "./rspack": {
28
+ "types": "./dist/src/rspack.d.ts",
29
+ "import": "./dist/src/rspack.js"
30
+ },
31
+ "./rsbuild": {
32
+ "types": "./dist/src/rsbuild.d.ts",
33
+ "import": "./dist/src/rsbuild.js"
34
+ },
35
+ "./esbuild": {
36
+ "types": "./dist/src/esbuild.d.ts",
37
+ "import": "./dist/src/esbuild.js"
38
+ },
39
+ "./farm": {
40
+ "types": "./dist/src/farm.d.ts",
41
+ "import": "./dist/src/farm.js"
42
+ },
43
+ "./bun": {
44
+ "types": "./dist/src/bun.d.ts",
45
+ "import": "./dist/src/bun.js"
46
+ }
47
+ },
48
+ "peerDependencies": {
49
+ "vite": ">=6"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "vite": {
53
+ "optional": true
54
+ }
55
+ },
56
+ "license": "MIT",
57
+ "author": "Jimmy Miller <jimmyhmiller@gmail.com>",
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "git+https://github.com/sweetener-ts/sweetener.git",
61
+ "directory": "packages/unplugin"
62
+ },
63
+ "homepage": "https://github.com/sweetener-ts/sweetener/tree/main/packages/unplugin#readme",
64
+ "bugs": {
65
+ "url": "https://github.com/sweetener-ts/sweetener/issues"
66
+ },
67
+ "keywords": [
68
+ "sweetener",
69
+ "macros",
70
+ "typescript",
71
+ "hygienic",
72
+ "syntax",
73
+ "vite",
74
+ "rollup",
75
+ "rolldown",
76
+ "webpack",
77
+ "rspack",
78
+ "rsbuild",
79
+ "esbuild",
80
+ "farm",
81
+ "bun",
82
+ "unplugin"
83
+ ],
84
+ "files": [
85
+ "dist"
86
+ ],
87
+ "dependencies": {
88
+ "@sweetener/compiler": "0.1.0-alpha.0",
89
+ "unplugin": "3.3.0",
90
+ "typescript": "npm:@typescript/typescript6@6.0.2"
91
+ },
92
+ "engines": {
93
+ "node": ">=24"
94
+ },
95
+ "publishConfig": {
96
+ "access": "public"
97
+ }
98
+ }