Skip to content

Commit 8aed9d4

Browse files
Filmbostock
andauthored
Adopt type=module (#192)
* Adopt type=module follow changes in d3-format: * type=module * add exports * remove zip * license: ISC * update dependencies * es6 rather than no-undef * remove Sublime project * add eslint.json * related d3/d3#3502; extract copyrights from LICENSE * update dependencies * stricter eslint * cleaner imports * Update README Co-authored-by: Mike Bostock <[email protected]>
1 parent 8c4d113 commit 8aed9d4

17 files changed

+1060
-927
lines changed

.eslintrc.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"ecmaVersion": 8
66
},
77
"env": {
8-
"es6": true,
9-
"node": true,
10-
"browser": true
8+
"es6": true
119
},
1210
"rules": {
1311
"no-cond-assign": 0

.github/eslint.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "eslint-compact",
5+
"pattern": [
6+
{
7+
"regexp": "^(.+):\\sline\\s(\\d+),\\scol\\s(\\d+),\\s(Error|Warning|Info)\\s-\\s(.+)\\s\\((.+)\\)$",
8+
"file": 1,
9+
"line": 2,
10+
"column": 3,
11+
"severity": 4,
12+
"message": 5,
13+
"code": 6
14+
}
15+
]
16+
}
17+
]
18+
}

.github/workflows/node.js.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
2+
3+
name: Node.js CI
4+
5+
on:
6+
push:
7+
branches: [ main ]
8+
pull_request:
9+
branches: [ main ]
10+
11+
jobs:
12+
build:
13+
14+
runs-on: ubuntu-latest
15+
16+
strategy:
17+
matrix:
18+
node-version: [14.x]
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- name: Use Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v1
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
- run: yarn --frozen-lockfile
27+
- run: |
28+
echo ::add-matcher::.github/eslint.json
29+
yarn run eslint src test --format=compact
30+
- run: yarn test

