feat(browser): add browser build, tests, and sample sha256 library method
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
||||
node_modules
|
||||
build
|
||||
test
|
||||
src/**.js
|
||||
|
||||
coverage
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
src
|
||||
config
|
||||
examples
|
||||
test
|
||||
tsconfig.json
|
||||
tslint.json
|
||||
.travis.yml
|
||||
.github
|
||||
build/temp
|
||||
|
||||
coverage
|
||||
.nyc_output
|
||||
|
||||
37
config/exports/build-tests.js
Normal file
37
config/exports/build-tests.js
Normal file
@@ -0,0 +1,37 @@
|
||||
// this script watches the tests exported by typescript, copies them to the test directories, and modifies the require("PKG.NAME") statements to test each build
|
||||
const cpx = require("cpx");
|
||||
const separator = require("path").sep;
|
||||
const Transform = require("stream").Transform;
|
||||
const pkg = require('../../package');
|
||||
const req = (path) => 'require("' + path + '")';
|
||||
const pathUp = (levels) => Array.from(Array(levels), () => '../').join('');
|
||||
|
||||
// replace instances of pkg.name with the proper route to the build being tested
|
||||
const makeTransform = (filePath, buildPath) => {
|
||||
const buildPathParts = buildPath.split(separator);
|
||||
// filePath includes build/main (-2), test/BUILD is 2 deep (+2),
|
||||
// remove filename (-1). Total is length - 2
|
||||
const pathToRoot = pathUp(filePath.split(separator).length - 1);
|
||||
const placeholder = req(pkg.name);
|
||||
return new Transform({
|
||||
transform(chunk, encoding, done) {
|
||||
const str = chunk.toString();
|
||||
const parts = str.split(placeholder)
|
||||
const newPath = req(pathToRoot + buildPath)
|
||||
const result = parts.join(newPath);
|
||||
this.push(result);
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// copy, then watch for changes to the tests
|
||||
const testsFromRoot = 'build/main/**/*.spec.js';
|
||||
const task = process.argv[2] === '-w' ? cpx.watch : cpx.copy;
|
||||
|
||||
task(testsFromRoot, 'test/main', {
|
||||
transform: (filePath) => makeTransform(filePath, pkg.main)
|
||||
})
|
||||
task(testsFromRoot, 'test/browser', {
|
||||
transform: (filePath) => makeTransform(filePath, pkg.browser.replace('.js', '.cjs.js'))
|
||||
})
|
||||
21
config/exports/rollup.config.js
Normal file
21
config/exports/rollup.config.js
Normal file
@@ -0,0 +1,21 @@
|
||||
// rollup.config.js
|
||||
import commonjs from 'rollup-plugin-commonjs';
|
||||
import nodeResolve from 'rollup-plugin-node-resolve';
|
||||
import alias from 'rollup-plugin-alias';
|
||||
|
||||
const substituteModulePaths = {
|
||||
'crypto': 'build/module/adapters/crypto.browser.js',
|
||||
'hash.js': 'build/temp/hash.js'
|
||||
}
|
||||
|
||||
export default {
|
||||
entry: 'build/module/index.js',
|
||||
sourceMap: true,
|
||||
plugins: [
|
||||
alias(substituteModulePaths),
|
||||
nodeResolve({
|
||||
browser: true
|
||||
}),
|
||||
commonjs()
|
||||
]
|
||||
}
|
||||
9
config/exports/tsconfig.module.json
Normal file
9
config/exports/tsconfig.module.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig",
|
||||
"compilerOptions": {
|
||||
"outDir": "../../build/module",
|
||||
"rootDir": "../../src",
|
||||
"module": "es6",
|
||||
"declaration": false
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"extends": "../tsconfig",
|
||||
"compilerOptions": {
|
||||
"outDir": "../build/module",
|
||||
"module": "es6",
|
||||
"declaration": false
|
||||
},
|
||||
"exclude": [
|
||||
"../node_modules/**",
|
||||
"../src/**/*.spec.ts"
|
||||
]
|
||||
}
|
||||
35
package.json
35
package.json
@@ -5,19 +5,27 @@
|
||||
"main": "build/main/index.js",
|
||||
"typings": "build/main/index.d.ts",
|
||||
"module": "build/module/index.js",
|
||||
"browser": "build/browser/index.js",
|
||||
"repository": "https://github.com/bitjson/typescript-starter",
|
||||
"author": "Jason Dreyzehner <jason@dreyzehner.com>",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"info": "npm-scripts-info",
|
||||
"build": "trash build && tsc -p tsconfig.json && tsc -p config/tsconfig.module.json",
|
||||
"build": "trash build && yarn build:main && yarn build:module && yarn build:browser-deps && yarn build:browser && yarn build:browser-cjs",
|
||||
"build:main": "tsc -p tsconfig.json",
|
||||
"build:module": "tsc -p config/exports/tsconfig.module.json",
|
||||
"build:browser-deps": "mkdirp build/temp && browserify node_modules/hash.js/lib/hash.js -o build/temp/hash.js",
|
||||
"build:browser": "rollup -c config/exports/rollup.config.js -f es -o build/browser/index.js",
|
||||
"build:browser-cjs": "rollup -c config/exports/rollup.config.js -f cjs -o build/browser/index.cjs.js",
|
||||
"build:tests": "node config/exports/build-tests.js",
|
||||
"lint": "tslint src/**/*.ts",
|
||||
"unit": "yarn build && nyc ava",
|
||||
"unit": "yarn build && yarn build:tests && nyc ava",
|
||||
"check-coverage": "nyc check-coverage --lines 100 --functions 100 --branches 100",
|
||||
"test": "yarn lint && yarn unit && yarn check-coverage",
|
||||
"test": "yarn lint && yarn unit && yarn check-coverage && ava",
|
||||
"watch": "trash build && multiview [yarn watch:build] [yarn watch:unit]",
|
||||
"watch:build": "tsc -p tsconfig.json -w",
|
||||
"watch:unit": "tsc -p tsconfig.json && ava --watch --verbose",
|
||||
"watch:build:tests": "node config/exports/build-tests.js -w",
|
||||
"cov": "yarn unit && yarn html-coverage && opn coverage/index.html",
|
||||
"html-coverage": "nyc report --reporter=html",
|
||||
"send-coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",
|
||||
@@ -46,12 +54,20 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^7.0.5",
|
||||
"ava": "^0.18.1",
|
||||
"ava": "^0.18.2",
|
||||
"browserify": "^14.1.0",
|
||||
"codecov": "^1.0.1",
|
||||
"cpx": "^1.5.0",
|
||||
"hash.js": "^1.0.3",
|
||||
"mkdirp": "^0.5.1",
|
||||
"multiview": "^2.3.1",
|
||||
"npm-scripts-info": "^0.3.6",
|
||||
"nyc": "^10.0.0",
|
||||
"opn-cli": "^3.1.0",
|
||||
"rollup": "^0.41.4",
|
||||
"rollup-plugin-alias": "^1.2.0",
|
||||
"rollup-plugin-commonjs": "^7.0.0",
|
||||
"rollup-plugin-node-resolve": "^2.0.0",
|
||||
"standard-version": "^4.0.0",
|
||||
"trash-cli": "^1.4.0",
|
||||
"tslint": "^4.0.2",
|
||||
@@ -82,15 +98,8 @@
|
||||
],
|
||||
"nyc": {
|
||||
"exclude": [
|
||||
"**/*.spec.js"
|
||||
]
|
||||
},
|
||||
"ava": {
|
||||
"files": [
|
||||
"build/main/**/*.spec.js"
|
||||
],
|
||||
"source": [
|
||||
"build/main/**/*"
|
||||
"**/*.spec.js",
|
||||
"build"
|
||||
]
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
8
src/adapters/crypto.browser.ts
Normal file
8
src/adapters/crypto.browser.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
// Must first be built by browserify.
|
||||
// https://github.com/rollup/rollup-plugin-commonjs/issues/105#issuecomment-281917166
|
||||
import hash from 'hash.js'
|
||||
|
||||
export function createHash (algorithm: 'sha256') {
|
||||
console.log(hash)
|
||||
return hash.sha256()
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { test } from 'ava'
|
||||
import * as lib from './'
|
||||
|
||||
test('functions can be used without es imports', (t) => {
|
||||
t.true(typeof lib.double === 'function')
|
||||
t.true(typeof lib.power === 'function')
|
||||
t.true(typeof lib.asyncABC === 'function')
|
||||
})
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from './lib/numberOps'
|
||||
export * from './lib/asyncOps'
|
||||
export * from './lib/async'
|
||||
export * from './lib/hash'
|
||||
export * from './lib/number'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { test } from 'ava'
|
||||
import { asyncABC } from '../'
|
||||
import { asyncABC } from 'typescript-starter'
|
||||
|
||||
test('getABC', async t => {
|
||||
t.deepEqual(await asyncABC(), ['a','b', 'c'])
|
||||
6
src/lib/hash.spec.ts
Normal file
6
src/lib/hash.spec.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { test } from 'ava'
|
||||
import { sha256 } from 'typescript-starter'
|
||||
|
||||
test('sha256', t => {
|
||||
t.deepEqual(sha256('test'), '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')
|
||||
})
|
||||
17
src/lib/hash.ts
Normal file
17
src/lib/hash.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { createHash } from 'crypto'
|
||||
|
||||
/**
|
||||
* Calculate the sha256 digest of a string. On Node.js, this will use the native module, in the browser, it will fall back to a pure javascript implementation.
|
||||
*
|
||||
* ### Example (es imports)
|
||||
* ```js
|
||||
* import { sha256 } from 'typescript-starter'
|
||||
* sha256('test')
|
||||
* // => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
|
||||
* ```
|
||||
*
|
||||
* @returns sha256 message digest
|
||||
*/
|
||||
export function sha256 (message: string) {
|
||||
return createHash('sha256').update(message).digest('hex')
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { test } from 'ava'
|
||||
import { double, power } from '../'
|
||||
import { double, power } from 'typescript-starter'
|
||||
|
||||
test('double', t => {
|
||||
t.deepEqual(double(2), 4)
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"extends": "./config/tsconfig.flexible",
|
||||
"extends": "./config/tsconfig.flexible", // also available: "./config/tsconfig.strict"
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"outDir": "build/main",
|
||||
@@ -17,7 +17,11 @@
|
||||
],
|
||||
"types" : [
|
||||
"node"
|
||||
]
|
||||
],
|
||||
"baseUrl": ".", // required for "paths"
|
||||
"paths": {
|
||||
"typescript-starter": ["src/index.ts"] // write tests without relative paths
|
||||
}
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
|
||||
Reference in New Issue
Block a user