convert post-install script to CJS so we can use require.resolve

this allows us to find where npm/yarn has installed deps, as we can't
assume our own node_modules directory when hydrogen is installed as a
dependency
This commit is contained in:
Bruno Windels 2021-03-08 14:40:21 +01:00
parent 0c0a4011f7
commit 0052a11a1e
2 changed files with 24 additions and 18 deletions

View File

@ -10,7 +10,7 @@
"test": "node_modules/.bin/impunity --entry-point src/main.js --force-esm-dirs lib/ src/", "test": "node_modules/.bin/impunity --entry-point src/main.js --force-esm-dirs lib/ src/",
"start": "node scripts/serve-local.js", "start": "node scripts/serve-local.js",
"build": "node --experimental-modules scripts/build.mjs", "build": "node --experimental-modules scripts/build.mjs",
"postinstall": "node ./scripts/post-install.mjs" "postinstall": "node ./scripts/post-install.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -14,21 +14,28 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import fsRoot from "fs"; const fsRoot = require("fs");
const fs = fsRoot.promises; const fs = fsRoot.promises;
import path from "path"; const path = require("path");
import { rollup } from 'rollup'; const { rollup } = require('rollup');
import { fileURLToPath } from 'url'; const { fileURLToPath } = require('url');
import { dirname } from 'path'; const { dirname } = require('path');
// needed to translate commonjs modules to esm // needed to translate commonjs modules to esm
import commonjs from '@rollup/plugin-commonjs'; const commonjs = require('@rollup/plugin-commonjs');
import { nodeResolve } from '@rollup/plugin-node-resolve'; const { nodeResolve } = require('@rollup/plugin-node-resolve');
import {removeDirIfExists} from "./common.mjs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const projectDir = path.join(__dirname, "../"); const projectDir = path.join(__dirname, "../");
async function removeDirIfExists(targetDir) {
try {
await fs.rmdir(targetDir, {recursive: true});
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
}
}
/** function used to resolve common-js require calls below. */ /** function used to resolve common-js require calls below. */
function packageIterator(request, start, defaultIterator) { function packageIterator(request, start, defaultIterator) {
// this is just working for bs58, would need to tune it further for other dependencies // this is just working for bs58, would need to tune it further for other dependencies
@ -60,10 +67,9 @@ async function commonjsToESM(src, dst) {
async function populateLib() { async function populateLib() {
const libDir = path.join(projectDir, "lib/"); const libDir = path.join(projectDir, "lib/");
const modulesDir = path.join(projectDir, "node_modules/");
await removeDirIfExists(libDir); await removeDirIfExists(libDir);
await fs.mkdir(libDir); await fs.mkdir(libDir);
const olmSrcDir = path.join(modulesDir, "olm/"); const olmSrcDir = path.dirname(require.resolve("olm"));
const olmDstDir = path.join(libDir, "olm/"); const olmDstDir = path.join(libDir, "olm/");
await fs.mkdir(olmDstDir); await fs.mkdir(olmDstDir);
for (const file of ["olm.js", "olm.wasm", "olm_legacy.js"]) { for (const file of ["olm.js", "olm.wasm", "olm_legacy.js"]) {
@ -72,19 +78,19 @@ async function populateLib() {
// transpile another-json to esm // transpile another-json to esm
await fs.mkdir(path.join(libDir, "another-json/")); await fs.mkdir(path.join(libDir, "another-json/"));
await commonjsToESM( await commonjsToESM(
path.join(modulesDir, 'another-json/another-json.js'), require.resolve('another-json/another-json.js'),
path.join(libDir, "another-json/index.js") path.join(libDir, "another-json/index.js")
); );
// transpile bs58 to esm // transpile bs58 to esm
await fs.mkdir(path.join(libDir, "bs58/")); await fs.mkdir(path.join(libDir, "bs58/"));
await commonjsToESM( await commonjsToESM(
path.join(modulesDir, 'bs58/index.js'), require.resolve('bs58/index.js'),
path.join(libDir, "bs58/index.js") path.join(libDir, "bs58/index.js")
); );
// transpile base64-arraybuffer to esm // transpile base64-arraybuffer to esm
await fs.mkdir(path.join(libDir, "base64-arraybuffer/")); await fs.mkdir(path.join(libDir, "base64-arraybuffer/"));
await commonjsToESM( await commonjsToESM(
path.join(modulesDir, 'base64-arraybuffer/lib/base64-arraybuffer.js'), require.resolve('base64-arraybuffer/lib/base64-arraybuffer.js'),
path.join(libDir, "base64-arraybuffer/index.js") path.join(libDir, "base64-arraybuffer/index.js")
); );
// this probably should no go in here, we can just import "aes-js" from legacy-extras.js // this probably should no go in here, we can just import "aes-js" from legacy-extras.js
@ -93,7 +99,7 @@ async function populateLib() {
// transpile aesjs to esm // transpile aesjs to esm
await fs.mkdir(path.join(libDir, "aes-js/")); await fs.mkdir(path.join(libDir, "aes-js/"));
await commonjsToESM( await commonjsToESM(
path.join(modulesDir, 'aes-js/index.js'), require.resolve('aes-js/index.js'),
path.join(libDir, "aes-js/index.js") path.join(libDir, "aes-js/index.js")
); );
// es6-promise is already written as an es module, // es6-promise is already written as an es module,
@ -102,7 +108,7 @@ async function populateLib() {
// is conveniently not placed in node_modules rather than symlinking. // is conveniently not placed in node_modules rather than symlinking.
await fs.mkdir(path.join(libDir, "es6-promise/")); await fs.mkdir(path.join(libDir, "es6-promise/"));
await commonjsToESM( await commonjsToESM(
path.join(modulesDir, 'es6-promise/lib/es6-promise/promise.js'), require.resolve('es6-promise/lib/es6-promise/promise.js'),
path.join(libDir, "es6-promise/index.js") path.join(libDir, "es6-promise/index.js")
); );
} }