Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@chialab/esbuild-rna

Package Overview
Dependencies
Maintainers
2
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chialab/esbuild-rna - npm Package Compare versions

Comparing version 0.15.40 to 0.16.0

162

lib/index.js
import path from 'path';
import crypto from 'crypto';
import { mkdir, readFile, writeFile } from 'fs/promises';
import { appendSearchParam, escapeRegexBody, getSearchParam } from '@chialab/node-resolve';
import { escapeRegexBody } from '@chialab/node-resolve';
import { loadSourcemap, inlineSourcemap, mergeSourcemaps } from '@chialab/estransform';

@@ -51,3 +51,3 @@ import { assignToResult, createOutputFile, createResult } from './helpers.js';

/**
* @typedef {{ code?: string, map?: import('@chialab/estransform').SourceMap|null, resolveDir?: string, errors?: import('esbuild').Message[], warnings?: import('esbuild').Message[] }} OnTransformResult
* @typedef {{ code?: string, map?: import('@chialab/estransform').SourceMap|null, resolveDir?: string, errors?: import('esbuild').Message[], warnings?: import('esbuild').Message[], watchFiles?: string[] }} OnTransformResult
*/

@@ -72,2 +72,10 @@

/**
* @typedef {Object} VirtualEntry
* @property {string} path
* @property {string|Buffer} contents
* @property {import('esbuild').Loader} [loader]
* @property {string} [resolveDir]
*/
/**
* @typedef {Object} BuildState

@@ -78,2 +86,3 @@ * @property {{ options: OnLoadOptions, callback: LoadCallback }[]} load

* @property {Map<string, Chunk>} files
* @property {Set<Result>} builds
* @property {DependenciesMap} dependencies

@@ -89,6 +98,4 @@ * @property {boolean} initialized

* @typedef {Object} EmitTransformOptions
* @property {string} entryPoint
* @property {import('esbuild').Loader} [loader]
* @property {string} [outdir]
* @property {string|Buffer} [contents]
* @property {boolean} [bundle]

@@ -106,2 +113,10 @@ * @property {boolean} [splitting]

/**
* @typedef {EmitTransformOptions & { path: string; contents?: string|Buffer }} EmitChunkOptions
*/
/**
* @typedef {EmitTransformOptions & { entryPoints: (string|VirtualEntry)[] }} EmitBuildOptions
*/
/**
* @type {WeakMap<import('esbuild').BuildOptions, BuildState>}

@@ -178,2 +193,3 @@ */

files: new Map(),
builds: new Set(),
dependencies: {},

@@ -202,2 +218,6 @@ initialized: false,

/**
* A map of emitted builds.
*/
builds: state.builds,
/**
* A list of collected dependencies.

@@ -373,6 +393,5 @@ */

