@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
224 lines (217 loc) • 7.54 kB
JavaScript
var chunkM72EQR66_cjs = require('./chunk-M72EQR66.cjs');
var chunkD7HZX3MP_cjs = require('./chunk-D7HZX3MP.cjs');
var chunkI5HAGX52_cjs = require('./chunk-I5HAGX52.cjs');
var chunkA43FUIC2_cjs = require('./chunk-A43FUIC2.cjs');
var types = require('@storm-software/config-tools/types');
var bundleRequire = require('bundle-require');
// src/unplugin/core/factory.ts
chunkA43FUIC2_cjs.init_cjs_shims();
// src/lib/unplugin/load.ts
chunkA43FUIC2_cjs.init_cjs_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(chunkD7HZX3MP_cjs.getSourceFile(args.id, contents), result);
}
}
return void 0;
}
chunkA43FUIC2_cjs.__name(handleLoad, "handleLoad");
// src/lib/unplugin/resolve-id.ts
chunkA43FUIC2_cjs.init_cjs_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 (bundleRequire.match(args.id, options.resolvePatterns) || bundleRequire.match(args.id, options.noExternal) || args.id.startsWith("internal:") || args.id.startsWith("virtual:")) {
return void 0;
}
if (bundleRequire.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 (bundleRequire.match(args.id, options.noExternal) || context.vfs.isRuntimeFile(args.id) || args.importer && context.vfs.isRuntimeFile(args.id, {
paths: [
args.importer
]
})) {
return void 0;
}
if (bundleRequire.match(args.id, options.external) || args.id.startsWith("node:")) {
return {
id: args.id,
external: true
};
}
}
}
return void 0;
}
chunkA43FUIC2_cjs.__name(handleResolveId, "handleResolveId");
// src/unplugin/core/factory.ts
function createUnpluginFactory(options) {
const { decorate, framework } = options ?? {};
return (userConfig, meta) => {
const log = chunkI5HAGX52_cjs.createLog("unplugin", {
logLevel: types.LogLevelLabel.INFO,
...userConfig
});
log(types.LogLevelLabel.DEBUG, "Initializing Unplugin");
try {
const inlineConfig = {
...userConfig,
command: "build",
variant: framework,
unplugin: meta
};
let engine;
let resolvePatterns = [];
async function buildStart() {
log(types.LogLevelLabel.DEBUG, "Storm Stack build plugin starting...");
engine = await chunkM72EQR66_cjs.Engine.create(inlineConfig);
if (engine.context.options.skipNodeModulesBundle) {
resolvePatterns = bundleRequire.tsconfigPathsToRegExp(engine.context.tsconfig.options.paths ?? []);
}
log(types.LogLevelLabel.DEBUG, "Prepare Storm Stack project...");
await engine.prepare(inlineConfig);
}
chunkA43FUIC2_cjs.__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
});
}
chunkA43FUIC2_cjs.__name(resolveId, "resolveId");
async function load(id) {
return handleLoad(engine.context, {
id
});
}
chunkA43FUIC2_cjs.__name(load, "load");
async function transform(code, id) {
return engine.context.compiler.getResult(chunkD7HZX3MP_cjs.getSourceFile(id, code), await engine.context.compiler.compile(engine.context, id, code));
}
chunkA43FUIC2_cjs.__name(transform, "transform");
async function writeBundle() {
log(types.LogLevelLabel.DEBUG, "Finalizing Storm Stack project output...");
await engine.finalize();
}
chunkA43FUIC2_cjs.__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(types.LogLevelLabel.FATAL, error?.message);
throw error;
}
};
}
chunkA43FUIC2_cjs.__name(createUnpluginFactory, "createUnpluginFactory");
exports.createUnpluginFactory = createUnpluginFactory;
;