Add support for esm and cjs modules

This commit is contained in:
Sebastian Miasojed
2024-11-25 10:25:15 +01:00
parent 892c9b5fbe
commit 4e024655a1
14 changed files with 149 additions and 157 deletions
View File
@@ -14,39 +14,11 @@ mergeInto(LibraryManager.library, {
var inputJson = UTF8ToString(inputPtr, inputLen);
// Inline worker code
const workerCode = `
// worker.js
// nodejs version
const { parentPort } = require('worker_threads');
parentPort.on('message', async (inputJson) => {
const { default: ModuleFactory } = await import('./resolc.js');
const newModule = await ModuleFactory();
// Create a virtual file for stdin
newModule.FS.writeFile('/in', inputJson);
// Call main on the new instance
const output = newModule.callMain(['--recursive-process']);
// Check the /err file content
const errorMessage = newModule.FS.readFile('/err', { encoding: 'utf8' });
if (errorMessage.length > 0) {
// If /err is not empty, throw an error with its content
throw new Error(errorMessage);
} else {
// If no error, read the output file
let outputFile = newModule.FS.readFile('/out', { encoding: 'utf8' });
parentPort.postMessage({ output: outputFile });
}
});`;
function compileWithWorker(inputJson, callback) {
return new Promise((resolve, reject) => {
// Create a new Worker
const worker = new Worker(workerCode, { eval: true });
const worker = new Worker(new URL('./worker.js', import.meta.url), {
type: 'module',
});
// Listen for messages from the worker
worker.on('message', (message) => {
+10 -2
View File
@@ -9,7 +9,15 @@
"solc": "^0.8.28"
},
"scripts": {
"init": "cp -r ../target/wasm32-unknown-emscripten/release/resolc.{js,wasm} .",
"test": "npm run init && node run_revive.js"
"build": "cp -r ../target/wasm32-unknown-emscripten/release/resolc.{js,wasm} ./src && npx rollup -c",
"test": "npm run build && node run_revive.js"
},
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/preset-env": "^7.26.0",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-node-resolve": "^15.3.0",
"rollup": "^4.27.3",
"rollup-plugin-copy": "^3.5.0"
}
}
+35
View File
@@ -0,0 +1,35 @@
import babel from '@rollup/plugin-babel';
import copy from 'rollup-plugin-copy';
import resolve from '@rollup/plugin-node-resolve';
const outputDirCJS = 'dist/revive-cjs';
const outputDirESM = 'dist/revive-esm';
export default {
input: ['src/resolc.js', 'src/worker.js'], // Adjust this to your main entry file
output: [
{
dir: outputDirCJS,
format: 'cjs',
exports: 'auto',
},
{
dir: outputDirESM,
format: 'esm',
},
],
plugins: [
babel({
exclude: 'node_modules/**',
presets: ['@babel/preset-env'],
babelHelpers: 'inline',
}),
resolve(),
copy({
targets: [
{ src: 'src/resolc.wasm', dest: outputDirCJS },
{ src: 'src/resolc.wasm', dest: outputDirESM },
],
})
],
};
+1 -1
View File
@@ -1,6 +1,6 @@
import solc from 'solc';
// Import the Emscripten module
import createRevive from './resolc.js';
import createRevive from './dist/revive-esm/resolc.js';
const compilerStandardJsonInput = {
language: 'Solidity',
+32
View File
@@ -0,0 +1,32 @@
import { parentPort } from 'worker_threads';
parentPort.on('message', async (inputJson) => {
const { default: createRevive } = await import(new URL('./resolc.js', import.meta.url));
const revive = await createRevive();
revive.setStdinData(inputJson);
let stdoutString = "";
revive.setStdoutCallback(function(char) {
if (char.charCodeAt(0) === '\n') {
console.log("new line")
exit
}
stdoutString += char;
});
let stderrString = "";
revive.setStderrCallback(function(char) {
stderrString += char;
});
// Call main on the new instance
const output = revive.callMain(['--recursive-process']);
if (stderrString.length > 0) {
// If /err is not empty, throw an error with its content
throw new Error(stderrString);
} else {
parentPort.postMessage({ output: stdoutString });
}
});