From f24e873a93cbcef53aad1fa5eed1ea9ab1af1c04 Mon Sep 17 00:00:00 2001 From: Omar Tawfik <15987992+OmarTawfik@users.noreply.github.com> Date: Thu, 6 Jul 2023 08:47:08 -0700 Subject: [PATCH] convert NPM package to CommonJS (#515) switch over the NPM package to use CommonJS modules instead of ES modules, to allow to be used from CommonJS packages. --- .changeset/real-planes-think.md | 5 +++++ .../npm/package/{src => }/generated/index.d.ts | 0 .../npm/package/{src => }/generated/index.js | 0 .../npm/package/{src/index.js => index.d.ts} | 0 crates/solidity/outputs/npm/package/index.js | 13 +++++++++++++ .../language/index.js => language/index.d.ts} | 0 .../outputs/npm/package/language/index.js | 11 +++++++++++ crates/solidity/outputs/npm/package/package.json | 13 +++---------- .../{src/syntax/index.js => syntax/index.d.ts} | 0 .../solidity/outputs/npm/package/syntax/index.js | 13 +++++++++++++ .../nodes/index.js => syntax/nodes/index.d.ts} | 2 +- .../outputs/npm/package/syntax/nodes/index.js | 15 +++++++++++++++ .../parser/index.js => syntax/parser/index.d.ts} | 0 .../outputs/npm/package/syntax/parser/index.js | 13 +++++++++++++ scripts/npm/napi/build.sh | 12 ++++++------ 15 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 .changeset/real-planes-think.md rename crates/solidity/outputs/npm/package/{src => }/generated/index.d.ts (100%) rename crates/solidity/outputs/npm/package/{src => }/generated/index.js (100%) rename crates/solidity/outputs/npm/package/{src/index.js => index.d.ts} (100%) create mode 100644 crates/solidity/outputs/npm/package/index.js rename crates/solidity/outputs/npm/package/{src/language/index.js => language/index.d.ts} (100%) create mode 100644 crates/solidity/outputs/npm/package/language/index.js rename crates/solidity/outputs/npm/package/{src/syntax/index.js => syntax/index.d.ts} (100%) create mode 100644 crates/solidity/outputs/npm/package/syntax/index.js rename crates/solidity/outputs/npm/package/{src/syntax/nodes/index.js => syntax/nodes/index.d.ts} (62%) create mode 100644 crates/solidity/outputs/npm/package/syntax/nodes/index.js rename crates/solidity/outputs/npm/package/{src/syntax/parser/index.js => syntax/parser/index.d.ts} (100%) create mode 100644 crates/solidity/outputs/npm/package/syntax/parser/index.js diff --git a/.changeset/real-planes-think.md b/.changeset/real-planes-think.md new file mode 100644 index 0000000000..0395062696 --- /dev/null +++ b/.changeset/real-planes-think.md @@ -0,0 +1,5 @@ +--- +"changelog": minor +--- + +switch over the NPM package to use CommonJS modules instead of ES modules. diff --git a/crates/solidity/outputs/npm/package/src/generated/index.d.ts b/crates/solidity/outputs/npm/package/generated/index.d.ts similarity index 100% rename from crates/solidity/outputs/npm/package/src/generated/index.d.ts rename to crates/solidity/outputs/npm/package/generated/index.d.ts diff --git a/crates/solidity/outputs/npm/package/src/generated/index.js b/crates/solidity/outputs/npm/package/generated/index.js similarity index 100% rename from crates/solidity/outputs/npm/package/src/generated/index.js rename to crates/solidity/outputs/npm/package/generated/index.js diff --git a/crates/solidity/outputs/npm/package/src/index.js b/crates/solidity/outputs/npm/package/index.d.ts similarity index 100% rename from crates/solidity/outputs/npm/package/src/index.js rename to crates/solidity/outputs/npm/package/index.d.ts diff --git a/crates/solidity/outputs/npm/package/index.js b/crates/solidity/outputs/npm/package/index.js new file mode 100644 index 0000000000..5e8a890c4f --- /dev/null +++ b/crates/solidity/outputs/npm/package/index.js @@ -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, +}); diff --git a/crates/solidity/outputs/npm/package/src/language/index.js b/crates/solidity/outputs/npm/package/language/index.d.ts similarity index 100% rename from crates/solidity/outputs/npm/package/src/language/index.js rename to crates/solidity/outputs/npm/package/language/index.d.ts diff --git a/crates/solidity/outputs/npm/package/language/index.js b/crates/solidity/outputs/npm/package/language/index.js new file mode 100644 index 0000000000..fc141eadba --- /dev/null +++ b/crates/solidity/outputs/npm/package/language/index.js @@ -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, +}); diff --git a/crates/solidity/outputs/npm/package/package.json b/crates/solidity/outputs/npm/package/package.json index 01369019cd..76f76b3cae 100644 --- a/crates/solidity/outputs/npm/package/package.json +++ b/crates/solidity/outputs/npm/package/package.json @@ -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" diff --git a/crates/solidity/outputs/npm/package/src/syntax/index.js b/crates/solidity/outputs/npm/package/syntax/index.d.ts similarity index 100% rename from crates/solidity/outputs/npm/package/src/syntax/index.js rename to crates/solidity/outputs/npm/package/syntax/index.d.ts diff --git a/crates/solidity/outputs/npm/package/syntax/index.js b/crates/solidity/outputs/npm/package/syntax/index.js new file mode 100644 index 0000000000..73210766fa --- /dev/null +++ b/crates/solidity/outputs/npm/package/syntax/index.js @@ -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, +}); diff --git a/crates/solidity/outputs/npm/package/src/syntax/nodes/index.js b/crates/solidity/outputs/npm/package/syntax/nodes/index.d.ts similarity index 62% rename from crates/solidity/outputs/npm/package/src/syntax/nodes/index.js rename to crates/solidity/outputs/npm/package/syntax/nodes/index.d.ts index 31192b3ae5..e5e8e16ab9 100644 --- a/crates/solidity/outputs/npm/package/src/syntax/nodes/index.js +++ b/crates/solidity/outputs/npm/package/syntax/nodes/index.d.ts @@ -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"; diff --git a/crates/solidity/outputs/npm/package/syntax/nodes/index.js b/crates/solidity/outputs/npm/package/syntax/nodes/index.js new file mode 100644 index 0000000000..859b682074 --- /dev/null +++ b/crates/solidity/outputs/npm/package/syntax/nodes/index.js @@ -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, +}); diff --git a/crates/solidity/outputs/npm/package/src/syntax/parser/index.js b/crates/solidity/outputs/npm/package/syntax/parser/index.d.ts similarity index 100% rename from crates/solidity/outputs/npm/package/src/syntax/parser/index.js rename to crates/solidity/outputs/npm/package/syntax/parser/index.d.ts diff --git a/crates/solidity/outputs/npm/package/syntax/parser/index.js b/crates/solidity/outputs/npm/package/syntax/parser/index.js new file mode 100644 index 0000000000..121319dccb --- /dev/null +++ b/crates/solidity/outputs/npm/package/syntax/parser/index.js @@ -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, +}); diff --git a/scripts/npm/napi/build.sh b/scripts/npm/napi/build.sh index e1c70a31cb..028104dfd8 100755 --- a/scripts/npm/napi/build.sh +++ b/scripts/npm/napi/build.sh @@ -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 ) @@ -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 @@ -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"