@soleil-se/build-app
Version:
Script for building WebApps, RESTApps and Widgets with Svelte in Sitevision.
58 lines (51 loc) • 1.46 kB
JavaScript
import { camelCase } from 'lodash-es';
export default function sitevision({ context } = {}) {
const ignored = [];
function isSitevisionScope(id) {
return id.startsWith('@sitevision/api');
}
function shouldResolve(importee) {
if (isSitevisionScope(importee)) {
return false;
}
return true;
}
function shouldLoad(id) {
return !ignored.includes(id);
}
function getVirtualModule(pkg) {
if (context === 'server') {
const variable = camelCase(pkg);
return `var ${variable} = require('${pkg}');\nexport default ${variable};`;
} if (context === 'client') {
return `export { default } from '${pkg}';`;
}
throw new Error('Invalid context, must be either "server" or "client".');
}
return {
name: 'rollup-plugin-sitevision',
resolveId(importee) {
if (shouldResolve(importee)) {
// Should be handled as usual.
return null;
}
// Most likely a Sitevision dependency.
// Do not ask other plugins or check the file system to find it.
if (!ignored.includes(importee)) {
ignored.push(importee);
}
return importee;
},
load(id) {
if (shouldLoad(id)) {
return null;
}
// The virtual source code for the provided Sitevision dependency.
const pkg = id.split('/').pop();
return {
code: getVirtualModule(pkg),
syntheticNamedExports: true,
};
},
};
}