Skip to content

Commit

Permalink
Merge pull request #154 from zama-ai/update-bundle-06
Browse files Browse the repository at this point in the history
[release] Update bundle 06
  • Loading branch information
immortal-tofu authored Dec 16, 2024
2 parents f40b193 + 2a3765f commit fc94ece
Show file tree
Hide file tree
Showing 12 changed files with 2,597 additions and 580 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/cdn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
release:
types: [released]

jobs:
publish:
permissions:
id-token: 'write'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20.x
- run: npm ci
- run: npm test
- run: BASE_PATH=https://cdn.zama.ai/fhevmjs/$(node -p "require('./package.json').version")/ npm run build
# todo push on S3
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
!lib/**/*
!web.d.ts
!web.js
!bundle.d.ts
!bundle.js
!node.d.ts
!node.js
!package.json
Expand Down
1 change: 1 addition & 0 deletions bundle.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/web';
12 changes: 12 additions & 0 deletions bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const waitForFunction =
(functionName) =>
async (...params) => {
if (window && window.fhevmjs) {
return window.fhevmjs[functionName](...params);
}
};

const initFhevm = waitForFunction('initFhevm');
const createInstance = waitForFunction('createInstance');

export { initFhevm, createInstance };
11 changes: 4 additions & 7 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import OMT from '@surma/rollup-plugin-off-main-thread';
import copy from 'rollup-plugin-copy';
import json from '@rollup/plugin-json';
import url from '@rollup/plugin-url';
import { wasm } from '@rollup/plugin-wasm';
import typescript from '@rollup/plugin-typescript';
import replace from '@rollup/plugin-replace';
import resolve from '@rollup/plugin-node-resolve';
Expand All @@ -18,7 +17,6 @@ const wasmBindgenRayon = fs.readdirSync(

const nodePlugins = [
json(),
wasm(),
commonjs(),
typescript({
tsconfig: './tsconfig.rollup.json',
Expand All @@ -38,10 +36,6 @@ const webPlugins = [
tsconfig: './tsconfig.rollup.json',
exclude: 'node_modules/**',
}),
wasm({
targetEnv: 'browser',
maxFileSize: 10000000,
}),
commonjs(),
resolve({
browser: true,
Expand All @@ -61,7 +55,10 @@ export default [
plugins: [
...webPlugins,
copy({
targets: [{ src: 'node_modules/tfhe/tfhe_bg.wasm', dest: 'lib/' }],
targets: [
{ src: 'node_modules/tfhe/tfhe_bg.wasm', dest: 'lib/' },
{ src: 'node_modules/tkms/kms_lib_bg.wasm', dest: 'lib/' },
],
}),
],
},
Expand Down
45 changes: 45 additions & 0 deletions config/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import { ignoreURL, changeLoadingWorker } from './vite.replace';

const basePath = process.env.BASE_PATH || '/';

// https://vitejs.dev/config/
export default defineConfig({
base: basePath,
build: {
lib: {
name: 'fhevmjs',
fileName: 'fhevmjs',
entry: ['lib/web.js'],
},
outDir: 'bundle',
},
plugins: [
changeLoadingWorker(basePath),
ignoreURL(basePath),
nodePolyfills(),
viteStaticCopy({
targets: [
{
src: 'lib/tfhe_bg.wasm',
dest: '.',
},
{
src: 'lib/kms_lib_bg.wasm',
dest: '.',
},
],
}),
],
worker: {
format: 'iife',
plugins: [ignoreURL(basePath)],
rollupOptions: {
output: {
entryFileNames: '[name].js',
},
},
},
});
70 changes: 70 additions & 0 deletions config/vite.replace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
export const changeLoadingWorker = (basePath) => ({
name: 'regex-replace', // name of the plugin
enforce: 'pre', // run before other plugins
transform(code, id) {
// Only apply transformations to .js files (or other specific conditions)
if (id.endsWith('.js')) {
const searchValue =
/const worker = new Worker\(\s*new URL\(['"]\.?\/?workerHelpers\.worker\.js['"],\s*import\.meta\.url\),\s*\{\s*type:\s*'module',?\s*\},?\s*\);/;

const replacement = `let worker;
try {
worker = new Worker(
new URL('./workerHelpers.worker.js', import.meta.url),
{
type: 'module'
}
);
} catch (e) {
const r = await fetch('${basePath}workerHelpers.worker.js');
const blob = await r.blob();
const blobUrl = URL.createObjectURL(blob);
worker = new Worker(blobUrl);
}`;

// Check that the worker change works.
if (id.match('lib/web.js')) {
const match = code.match(searchValue);
if (!match)
throw new Error(
'Impossible to replace Worker with iife implementation. Source code (lib/web.js) changed.',
);
}

// Replace occurrences according to the regex pattern
const newCode = code.replace(searchValue, replacement);

return {
code: newCode,
map: null, // provide source map if available or required
};
}
// Return null to signify no transformation for non-JS files or uninterested files
return null;
},
});

const wasmPattern = /'([a-zA-Z0-9_]+)\.wasm'/g;

export const ignoreURL = (basePath) => ({
name: 'regex-replace', // name of the plugin
enforce: 'pre', // run before other plugins
transform(code, id) {
// Only apply transformations to .js files (or other specific conditions)
if (id.endsWith('.js')) {
const pattern = wasmPattern; // Your regex pattern here
const replacement = `/* @vite-ignore */ '${basePath}$1.wasm'`; // Your replacement string here

// Replace occurrences according to the regex pattern
// const newCode = code.replace(pattern, replacement);
const newCode = code.replace(wasmPattern, replacement);

return {
code: newCode,
map: null, // provide source map if available or required
};
}
// Return null to signify no transformation for non-JS files or uninterested files
return null;
},
});
79 changes: 0 additions & 79 deletions config/webpack.config.cjs

This file was deleted.

Loading

0 comments on commit fc94ece

Please sign in to comment.