Skip to content

Commit

Permalink
scaffold common package
Browse files Browse the repository at this point in the history
  • Loading branch information
mistermoe authored and frankhinek committed Jun 13, 2023
1 parent e1c4700 commit d1e2d10
Show file tree
Hide file tree
Showing 17 changed files with 7,556 additions and 2,350 deletions.
9,534 changes: 7,192 additions & 2,342 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"workspaces": [
"packages/web5-agent",
"packages/credentials",
"packages/common",
"packages/crypto",
"packages/web5-agent",
"packages/dids",
"packages/web5",
"packages/credentials",
"packages/web5-user-agent",
"packages/web5-proxy-agent",
"packages/web5-user-agent"
"packages/web5"
],
"scripts": {
"clean": "npx npkill -d $(pwd) -t node_modules && npx npkill -d $(pwd) -t dist",
Expand Down
18 changes: 18 additions & 0 deletions packages/common/.c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"all": true,
"cache": false,
"extension": [
".js"
],
"include": [
"__tests__/src/**"
],
"exclude": [
"__tests__/src/main.js",
"__tests__/types/**"
],
"reporter": [
"cobertura",
"text"
]
}
5 changes: 5 additions & 0 deletions packages/common/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"enable-source-maps": true,
"exit": true,
"spec": ["__tests__/**/*.spec.js"]
}
55 changes: 55 additions & 0 deletions packages/common/build/bundles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import esbuild from 'esbuild';
import browserConfig from './esbuild-browser-config.cjs';

// cjs bundle for Electron apps. external dependencies bundled except LevelDB
// Remove if/when the following PR is merged and this bundle is no longer needed by Electron apps
// https://github.com/electron/electron/pull/37535
esbuild.buildSync({
platform : 'node',
bundle : true,
format : 'cjs',
// packages: 'external',
external : ['level'],
sourcemap : true,
entryPoints : ['./src/main.ts'],
outfile : './dist/electron/main.cjs',
allowOverwrite : true,
});

// cjs bundle. external dependencies **not** bundled
esbuild.buildSync({
platform : 'node',
bundle : true,
format : 'cjs',
packages : 'external',
sourcemap : true,
entryPoints : ['./src/main.ts'],
outfile : './dist/cjs/main.cjs',
allowOverwrite : true,
});

// esm bundle. external dependencies **not** bundled
esbuild.buildSync({
platform : 'node',
bundle : true,
format : 'esm',
packages : 'external',
sourcemap : true,
entryPoints : ['./src/main.ts'],
outfile : './dist/esm/main.mjs',
allowOverwrite : true,
});

// esm polyfilled bundle for browser
esbuild.build({
...browserConfig,
outfile: 'dist/browser.mjs',
});

// iife polyfilled bundle for browser
esbuild.build({
...browserConfig,
format : 'iife',
globalName : 'Web5',
outfile : 'dist/browser.js',
});
19 changes: 19 additions & 0 deletions packages/common/build/esbuild-browser-config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const polyfillProviderPlugin = require('node-stdlib-browser/helpers/esbuild/plugin');
const stdLibBrowser = require('node-stdlib-browser');

/** @type {import('esbuild').BuildOptions} */
module.exports = {
entryPoints : ['./src/main.ts'],
bundle : true,
format : 'esm',
sourcemap : true,
minify : true,
platform : 'browser',
target : ['chrome101', 'firefox108', 'safari16'],
inject : [require.resolve('node-stdlib-browser/helpers/esbuild/shim')],
plugins : [polyfillProviderPlugin(stdLibBrowser)],
define : {
'global': 'globalThis',
},
};
86 changes: 86 additions & 0 deletions packages/common/karma.conf.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-disable @typescript-eslint/no-var-requires */
// Karma is what we're using to run our tests in browser environments
// Karma does not support .mjs

// playwright acts as a safari executable on windows and mac
const playwright = require('@playwright/test');
const esbuildBrowserConfig = require('./build/esbuild-browser-config.cjs');

// use playwright chrome exec path as run target for chromium tests
process.env.CHROME_BIN = playwright.chromium.executablePath();

// use playwright webkit exec path as run target for safari tests
process.env.WEBKIT_HEADLESS_BIN = playwright.webkit.executablePath();

// use playwright firefox exec path as run target for firefox tests
process.env.FIREFOX_BIN = playwright.firefox.executablePath();

