Skip to content

Commit

Permalink
sdk: rollup fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Dec 1, 2024
1 parent 4df0eec commit 339c934
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 43 deletions.
144 changes: 102 additions & 42 deletions sdk/rollup.nodejs.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,110 @@ import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import virtual from '@rollup/plugin-virtual';
import { defineConfig } from 'rollup';
import fs from 'fs';
import path from 'path';

const config = defineConfig({
input: 'src/entry.ts',
output: {
sourcemap: true,
file: `${process.env.NODE_ENV === 'production' ? 'dist' : 'out'}/main.nodejs.js`,
format: 'module',
banner: `//# sourceURL=/plugin/main.nodejs.js`,
},
external: [
'unifi-protect',
],

plugins: [
virtual({
'src/entry.ts':
// replace createRequire to force rollup.
function replaceCreateRequire() {
return {
name: 'replace-create-require',
transform(code, id) {
const packageRequireRegex = /const\s+.*?\s*=\s*createRequire.*?;/;
if (packageRequireRegex.test(code)) {
return {
code: code.replace(
packageRequireRegex,
'',
),
map: null,
};
}
return null;
}
};
}

const cwd = process.cwd();
const packageJson = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json').toString()));
const external = Object.keys(packageJson.optionalDependencies || {});
const tsconfig = JSON.parse(fs.readFileSync(path.join(cwd, 'tsconfig.json').toString()));


const defaultMainNodeJs = 'main.nodejs.js';
const entries = [];
if (packageJson.exports) {
for (const [key, value] of Object.entries(packageJson.exports)) {
entries.push({
filename: key,
output: value,
});
}
}
else {
if (fs.existsSync('./src/main.ts')) {
entries.push({
filename: './src/main.ts',
output: defaultMainNodeJs,
});
}
}

if (!entries?.length)
throw new Error('unable to locate src/main.ts');

const config = defineConfig(
entries.map(entry => ({
input: `${entry.filename.slice(0, -3)}.nodejs.ts`,
output: {
strict: false,
sourcemap: true,
preserveModules: false,

inlineDynamicImports:true,
file: `${process.env.NODE_ENV === 'production' ? 'dist' : 'out'}/${entry.output}`,
// dir: `${process.env.NODE_ENV === 'production' ? 'dist' : 'out'}`,

format: packageJson.type === 'module' ? 'esm' : 'commonjs',
// necessary for es module since it is loaded from a file.
// no harm having this for commonjs since this is the same path server uses.
banner: (entry) => {
return `//# sourceURL=/plugin/${entry.name}.js
`
export * from './src/main.ts';
export { default } from './src/main.ts';
`,
}),
typescript({
target: 'es2021',
compilerOptions: {
moduleResolution: 'Node16',
module: "esnext",
strict: true,
sourceMap: true,
resolveJsonModule: true,
esModuleInterop: true,
allowSyntheticDefaultImports: true,
},
}),
commonjs({
// need ts extension so require calls in ts get resolved.
extensions: ['.js', '.ts'],
transformMixedEsModules: true,
}),
resolve({
browser: true,
preferBuiltins: false,
}),
json(),
]
});
},
external,

plugins: [
replaceCreateRequire(),
// use this to inject sdk init.
virtual({
[`${entry.filename.slice(0, -3)}.nodejs.ts`]:
`
export * from '${entry.filename}';
export { sdkInit } from '@scrypted/sdk';
` +
(!entry.filename.endsWith('main.ts')
? ''

: `
export { default } from '${entry.filename}';
`)
}),

typescript(tsconfig),
commonjs({
// need ts extension so require calls in ts get resolved.
extensions: ['.js', '.ts'],
transformMixedEsModules: true,
}),
resolve({
extensions: ['.js', '.ts'],
browser: false,
preferBuiltins: true,
}),
json(),
]
})),
);

export default config;
3 changes: 2 additions & 1 deletion sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ try {
mediaManager,
systemManager,
pluginHostAPI,
...runtimeAPI,
pluginRuntimeAPI: runtimeAPI,
});

try {
Expand All @@ -241,6 +241,7 @@ catch (e) {
export default sdk;

export function sdkInit(sdkInit: {
log: Logger,
deviceManager: DeviceManager,
endpointManager: EndpointManager,
mediaManager: MediaManager,
Expand Down

0 comments on commit 339c934

Please sign in to comment.