Skip to content

Commit

Permalink
Add support for CJS projects (#2487)
Browse files Browse the repository at this point in the history
* Add support for CJS projects

* Add Changelog entry

* Add Changelog entry

* minor fixes to package.json

* replace yarn install v1.22.15
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.02s. with npm <command>

Usage:

npm install        install all the dependencies in your project
npm install <foo>  add the <foo> dependency to your project
npm test           run this project's tests
npm run <foo>      run the script named <foo>
npm <command> -h   quick help on <command>
npm -l             display usage info for all commands
npm help <term>    search for help on <term>
npm help npm       more involved overview

All commands:

    access, adduser, audit, bin, bugs, cache, ci, completion,
    config, dedupe, deprecate, diff, dist-tag, docs, doctor,
    edit, exec, explain, explore, find-dupes, fund, get, help,
    hook, init, install, install-ci-test, install-test, link,
    ll, login, logout, ls, org, outdated, owner, pack, ping,
    pkg, prefix, profile, prune, publish, rebuild, repo,
    restart, root, run-script, search, set, set-script,
    shrinkwrap, star, stars, start, stop, team, test, token,
    uninstall, unpublish, unstar, update, version, view, whoami

Specify configs in the ini-formatted file:
    /home/wirednkod/.npmrc
or on the command line via: npm <command> --key=value

More configuration info: npm help config
Configuration fields: npm help 7 config

[email protected] /home/wirednkod/.nvm/versions/node/v17.6.0/lib/node_modules/npm

* replace yarn install v1.22.15
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 0.02s. with npm <command>

Usage:

npm install        install all the dependencies in your project
npm install <foo>  add the <foo> dependency to your project
npm test           run this project's tests
npm run <foo>      run the script named <foo>
npm <command> -h   quick help on <command>
npm -l             display usage info for all commands
npm help <term>    search for help on <term>
npm help npm       more involved overview

All commands:

    access, adduser, audit, bin, bugs, cache, ci, completion,
    config, dedupe, deprecate, diff, dist-tag, docs, doctor,
    edit, exec, explain, explore, find-dupes, fund, get, help,
    hook, init, install, install-ci-test, install-test, link,
    ll, login, logout, ls, org, outdated, owner, pack, ping,
    pkg, prefix, profile, prune, publish, rebuild, repo,
    restart, root, run-script, search, set, set-script,
    shrinkwrap, star, stars, start, stop, team, test, token,
    uninstall, unpublish, unstar, update, version, view, whoami

Specify configs in the ini-formatted file:
    /home/wirednkod/.npmrc
or on the command line via: npm <command> --key=value

More configuration info: npm help config
Configuration fields: npm help 7 config

[email protected] /home/wirednkod/.nvm/versions/node/v17.6.0/lib/node_modules/npm

* replace yarn install v1.22.15
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.02s. with npm <command>

Usage:

npm install        install all the dependencies in your project
npm install <foo>  add the <foo> dependency to your project
npm test           run this project's tests
npm run <foo>      run the script named <foo>
npm <command> -h   quick help on <command>
npm -l             display usage info for all commands
npm help <term>    search for help on <term>
npm help npm       more involved overview

All commands:

    access, adduser, audit, bin, bugs, cache, ci, completion,
    config, dedupe, deprecate, diff, dist-tag, docs, doctor,
    edit, exec, explain, explore, find-dupes, fund, get, help,
    hook, init, install, install-ci-test, install-test, link,
    ll, login, logout, ls, org, outdated, owner, pack, ping,
    pkg, prefix, profile, prune, publish, rebuild, repo,
    restart, root, run-script, search, set, set-script,
    shrinkwrap, star, stars, start, stop, team, test, token,
    uninstall, unpublish, unstar, update, version, view, whoami

Specify configs in the ini-formatted file:
    /home/wirednkod/.npmrc
or on the command line via: npm <command> --key=value

More configuration info: npm help config
Configuration fields: npm help 7 config

[email protected] /home/wirednkod/.nvm/versions/node/v17.6.0/lib/node_modules/npm

* remove erroneous files

* remove 'extra' demo and fix pacakge.json accordingly

* remove sed

* Update bin/wasm-node/javascript/package.json

Co-authored-by: Pierre Krieger <[email protected]>

* Update bin/wasm-node/javascript/package.json

Co-authored-by: Pierre Krieger <[email protected]>

* Update bin/wasm-node/javascript/tsconfig-cjs.json

Co-authored-by: Pierre Krieger <[email protected]>

* Address PR comments

* Fix Changelog

* Fix Changelog

* remove from gitignore fix-package-type file

* Fix tests; add module in package json in order to avoid renaming to mjs all files; revert prepare extension from mjs to js

* remove type module and rename files to mjs

* Remove yarn lock

Co-authored-by: Pierre Krieger <[email protected]>
  • Loading branch information
wirednkod and tomaka authored Jul 13, 2022
1 parent 14fdcbf commit f27ec82
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 21 deletions.
4 changes: 4 additions & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added

- Add support for CommonJS projects. ([#2487](https://github.com/paritytech/smoldot/pull/2487))

### Changed

- No WebWorker/worker thread is spawned anymore by the JavaScript code. The WebAssembly virtual machine that runs smoldot is now directly instantiated by the `start` function. This should fix compatibility issues with various JavaScript bundlers. ([#2498](https://github.com/paritytech/smoldot/pull/2498))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// This file launches a WebSocket server that exposes JSON-RPC functions.

import * as smoldot from '../dist/index.js';
import * as smoldot from '../dist/mjs/index.js';
import { default as websocket } from 'websocket';
import * as http from 'node:http';
import * as process from 'node:process';
Expand Down
16 changes: 16 additions & 0 deletions bin/wasm-node/javascript/fix-package-type.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
# Adds package.json files to cjs/mjs subtrees with the respective type needed for
# the target environment.
#

cat >dist/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF

cat >dist/mjs/package.json <<!EOF
{
"type": "module"
}
!EOF
2 changes: 2 additions & 0 deletions bin/wasm-node/javascript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions bin/wasm-node/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,25 @@
"url": "https://github.com/paritytech/smoldot/issues"
},
"files": ["dist"],
"type": "module",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"main": "dist/cjs/index.js",
"types": "dist/mjs/index.d.ts",
"module": "dist/mjs/index.js",
"exports": {
".": {
"import": "./dist/mjs/index.js",
"require": "./dist/cjs/index.js"
}
},
"scripts": {
"prepublishOnly": "node prepare.js --release && rimraf ./dist && tsc",
"build": "node prepare.js --release && rimraf ./dist && tsc",
"start": "node prepare.js --debug && rimraf ./dist && tsc && node demo/demo.js",
"test": "node prepare.js --debug && rimraf ./dist && tsc && ava --timeout=2m --concurrency 2 --no-worker-threads"
"buildModules": "tsc -p tsconfig-mjs.json && tsc -p tsconfig-cjs.json && bash fix-package-type.sh",
"prepublishOnly": "node prepare.mjs --release && rimraf ./dist && npm run buildModules",
"build": "node prepare.mjs --release && rimraf ./dist && npm run buildModules",
"start": "node prepare.mjs --debug && rimraf ./dist && npm run buildModules && node demo/demo.mjs",
"test": "node prepare.mjs --debug && rimraf ./dist && npm run buildModules && ava --timeout=2m --concurrency 2 --no-worker-threads"
},
"browser": {
"./dist/compat/index.js": "./dist/compat/index-browser-overwrite.js"
"./dist/mjs/compat/index.js": "./dist/mjs/compat/index-browser-overwrite.js",
"./dist/cjs/compat/index.js": "./dist/cjs/compat/index-browser-overwrite.js"
},
"dependencies": {
"buffer": "^6.0.1",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import test from 'ava';
import * as fs from 'node:fs';
import { start } from "../dist/index.js";
import { start } from "../dist/mjs/index.js";

const westendSpec = fs.readFileSync('./test/westend.json', 'utf8');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import test from 'ava';
import * as fs from 'node:fs';
import { start } from "../dist/index.js";
import { start } from "../dist/mjs/index.js";

const westendSpec = fs.readFileSync('./test/westend.json', 'utf8');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import test from 'ava';
import * as fs from 'node:fs';
import { start } from "../dist/index.js";
import { start } from "../dist/mjs/index.js";

const westendSpec = fs.readFileSync('./test/westend.json', 'utf8');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import test from 'ava';
import * as fs from 'node:fs';
import { start } from "../dist/index.js";
import { start } from "../dist/mjs/index.js";

const westendSpec = fs.readFileSync('./test/westend.json', 'utf8');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import test from 'ava';
import * as fs from 'node:fs';
import { start } from "../dist/index.js";
import { start } from "../dist/mjs/index.js";

const westendSpec = fs.readFileSync('./test/westend.json', 'utf8');

Expand Down
11 changes: 11 additions & 0 deletions bin/wasm-node/javascript/tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"lib": [
"es2015"
],
"target": "es2015",
"module": "commonjs",
"outDir": "./dist/cjs",
},
}
11 changes: 11 additions & 0 deletions bin/wasm-node/javascript/tsconfig-mjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"lib": [
"es2020"
],
"target": "es2020",
"module": "es2020",
"outDir": "./dist/mjs",
}
}
8 changes: 1 addition & 7 deletions bin/wasm-node/javascript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
{
"compilerOptions": {
"lib": [
"es2020"
],
"target": "es2020",
"module": "es2020",
"outDir": "./dist",
"declaration": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,

"esModuleInterop": true,
"strict": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
Expand Down

0 comments on commit f27ec82

Please sign in to comment.