@pitlane/assets 0.0.0-stage → 0.2.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/CHANGELOG.md +33 -0
- package/LICENSE +21 -0
- package/LICENSE.upstream +23 -0
- package/README.md +114 -2
- package/dist/build-BbA-Zq1m.mjs +161 -0
- package/dist/build.d.mts +21 -0
- package/dist/build.mjs +2 -0
- package/dist/index.d.mts +48 -0
- package/dist/index.mjs +146 -0
- package/dist/manifest.d.mts +18 -0
- package/dist/manifest.mjs +17 -0
- package/dist/specifier-DLC81sXY.mjs +47 -0
- package/dist/types-DlTsImzF.d.mts +106 -0
- package/dist/vite-plugin.d.mts +58 -0
- package/dist/vite-plugin.mjs +1411 -0
- package/package.json +71 -3
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//#region src/source-key.ts
|
|
2
|
+
/**
|
|
3
|
+
* Reduces any accepted spelling of a source module to its portable key: the
|
|
4
|
+
* slash-separated path from the project root, such as `app/counter.tsx` or,
|
|
5
|
+
* for a linked module outside the root, `../shared/widget.ts`.
|
|
6
|
+
*
|
|
7
|
+
* `./`, a leading `/` (a project-root marker), `file:`, and an `#export`
|
|
8
|
+
* fragment are dropped, and dot segments collapse lexically. Returns `null`
|
|
9
|
+
* for an absolute file URL, whose meaning depends on the machine that wrote
|
|
10
|
+
* it.
|
|
11
|
+
*/
|
|
12
|
+
function normalizeSourceKey(path) {
|
|
13
|
+
let key = path;
|
|
14
|
+
let fragment = key.indexOf("#");
|
|
15
|
+
if (fragment !== -1) key = key.slice(0, fragment);
|
|
16
|
+
if (key.startsWith("file:")) {
|
|
17
|
+
key = key.slice(5);
|
|
18
|
+
if (key.startsWith("/")) return null;
|
|
19
|
+
}
|
|
20
|
+
let segments = [];
|
|
21
|
+
for (let segment of key.split("/")) {
|
|
22
|
+
if (segment === "" || segment === ".") continue;
|
|
23
|
+
if (segment === ".." && segments.length > 0 && segments.at(-1) !== "..") segments.pop();
|
|
24
|
+
else segments.push(segment);
|
|
25
|
+
}
|
|
26
|
+
return segments.join("/");
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/specifier.ts
|
|
30
|
+
/**
|
|
31
|
+
* Each module of the `pitlane` umbrella adds the package it re-exports to
|
|
32
|
+
* this set when it loads, so a package can name itself the way the app
|
|
33
|
+
* imports it. An app that installed only `pitlane` cannot import
|
|
34
|
+
* `@pitlane/assets/vite-plugin` under a strict package manager.
|
|
35
|
+
*/
|
|
36
|
+
const UMBRELLA_PACKAGES = Symbol.for("pitlane.umbrella.packages");
|
|
37
|
+
/** The specifier the app imports `subpath` of this package by, for error messages. */
|
|
38
|
+
function assetsSpecifier(subpath) {
|
|
39
|
+
return `${globalThis[UMBRELLA_PACKAGES]?.has("@pitlane/assets") ? "pitlane/assets" : "@pitlane/assets"}/${subpath}`;
|
|
40
|
+
}
|
|
41
|
+
/** Quotes and joins names for a message: `"a"`, `"a" and "b"`, `"a", "b", and "c"`. */
|
|
42
|
+
function quotedList(names, type) {
|
|
43
|
+
let quoted = names.map((name) => `"${name}"`);
|
|
44
|
+
return new Intl.ListFormat("en", { type }).format(quoted);
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { quotedList as n, normalizeSourceKey as r, assetsSpecifier as t };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* An import map as the HTML standard defines it: specifier mappings, optional
|
|
4
|
+
* scoped mappings, and optional integrity metadata.
|
|
5
|
+
*/
|
|
6
|
+
interface ImportMap {
|
|
7
|
+
imports: Record<string, string>;
|
|
8
|
+
scopes?: Record<string, Record<string, string>>;
|
|
9
|
+
integrity?: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* What a browser needs to load one script entry: its URL, the URLs worth
|
|
13
|
+
* `modulepreload` hints, and the import map its imports resolve through.
|
|
14
|
+
*
|
|
15
|
+
* Structurally the `ScriptEntry` of `remix/assets`.
|
|
16
|
+
*/
|
|
17
|
+
interface ScriptEntry {
|
|
18
|
+
/** The public URL of the entry module. */
|
|
19
|
+
href: string;
|
|
20
|
+
/** The entry module's URL followed by its static JavaScript dependencies, shallowest first. */
|
|
21
|
+
preloads: string[];
|
|
22
|
+
/** The complete client import map, or `{ imports: {} }` when chunk import maps are disabled. */
|
|
23
|
+
importMap: ImportMap;
|
|
24
|
+
}
|
|
25
|
+
/** The JavaScript and CSS one source module requires in one environment. */
|
|
26
|
+
interface AssetMetadata {
|
|
27
|
+
/** Public URLs of the chunks holding the module and their static JavaScript dependencies. */
|
|
28
|
+
preloads: string[];
|
|
29
|
+
/** Public URLs of the stylesheets those chunks require. */
|
|
30
|
+
stylesheets: string[];
|
|
31
|
+
}
|
|
32
|
+
/** Every source module observed in one environment's graph, keyed by portable source key. */
|
|
33
|
+
interface AssetEnvironment {
|
|
34
|
+
role: "client" | "server";
|
|
35
|
+
modules: Record<string, AssetMetadata>;
|
|
36
|
+
}
|
|
37
|
+
/** The manifest a production build produces. */
|
|
38
|
+
interface BuildAssetsManifest {
|
|
39
|
+
mode: "build";
|
|
40
|
+
environments: Record<string, AssetEnvironment>;
|
|
41
|
+
/** Browser-entry source keys mapped to public URLs. */
|
|
42
|
+
entries: Record<string, string>;
|
|
43
|
+
/** Non-script asset source keys mapped to public URLs. */
|
|
44
|
+
assets: Record<string, string>;
|
|
45
|
+
importMap: ImportMap;
|
|
46
|
+
/** The server environment this manifest was written for. */
|
|
47
|
+
serverEnvironment?: string;
|
|
48
|
+
}
|
|
49
|
+
/** The manifest a development server supplies, refreshed as the graph changes. */
|
|
50
|
+
interface DevAssetsManifest {
|
|
51
|
+
mode: "dev";
|
|
52
|
+
environments: Record<string, AssetEnvironment>;
|
|
53
|
+
/** Browser-entry source keys mapped to development URLs. */
|
|
54
|
+
entries: Record<string, string>;
|
|
55
|
+
/** Non-script asset source keys mapped to development URLs. */
|
|
56
|
+
assets: Record<string, string>;
|
|
57
|
+
/** The server environment this manifest was written for. */
|
|
58
|
+
serverEnvironment?: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Everything `createAssetResolver` accepts. `{ mode: "unavailable" }` is
|
|
62
|
+
* what `@pitlane/assets/manifest` exports when no build integration replaced it.
|
|
63
|
+
*/
|
|
64
|
+
type AssetsManifest = BuildAssetsManifest | DevAssetsManifest | {
|
|
65
|
+
mode: "unavailable";
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* A bundler's output, normalized for `createAssetManifest`.
|
|
69
|
+
*
|
|
70
|
+
* Paths and filenames are already portable: source keys are relative to the
|
|
71
|
+
* project root, and `file`/`stylesheets` are emitted filenames relative to
|
|
72
|
+
* `base` or absolute public URLs.
|
|
73
|
+
*/
|
|
74
|
+
interface AssetBuild {
|
|
75
|
+
/** The public base relative emitted filenames resolve against, such as `/` or `https://cdn.example/app/`. */
|
|
76
|
+
base: string;
|
|
77
|
+
/** Every participating environment by name. Exactly one has the client role. */
|
|
78
|
+
environments: Record<string, AssetBuildEnvironment>;
|
|
79
|
+
/** The import map the bundler generated for the client build, when it generated one. */
|
|
80
|
+
importMap?: ImportMap;
|
|
81
|
+
}
|
|
82
|
+
/** One environment's output graph. */
|
|
83
|
+
interface AssetBuildEnvironment {
|
|
84
|
+
role: "client" | "server";
|
|
85
|
+
/** Emitted chunks by opaque, environment-local identity. */
|
|
86
|
+
chunks: Record<string, AssetBuildChunk>;
|
|
87
|
+
/** Browser-entry source keys mapped to chunk identities. Populated only on the client. */
|
|
88
|
+
entries: Record<string, string>;
|
|
89
|
+
/** Source keys mapped to emitted non-script files. */
|
|
90
|
+
assets: Record<string, string>;
|
|
91
|
+
}
|
|
92
|
+
/** One emitted JavaScript chunk. */
|
|
93
|
+
interface AssetBuildChunk {
|
|
94
|
+
/** The emitted filename, or an absolute public URL. */
|
|
95
|
+
file: string;
|
|
96
|
+
/** Source keys of every module the chunk contains. */
|
|
97
|
+
modules: string[];
|
|
98
|
+
/** Chunk identities this chunk imports statically, in import order. */
|
|
99
|
+
imports: string[];
|
|
100
|
+
/** Chunk identities this chunk imports dynamically. */
|
|
101
|
+
dynamicImports: string[];
|
|
102
|
+
/** Emitted stylesheet filenames, or absolute public URLs, the chunk requires. */
|
|
103
|
+
stylesheets: string[];
|
|
104
|
+
}
|
|
105
|
+
//#endregion
|
|
106
|
+
export { AssetMetadata as a, DevAssetsManifest as c, AssetEnvironment as i, ImportMap as l, AssetBuildChunk as n, AssetsManifest as o, AssetBuildEnvironment as r, BuildAssetsManifest as s, AssetBuild as t, ScriptEntry as u };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { PluginOption } from "vite";
|
|
2
|
+
//#region src/vite/state.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* The manifest module a build writes at the root of each server environment's
|
|
5
|
+
* output directory. The server chunks that read the manifest import this file
|
|
6
|
+
* by a relative path, so a host that deploys a single module must bundle it in.
|
|
7
|
+
*/
|
|
8
|
+
export declare const ASSETS_MANIFEST_FILE = "__pitlane_assets_manifest.js";
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/vite-plugin.d.ts
|
|
11
|
+
/** Options for {@link assets}. `remix()` from `@pitlane/vite-plugin-remix` accepts them as `remix({ assets })`. */
|
|
12
|
+
export interface AssetsPluginOptions {
|
|
13
|
+
/** Extra source keys to make available as client entries or assets. */
|
|
14
|
+
include?: string[];
|
|
15
|
+
/** Server environments whose module graphs provide metadata. Defaults to `["ssr"]`. */
|
|
16
|
+
serverEnvironments?: string[];
|
|
17
|
+
/** Override Vite's native setting. With neither setting present, maps are disabled. */
|
|
18
|
+
chunkImportMap?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Files and globs, relative to the Vite root, that may reach the browser.
|
|
21
|
+
* Setting it turns on the browser boundary: a build that would publish any
|
|
22
|
+
* other file fails, and `vite dev` refuses to serve one. Read as
|
|
23
|
+
* `remix/assets` reads it; a directory path matches every file below it.
|
|
24
|
+
*/
|
|
25
|
+
allowFiles?: string[];
|
|
26
|
+
/**
|
|
27
|
+
* Packages that may reach the browser, with their `dependencies` and
|
|
28
|
+
* installed `optionalDependencies`. Peer dependencies are not included.
|
|
29
|
+
* Requires `allowFiles`.
|
|
30
|
+
*/
|
|
31
|
+
allowPackages?: string[];
|
|
32
|
+
/** Files and globs refused even when `allowFiles` or `allowPackages` allows them. Requires `allowFiles`. */
|
|
33
|
+
denyFiles?: string[];
|
|
34
|
+
}
|
|
35
|
+
/** Browser entries declared by one server module's development transform. */
|
|
36
|
+
export interface AssetEntryRegistration {
|
|
37
|
+
/** The server environment that transformed the owner. */
|
|
38
|
+
environment: string;
|
|
39
|
+
/** The resolved server module ID declaring these entries. */
|
|
40
|
+
owner: string;
|
|
41
|
+
/** Resolved browser module IDs; an empty list removes the owner's declarations. */
|
|
42
|
+
entries: string[];
|
|
43
|
+
}
|
|
44
|
+
/** Available on the resolved plugin named `pitlane-assets`. */
|
|
45
|
+
export interface AssetsPluginApi {
|
|
46
|
+
/** Replace an owner's declarations; an empty list removes them. Builds use `emitFile` instead. */
|
|
47
|
+
setBrowserEntries(registration: AssetEntryRegistration): void;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The Vite plugins that give `@pitlane/assets` its manifest: they serve
|
|
51
|
+
* browser entries during `vite dev`, emit them in a build, and replace
|
|
52
|
+
* `@pitlane/assets/manifest` with the manifest of the current dev server or
|
|
53
|
+
* build. Setting `allowFiles` also enforces the browser boundary.
|
|
54
|
+
*
|
|
55
|
+
* @throws When `allowPackages` or `denyFiles` is set without `allowFiles`.
|
|
56
|
+
*/
|
|
57
|
+
export declare function assets(options?: AssetsPluginOptions): PluginOption;
|
|
58
|
+
//#endregion
|