Skip to content

Commit

Permalink
Move scripts to a separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed May 28, 2020
1 parent 401a95b commit 188fe1c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -34,10 +34,10 @@
],
"scripts": {
"build:cjs": "wasm-pack build --release --target nodejs -d nodejs",
"build:esm": "node ./createMJS.js",
"build:esm": "node ./scripts/createMJS.js",
"build:web": "wasm-pack build --release --target web -d web",
"build:all": "npm run build:cjs && npm run build:esm && npm run build:web",
"build": "npm run build:all && node ./updatePackage.json.js",
"build": "npm run build:all && node ./scripts/updatePackageExports.js",
"preversion": "echo 'Version number is set in Cargo.toml.' && exit 1",
"test": "node tests/cjs.js && node tests/esm.mjs && deno test --allow-read tests/deno.ts"
},
Expand Down
12 changes: 9 additions & 3 deletions createMJS.js → scripts/createMJS.js
@@ -1,12 +1,18 @@
#!/usr/bin/env node
"use strict";

const fs = require("fs");
const path = require("path");

const PACKAGE_ROOT = path.join(__dirname, "..", "nodejs");

const pkgPackageConfigFile = "./nodejs/package.json";
const pkgPackageConfigFile = path.join(PACKAGE_ROOT, "package.json");
const pkg = require(pkgPackageConfigFile);

const { main } = pkg;
const esmMain = main.replace(/\.js$/, ".mjs");

const cjs = fs.readFileSync(`./nodejs/${main}`, "utf8");
const cjs = fs.readFileSync(join(PACKAGE_ROOT, main), "utf8");

const esm =
"const module = {exports:{}};" +
Expand Down Expand Up @@ -37,7 +43,7 @@ function* getExports(input) {
}
}

fs.writeFileSync(`./nodejs/${esmMain}`, esm);
fs.writeFileSync(path.join(PACKAGE_ROOT, esmMain), esm);

// Mutate package.json
pkg.module = esmMain;
Expand Down
13 changes: 7 additions & 6 deletions updatePackage.json.js → scripts/updatePackageExports.js
@@ -1,10 +1,11 @@
#!/usr/bin/env node
"use strict";

const fs = require("fs");
const pkg = require("./package.json");
const pkg = require("../package.json");

const getFilesToDist = (folderName) =>
require(`./${folderName}/package.json`).files.map(
require(`../${folderName}/package.json`).files.map(
(filePath) => `${folderName}/${filePath}`
);

Expand All @@ -15,17 +16,17 @@ pkg.files = [
];
pkg.exports = {
".": {
require: `./nodejs/${require("./nodejs/package.json").main}`,
import: `./nodejs/${require("./nodejs/package.json").module}`,
browser: `./web/${require("./web/package.json").module}`,
require: `./nodejs/${require("../nodejs/package.json").main}`,
import: `./nodejs/${require("../nodejs/package.json").module}`,
browser: `./web/${require("../web/package.json").module}`,
},
};
pkg.main = pkg.exports["."].require;
pkg.module = pkg.exports["."].browser;
pkg.browser = pkg.exports["."].browser;

fs.writeFileSync(
require.resolve("./package.json"),
require.resolve("../package.json"),
JSON.stringify(pkg, null, 2) + "\n"
);

Expand Down

0 comments on commit 188fe1c

Please sign in to comment.