@eserstack/laroux-bundler 0.0.0-stage → 5.0.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/LICENSE +13 -0
- package/README.md +431 -2
- package/dist/adapters/fonts/font-optimizer.d.ts +28 -0
- package/dist/adapters/fonts/font-optimizer.js +74 -0
- package/dist/adapters/fonts/index.d.ts +18 -0
- package/dist/adapters/fonts/index.js +78 -0
- package/dist/adapters/fonts/mod.d.ts +6 -0
- package/dist/adapters/fonts/mod.js +7 -0
- package/dist/adapters/lightningcss/css-processor.d.ts +39 -0
- package/dist/adapters/lightningcss/css-processor.js +103 -0
- package/dist/adapters/lightningcss/mod.d.ts +5 -0
- package/dist/adapters/lightningcss/mod.js +6 -0
- package/dist/adapters/mod.d.ts +13 -0
- package/dist/adapters/mod.js +21 -0
- package/dist/adapters/prebuilt-bundler/mod.d.ts +5 -0
- package/dist/adapters/prebuilt-bundler/mod.js +6 -0
- package/dist/adapters/prebuilt-bundler/prebuilt-bundler.d.ts +22 -0
- package/dist/adapters/prebuilt-bundler/prebuilt-bundler.js +60 -0
- package/dist/adapters/react/mod.d.ts +10 -0
- package/dist/adapters/react/mod.js +16 -0
- package/dist/adapters/react/plugin.d.ts +17 -0
- package/dist/adapters/react/plugin.js +62 -0
- package/dist/adapters/react/rsc-analyze.d.ts +29 -0
- package/dist/adapters/react/rsc-analyze.js +116 -0
- package/dist/adapters/react/rsc-client-entry.d.ts +26 -0
- package/dist/adapters/react/rsc-client-entry.js +145 -0
- package/dist/adapters/react/rsc-module-map.d.ts +41 -0
- package/dist/adapters/react/rsc-module-map.js +81 -0
- package/dist/adapters/react/rsc-rewrite-imports.d.ts +38 -0
- package/dist/adapters/react/rsc-rewrite-imports.js +409 -0
- package/dist/adapters/react/rsc-transform.d.ts +26 -0
- package/dist/adapters/react/rsc-transform.js +101 -0
- package/dist/adapters/runtime-bundler/mod.d.ts +6 -0
- package/dist/adapters/runtime-bundler/mod.js +6 -0
- package/dist/adapters/runtime-bundler/runtime-bundler.d.ts +85 -0
- package/dist/adapters/runtime-bundler/runtime-bundler.js +244 -0
- package/dist/adapters/sharp/image-optimizer.d.ts +52 -0
- package/dist/adapters/sharp/image-optimizer.js +261 -0
- package/dist/adapters/sharp/mod.d.ts +6 -0
- package/dist/adapters/sharp/mod.js +6 -0
- package/dist/adapters/tailwindcss/compile.d.ts +22 -0
- package/dist/adapters/tailwindcss/compile.js +29 -0
- package/dist/adapters/tailwindcss/critical-css-config.d.ts +46 -0
- package/dist/adapters/tailwindcss/critical-css-config.js +78 -0
- package/dist/adapters/tailwindcss/critical-page-css.d.ts +61 -0
- package/dist/adapters/tailwindcss/critical-page-css.js +431 -0
- package/dist/adapters/tailwindcss/critical-universal-css.d.ts +68 -0
- package/dist/adapters/tailwindcss/critical-universal-css.js +250 -0
- package/dist/adapters/tailwindcss/mod.d.ts +10 -0
- package/dist/adapters/tailwindcss/mod.js +11 -0
- package/dist/adapters/tailwindcss/plugin.d.ts +16 -0
- package/dist/adapters/tailwindcss/plugin.js +107 -0
- package/dist/adapters/tailwindcss/stylesheet-loader.d.ts +15 -0
- package/dist/adapters/tailwindcss/stylesheet-loader.js +83 -0
- package/dist/config.d.ts +27 -0
- package/dist/config.js +31 -0
- package/dist/css-modules.d.ts +49 -0
- package/dist/css-modules.js +146 -0
- package/dist/domain/build-cache.d.ts +107 -0
- package/dist/domain/build-cache.js +128 -0
- package/dist/domain/build-validator.d.ts +68 -0
- package/dist/domain/build-validator.js +207 -0
- package/dist/domain/bundler.d.ts +136 -0
- package/dist/domain/bundler.js +225 -0
- package/dist/domain/chunk-manifest.d.ts +40 -0
- package/dist/domain/chunk-manifest.js +188 -0
- package/dist/domain/client-action-stub.d.ts +30 -0
- package/dist/domain/client-action-stub.js +167 -0
- package/dist/domain/copy-path.d.ts +15 -0
- package/dist/domain/copy-path.js +29 -0
- package/dist/domain/css-plugin.d.ts +91 -0
- package/dist/domain/css-plugin.js +6 -0
- package/dist/domain/css-processor.d.ts +57 -0
- package/dist/domain/css-processor.js +33 -0
- package/dist/domain/font-optimizer.d.ts +46 -0
- package/dist/domain/font-optimizer.js +17 -0
- package/dist/domain/framework-plugin.d.ts +106 -0
- package/dist/domain/framework-plugin.js +20 -0
- package/dist/domain/image-optimizer.d.ts +74 -0
- package/dist/domain/image-optimizer.js +16 -0
- package/dist/domain/import-map.d.ts +90 -0
- package/dist/domain/import-map.js +187 -0
- package/dist/domain/mod.d.ts +32 -0
- package/dist/domain/mod.js +20 -0
- package/dist/domain/route-generator.d.ts +34 -0
- package/dist/domain/route-generator.js +305 -0
- package/dist/domain/route-scanner.d.ts +72 -0
- package/dist/domain/route-scanner.js +282 -0
- package/dist/domain/server-action-transform.d.ts +28 -0
- package/dist/domain/server-action-transform.js +147 -0
- package/dist/domain/virtual-source.d.ts +63 -0
- package/dist/domain/virtual-source.js +238 -0
- package/dist/import-map-resolver-plugin.d.ts +47 -0
- package/dist/import-map-resolver-plugin.js +109 -0
- package/dist/mod.d.ts +57 -0
- package/dist/mod.js +56 -0
- package/dist/server-externals-plugin.d.ts +29 -0
- package/dist/server-externals-plugin.js +63 -0
- package/dist/system.d.ts +157 -0
- package/dist/system.js +1401 -0
- package/dist/types.d.ts +60 -0
- package/dist/types.js +38 -0
- package/package.json +83 -3
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Virtual Source Directory Handler
|
|
4
|
+
*
|
|
5
|
+
* Creates a temporary copy of source files for bundling.
|
|
6
|
+
* This allows CSS module imports to be rewritten without modifying original source.
|
|
7
|
+
*
|
|
8
|
+
* The virtual source is created in dist/_bundle_src/ and cleaned up after bundling.
|
|
9
|
+
*/
|
|
10
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
11
|
+
import * as patterns from "@eserstack/standards/patterns";
|
|
12
|
+
import * as logging from "@eserstack/logging";
|
|
13
|
+
// Pattern to ignore node_modules and hidden files
|
|
14
|
+
const IGNORE_PATTERN = /(?:node_modules|\/\.|^\.|\\\.)/;
|
|
15
|
+
const vsLogger = logging.logger.getLogger(["laroux-bundler", "virtual-source"]);
|
|
16
|
+
export const VIRTUAL_SRC_DIR = "_bundle_src";
|
|
17
|
+
/**
|
|
18
|
+
* Get the srcDir name relative to projectRoot
|
|
19
|
+
* e.g., if srcDir is "/project/src", returns "src"
|
|
20
|
+
* e.g., if srcDir is "/project/source", returns "source"
|
|
21
|
+
*/
|
|
22
|
+
function getSrcDirName(srcDir, projectRoot) {
|
|
23
|
+
return runtime.path.relative(projectRoot, srcDir);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create a virtual source directory by copying src/ to dist/_bundle_src/src/
|
|
27
|
+
*
|
|
28
|
+
* This creates an isolated copy where CSS imports can be rewritten
|
|
29
|
+
* without modifying the original source files.
|
|
30
|
+
*
|
|
31
|
+
* The virtual source maintains the same directory structure as the original project:
|
|
32
|
+
* - dist/_bundle_src/src/app/... (mirrors src/app/...)
|
|
33
|
+
*
|
|
34
|
+
* This allows functions that expect projectRoot/src/ to work correctly.
|
|
35
|
+
*
|
|
36
|
+
* @param options - Virtual source options
|
|
37
|
+
* @returns Virtual source result with cleanup function (virtualSrcDir is the virtual "project root")
|
|
38
|
+
*/
|
|
39
|
+
export async function createVirtualSource(options) {
|
|
40
|
+
const { projectRoot, distDir, srcDir, changedFiles } = options;
|
|
41
|
+
// Get the srcDir name relative to projectRoot (e.g., "src" or "source")
|
|
42
|
+
const srcDirName = getSrcDirName(srcDir, projectRoot);
|
|
43
|
+
// virtualSrcDir acts as the "project root" for the virtual source
|
|
44
|
+
const virtualSrcDir = runtime.path.resolve(distDir, VIRTUAL_SRC_DIR);
|
|
45
|
+
// virtualSrcSubdir is where files are actually copied (maintains srcDir structure)
|
|
46
|
+
const virtualSrcSubdir = runtime.path.resolve(virtualSrcDir, srcDirName);
|
|
47
|
+
vsLogger.debug(`Creating virtual source: ${virtualSrcDir}`);
|
|
48
|
+
vsLogger.debug(` Virtual src subdir: ${virtualSrcSubdir}`);
|
|
49
|
+
// Check if virtual source already exists for incremental update
|
|
50
|
+
const virtualSourceExists = await runtime.fs.exists(virtualSrcSubdir);
|
|
51
|
+
if (changedFiles && changedFiles.size > 0 && virtualSourceExists) {
|
|
52
|
+
// INCREMENTAL MODE: Only update changed files
|
|
53
|
+
vsLogger.debug(`Incremental update: ${changedFiles.size} file(s) changed`);
|
|
54
|
+
await incrementalCopySourceFiles(changedFiles, srcDir, virtualSrcSubdir);
|
|
55
|
+
vsLogger.debug(`Virtual source updated incrementally: ${changedFiles.size} file(s)`);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
// FULL COPY MODE: First build or no changed files specified
|
|
59
|
+
// Clean any existing virtual source
|
|
60
|
+
if (await runtime.fs.exists(virtualSrcDir)) {
|
|
61
|
+
await runtime.fs.remove(virtualSrcDir, { recursive: true });
|
|
62
|
+
}
|
|
63
|
+
await runtime.fs.ensureDir(virtualSrcDir);
|
|
64
|
+
// Copy src/ to virtual source's src/ subdirectory
|
|
65
|
+
// This maintains the same structure as original project
|
|
66
|
+
try {
|
|
67
|
+
await copySourceFiles(srcDir, virtualSrcSubdir, projectRoot);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
// The caller gets no cleanup handle when creation fails, so a partial
|
|
71
|
+
// copy is removed here before the error propagates.
|
|
72
|
+
await removeVirtualSource(virtualSrcDir);
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
vsLogger.debug(`Virtual source created: ${virtualSrcDir}`);
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
virtualSrcDir,
|
|
79
|
+
cleanup: () => removeVirtualSource(virtualSrcDir),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Remove the virtual source directory. A failure is logged and not thrown:
|
|
84
|
+
* cleanup runs in `finally` blocks, where a second error must not hide the
|
|
85
|
+
* build error that is already propagating.
|
|
86
|
+
*/
|
|
87
|
+
async function removeVirtualSource(virtualSrcDir) {
|
|
88
|
+
vsLogger.debug(`Cleaning up virtual source: ${virtualSrcDir}`);
|
|
89
|
+
try {
|
|
90
|
+
await runtime.fs.remove(virtualSrcDir, { recursive: true });
|
|
91
|
+
vsLogger.debug("Virtual source cleaned up");
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
vsLogger
|
|
95
|
+
.with({ error })
|
|
96
|
+
.warn(`remove virtual source ${virtualSrcDir}; delete it by hand`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Incrementally copy only changed source files to virtual directory
|
|
101
|
+
* Used for fast rebuilds in watch mode
|
|
102
|
+
*/
|
|
103
|
+
async function incrementalCopySourceFiles(changedFiles, srcDir, virtualDir) {
|
|
104
|
+
// JS/TS extensions from standards + json and css for bundling
|
|
105
|
+
const relevantExtensions = [...patterns.JS_FILE_EXTENSIONS, "json", "css"];
|
|
106
|
+
let copiedCount = 0;
|
|
107
|
+
let deletedCount = 0;
|
|
108
|
+
for (const filePath of changedFiles) {
|
|
109
|
+
// Only process files within srcDir
|
|
110
|
+
if (!filePath.startsWith(srcDir)) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
// Check if file has relevant extension
|
|
114
|
+
if (!patterns.hasExtension(filePath, relevantExtensions)) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
// Calculate target path
|
|
118
|
+
const relativePath = runtime.path.relative(srcDir, filePath);
|
|
119
|
+
const targetPath = runtime.path.resolve(virtualDir, relativePath);
|
|
120
|
+
// One file at a time keeps open file handles bounded; a branch switch
|
|
121
|
+
// can change more files than the open-file limit.
|
|
122
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
123
|
+
const outcome = await syncChangedFile(filePath, targetPath);
|
|
124
|
+
if (outcome === "copied") {
|
|
125
|
+
copiedCount++;
|
|
126
|
+
}
|
|
127
|
+
else if (outcome === "deleted") {
|
|
128
|
+
deletedCount++;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (copiedCount > 0 || deletedCount > 0) {
|
|
132
|
+
vsLogger.debug(`Incremental update: ${copiedCount} copied, ${deletedCount} deleted`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Mirror one changed source file into the virtual directory: copy it when it
|
|
137
|
+
* exists, remove its copy when it was deleted
|
|
138
|
+
*/
|
|
139
|
+
async function syncChangedFile(filePath, targetPath) {
|
|
140
|
+
// Check if source file exists
|
|
141
|
+
try {
|
|
142
|
+
await runtime.fs.stat(filePath);
|
|
143
|
+
// File exists - copy it
|
|
144
|
+
await runtime.fs.ensureDir(runtime.path.dirname(targetPath));
|
|
145
|
+
await runtime.fs.copyFile(filePath, targetPath);
|
|
146
|
+
return "copied";
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
// File was deleted - remove from virtual source
|
|
150
|
+
try {
|
|
151
|
+
await runtime.fs.remove(targetPath);
|
|
152
|
+
return "deleted";
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
// Target doesn't exist, that's fine
|
|
156
|
+
return "unchanged";
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Copy source files to virtual directory
|
|
162
|
+
* Preserves directory structure, only copies relevant files
|
|
163
|
+
*/
|
|
164
|
+
async function copySourceFiles(srcDir, virtualDir, _projectRoot) {
|
|
165
|
+
let copiedCount = 0;
|
|
166
|
+
// JS/TS extensions from standards + json and css for bundling
|
|
167
|
+
const validExtensions = [...patterns.JS_FILE_EXTENSIONS, "json", "css"];
|
|
168
|
+
// walk includes all file types (walkFiles only supports JS files)
|
|
169
|
+
for await (const entry of runtime.fs.walk(srcDir, { includeDirs: false })) {
|
|
170
|
+
// Check extension
|
|
171
|
+
if (!patterns.hasExtension(entry.path, validExtensions)) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
// Check ignore pattern
|
|
175
|
+
const relativePath = runtime.path.relative(srcDir, entry.path);
|
|
176
|
+
if (IGNORE_PATTERN.test(relativePath)) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
// Calculate target path
|
|
180
|
+
const targetPath = runtime.path.resolve(virtualDir, relativePath);
|
|
181
|
+
// Ensure directory exists
|
|
182
|
+
await runtime.fs.ensureDir(runtime.path.dirname(targetPath));
|
|
183
|
+
// Copy file
|
|
184
|
+
await runtime.fs.copyFile(entry.path, targetPath);
|
|
185
|
+
copiedCount++;
|
|
186
|
+
}
|
|
187
|
+
vsLogger.debug(`Copied ${copiedCount} source file(s) to virtual source`);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Translate a path from original source to virtual source
|
|
191
|
+
*
|
|
192
|
+
* @param originalPath - Path in original src/ (e.g., /project/src/app/page.tsx)
|
|
193
|
+
* @param srcDir - Original src directory (e.g., /project/src)
|
|
194
|
+
* @param virtualSrcDir - Virtual "project root" (e.g., /project/dist/_bundle_src)
|
|
195
|
+
* @param srcDirName - Name of src directory relative to project (e.g., "src")
|
|
196
|
+
* @returns Path in virtual source (e.g., /project/dist/_bundle_src/src/app/page.tsx)
|
|
197
|
+
*/
|
|
198
|
+
export function translateToVirtualPath(originalPath, srcDir, virtualSrcDir, srcDirName) {
|
|
199
|
+
const relativePath = runtime.path.relative(srcDir, originalPath);
|
|
200
|
+
// Files are in virtualSrcDir/{srcDirName}/ to maintain project structure
|
|
201
|
+
// If srcDirName not provided, derive from srcDir basename
|
|
202
|
+
const actualSrcDirName = srcDirName ?? runtime.path.basename(srcDir);
|
|
203
|
+
return runtime.path.resolve(virtualSrcDir, actualSrcDirName, relativePath);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Translate a path from virtual source back to original source
|
|
207
|
+
*
|
|
208
|
+
* @param virtualPath - Path in virtual source (e.g., /project/dist/_bundle_src/src/app/page.tsx)
|
|
209
|
+
* @param srcDir - Original src directory (e.g., /project/src)
|
|
210
|
+
* @param virtualSrcDir - Virtual "project root" (e.g., /project/dist/_bundle_src)
|
|
211
|
+
* @param srcDirName - Name of src directory relative to project (e.g., "src")
|
|
212
|
+
* @returns Path in original src/ (e.g., /project/src/app/page.tsx)
|
|
213
|
+
*/
|
|
214
|
+
export function translateFromVirtualPath(virtualPath, srcDir, virtualSrcDir, srcDirName) {
|
|
215
|
+
// Files are in virtualSrcDir/{srcDirName}/
|
|
216
|
+
// If srcDirName not provided, derive from srcDir basename
|
|
217
|
+
const actualSrcDirName = srcDirName ?? runtime.path.basename(srcDir);
|
|
218
|
+
const virtualSrcSubdir = runtime.path.resolve(virtualSrcDir, actualSrcDirName);
|
|
219
|
+
const relativePath = runtime.path.relative(virtualSrcSubdir, virtualPath);
|
|
220
|
+
return runtime.path.resolve(srcDir, relativePath);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Translate client component paths to use virtual source
|
|
224
|
+
* Also updates relativePath to match the virtual source structure
|
|
225
|
+
*/
|
|
226
|
+
export function translateClientComponents(components, srcDir, virtualSrcDir) {
|
|
227
|
+
return components.map((component) => {
|
|
228
|
+
const newFilePath = translateToVirtualPath(component.filePath, srcDir, virtualSrcDir);
|
|
229
|
+
// Update relativePath to include _bundle_src prefix
|
|
230
|
+
// Original: src/app/icon.tsx -> New: _bundle_src/src/app/icon.tsx
|
|
231
|
+
const newRelativePath = `${VIRTUAL_SRC_DIR}/${component.relativePath}`;
|
|
232
|
+
return {
|
|
233
|
+
...component,
|
|
234
|
+
filePath: newFilePath,
|
|
235
|
+
relativePath: newRelativePath,
|
|
236
|
+
};
|
|
237
|
+
});
|
|
238
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import Map Resolver Plugin for Bundlers
|
|
3
|
+
*
|
|
4
|
+
* Decides what happens to the bare imports of a bundle:
|
|
5
|
+
* 1. Packages with a browser shim are replaced by the shim
|
|
6
|
+
* 2. Explicit externals stay external, keeping their bare specifier
|
|
7
|
+
* 3. With autoMarkExternal, every other bare import stays external too
|
|
8
|
+
* 4. Otherwise rolldown resolves the import from the importing file through
|
|
9
|
+
* node_modules, so an app's dependencies resolve from the app, in pnpm's
|
|
10
|
+
* isolated layout as well
|
|
11
|
+
*
|
|
12
|
+
* Bare specifiers are never rewritten (ADR 0004). Runs as a Rolldown plugin.
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
import type { BundlerPlugin } from "@eserstack/bundler/backends";
|
|
17
|
+
import type { ResolvedBrowserShimsConfig } from "./types.ts";
|
|
18
|
+
export type ImportMapResolverOptions = {
|
|
19
|
+
/** Project root directory */
|
|
20
|
+
projectRoot: string;
|
|
21
|
+
/** Browser shims configuration for server-only packages */
|
|
22
|
+
browserShims: ResolvedBrowserShimsConfig;
|
|
23
|
+
/** Cache of the externals decided so far, keyed by specifier */
|
|
24
|
+
cache?: Map<string, string>;
|
|
25
|
+
/**
|
|
26
|
+
* Keep every bare import external (default: true). Set it to false for
|
|
27
|
+
* browser bundles, which inline their packages from node_modules.
|
|
28
|
+
* See ADR 0004 (bundler external import specifiers).
|
|
29
|
+
*/
|
|
30
|
+
autoMarkExternal?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Explicit list of external packages (supports prefix matching).
|
|
33
|
+
* e.g., ["@eserstack/laroux-server"] matches "@eserstack/laroux-server/action-registry"
|
|
34
|
+
*/
|
|
35
|
+
externals?: string[];
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Create an import map resolver plugin for the bundler.
|
|
39
|
+
*
|
|
40
|
+
* This plugin:
|
|
41
|
+
* 1. Intercepts every bare specifier and node: import
|
|
42
|
+
* 2. Returns the browser shim of a package that has one
|
|
43
|
+
* 3. Marks explicit externals, and with autoMarkExternal every bare import,
|
|
44
|
+
* as external without rewriting the specifier
|
|
45
|
+
* 4. Leaves every other bare import to rolldown's node_modules resolution
|
|
46
|
+
*/
|
|
47
|
+
export declare function createImportMapResolverPlugin(options: ImportMapResolverOptions): BundlerPlugin;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Import Map Resolver Plugin for Bundlers
|
|
4
|
+
*
|
|
5
|
+
* Decides what happens to the bare imports of a bundle:
|
|
6
|
+
* 1. Packages with a browser shim are replaced by the shim
|
|
7
|
+
* 2. Explicit externals stay external, keeping their bare specifier
|
|
8
|
+
* 3. With autoMarkExternal, every other bare import stays external too
|
|
9
|
+
* 4. Otherwise rolldown resolves the import from the importing file through
|
|
10
|
+
* node_modules, so an app's dependencies resolve from the app, in pnpm's
|
|
11
|
+
* isolated layout as well
|
|
12
|
+
*
|
|
13
|
+
* Bare specifiers are never rewritten (ADR 0004). Runs as a Rolldown plugin.
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
import * as logging from "@eserstack/logging";
|
|
18
|
+
import * as importMap from "./domain/import-map.js";
|
|
19
|
+
const resolverLogger = logging.logger.getLogger([
|
|
20
|
+
"laroux-bundler",
|
|
21
|
+
"import-map-resolver",
|
|
22
|
+
]);
|
|
23
|
+
/** The cache value that marks a specifier as external. */
|
|
24
|
+
const EXTERNAL = "external";
|
|
25
|
+
/**
|
|
26
|
+
* Create an import map resolver plugin for the bundler.
|
|
27
|
+
*
|
|
28
|
+
* This plugin:
|
|
29
|
+
* 1. Intercepts every bare specifier and node: import
|
|
30
|
+
* 2. Returns the browser shim of a package that has one
|
|
31
|
+
* 3. Marks explicit externals, and with autoMarkExternal every bare import,
|
|
32
|
+
* as external without rewriting the specifier
|
|
33
|
+
* 4. Leaves every other bare import to rolldown's node_modules resolution
|
|
34
|
+
*/
|
|
35
|
+
export function createImportMapResolverPlugin(options) {
|
|
36
|
+
const cache = options.cache ?? new Map();
|
|
37
|
+
const autoMarkExternal = options.autoMarkExternal ?? true;
|
|
38
|
+
const externals = options.externals ?? [];
|
|
39
|
+
// Check if specifier matches any explicit external (supports prefix matching)
|
|
40
|
+
const isExplicitExternal = (specifier) => {
|
|
41
|
+
for (const external of externals) {
|
|
42
|
+
if (specifier === external || specifier.startsWith(`${external}/`)) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
};
|
|
48
|
+
// Merge the package and node builtin shims into a single lookup map
|
|
49
|
+
const browserShims = {
|
|
50
|
+
...options.browserShims.jsr,
|
|
51
|
+
...options.browserShims.nodeBuiltins,
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
name: "import-map-resolver",
|
|
55
|
+
setup(build) {
|
|
56
|
+
// Match ALL bare specifiers
|
|
57
|
+
build.onResolve({ filter: /^[^./]/ }, (args) => {
|
|
58
|
+
const specifier = args.path;
|
|
59
|
+
// Skip if not a bare specifier
|
|
60
|
+
if (!importMap.isBareSpecifier(specifier)) {
|
|
61
|
+
return {};
|
|
62
|
+
}
|
|
63
|
+
if (cache.get(specifier) === EXTERNAL) {
|
|
64
|
+
return { external: true };
|
|
65
|
+
}
|
|
66
|
+
// Check explicit externals first (supports prefix matching)
|
|
67
|
+
if (isExplicitExternal(specifier)) {
|
|
68
|
+
cache.set(specifier, EXTERNAL);
|
|
69
|
+
resolverLogger.debug(`Marking ${specifier} as external (explicit)`);
|
|
70
|
+
return { external: true };
|
|
71
|
+
}
|
|
72
|
+
// Check if we have a browser shim for this specifier
|
|
73
|
+
if (browserShims[specifier] !== undefined) {
|
|
74
|
+
resolverLogger.debug(`Using browser shim for ${specifier}`);
|
|
75
|
+
return { path: `\0virtual:${specifier}`, namespace: "browser-shim" };
|
|
76
|
+
}
|
|
77
|
+
if (autoMarkExternal) {
|
|
78
|
+
cache.set(specifier, EXTERNAL);
|
|
79
|
+
resolverLogger.debug(`Marking ${specifier} as external`);
|
|
80
|
+
// Keep the bare specifier; it resolves from node_modules at run time
|
|
81
|
+
return { external: true };
|
|
82
|
+
}
|
|
83
|
+
// Browser bundles inline the package: rolldown resolves it from the
|
|
84
|
+
// importing file through node_modules
|
|
85
|
+
return {};
|
|
86
|
+
});
|
|
87
|
+
// Match node: protocol imports (for browser shimming)
|
|
88
|
+
build.onResolve({ filter: /^node:/ }, (args) => {
|
|
89
|
+
const specifier = args.path;
|
|
90
|
+
if (browserShims[specifier] !== undefined) {
|
|
91
|
+
resolverLogger.debug(`Using browser shim for ${specifier}`);
|
|
92
|
+
return { path: `\0virtual:${specifier}`, namespace: "browser-shim" };
|
|
93
|
+
}
|
|
94
|
+
// No shim available - mark as external
|
|
95
|
+
resolverLogger.warn(`Node builtin ${specifier} has no browser shim - marking as external`);
|
|
96
|
+
return { external: true };
|
|
97
|
+
});
|
|
98
|
+
// Load virtual shim modules
|
|
99
|
+
build.onLoad({ filter: /.*/, namespace: "browser-shim" }, (args) => {
|
|
100
|
+
const specifier = args.path.replace("\0virtual:", "");
|
|
101
|
+
const contents = browserShims[specifier];
|
|
102
|
+
if (contents !== undefined) {
|
|
103
|
+
return { contents, loader: "js" };
|
|
104
|
+
}
|
|
105
|
+
return {};
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @eserstack/laroux-bundler
|
|
3
|
+
*
|
|
4
|
+
* Framework-agnostic build system with hexagonal architecture.
|
|
5
|
+
*
|
|
6
|
+
* ## Architecture
|
|
7
|
+
*
|
|
8
|
+
* - **Domain**: Core interfaces and business logic (`./domain/mod.ts`)
|
|
9
|
+
* - **Adapters**: Framework/tool implementations (`./adapters/mod.ts`)
|
|
10
|
+
*
|
|
11
|
+
* ## Usage - Explicit Composition
|
|
12
|
+
*
|
|
13
|
+
* Import adapters explicitly and pass to build functions.
|
|
14
|
+
* This ensures tree-shaking works, dependencies are visible, and types are checked.
|
|
15
|
+
*
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import * as larouxBundler from "@eserstack/laroux-bundler";
|
|
18
|
+
* import * as fonts from "@eserstack/laroux-bundler/adapters/fonts";
|
|
19
|
+
* import * as lightningcss from "@eserstack/laroux-bundler/adapters/lightningcss";
|
|
20
|
+
* import * as react from "@eserstack/laroux-bundler/adapters/react";
|
|
21
|
+
* import * as sharp from "@eserstack/laroux-bundler/adapters/sharp";
|
|
22
|
+
* import * as tailwindcss from "@eserstack/laroux-bundler/adapters/tailwindcss";
|
|
23
|
+
*
|
|
24
|
+
* async function buildApp(config: larouxBundler.BuildConfig) {
|
|
25
|
+
* const context = larouxBundler.createBuildContext(config, {
|
|
26
|
+
* framework: react.reactPlugin,
|
|
27
|
+
* css: tailwindcss.createTailwindPlugin(),
|
|
28
|
+
* cssProcessor: lightningcss.lightningCssProcessor,
|
|
29
|
+
* fontOptimizer: fonts.googleFontOptimizer,
|
|
30
|
+
* imageOptimizer: sharp.sharpImageOptimizer,
|
|
31
|
+
* bundlerBackend: "rolldown",
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* return await larouxBundler.build(context);
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* ## Available Adapters
|
|
39
|
+
*
|
|
40
|
+
* - `@eserstack/laroux-bundler/adapters/react` - React Server Components
|
|
41
|
+
* - `@eserstack/laroux-bundler/adapters/tailwindcss` - Tailwind CSS v4
|
|
42
|
+
* - `@eserstack/laroux-bundler/adapters/lightningcss` - LightningCSS processor (`lightningCssProcessor`)
|
|
43
|
+
* - `@eserstack/laroux-bundler/adapters/sharp` - Image optimization (`sharpImageOptimizer`)
|
|
44
|
+
* - `@eserstack/laroux-bundler/adapters/fonts` - Font optimization (`googleFontOptimizer`)
|
|
45
|
+
* - `@eserstack/laroux-bundler/adapters/runtime-bundler` - Dev mode runtime bundler
|
|
46
|
+
* - `@eserstack/laroux-bundler/adapters/prebuilt-bundler` - Production prebuilt bundler
|
|
47
|
+
*/
|
|
48
|
+
export type { BaseChunkInfo, BaseChunkManifest, BuildIsolationReport, BundleData, BundleOptions, BundleOutput, Bundler, BundleResult, ChunkManifest, ClientComponent, ClientComponentCache, ClientComponentCacheEntry, ComponentChunkInfo, CriticalCssResult, CSSModuleCacheEntry, CssPlugin, CssPluginContext, CssPluginOptions, CssProcessor, CssProcessorOptions, FileInfo, FileViolation, FontOptimizationOptions, FontOptimizer, FrameworkPlugin, ImageManifest, ImageOptimizationRequest, ImageOptimizer, ImageVariant, ImportMap, ImportMapEntry, ModuleMap, ModuleMapEntry, OptimizedFonts, OptimizedImage, RouteCacheEntry, RouteScanCache, ScannedProxy, ScannedRoute, ScanResult, ScanRoutesOptions, ServerActionTransformResult, ServerBundleOptions, ServerBundleResult, TransformResult, UniversalCssResult, VirtualSourceOptions, VirtualSourceResult, } from "./domain/mod.ts";
|
|
49
|
+
export { analyzeBundleResult, BuildCache, BuildIsolationError, bundle, bundleServerComponents, ChunkManifestError, cleanSrcArtifacts, createIsolatedWriter, createVirtualSource, generateApiRouteFile, generateChunkManifest, generateProxyFile, generateRouteFile, getComponentName, getExternals, getLayoutName, isExternal, isWithinOutputDir, loadChunkManifest, loadImportMap, logBundleStats, logManifest, noopCssProcessor, noopFontOptimizer, noopImageOptimizer, noopPlugin, resolveSpecifier, saveChunkManifest, scanRoutes, transformServerActionFile, transformServerActions, translateClientComponents, translateFromVirtualPath, translateToVirtualPath, validateBuildIsolation, VIRTUAL_SRC_DIR, } from "./domain/mod.ts";
|
|
50
|
+
export { build, type BuildContext, type BuildPlugins, type BuildResult, createBuildContext, ensureBuildIsReady, watch, } from "./system.ts";
|
|
51
|
+
export type { BuildSettings, BundlerBackend } from "./config.ts";
|
|
52
|
+
export { DEVELOPMENT_SETTINGS, PRODUCTION_SETTINGS } from "./config.ts";
|
|
53
|
+
export type { BuildConfig } from "./types.ts";
|
|
54
|
+
export type { FontDefinition, FontDisplay, FontProvider, FontStyle, FontWeight, ImageFormat, ImageOutputFormat, ImagePlaceholder, LogLevel, ResolvedBrowserShimsConfig, ResolvedImageConfig, ResolvedImageQuality, } from "./types.ts";
|
|
55
|
+
export { BuildError, buildErrors } from "./types.ts";
|
|
56
|
+
export { createServerExternalsPlugin, type ServerExternalsPluginOptions, } from "./server-externals-plugin.ts";
|
|
57
|
+
export { createImportMapResolverPlugin, type ImportMapResolverOptions, } from "./import-map-resolver-plugin.ts";
|
package/dist/mod.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* @eserstack/laroux-bundler
|
|
4
|
+
*
|
|
5
|
+
* Framework-agnostic build system with hexagonal architecture.
|
|
6
|
+
*
|
|
7
|
+
* ## Architecture
|
|
8
|
+
*
|
|
9
|
+
* - **Domain**: Core interfaces and business logic (`./domain/mod.ts`)
|
|
10
|
+
* - **Adapters**: Framework/tool implementations (`./adapters/mod.ts`)
|
|
11
|
+
*
|
|
12
|
+
* ## Usage - Explicit Composition
|
|
13
|
+
*
|
|
14
|
+
* Import adapters explicitly and pass to build functions.
|
|
15
|
+
* This ensures tree-shaking works, dependencies are visible, and types are checked.
|
|
16
|
+
*
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import * as larouxBundler from "@eserstack/laroux-bundler";
|
|
19
|
+
* import * as fonts from "@eserstack/laroux-bundler/adapters/fonts";
|
|
20
|
+
* import * as lightningcss from "@eserstack/laroux-bundler/adapters/lightningcss";
|
|
21
|
+
* import * as react from "@eserstack/laroux-bundler/adapters/react";
|
|
22
|
+
* import * as sharp from "@eserstack/laroux-bundler/adapters/sharp";
|
|
23
|
+
* import * as tailwindcss from "@eserstack/laroux-bundler/adapters/tailwindcss";
|
|
24
|
+
*
|
|
25
|
+
* async function buildApp(config: larouxBundler.BuildConfig) {
|
|
26
|
+
* const context = larouxBundler.createBuildContext(config, {
|
|
27
|
+
* framework: react.reactPlugin,
|
|
28
|
+
* css: tailwindcss.createTailwindPlugin(),
|
|
29
|
+
* cssProcessor: lightningcss.lightningCssProcessor,
|
|
30
|
+
* fontOptimizer: fonts.googleFontOptimizer,
|
|
31
|
+
* imageOptimizer: sharp.sharpImageOptimizer,
|
|
32
|
+
* bundlerBackend: "rolldown",
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* return await larouxBundler.build(context);
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* ## Available Adapters
|
|
40
|
+
*
|
|
41
|
+
* - `@eserstack/laroux-bundler/adapters/react` - React Server Components
|
|
42
|
+
* - `@eserstack/laroux-bundler/adapters/tailwindcss` - Tailwind CSS v4
|
|
43
|
+
* - `@eserstack/laroux-bundler/adapters/lightningcss` - LightningCSS processor (`lightningCssProcessor`)
|
|
44
|
+
* - `@eserstack/laroux-bundler/adapters/sharp` - Image optimization (`sharpImageOptimizer`)
|
|
45
|
+
* - `@eserstack/laroux-bundler/adapters/fonts` - Font optimization (`googleFontOptimizer`)
|
|
46
|
+
* - `@eserstack/laroux-bundler/adapters/runtime-bundler` - Dev mode runtime bundler
|
|
47
|
+
* - `@eserstack/laroux-bundler/adapters/prebuilt-bundler` - Production prebuilt bundler
|
|
48
|
+
*/
|
|
49
|
+
export { analyzeBundleResult, BuildCache, BuildIsolationError, bundle, bundleServerComponents, ChunkManifestError, cleanSrcArtifacts, createIsolatedWriter, createVirtualSource, generateApiRouteFile, generateChunkManifest, generateProxyFile, generateRouteFile, getComponentName, getExternals, getLayoutName, isExternal, isWithinOutputDir, loadChunkManifest, loadImportMap, logBundleStats, logManifest, noopCssProcessor, noopFontOptimizer, noopImageOptimizer, noopPlugin, resolveSpecifier, saveChunkManifest, scanRoutes, transformServerActionFile, transformServerActions, translateClientComponents, translateFromVirtualPath, translateToVirtualPath, validateBuildIsolation, VIRTUAL_SRC_DIR, } from "./domain/mod.js";
|
|
50
|
+
// Re-export build system
|
|
51
|
+
export { build, createBuildContext, ensureBuildIsReady, watch, } from "./system.js";
|
|
52
|
+
export { DEVELOPMENT_SETTINGS, PRODUCTION_SETTINGS } from "./config.js";
|
|
53
|
+
export { BuildError, buildErrors } from "./types.js";
|
|
54
|
+
// Re-export bundler plugins
|
|
55
|
+
export { createServerExternalsPlugin, } from "./server-externals-plugin.js";
|
|
56
|
+
export { createImportMapResolverPlugin, } from "./import-map-resolver-plugin.js";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server Externals Plugin
|
|
3
|
+
*
|
|
4
|
+
* A minimal bundler plugin that marks specific packages as external for server bundling.
|
|
5
|
+
* Unlike the full import-map-resolver, this plugin:
|
|
6
|
+
* 1. ONLY intercepts packages in the externals list
|
|
7
|
+
* 2. Lets rolldown's native resolution handle everything else
|
|
8
|
+
*
|
|
9
|
+
* This ensures npm packages like react, lucide-react, etc. are properly bundled
|
|
10
|
+
* while @eserstack/laroux and @eserstack/laroux-server remain external (resolved from node_modules at runtime).
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
import type { BundlerPlugin } from "@eserstack/bundler/backends";
|
|
15
|
+
export type ServerExternalsPluginOptions = {
|
|
16
|
+
/**
|
|
17
|
+
* List of packages to mark as external.
|
|
18
|
+
* Supports prefix matching for subpath imports.
|
|
19
|
+
*/
|
|
20
|
+
externals: string[];
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Create a minimal server externals plugin.
|
|
24
|
+
*
|
|
25
|
+
* This plugin ONLY marks specified packages as external.
|
|
26
|
+
* All other imports are passed through to rolldown's native resolution,
|
|
27
|
+
* which will bundle them from node_modules.
|
|
28
|
+
*/
|
|
29
|
+
export declare function createServerExternalsPlugin(options: ServerExternalsPluginOptions): BundlerPlugin;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Server Externals Plugin
|
|
4
|
+
*
|
|
5
|
+
* A minimal bundler plugin that marks specific packages as external for server bundling.
|
|
6
|
+
* Unlike the full import-map-resolver, this plugin:
|
|
7
|
+
* 1. ONLY intercepts packages in the externals list
|
|
8
|
+
* 2. Lets rolldown's native resolution handle everything else
|
|
9
|
+
*
|
|
10
|
+
* This ensures npm packages like react, lucide-react, etc. are properly bundled
|
|
11
|
+
* while @eserstack/laroux and @eserstack/laroux-server remain external (resolved from node_modules at runtime).
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import * as logging from "@eserstack/logging";
|
|
16
|
+
const pluginLogger = logging.logger.getLogger([
|
|
17
|
+
"laroux-bundler",
|
|
18
|
+
"server-externals",
|
|
19
|
+
]);
|
|
20
|
+
/**
|
|
21
|
+
* Check if a specifier matches any of the external packages.
|
|
22
|
+
* Supports both exact matches and subpath imports:
|
|
23
|
+
* - "@eserstack/laroux-server" matches "@eserstack/laroux-server"
|
|
24
|
+
* - "@eserstack/laroux-server" matches "@eserstack/laroux-server/action-registry"
|
|
25
|
+
*/
|
|
26
|
+
function isExternalPackage(specifier, externals) {
|
|
27
|
+
for (const pkg of externals) {
|
|
28
|
+
if (specifier === pkg || specifier.startsWith(`${pkg}/`)) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create a minimal server externals plugin.
|
|
36
|
+
*
|
|
37
|
+
* This plugin ONLY marks specified packages as external.
|
|
38
|
+
* All other imports are passed through to rolldown's native resolution,
|
|
39
|
+
* which will bundle them from node_modules.
|
|
40
|
+
*/
|
|
41
|
+
export function createServerExternalsPlugin(options) {
|
|
42
|
+
const { externals } = options;
|
|
43
|
+
// Build regex pattern for matching externals
|
|
44
|
+
// e.g., ["@eserstack/laroux", "@eserstack/laroux-server"] -> /^(@eser\/laroux|@eser\/laroux-server)($|\/)/
|
|
45
|
+
const escapedPackages = externals.map((pkg) => pkg.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
46
|
+
const pattern = new RegExp(`^(${escapedPackages.join("|")})($|\\/)`);
|
|
47
|
+
pluginLogger.debug(`Server externals plugin initialized with ${externals.length} packages: ${externals.join(", ")}`);
|
|
48
|
+
return {
|
|
49
|
+
name: "server-externals",
|
|
50
|
+
setup(build) {
|
|
51
|
+
// Only intercept packages that match our externals list
|
|
52
|
+
build.onResolve({ filter: pattern }, (args) => {
|
|
53
|
+
const specifier = args.path;
|
|
54
|
+
if (isExternalPackage(specifier, externals)) {
|
|
55
|
+
pluginLogger.debug(`Marking as external: ${specifier}`);
|
|
56
|
+
return { external: true };
|
|
57
|
+
}
|
|
58
|
+
// This shouldn't happen due to the filter, but just in case
|
|
59
|
+
return {};
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|