@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
222 lines (216 loc) • 7.12 kB
JavaScript
import { Engine } from './chunk-4DIXSZON.js';
import { getSourceFile } from './chunk-LK23XV73.js';
import { createLog } from './chunk-K7ITYUIA.js';
import { init_esm_shims, __name } from './chunk-QH7NXH7H.js';
import { LogLevelLabel } from '@storm-software/config-tools/types';
import { match, tsconfigPathsToRegExp } from 'bundle-require';
// src/unplugin/core/factory.ts
init_esm_shims();
// src/lib/unplugin/load.ts
init_esm_shims();
async function handleLoad(context, args, options = {}) {
if (args.id) {
const resolvedPath = context.vfs.resolvePath(args.id, {
type: "file"
});
if (resolvedPath) {
const contents = await context.vfs.readFile(resolvedPath);
if (!contents) {
return void 0;
}
const result = await context.compiler.compile(context, resolvedPath, contents, options);
return context.compiler.getResult(getSourceFile(args.id, contents), result);
}
}
return void 0;
}
__name(handleLoad, "handleLoad");
// src/lib/unplugin/resolve-id.ts
init_esm_shims();
var NON_NODE_MODULE_REGEX = /^[A-Z]:[/\\]|^\.{0,2}\/|^\.{1,2}$/;
async function handleResolveId(context, args, options = {}) {
if (args.id) {
if (options.skipNodeModulesBundle) {
if (context.vfs.isVirtualFile(args.id) || args.importer && context.vfs.isVirtualFile(args.id, {
paths: [
args.importer
]
})) {
const resolvedPath = args.importer ? context.vfs.resolvePath(args.id, {
paths: [
args.importer
]
}) : context.vfs.resolvePath(args.id);
if (resolvedPath) {
return {
id: resolvedPath,
external: context.options.projectType !== "application"
};
}
}
if (context.vfs.isTsconfigPath(args.id)) {
const tsconfigPath = context.vfs.resolveTsconfigPath(args.id);
const tsconfigPathPackage = context.vfs.resolveTsconfigPathPackage(args.id);
if (tsconfigPath && tsconfigPathPackage) {
return {
id: tsconfigPath,
external: Boolean(!options.noExternal?.includes(tsconfigPathPackage) && (options.external?.includes(tsconfigPathPackage) ?? context.options.projectType !== "application"))
};
}
}
if (match(args.id, options.resolvePatterns) || match(args.id, options.noExternal) || args.id.startsWith("internal:") || args.id.startsWith("virtual:")) {
return void 0;
}
if (match(args.id, options.external) || args.id.startsWith("node:")) {
return {
id: args.id,
external: true
};
}
if (!NON_NODE_MODULE_REGEX.test(args.id)) {
return {
id: args.id,
external: true
};
}
} else {
if (context.vfs.isVirtualFile(args.id) || args.importer && context.vfs.isVirtualFile(args.id, {
paths: [
args.importer
]
})) {
const resolvedPath = args.importer ? context.vfs.resolvePath(args.id, {
paths: [
args.importer
]
}) : context.vfs.resolvePath(args.id);
if (resolvedPath) {
return {
id: resolvedPath,
external: context.options.projectType !== "application"
};
}
}
if (context.vfs.isTsconfigPath(args.id)) {
const tsconfigPath = context.vfs.resolveTsconfigPath(args.id);
const tsconfigPathPackage = context.vfs.resolveTsconfigPathPackage(args.id);
if (tsconfigPath && tsconfigPathPackage) {
return {
id: tsconfigPath,
external: Boolean(!options.noExternal?.includes(tsconfigPathPackage) && (options.external?.includes(tsconfigPathPackage) ?? context.options.projectType !== "application"))
};
}
}
if (match(args.id, options.noExternal) || context.vfs.isRuntimeFile(args.id) || args.importer && context.vfs.isRuntimeFile(args.id, {
paths: [
args.importer
]
})) {
return void 0;
}
if (match(args.id, options.external) || args.id.startsWith("node:")) {
return {
id: args.id,
external: true
};
}
}
}
return void 0;
}
__name(handleResolveId, "handleResolveId");
// src/unplugin/core/factory.ts
function createUnpluginFactory(options) {
const { decorate, framework } = options ?? {};
return (userConfig, meta) => {
const log = createLog("unplugin", {
logLevel: LogLevelLabel.INFO,
...userConfig
});
log(LogLevelLabel.DEBUG, "Initializing Unplugin");
try {
const inlineConfig = {
...userConfig,
command: "build",
variant: framework,
unplugin: meta
};
let engine;
let resolvePatterns = [];
async function buildStart() {
log(LogLevelLabel.DEBUG, "Storm Stack build plugin starting...");
engine = await Engine.create(inlineConfig);
if (engine.context.options.skipNodeModulesBundle) {
resolvePatterns = tsconfigPathsToRegExp(engine.context.tsconfig.options.paths ?? []);
}
log(LogLevelLabel.DEBUG, "Prepare Storm Stack project...");
await engine.prepare(inlineConfig);
}
__name(buildStart, "buildStart");
async function resolveId(id, importer, options2 = {
isEntry: false
}) {
return handleResolveId(engine.context, {
id,
importer,
options: options2
}, {
skipNodeModulesBundle: engine.context.options.skipNodeModulesBundle,
external: engine.context.options.external,
noExternal: engine.context.options.noExternal,
resolvePatterns
});
}
__name(resolveId, "resolveId");
async function load(id) {
return handleLoad(engine.context, {
id
});
}
__name(load, "load");
async function transform(code, id) {
return engine.context.compiler.getResult(getSourceFile(id, code), await engine.context.compiler.compile(engine.context, id, code));
}
__name(transform, "transform");
async function writeBundle() {
log(LogLevelLabel.DEBUG, "Finalizing Storm Stack project output...");
await engine.finalize();
}
__name(writeBundle, "writeBundle");
const result = {
name: "storm-stack",
enforce: "pre",
resolveId: {
filter: {
id: {
include: [
/.*/
]
}
},
handler: resolveId
},
load: {
filter: {
id: {
include: [
/.*/,
/^storm:/
]
}
},
handler: load
},
transform,
buildStart,
writeBundle
};
return decorate ? decorate(engine, result) : result;
} catch (error) {
log(LogLevelLabel.FATAL, error?.message);
throw error;
}
};
}
__name(createUnpluginFactory, "createUnpluginFactory");
export { createUnpluginFactory };