Skip to content

Commit

Permalink
convert NPM package to CommonJS (#515)
Browse files Browse the repository at this point in the history
switch over the NPM package to use CommonJS modules instead of ES
modules, to allow to be used from CommonJS packages.
  • Loading branch information
OmarTawfik authored Jul 6, 2023
1 parent 21b473d commit f24e873
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-planes-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"changelog": minor
---

switch over the NPM package to use CommonJS modules instead of ES modules.
13 changes: 13 additions & 0 deletions crates/solidity/outputs/npm/package/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* __SLANG_PUBLIC_API_SYNC__ (please keep in sync across all other instances)
*/

"use strict";

const language = require("./language");
const syntax = require("./syntax");

module.exports = Object.freeze({
language,
syntax,
});
11 changes: 11 additions & 0 deletions crates/solidity/outputs/npm/package/language/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* __SLANG_PUBLIC_API_SYNC__ (please keep in sync across all other instances)
*/

"use strict";

const generated = require("../generated");

module.exports = Object.freeze({
Language: generated.Language,
});
13 changes: 3 additions & 10 deletions crates/solidity/outputs/npm/package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,14 @@
"email": "omar@nomic.foundation"
}
],
"type": "commonjs",
"files": [
"src/**/*.d.ts",
"src/**/*.js",
"**/*.d.ts",
"**/*.js",
"CHANGELOG.md",
"LICENSE",
"README.md"
],
"__exports_comment__": "__SLANG_PUBLIC_API_SYNC__ (please keep in sync across all other instances)",
"exports": {
".": "./src/index.js",
"./language": "./src/language/index.js",
"./syntax": "./src/syntax/index.js",
"./syntax/nodes": "./src/syntax/nodes/index.js",
"./syntax/parser": "./src/syntax/parser/index.js"
},
"devDependencies": {
"@napi-rs/cli": "2.16.1",
"prettier": "2.8.8"
Expand Down
13 changes: 13 additions & 0 deletions crates/solidity/outputs/npm/package/syntax/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* __SLANG_PUBLIC_API_SYNC__ (please keep in sync across all other instances)
*/

"use strict";

const nodes = require("./nodes");
const parser = require("./parser");

module.exports = Object.freeze({
nodes,
parser,
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* __SLANG_PUBLIC_API_SYNC__ (please keep in sync across all other instances)
*/

export { NodeType, RuleKind, RuleNode, TokenNode, TokenKind } from "../../generated";
export { NodeType, RuleKind, RuleNode, TokenKind, TokenNode } from "../../generated";
15 changes: 15 additions & 0 deletions crates/solidity/outputs/npm/package/syntax/nodes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* __SLANG_PUBLIC_API_SYNC__ (please keep in sync across all other instances)
*/

"use strict";

const generated = require("../../generated");

module.exports = Object.freeze({
NodeType: generated.NodeType,
RuleKind: generated.RuleKind,
RuleNode: generated.RuleNode,
TokenKind: generated.TokenKind,
TokenNode: generated.TokenNode,
});
13 changes: 13 additions & 0 deletions crates/solidity/outputs/npm/package/syntax/parser/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* __SLANG_PUBLIC_API_SYNC__ (please keep in sync across all other instances)
*/

"use strict";

const generated = require("../../generated");

module.exports = Object.freeze({
ParseError: generated.ParseError,
ParseOutput: generated.ParseOutput,
ProductionKind: generated.ProductionKind,
});
12 changes: 6 additions & 6 deletions scripts/npm/napi/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ function _napi_build() {
fi

# Navigate to where files should be generated:
cd "$PACKAGE_DIR/src/generated"
cd "$PACKAGE_DIR/generated"

command=(
napi build
--platform
--config "../../package.json"
--cargo-cwd "../../../crate"
--config "../package.json"
--cargo-cwd "../../crate"
--target "$target"
--no-const-enum
)
Expand All @@ -55,8 +55,8 @@ function _process_generated_files() {
printf "\n\n📚 Processing Generated Files: 📚\n\n\n"

generated_files=(
"$PACKAGE_DIR/src/generated/index.d.ts"
"$PACKAGE_DIR/src/generated/index.js"
"$PACKAGE_DIR/generated/index.d.ts"
"$PACKAGE_DIR/generated/index.js"
)

for file in "${generated_files[@]}"; do
Expand All @@ -80,7 +80,7 @@ function _process_bindings() {
# Populate artifacts folder with all binding files:

mkdir -p "$PACKAGE_DIR/artifacts"
artifacts="$(_list_source_files "$PACKAGE_DIR/src/generated" "*.node")"
artifacts="$(_list_source_files "$PACKAGE_DIR/generated" "*.node")"

echo "$artifacts" | while read -r artifact; do
cp "$artifact" "$PACKAGE_DIR/artifacts"
Expand Down

0 comments on commit f24e873

Please sign in to comment.