* Check if path has been emitted by build.
* @param {string} path The path to check.
* @param {string} id The chunk id.
*/
isEmittedPath(path) {
const id = getSearchParam(path, 'hash');
isEmittedPath(id) {
for (const chunk of state.chunks.values()) {

@@ -455,3 +474,3 @@ if (chunk.id === id) {

id,
path: appendSearchParam(`./${path.relative(virtualOutDir, outputFile)}`, 'hash', id),
path: path.relative(virtualOutDir, outputFile),
};

@@ -464,3 +483,3 @@ rnaBuild.files.set(source, chunkResult);

* Programmatically emit a chunk reference.
* @param {EmitTransformOptions} options Esbuild transform options.
* @param {EmitChunkOptions} options Esbuild transform options.
* @returns {Promise<Chunk>} The output chunk reference.

@@ -498,3 +517,3 @@ */

config.stdin = {
sourcefile: options.entryPoint,
sourcefile: options.path,
contents: options.contents.toString(),

@@ -505,3 +524,3 @@ loader: options.loader,

} else {
config.entryPoints = [options.entryPoint];
config.entryPoints = [options.path];
}

@@ -535,5 +554,5 @@

id,
path: appendSearchParam(`./${path.relative(virtualOutDir, resolvedOutputFile)}`, 'hash', id),
path: path.relative(virtualOutDir, resolvedOutputFile),
};
state.chunks.set(options.entryPoint, chunkResult);
state.chunks.set(options.path, chunkResult);

@@ -543,2 +562,59 @@ return chunkResult;

/**
* Programmatically emit a sub build.
* @param {EmitBuildOptions} options Esbuild build options.
* @returns {Promise<Result>} The output build reference.
*/
async emitBuild(options) {
const format = options.format ?? build.initialOptions.format;
const entryPoints = options.entryPoints;
/** @type {import('esbuild').BuildOptions} */
const config = {
...build.initialOptions,
entryPoints: entryPoints.map((entryPoint) => {
if (typeof entryPoint === 'string') {
return entryPoint;
}
return entryPoint.path;
}),
format,
outdir: options.outdir ? path.resolve(virtualOutDir, `./${options.outdir}`) : fullOutDir,
bundle: options.bundle ?? build.initialOptions.bundle,
splitting: format === 'esm' ? (options.splitting ?? build.initialOptions.splitting ?? true) : false,
platform: options.platform ?? build.initialOptions.platform,
target: options.target ?? build.initialOptions.target,
plugins: options.plugins ?? build.initialOptions.plugins,
external: options.external ?? build.initialOptions.external,
jsxFactory: ('jsxFactory' in options) ? options.jsxFactory : build.initialOptions.jsxFactory,
entryNames: build.initialOptions.chunkNames || build.initialOptions.entryNames,
write,
globalName: undefined,
outfile: undefined,
metafile: true,
};
const plugins = config.plugins || [];
plugins.unshift({
name: 'emit-virtual-modules',
async setup(build) {
const { addVirtualModule } = useRna(build);
entryPoints.forEach((entryPoint) => {
if (typeof entryPoint !== 'object') {
return;
}
if (entryPoint.contents) {
addVirtualModule(entryPoint);
}
});
},
});
const result = /** @type {Result} */ (await esbuild.build(config));
state.builds.add(result);
return result;
},
/**
* Wrap esbuild onLoad hook in order to collect the load callback.

@@ -641,2 +717,24 @@ * @param {OnLoadOptions} options The filter for onLoad hook.

},
/**
* Add a virtual module to the build.
* @param {VirtualEntry} entry The virtual module entry.
*/
addVirtualModule(entry) {
const resolveDir = entry.resolveDir || rootDir;
const virtualFilePath = path.isAbsolute(entry.path) ? entry.path : path.join(resolveDir, entry.path);
const virtualFilter = new RegExp(escapeRegexBody(virtualFilePath));
build.onResolve({ filter: new RegExp(`^${escapeRegexBody(entry.path)}$`) }, () => ({
path: virtualFilePath,
namespace: 'file',
}));
rnaBuild.onLoad({ filter: virtualFilter }, (args) => ({
...args,
contents: entry.contents,
namespace: 'file',
loader: entry.loader || loaders[path.extname(args.path)] || 'file',
resolveDir: entry.resolveDir || path.dirname(virtualFilePath),
}));
},
};

@@ -649,10 +747,7 @@

build.onResolve({ filter: new RegExp(escapeRegexBody(sourceFile)) }, (args) => ({
path: args.path,
}));
rnaBuild.onLoad({ filter: new RegExp(escapeRegexBody(sourceFile)) }, () => ({
rnaBuild.addVirtualModule({
path: sourceFile,
contents: stdin.contents,
loader: stdin.loader || loaders[path.extname(sourceFile)] || 'file',
}));
loader: stdin.loader,
});
}

@@ -685,17 +780,20 @@

