Skip to content

Commit

Permalink
BREAKING: remove the UMD global build lib/ folder, move module output…
Browse files Browse the repository at this point in the history
… from es/ to dist/, convert all our code to JavaScript modules

To migrate: use JavaScript modules, f.e. `import * as kiwi from '@lume/kiwi'. If you have a web app and are not using a build system (f.e. Webpack, Vite, Rollup, or similar) that knows how to resolve the names of libraries (f.e. the `'@lume/kiwi'` in the `import` statement), then you need to use an `importmap`. See `example/index.html` for a `<script type="importmap">` example, and MDN web docs on import maps: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap
  • Loading branch information
trusktr committed Oct 23, 2023
1 parent af0f448 commit 45fa466
Show file tree
Hide file tree
Showing 36 changed files with 550 additions and 2,106 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig: http://editorconfig.org

root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md,yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
tmp/
es/
dist/
/coverage
lib/
4 changes: 2 additions & 2 deletions .prettierrc.cjs → .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
useTabs: true,
semi: false,
singleQuote: true,
Expand All @@ -7,5 +7,5 @@ module.exports = {
printWidth: 120,
arrowParens: 'avoid',

overrides: [{files: '*.md', options: {tabWidth: 2, useTabs: false}}],
overrides: [{files: ['*.md', '*.yml'], options: {useTabs: false, tabWidth: 2}}],
}
63 changes: 46 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,21 @@ compiler).

## Index

- [Getting started](#getting-started)
- [Install](#install)
- [Usage](#usage)
- [Documentation](#documentation)
- [Benchmarks](#benchmarks)
- [Tests](#tests)

## Getting started
## Install

Install using NPM:

```sh
npm install @lume/kiwi
```

then either load `kiwi` as a global variable using a script tag,

```html
<script src="/node_modules/kiwi/lib/kiwi.js"></script>
<script>
console.log(kiwi)
// ...use kiwi...
</script>
```

or import it into your ES Module script:
then import it into your project:

```js
import * as kiwi from '@lume/kiwi'
Expand All @@ -47,6 +37,23 @@ console.log(kiwi)
// ...use kiwi...
```

If you have a plain web app with no build, or a non-browser JS runtime that also
supports import maps like Deno, you'll need to add `@lume/kiwi` to your
`importmap` script so that the browser knows where to import kiwi from. F.e.
something like this:

```html
<script type="importmap">
{
"imports": {
"@lume/kiwi": "./node_modules/@lume/kiwi/dist/kiwi.js"
}
}
</script>
```

## Usage

The following example creates a solver which automatically calculates a width based on some constraints:

```js
Expand Down Expand Up @@ -84,6 +91,24 @@ To run the benchmark locally using nodejs, _clone or download this repository_ a
npm install
npm run bench

Statically serve the project, f.e. `npx five-server .` which opens a new browser
tab, then visit `/bench/index.html` to verify that the benchmark also runs in a
browser.

Sample result output:

```
----- Running creation benchmark...
Cassowary.js x 2,597 ops/sec ±1.56% (93 runs sampled)
kiwi x 26,243 ops/sec ±1.34% (91 runs sampled)
kiwi new API x 20,840 ops/sec ±7.19% (80 runs sampled)
Fastest is kiwi (± 10.11x faster)
----- Running solving benchmark...
Cassowary.js x 260,002 ops/sec ±2.62% (89 runs sampled)
kiwi x 595,455 ops/sec ±1.74% (89 runs sampled)
Fastest is kiwi (± 2.29x faster)
```

## Tests