LICENSE

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
Copyright 2010-2016 Mike Bostock
2-
All rights reserved.
3-
4-
Redistribution and use in source and binary forms, with or without modification,
5-
are permitted provided that the following conditions are met:
6-
7-
* Redistributions of source code must retain the above copyright notice, this
8-
list of conditions and the following disclaimer.
9-
10-
* Redistributions in binary form must reproduce the above copyright notice,
11-
this list of conditions and the following disclaimer in the documentation
12-
and/or other materials provided with the distribution.
13-
14-
* Neither the name of the author nor the names of contributors may be used to
15-
endorse or promote products derived from this software without specific prior
16-
written permission.
17-
18-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22-
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25-
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1+
Copyright 2010-2021 Mike Bostock
2+
3+
Permission to use, copy, modify, and/or distribute this software for any purpose
4+
with or without fee is hereby granted, provided that the above copyright notice
5+
and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
11+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
12+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
13+
THIS SOFTWARE.

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,28 @@ To use this module, create a [simulation](#simulation) for an array of [nodes](#
1818

1919
## Installing
2020

21-
If you use NPM, `npm install d3-force`. Otherwise, download the [latest release](https://github.com/d3/d3-force/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-force.v2.min.js) or as part of [D3](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3_force` global is exported:
21+
If you use npm, `npm install d3-force`. You can also download the [latest release on GitHub](https://github.com/d3/d3-force/releases/latest). For vanilla HTML in modern browsers, import d3-force from Skypack:
2222

2323
```html
24-
<script src="https://d3js.org/d3-dispatch.v2.min.js"></script>
25-
<script src="https://d3js.org/d3-quadtree.v2.min.js"></script>
26-
<script src="https://d3js.org/d3-timer.v2.min.js"></script>
27-
<script src="https://d3js.org/d3-force.v2.min.js"></script>
24+
<script type="module">
25+
26+
import {forceSimulation} from "https://cdn.skypack.dev/d3-force@3";
27+
28+
const simulation = forceSimulation(nodes);
29+
30+
</script>
31+
```
32+
33+
For legacy environments, you can load d3-force’s UMD bundle from an npm-based CDN such as jsDelivr; a `d3` global is exported:
34+
35+
```html
36+
<script src="https://cdn.jsdelivr.net/npm/d3-dispatch@3"></script>
37+
<script src="https://cdn.jsdelivr.net/npm/d3-quadtree@3"></script>
38+
<script src="https://cdn.jsdelivr.net/npm/d3-timer@3"></script>
39+
<script src="https://cdn.jsdelivr.net/npm/d3-force@3"></script>
2840
<script>
2941
30-
var simulation = d3.forceSimulation(nodes);
42+
const simulation = d3.forceSimulation(nodes);
3143
3244
</script>
3345
```

d3-force.sublime-project

Lines changed: 0 additions & 17 deletions
This file was deleted.

package.json

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
"name": "d3-force",
33
"version": "2.1.1",
44
"description": "Force-directed graph layout using velocity Verlet integration.",
5+
"homepage": "https://d3js.org/d3-force/",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/d3/d3-force.git"
9+
},
510
"keywords": [
611
"d3",
712
"d3-module",
@@ -12,40 +17,42 @@
1217
"verlet",
1318
"infovis"
1419
],
15-
"homepage": "https://d3js.org/d3-force/",
16-
"license": "BSD-3-Clause",
20+
"license": "ISC",
1721
"author": {
1822
"name": "Mike Bostock",
19-
"url": "http://bost.ocks.org/mike"
20-
},
21-
"main": "dist/d3-force.js",
22-
"unpkg": "dist/d3-force.min.js",
23-
"jsdelivr": "dist/d3-force.min.js",
24-
"module": "src/index.js",
25-
"repository": {
26-
"type": "git",
27-
"url": "https://github.com/d3/d3-force.git"
28-
},
29-
"scripts": {
30-
"pretest": "rollup -c",
31-
"test": "tape 'test/**/*-test.js' && eslint src",
32-
"prepublishOnly": "rm -rf dist && yarn test",
33-
"postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../${npm_package_name}/dist/${npm_package_name}.js ${npm_package_name}.v${npm_package_version%%.*}.js && cp ../${npm_package_name}/dist/${npm_package_name}.min.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git add ${npm_package_name}.v${npm_package_version%%.*}.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git commit -m \"${npm_package_name} ${npm_package_version}\" && git push && cd - && zip -j dist/${npm_package_name}.zip -- LICENSE README.md dist/${npm_package_name}.js dist/${npm_package_name}.min.js"
23+
"url": "https://bost.ocks.org/mike"
3424
},
25+
"type": "module",
3526
"files": [
3627
"src/**/*.js",
3728
"dist/**/*.js"
3829
],
39-
"dependencies": {
40-
"d3-dispatch": "1 - 2",
41-
"d3-quadtree": "1 - 2",
42-
"d3-timer": "1 - 2"
30+
"module": "src/index.js",
31+
"main": "src/index.js",
32+
"jsdelivr": "dist/d3-force.min.js",
33+
"unpkg": "dist/d3-force.min.js",
34+
"exports": {
35+
"umd": "./dist/d3-force.min.js",
36+
"default": "./src/index.js"
4337
},
4438
"sideEffects": false,
39+
"dependencies": {
40+
"d3-dispatch": "1 - 3",
41+
"d3-quadtree": "1 - 3",
42+
"d3-timer": "1 - 3"
43+
},
4544
"devDependencies": {
46-
"eslint": "6",
47-
"rollup": "1",
48-
"rollup-plugin-terser": "5",
49-
"tape": "4"
45+
"eslint": "7",
46+
"mocha": "8",
47+
"rollup": "2",
48+
"rollup-plugin-terser": "7"
49+
},
50+
"scripts": {
51+
"test": "mocha 'test/**/*-test.js' && eslint src test",
52+
"prepublishOnly": "rm -rf dist && yarn test && rollup -c",
53+
"postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../${npm_package_name}/dist/${npm_package_name}.js ${npm_package_name}.v${npm_package_version%%.*}.js && cp ../${npm_package_name}/dist/${npm_package_name}.min.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git add ${npm_package_name}.v${npm_package_version%%.*}.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git commit -m \"${npm_package_name} ${npm_package_version}\" && git push && cd -"
54+
},
55+
"engines": {
56+
"node": ">=12"
5057
}
5158
}

rollup.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
import {readFileSync} from "fs";
12
import {terser} from "rollup-plugin-terser";
23
import * as meta from "./package.json";
34

5+
// Extract copyrights from the LICENSE.
6+
const copyright = readFileSync("./LICENSE", "utf-8")
7+
.split(/\n/g)
8+
.filter(line => /^Copyright\s+/.test(line))
9+
.map(line => line.replace(/^Copyright\s+/, ""))
10+
.join(", ");
11+
412
const config = {
513
input: "src/index.js",
614
external: Object.keys(meta.dependencies || {}).filter(key => /^d3-/.test(key)),
@@ -10,7 +18,7 @@ const config = {
1018
format: "umd",
1119
indent: false,
1220
extend: true,
13-
banner: `// ${meta.homepage} v${meta.version} Copyright ${(new Date).getFullYear()} ${meta.author.name}`,
21+
banner: `// ${meta.homepage} v${meta.version} Copyright ${copyright}`,
1422
globals: Object.assign({}, ...Object.keys(meta.dependencies || {}).filter(key => /^d3-/.test(key)).map(key => ({[key]: "d3"})))
1523
},
1624
plugins: []

test/.eslintrc.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "eslint:recommended",
3+
"parserOptions": {
4+
"sourceType": "module",
5+
"ecmaVersion": 8
6+
},
7+
"env": {
8+
"es6": true,
9+
"mocha": true
10+
}
11+
}

test/asserts.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import assert from "assert";
2+
3+
export function assertNodeEqual(actual, expected, delta = 1e-6) {
4+
assert(nodeEqual(actual, expected, delta), `${actual} and ${expected} should be similar`);
5+
}
6+
7+
function nodeEqual(actual, expected, delta) {
8+
return actual.index == expected.index
9+
&& Math.abs(actual.x - expected.x) < delta
10+
&& Math.abs(actual.vx - expected.vx) < delta
11+
&& Math.abs(actual.y - expected.y) < delta
12+
&& Math.abs(actual.vy - expected.vy) < delta
13+
&& !(Math.abs(actual.fx - expected.fx) > delta)
14+
&& !(Math.abs(actual.fy - expected.fy) > delta);
15+
}

0 commit comments

Comments
 (0)