rnaResult.dependencies = state.dependencies;
rnaBuild.chunks.forEach((result) => assignToResult(rnaResult, result));
rnaBuild.files.forEach((result) => assignToResult(rnaResult, result));
state.chunks.forEach((result) => assignToResult(rnaResult, result));
state.files.forEach((result) => assignToResult(rnaResult, result));
state.builds.forEach((result) => assignToResult(rnaResult, result));
const outputs = { ...rnaResult.metafile.outputs };
for (const outputKey in outputs) {
const output = outputs[outputKey];
if (!output.entryPoint) {
continue;
}
if (rnaResult.metafile) {
const outputs = { ...rnaResult.metafile.outputs };
for (const outputKey in outputs) {
const output = outputs[outputKey];
if (!output.entryPoint) {
continue;
}
const entryPoint = path.resolve(rootDir, output.entryPoint.split('?')[0]);
const dependencies = Object.keys(output.inputs)
.map((input) => path.resolve(rootDir, input.split('?')[0]));
const entryPoint = path.resolve(rootDir, output.entryPoint.split('?')[0]);
const dependencies = Object.keys(output.inputs)
.map((input) => path.resolve(rootDir, input.split('?')[0]));
rnaBuild.collectDependencies(entryPoint, dependencies);
rnaBuild.collectDependencies(entryPoint, dependencies);
}
}

@@ -702,0 +800,0 @@ });

6

package.json
{
"name": "@chialab/esbuild-rna",
"type": "module",
"version": "0.15.40",
"version": "0.16.0",
"description": "A framework for esbuild plugins with transform and emit capabilities.",

@@ -27,4 +27,4 @@ "main": "lib/index.js",

"dependencies": {
"@chialab/estransform": "^0.15.28",
"@chialab/node-resolve": "^0.15.28"
"@chialab/estransform": "^0.16.0",
"@chialab/node-resolve": "^0.16.0"
},

@@ -31,0 +31,0 @@ "devDependencies": {

@@ -16,2 +16,6 @@ /// <reference types="node" />

/**
* A map of emitted builds.
*/
builds: Set<Result>;
/**
* A list of collected dependencies.

@@ -73,5 +77,5 @@ */

* Check if path has been emitted by build.
* @param {string} path The path to check.
* @param {string} id The chunk id.
*/
isEmittedPath(path: string): boolean;
isEmittedPath(id: string): boolean;
/**

@@ -86,7 +90,13 @@ * Programmatically emit file reference.

* Programmatically emit a chunk reference.
* @param {EmitTransformOptions} options Esbuild transform options.
* @param {EmitChunkOptions} options Esbuild transform options.
* @returns {Promise<Chunk>} The output chunk reference.
*/
emitChunk(options: EmitTransformOptions): Promise<Chunk>;
emitChunk(options: EmitChunkOptions): Promise<Chunk>;
/**
* Programmatically emit a sub build.
* @param {EmitBuildOptions} options Esbuild build options.
* @returns {Promise<Result>} The output build reference.
*/
emitBuild(options: EmitBuildOptions): Promise<Result>;
/**
* Wrap esbuild onLoad hook in order to collect the load callback.

@@ -119,2 +129,7 @@ * @param {OnLoadOptions} options The filter for onLoad hook.

collectDependencies(importer: string, dependencies: string[]): DependenciesMap;
/**
* Add a virtual module to the build.
* @param {VirtualEntry} entry The virtual module entry.
*/
addVirtualModule(entry: VirtualEntry): void;
};

@@ -151,2 +166,3 @@ /**

warnings?: import('esbuild').Message[];
watchFiles?: string[];
};

@@ -169,2 +185,8 @@ export type TransformCallback = (args: import('esbuild').OnLoadArgs & {

};
export type VirtualEntry = {
path: string;
contents: string | Buffer;
loader?: import("esbuild").Loader | undefined;
resolveDir?: string | undefined;
};
export type BuildState = {

@@ -181,2 +203,3 @@ load: {

files: Map<string, Chunk>;
builds: Set<Result>;
dependencies: DependenciesMap;

@@ -187,6 +210,4 @@ initialized: boolean;

export type EmitTransformOptions = {
entryPoint: string;
loader?: import("esbuild").Loader | undefined;
outdir?: string | undefined;
contents?: string | Buffer | undefined;
bundle?: boolean | undefined;

@@ -202,2 +223,9 @@ splitting?: boolean | undefined;

};
export type EmitChunkOptions = EmitTransformOptions & {
path: string;
contents?: string | Buffer;
};
export type EmitBuildOptions = EmitTransformOptions & {
entryPoints: (string | VirtualEntry)[];
};
import path from "path";
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc