Skip to content

Commit

Permalink
Add way to test whether SDK works in ESM and CommonJS
Browse files Browse the repository at this point in the history
  • Loading branch information
MadLittleMods committed Apr 6, 2022
1 parent dd06d78 commit 2401b7f
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -11,6 +11,7 @@
"lint-ci": "eslint src/",
"test": "impunity --entry-point src/platform/web/main.js src/platform/web/Platform.js --force-esm-dirs lib/ src/ --root-dir src/",
"test:postcss": "impunity --entry-point scripts/postcss/test.js ",
"test:sdk": "cd ./scripts/sdk/test/ && yarn --no-lockfile && node test-sdk-in-esm-vite-build-env.js && node test-sdk-in-commonjs-env.js",
"start": "vite --port 3000",
"build": "vite build",
"build:sdk": "./scripts/sdk/build.sh",
Expand Down
3 changes: 3 additions & 0 deletions scripts/sdk/test/.gitignore
@@ -0,0 +1,3 @@
node_modules
dist
yarn.lock
2 changes: 2 additions & 0 deletions scripts/sdk/test/deps.d.ts
@@ -0,0 +1,2 @@
// Keep TypeScripts from complaining about hydrogen-view-sdk not having types yet
declare module "hydrogen-view-sdk";
21 changes: 21 additions & 0 deletions scripts/sdk/test/esm-entry.ts
@@ -0,0 +1,21 @@
import * as hydrogenViewSdk from "hydrogen-view-sdk";
import downloadSandboxPath from 'hydrogen-view-sdk/download-sandbox.html?url';
import workerPath from 'hydrogen-view-sdk/main.js?url';
import olmWasmPath from '@matrix-org/olm/olm.wasm?url';
import olmJsPath from '@matrix-org/olm/olm.js?url';
import olmLegacyJsPath from '@matrix-org/olm/olm_legacy.js?url';
const assetPaths = {
downloadSandbox: downloadSandboxPath,
worker: workerPath,
olm: {
wasm: olmWasmPath,
legacyBundle: olmLegacyJsPath,
wasmBundle: olmJsPath
}
};
import "hydrogen-view-sdk/style.css";

console.log('hydrogenViewSdk', hydrogenViewSdk);
console.log('assetPaths', assetPaths);

console.log('Entry ESM works ✅');
12 changes: 12 additions & 0 deletions scripts/sdk/test/index.html
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>
<body>
<div id="app" class="hydrogen"></div>
<script type="module" src="./esm-entry.ts"></script>
</body>
</html>
8 changes: 8 additions & 0 deletions scripts/sdk/test/package.json
@@ -0,0 +1,8 @@
{
"name": "test-sdk",
"version": "0.0.0",
"description": "",
"dependencies": {
"hydrogen-view-sdk": "link:../../../target"
}
}
13 changes: 13 additions & 0 deletions scripts/sdk/test/test-sdk-in-commonjs-env.js
@@ -0,0 +1,13 @@
// Make sure the SDK can be used in a CommonJS environment.
// Usage: node scripts/sdk/test/test-sdk-in-commonjs-env.js
const hydrogenViewSdk = require('hydrogen-view-sdk');

// Test that the "exports" are available:
// Worker
require.resolve('hydrogen-view-sdk/main.js');
// Styles
require.resolve('hydrogen-view-sdk/style.css');
// Can access files in the assets/* directory
require.resolve('hydrogen-view-sdk/assets/main.js');

console.log('SDK works in CommonJS ✅');
19 changes: 19 additions & 0 deletions scripts/sdk/test/test-sdk-in-esm-vite-build-env.js
@@ -0,0 +1,19 @@
const { resolve } = require('path');
const { build } = require('vite');

async function main() {
await build({
outDir: './dist',
build: {
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html')
}
}
}
});

console.log('SDK works in Vite build ✅');
}

main();

0 comments on commit 2401b7f

Please sign in to comment.