module.exports = function (config) {
config.set({
plugins: [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-webkit-launcher',
'karma-esbuild',
'karma-mocha',
'karma-mocha-reporter',
],

// frameworks to use
// available frameworks: https://www.npmjs.com/search?q=keywords:karma-adapter
frameworks: ['mocha'],

// Increase Mocha's default timeout of 2 seconds to prevent timeouts during GitHub CI runs.
client: {
mocha: {
timeout: 10000 // 10 seconds
}
},


// list of files / patterns to load in the browser
files: [
{ pattern: 'tests/**/*.spec.ts', watched: false },
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://www.npmjs.com/search?q=keywords:karma-preprocessor
preprocessors: {
'tests/**/*.spec.ts': ['esbuild'],
},

esbuild: esbuildBrowserConfig,

// list of files / patterns to exclude
exclude: [],

// test results reporter to use
// available reporters: https://www.npmjs.com/search?q=keywords:karma-reporter
reporters: ['mocha'],

// web server port
port: 9876,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN ||
// config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,

concurrency: 1,

// start these browsers
// available browser launchers: https://www.npmjs.com/search?q=keywords:karma-launcher
browsers: ['ChromeHeadless', 'FirefoxHeadless', 'WebkitHeadless'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,

// Increase browser timeouts to avoid DISCONNECTED messages during GitHub CI runs.
browserDisconnectTimeout : 10000, // default 2000
browserDisconnectTolerance : 1, // default 0
});
};
107 changes: 107 additions & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"name": "@tbd54566975/common",
"version": "0.1.0",
"type": "module",
"main": "./dist/cjs/main.cjs",
"module": "./dist/esm/main.mjs",
"types": "./dist/types/main.d.ts",
"scripts": {
"build": "rimraf dist && node build/bundles.js && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json && tsc",
"lint": "eslint . --ext .ts --max-warnings 0",
"lint:fix": "eslint . --ext .ts --fix",
"test:node": "rimraf __tests__ && tsc -p tsconfig.test.json && c8 mocha",
"test:browser": "karma start karma.conf.cjs"
},
"homepage": "https://github.com/TBD54566975/web5-js/tree/main/packages/common#readme",
"bugs": "https://github.com/TBD54566975/web5-js/issues",
"repository": {
"type": "git",
"url": "git+https://github.com/TBD54566975/web5-js",
"directory": "packages/common"
},
"license": "Apache-2.0",
"contributors": [
{
"name": "Daniel Buchner",
"url": "https://github.com/csuwildcat"
},
{
"name": "Frank Hinek",
"url": "https://github.com/frankhinek"
},
{
"name": "Moe Jangda",
"url": "https://github.com/mistermoe"
}
],
"files": [
"dist",
"src"
],
"exports": {
".": {
"import": "./dist/esm/main.mjs",
"require": "./dist/cjs/main.cjs",
"types": "./dist/types/main.d.ts"
},
"./browser": {
"import": "./dist/browser.mjs",
"require": "./dist/browser.js",
"types": "./dist/types/main.d.ts"
},
"./electron": {
"import": "./dist/esm/main.mjs",
"require": "./dist/electron/main.cjs",
"types": "./dist/types/main.d.ts"
}
},
"browser": {
"./dist/esm/main.mjs": "./dist/browser.mjs",
"./dist/cjs/main.cjs": "./dist/browser.js",
"types": "./dist/types/main.d.ts"
},
"keywords": [
"decentralized",
"decentralized-applications",
"decentralized-identity",
"decentralized-web",
"vcs",
"verifiable credentials",
"web5"
],
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=18.0.0"
},
"dependencies": {},
"devDependencies": {
"@types/chai": "4.3.0",
"@types/eslint": "8.37.0",
"@types/mocha": "10.0.1",
"@typescript-eslint/eslint-plugin": "5.59.0",
"@typescript-eslint/parser": "5.59.0",
"c8": "7.14.0",
"chai": "4.3.7",
"esbuild": "0.16.7",
"eslint": "8.39.0",
"eslint-plugin-mocha": "10.1.0",
"karma": "6.4.1",
"karma-chai": "0.1.0",
"karma-chrome-launcher": "3.1.1",
"karma-esbuild": "2.2.5",
"karma-firefox-launcher": "2.1.2",
"karma-mocha": "2.0.1",
"karma-mocha-reporter": "2.2.5",
"karma-webkit-launcher": "2.1.0",
"mocha": "10.2.0",
"node-stdlib-browser": "1.2.0",
"playwright": "1.31.2",
"rimraf": "4.4.0",
"typescript": "5.0.4"
},
"overrides": {
"socket.io-parser@>4.0.4 <4.2.3": "4.2.3"
}
}
1 change: 1 addition & 0 deletions packages/common/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types.js';
File renamed without changes.
7 changes: 7 additions & 0 deletions packages/common/tests/example.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect } from 'chai';

describe('example', () => {
it('works', () => {
expect(true).to.be.true;
});
});
28 changes: 28 additions & 0 deletions packages/common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compilerOptions": {
"strict": true,
"lib": [
"DOM",
"ES6"
],
"allowJs": true,
"target": "es6",
"module": "ESNext", // Required for enabling JavaScript import assertion support
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"declarationDir": "dist/types",
"sourceMap": true,
// `NodeNext` will throw compilation errors if relative import paths are missing file extension
// reference: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#ecmascript-module-support-in-node-js
"moduleResolution": "NodeNext",
"esModuleInterop": true
},
"include": [
"src",
],
"exclude": [
"node_modules",
"dist"
]
}
29 changes: 29 additions & 0 deletions packages/common/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"lib": [
"DOM",
"ES6"
],
"target": "es6",
"module": "ESNext", // Required for enabling JavaScript import assertion support
"declaration": true,
"declarationMap": true,
"outDir": "__tests__",
"declarationDir": "__tests__/types",
"sourceMap": true,
// `NodeNext` will throw compilation errors if relative import paths are missing file extension
// reference: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#ecmascript-module-support-in-node-js
"moduleResolution": "NodeNext",
// allows us to import json files
"resolveJsonModule": true,
"esModuleInterop": true
},
"include": [
"src",
"tests",
],
"exclude": [
"node_modules",
"dist"
]
}
Loading

0 comments on commit d1e2d10

Please sign in to comment.