To run the tests in the browser, [just visit this page](https://rawgit.com/IjzerenHein/kiwi/master/test/index.html).
Expand All @@ -93,16 +118,20 @@ To run the tests locally using nodejs, _clone or download this repository_ and e
npm install
npm run build && npm run test

Start a static server, f.e. `npx five-server .` which opens a new browser tab,
and visit `/test/index.html` to verify that tests also pass in a browser.

## Contribute

If you like this project and want to support it, show some love
and give it a star.
If you like this project and want to support it, show some love and give it a
star, try it and report any bugs, write new feature ideas, or even
open a pull request!

## License

© 2013 Nucleic Development Team
© 2021 Joseph Orbegoso Pea (http://github.com/trusktr)
© 2021 LUME
© 2021 Lume

[![License](https://img.shields.io/badge/license-BDS%203--clause-brightgreen)](<https://tldrlegal.com/license/bsd-3-clause-license-(revised)>)

Expand Down
12 changes: 10 additions & 2 deletions bench/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
<head>
<meta charset="utf-8" />
<title>Benchmark Tests</title>
<script type="importmap">
{
"imports": {
"@lume/kiwi": "../dist/kiwi.js"
}
}
</script>
</head>
<body>
<pre id="log" style="font-size: 16px"></pre>
Expand All @@ -13,7 +20,8 @@
type="text/javascript"
src="https://rawcdn.githack.com/slightlyoff/cassowary.js/68f82a0a9c365701a1cba8d929c5c7be097cd716/bin/c.min.js"
></script>
<script type="text/javascript" src="../lib/kiwi.js"></script>
<script type="text/javascript" src="./main.cjs"></script>
<script type="module">
import './main.js'
</script>
</body>
</html>
22 changes: 8 additions & 14 deletions bench/main.cjs → bench/main.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
/*global module:false*/
/*eslint strict:false, quotes: [2, "single"] */

var assert = typeof window === 'undefined' ? require('assert') : window.chai.assert
var _ = typeof window === 'undefined' ? require('lodash') : window._
var Platform = typeof window === 'undefined' ? require('platform') : window.Platform
var Benchmark = typeof window === 'undefined' ? require('benchmark') : window.Benchmark
var c = typeof window === 'undefined' ? require('cassowary') : window.c // cassowary

var getKiwi = () => {
return new Promise(resolve => {
typeof window === 'undefined' ? resolve(import('../es/kiwi.js')) : resolve(window.kiwi)
})
}
var assert = typeof window === 'undefined' ? (await import('assert')).default : window.chai.assert
var _ = typeof window === 'undefined' ? (await import('lodash')).default : window._
var Platform = typeof window === 'undefined' ? (await import('platform')).default : window.Platform
var Benchmark = typeof window === 'undefined' ? (await import('benchmark')).default : window.Benchmark
var c = typeof window === 'undefined' ? (await import('cassowary')).default : window.c // cassowary

main()
async function main() {
const kiwi = await getKiwi()
const kiwi = await import('@lume/kiwi')

var logElement
function log(message) {
Expand All @@ -28,7 +22,7 @@ async function main() {

function createKiwiSolver() {
var solver = new kiwi.Solver()
var strength = new kiwi.Strength.create(0, 900, 1000)
var strength = kiwi.Strength.create(0, 900, 1000)

// super-view
var superView = {
Expand Down Expand Up @@ -177,7 +171,7 @@ async function main() {

function createKiwiSolverNewAPI() {
var solver = new kiwi.Solver()
var strength = new kiwi.Strength.create(0, 900, 1000)
var strength = kiwi.Strength.create(0, 900, 1000)

// super-view
var superView = {
Expand Down
File renamed without changes.
File renamed without changes.
36 changes: 18 additions & 18 deletions es/constraint.js → dist/constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ export var Operator;
* @param {Expression} [rhs] Right hand side of the expression.
* @param {Number} [strength=Strength.required] The strength of the constraint.
*/
var Constraint = /** @class */ (function () {
function Constraint(expression, operator, rhs, strength) {
if (strength === void 0) { strength = Strength.required; }
this._id = CnId++;
export class Constraint {
constructor(expression, operator, rhs, strength = Strength.required) {
this._operator = operator;
this._strength = Strength.clip(strength);
if (rhs === undefined && expression instanceof Expression) {
Expand All @@ -46,41 +44,43 @@ var Constraint = /** @class */ (function () {
* Returns the unique id number of the constraint.
* @private
*/
Constraint.prototype.id = function () {
id() {
return this._id;
};
}
/**
* Returns the expression of the constraint.
*
* @return {Expression} expression
*/
Constraint.prototype.expression = function () {
expression() {
return this._expression;
};
}
/**
* Returns the relational operator of the constraint.
*
* @return {Operator} linear constraint operator
*/
Constraint.prototype.op = function () {
op() {
return this._operator;
};
}
/**
* Returns the strength of the constraint.
*
* @return {Number} strength
*/
Constraint.prototype.strength = function () {
strength() {
return this._strength;
};
Constraint.prototype.toString = function () {
}
toString() {
return (this._expression.toString() + ' ' + ['<=', '>=', '='][this._operator] + ' 0 (' + this._strength.toString() + ')');
};
return Constraint;
}());
export { Constraint };
}
_expression;
_operator;
_strength;
_id = CnId++;
}
/**
* The internal constraint id counter.
* @private
*/
var CnId = 0;
let CnId = 0;
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 45fa466

Please sign in to comment.