@barefootjs/erb 0.30.6 → 0.31.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/dist/adapter/erb-adapter.d.ts +0 -1
- package/dist/adapter/erb-adapter.d.ts.map +1 -1
- package/dist/adapter/expr/emitters.d.ts +1 -1
- package/dist/adapter/expr/emitters.d.ts.map +1 -1
- package/dist/adapter/index.js +11 -3
- package/dist/index.js +11 -3
- package/dist/vite.d.ts +38 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +6634 -0
- package/lib/barefoot_js.rb +28 -3
- package/package.json +22 -9
- package/src/__tests__/erb-adapter.test.ts +144 -0
- package/src/__tests__/vite.test.ts +125 -0
- package/src/adapter/erb-adapter.ts +27 -6
- package/src/adapter/expr/emitters.ts +60 -32
- package/src/vite.ts +216 -0
- package/dist/build.d.ts +0 -28
- package/dist/build.d.ts.map +0 -1
- package/dist/build.js +0 -189276
- package/src/build.ts +0 -37
package/src/vite.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@barefootjs/erb/vite` — an ERB-specific COMPOSITION of core
|
|
3
|
+
* `@barefootjs/vite`'s `barefoot()`, mirroring `@barefootjs/go-template/
|
|
4
|
+
* vite`'s, `@barefootjs/hono/vite`'s, `@barefootjs/blade/vite`'s, and
|
|
5
|
+
* `@barefootjs/jinja/vite`'s shape and naming (`barefoot`, named AND
|
|
6
|
+
* default export; a user never passes `adapter`, this constructs
|
|
7
|
+
* `ErbAdapter` itself).
|
|
8
|
+
*
|
|
9
|
+
* import { barefoot } from '@barefootjs/erb/vite'
|
|
10
|
+
*
|
|
11
|
+
* export default defineConfig({
|
|
12
|
+
* base: '/integrations/rails/client/',
|
|
13
|
+
* build: { outDir: 'dist/client' },
|
|
14
|
+
* plugins: barefoot({
|
|
15
|
+
* components: ['../shared/components', '../shared/blog'],
|
|
16
|
+
* templates: 'dist/templates',
|
|
17
|
+
* }),
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* ## Why this needs no `afterEmit`-driven type-combination step (unlike Go)
|
|
21
|
+
*
|
|
22
|
+
* `@barefootjs/go-template/vite` exists mainly to combine every discovered
|
|
23
|
+
* file's `types` fragment into ONE compilable `components.go` — Go's
|
|
24
|
+
* per-file fragments assume a shared `randomID` helper and a single package
|
|
25
|
+
* header, so they are not independently valid Go source, and an unused
|
|
26
|
+
* import fails the build outright (see that module's docstring).
|
|
27
|
+
* `ErbAdapter.generate()` never produces a `types` section at all (ERB
|
|
28
|
+
* templates have no JS-style imports/types/exports to combine — see
|
|
29
|
+
* `erb-adapter.ts`'s `generate()`, whose `sections.types` is always `''`),
|
|
30
|
+
* so there is nothing across files to stitch together, and no
|
|
31
|
+
* unused-import failure mode to guard against either. So this composition's
|
|
32
|
+
* core job is just what core's `barefoot()` already does: construct
|
|
33
|
+
* `ErbAdapter` and hand it to core.
|
|
34
|
+
*
|
|
35
|
+
* ## No `adapterOptions` either — the other thing Go/Hono still need
|
|
36
|
+
*
|
|
37
|
+
* `ErbAdapterOptions` has exactly two fields, `clientJsBasePath` and
|
|
38
|
+
* `barefootJsPath` — and `ErbAdapter.generateScriptRegistrations` only
|
|
39
|
+
* falls back to them when `scriptAssets` is `undefined` (i.e. the adapter
|
|
40
|
+
* is invoked without this Vite plugin). Core's `barefoot()` plugin ALWAYS
|
|
41
|
+
* passes a resolved `scriptAssets` array (build: manifest-hashed; dev:
|
|
42
|
+
* origin-based — see `plugin.ts`), so that fallback is dead code on every
|
|
43
|
+
* Vite-driven build.
|
|
44
|
+
* Unlike Go (`packageName`, still real) or Hono (`clientJsFilename`, still
|
|
45
|
+
* real), ERB has no adapter option left with any effect once Vite drives
|
|
46
|
+
* the build — so this options interface simply omits the field rather than
|
|
47
|
+
* plumbing through two options that would always be ignored.
|
|
48
|
+
*
|
|
49
|
+
* ## `assets` — the one thing this DOES need, mirroring go-template/hono/blade/jinja
|
|
50
|
+
*
|
|
51
|
+
* A hand-written, non-component client bootstrap (e.g. an integration's
|
|
52
|
+
* `client/router-entry.ts`, which boots `@barefootjs/router` for the blog)
|
|
53
|
+
* isn't a `.tsx` component, so core's own discovery/`scriptAssets` machinery
|
|
54
|
+
* never sees it — but the compiled blog shell still needs a `<script src>`
|
|
55
|
+
* for it, and that URL is only knowable after Vite bundles it (dev:
|
|
56
|
+
* origin-based; build: manifest-hashed). `assets` resolves exactly that,
|
|
57
|
+
* into a generated JSON file the Ruby app reads at request time (the same
|
|
58
|
+
* `dist/templates/manifest.json` — read-a-JSON-file-at-runtime — idiom the
|
|
59
|
+
* Rails/Sinatra examples already use for `ssrDefaults`; unlike Go/Hono
|
|
60
|
+
* there is no compile step on the Ruby side, so plain JSON is enough — no
|
|
61
|
+
* generated Go/TS source needed). See `@barefootjs/go-template/vite`'s
|
|
62
|
+
* docstring for the full "why a companion config-capture plugin" rationale
|
|
63
|
+
* (`afterEmit`'s `AfterEmitContext` is deliberately narrow — no
|
|
64
|
+
* `ResolvedConfig`, no dev origin, no manifest — so a tiny second plugin
|
|
65
|
+
* captures those via its own `configResolved`/`configureServer` hooks for
|
|
66
|
+
* `afterEmit`, in the same closure, to read).
|
|
67
|
+
*/
|
|
68
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises'
|
|
69
|
+
import { dirname, resolve } from 'node:path'
|
|
70
|
+
import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite'
|
|
71
|
+
import { barefoot as coreBarefoot } from '@barefootjs/vite'
|
|
72
|
+
import type { AfterEmitContext } from '@barefootjs/vite'
|
|
73
|
+
import { devModuleUrl, loadManifest, resolveDevOrigin, resolveScriptAssets, toPosixRelative } from '@barefootjs/vite'
|
|
74
|
+
import { ErbAdapter } from './adapter/index.ts'
|
|
75
|
+
|
|
76
|
+
export interface ErbViteOptions {
|
|
77
|
+
/** Source directories to scan for `.tsx` components, relative to the
|
|
78
|
+
* Vite project root (or absolute). */
|
|
79
|
+
components: string[]
|
|
80
|
+
/** Where compiled `.erb` templates and `ssrDefaults` land — relative to
|
|
81
|
+
* the Vite project root (or absolute). This is a backend source
|
|
82
|
+
* directory Ruby reads, NOT `build.outDir` (Vite's client-asset
|
|
83
|
+
* output). */
|
|
84
|
+
templates: string
|
|
85
|
+
/**
|
|
86
|
+
* Extra, non-component script entries whose Vite-resolved URL (dev:
|
|
87
|
+
* origin-based; production: content-hashed manifest path) should be
|
|
88
|
+
* exposed to the Ruby app as a generated JSON asset map — e.g. a
|
|
89
|
+
* hand-written client bootstrap script that isn't a `.tsx` component, so
|
|
90
|
+
* it never goes through core's discovery/`scriptAssets` machinery, but
|
|
91
|
+
* still needs a `<script src="...">` URL only knowable after bundling.
|
|
92
|
+
*
|
|
93
|
+
* Keyed by the identifier the resolved URL should appear under in the
|
|
94
|
+
* generated map; values are entry paths relative to the Vite project
|
|
95
|
+
* root. You must ALSO register the same path as a Rollup entry yourself
|
|
96
|
+
* via stock `build.rollupOptions.input` — this plugin never adds
|
|
97
|
+
* bundling configuration on your behalf; this option only resolves the
|
|
98
|
+
* URL Vite already bundled it to, it doesn't request the bundling.
|
|
99
|
+
*/
|
|
100
|
+
assets?: Record<string, string>
|
|
101
|
+
/** Output path for the generated JSON asset map, relative to the Vite
|
|
102
|
+
* project root. Default: 'dist/bf-assets.json'. Ignored when `assets`
|
|
103
|
+
* is empty. Placed under `dist/` (already gitignored) rather than
|
|
104
|
+
* committed like Go's `bf_assets.go`: Ruby reads this file at REQUEST
|
|
105
|
+
* time, so — unlike Go, which must compile a static map into the
|
|
106
|
+
* binary — there is nothing to commit; a fresh copy is generated on
|
|
107
|
+
* every build (dev AND production) and never checked in. */
|
|
108
|
+
assetsOutputFile?: string
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** write-if-changed: writes `content` to `absPath` only if it differs from
|
|
112
|
+
* what's already there, logging `label` when it actually wrote. Avoids
|
|
113
|
+
* touching mtime (and so falsely tripping a file watcher, e.g. Ruby's own
|
|
114
|
+
* dev server) on a pass that produced byte-identical output. */
|
|
115
|
+
async function writeIfChanged(absPath: string, content: string, label: string): Promise<void> {
|
|
116
|
+
const prev = await readFile(absPath, 'utf-8').catch(() => null)
|
|
117
|
+
if (prev === content) return
|
|
118
|
+
// Default `assetsOutputFile` lives under `dist/` — a directory `vite
|
|
119
|
+
// build` may not have created yet on a clean checkout — so ensure it
|
|
120
|
+
// exists before writing.
|
|
121
|
+
await mkdir(dirname(absPath), { recursive: true })
|
|
122
|
+
await writeFile(absPath, content)
|
|
123
|
+
console.log(`Generated: ${label}`)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Resolves ONE asset entry's URL for the current pass: manifest-hashed for
|
|
127
|
+
* `mode: 'build'`, dev-origin-based for `mode: 'dev'`. Throws with an
|
|
128
|
+
* actionable message when the entry isn't in the build manifest — almost
|
|
129
|
+
* always means the caller forgot to also add it to
|
|
130
|
+
* `build.rollupOptions.input`. */
|
|
131
|
+
function resolveAssetUrl(
|
|
132
|
+
ctx: AfterEmitContext,
|
|
133
|
+
config: ResolvedConfig,
|
|
134
|
+
devServer: ViteDevServer | undefined,
|
|
135
|
+
entryRelPath: string,
|
|
136
|
+
manifest: Record<string, { file: string }> | undefined,
|
|
137
|
+
): string {
|
|
138
|
+
const absPath = resolve(ctx.projectDir, entryRelPath)
|
|
139
|
+
if (ctx.mode === 'dev') {
|
|
140
|
+
if (!devServer) throw new Error(`[erb/vite] asset "${entryRelPath}": dev server not ready`)
|
|
141
|
+
return devModuleUrl(config, resolveDevOrigin(devServer), absPath)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const manifestKey = toPosixRelative(config.root, absPath)
|
|
145
|
+
const [url] = resolveScriptAssets(manifest ?? {}, manifestKey, config.base)
|
|
146
|
+
if (!url) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
`[erb/vite] asset "${entryRelPath}" was not found in the build manifest. ` +
|
|
149
|
+
`Did you also add it to build.rollupOptions.input?`,
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
return url
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Builds and write-if-changed's the generated JSON asset map. No-op when
|
|
156
|
+
* `assets` is empty. */
|
|
157
|
+
async function writeAssetMap(
|
|
158
|
+
ctx: AfterEmitContext,
|
|
159
|
+
config: ResolvedConfig,
|
|
160
|
+
devServer: ViteDevServer | undefined,
|
|
161
|
+
assets: Record<string, string>,
|
|
162
|
+
assetsOutputFile: string,
|
|
163
|
+
): Promise<void> {
|
|
164
|
+
const keys = Object.keys(assets)
|
|
165
|
+
if (keys.length === 0) return
|
|
166
|
+
|
|
167
|
+
const manifest = ctx.mode === 'build' ? await loadManifest(ctx.outDir, config.build.manifest) : undefined
|
|
168
|
+
|
|
169
|
+
const resolved: Record<string, string> = {}
|
|
170
|
+
for (const name of keys) {
|
|
171
|
+
resolved[name] = resolveAssetUrl(ctx, config, devServer, assets[name]!, manifest)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const content = `${JSON.stringify(resolved, null, 2)}\n`
|
|
175
|
+
await writeIfChanged(resolve(ctx.projectDir, assetsOutputFile), content, assetsOutputFile)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function barefoot(options: ErbViteOptions): Plugin[] {
|
|
179
|
+
const assets = options.assets ?? {}
|
|
180
|
+
const assetsOutputFile = options.assetsOutputFile ?? 'dist/bf-assets.json'
|
|
181
|
+
|
|
182
|
+
// Populated by `erbAssetsConfigCapture` below (only added to the
|
|
183
|
+
// returned array when `assets` is non-empty). Read by `afterEmit` — see
|
|
184
|
+
// `@barefootjs/go-template/vite`'s identical mechanism for the ordering
|
|
185
|
+
// guarantee (`configResolved`/`configureServer` always run before the
|
|
186
|
+
// eager pass that triggers `afterEmit`, for both build and dev).
|
|
187
|
+
let resolvedConfig: ResolvedConfig | undefined
|
|
188
|
+
let devServer: ViteDevServer | undefined
|
|
189
|
+
|
|
190
|
+
const core = coreBarefoot({
|
|
191
|
+
adapter: new ErbAdapter(),
|
|
192
|
+
components: options.components,
|
|
193
|
+
templates: options.templates,
|
|
194
|
+
async afterEmit(ctx) {
|
|
195
|
+
if (Object.keys(assets).length > 0 && resolvedConfig) {
|
|
196
|
+
await writeAssetMap(ctx, resolvedConfig, devServer, assets, assetsOutputFile)
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
if (Object.keys(assets).length === 0) return [core]
|
|
202
|
+
|
|
203
|
+
const erbAssetsConfigCapture: Plugin = {
|
|
204
|
+
name: 'barefoot-erb-assets-config-capture',
|
|
205
|
+
configResolved(config) {
|
|
206
|
+
resolvedConfig = config
|
|
207
|
+
},
|
|
208
|
+
configureServer(server) {
|
|
209
|
+
devServer = server
|
|
210
|
+
},
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return [core, erbAssetsConfigCapture]
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export { barefoot as default }
|
package/dist/build.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { BuildOptions } from '@barefootjs/jsx';
|
|
2
|
-
import { ErbAdapter } from './adapter/index.ts';
|
|
3
|
-
import type { ErbAdapterOptions } from './adapter/index.ts';
|
|
4
|
-
export interface ErbBuildOptions extends BuildOptions {
|
|
5
|
-
/** Adapter-specific options passed to ErbAdapter */
|
|
6
|
-
adapterOptions?: ErbAdapterOptions;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Create a BarefootBuildConfig for ERB (Embedded Ruby) template projects.
|
|
10
|
-
*
|
|
11
|
-
* Uses structural typing — does not import BarefootBuildConfig to avoid a
|
|
12
|
-
* circular dependency between @barefootjs/erb and @barefootjs/cli.
|
|
13
|
-
*/
|
|
14
|
-
export declare function createConfig(options?: ErbBuildOptions): {
|
|
15
|
-
adapter: ErbAdapter;
|
|
16
|
-
paths: import("@barefootjs/jsx").BarefootPaths | undefined;
|
|
17
|
-
components: string[] | undefined;
|
|
18
|
-
outDir: string | undefined;
|
|
19
|
-
minify: boolean | undefined;
|
|
20
|
-
contentHash: boolean | undefined;
|
|
21
|
-
externals: Record<string, import("@barefootjs/jsx").ExternalSpec> | undefined;
|
|
22
|
-
externalsBasePath: string | undefined;
|
|
23
|
-
bundleEntries: import("@barefootjs/jsx").BundleEntry[] | undefined;
|
|
24
|
-
localImportPrefixes: string[] | undefined;
|
|
25
|
-
outputLayout: import("@barefootjs/jsx").OutputLayout;
|
|
26
|
-
postBuild: ((ctx: import("@barefootjs/jsx").PostBuildContext) => Promise<void> | void) | undefined;
|
|
27
|
-
};
|
|
28
|
-
//# sourceMappingURL=build.d.ts.map
|
package/dist/build.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAC/C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAE3D,MAAM,WAAW,eAAgB,SAAQ,YAAY;IACnD,oDAAoD;IACpD,cAAc,CAAC,EAAE,iBAAiB,CAAA;CACnC;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,eAAoB;IAEtD,OAAO;IACP,KAAK;IACL,UAAU;IACV,MAAM;IACN,MAAM;IACN,WAAW;IACX,SAAS;IACT,iBAAiB;IACjB,aAAa;IACb,mBAAmB;IACnB,YAAY;IAKZ,SAAS;EAEZ"}
|