Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
{
"extends": "eslint:recommended",
"env": {
"browser": true,
"node": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": "latest"
},
"env": {
"es6": true,
"mocha": true,
"node": true
},
"rules": {
"indent": [ "error", 2 ],
"linebreak-style": [ "error", "unix" ],
"no-prototype-builtins": 0,
"semi": [ "error", "always" ]
}
{
"extends": "eslint:recommended",
"env": {
"browser": true,
"es6": true,
"node": true,
"jest": true
},
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"allowImportExportEverywhere": true
},
"rules": {
"indent": [ "error", 2 ],
"linebreak-style": [ "error", "unix" ],
"no-prototype-builtins": 0,
"semi": [ "error", "always" ]
},
"ignorePatterns": ["dist/*", "build/*", "index.d.ts"]
}
17 changes: 8 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
node-version: [18.x]

steps:
- uses: actions/checkout@v2
- uses: browser-actions/setup-chrome@latest
- name: Checkout repository
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
Expand All @@ -27,16 +27,15 @@ jobs:
- name: Install dependencies
run: |
npm install
sudo apt-get install graphviz xvfb

- name: Build
run: |
make build/graphlib.js build/graphlib.min.js

- name: Lint
run: |
make lint

- name: Build
run: |
make build

- name: Test
run: |
KARMA_OPTS="--browsers Chrome" xvfb-run --auto-servernum make -e test
make test
68 changes: 15 additions & 53 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,74 +1,36 @@
MOD = graphlib

NPM = npm
NYC = nyc
BROWSERIFY = ./node_modules/browserify/bin/cmd.js
JSHINT = ./node_modules/jshint/bin/jshint
ESLINT = ./node_modules/eslint/bin/eslint.js
KARMA = ./node_modules/karma/bin/karma
MOCHA = ./node_modules/mocha/bin/_mocha
UGLIFY = ./node_modules/uglify-js/bin/uglifyjs

JSHINT_OPTS = --reporter node_modules/jshint-stylish/index.js
MOCHA_OPTS = -R dot

BUILD_DIR = build
COVERAGE_DIR = ./.nyc_output
DIST_DIR = dist

SRC_FILES = index.js lib/version.js $(shell find lib -type f -name '*.js')
TEST_FILES = $(shell find test -type f -name '*.js' | grep -v 'bundle-test.js' | grep -v 'test-main.js')
BUILD_FILES = $(addprefix $(BUILD_DIR)/, \
$(MOD).js $(MOD).min.js \
$(MOD).core.js $(MOD).core.min.js)

DIRS = $(BUILD_DIR)
TEST_FILES = $(shell find test -type f -name '*.js' | grep -v 'bundle-test.js')
BUILD_FILES = $(addprefix $(DIST_DIR)/, $(MOD).cjs.js $(MOD).esm.js $(MOD).min.js $(MOD).js)

.PHONY: all bench clean browser-test unit-test test dist
.PHONY: all bench clean test dist lint build release node_modules

all: unit-test lint
all: build test

bench: unit-test lint
bench: test
@src/bench.js

lib/version.js: package.json
@src/release/make-version.js > $@

$(DIRS):
@mkdir -p $@

test: unit-test browser-test

unit-test: $(SRC_FILES) $(TEST_FILES) node_modules | $(BUILD_DIR)
$(NYC) $(MOCHA) --dir $(COVERAGE_DIR) -- $(MOCHA_OPTS) $(TEST_FILES) || $(MOCHA) $(MOCHA_OPTS) $(TEST_FILES)

browser-test: $(BUILD_DIR)/$(MOD).js $(BUILD_DIR)/$(MOD).core.js
$(KARMA) start --single-run $(KARMA_OPTS)
$(KARMA) start karma.core.conf.js --single-run $(KARMA_OPTS)

bower.json: package.json src/release/make-bower.json.js
@src/release/make-bower.json.js > $@

lint:
@$(JSHINT) $(JSHINT_OPTS) $(filter-out node_modules, $?)
@$(ESLINT) $(SRC_FILES) $(TEST_FILES)

$(BUILD_DIR)/$(MOD).js: index.js $(SRC_FILES) | unit-test
@$(BROWSERIFY) $< > $@ -s graphlib

$(BUILD_DIR)/$(MOD).min.js: $(BUILD_DIR)/$(MOD).js
@$(UGLIFY) $< --comments '@license' > $@
@echo "Running lint check via npm (ESLint)..."
@$(NPM) run lint

$(BUILD_DIR)/$(MOD).core.js: index.js $(SRC_FILES) | unit-test
@$(BROWSERIFY) $< > $@ --no-bundle-external -s graphlib
build:
@echo "Running project build via npm (esbuild)..."
@$(NPM) run build

$(BUILD_DIR)/$(MOD).core.min.js: $(BUILD_DIR)/$(MOD).core.js
@$(UGLIFY) $< --comments '@license' > $@
test: lint
@$(NPM) run test

dist: $(BUILD_FILES) | bower.json test
@rm -rf $@
@mkdir -p $@
@cp $^ dist
dist: build test
@echo "Dist files are built in 'dist/' by the 'build' target."

release: dist
@echo
Expand All @@ -77,7 +39,7 @@ release: dist
@src/release/release.sh $(MOD) dist

clean:
rm -rf $(BUILD_DIR)
rm -rf build dist coverage

node_modules: package.json
@$(NPM) install
Expand Down
60 changes: 60 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const esbuild = require('esbuild');
const fs = require('fs');
const { dependencies } = require('./package.json');

// Get all production dependencies to be marked as external (not bundled)
const external = Object.keys(dependencies || {});

const sharedConfig = {
entryPoints: ['index.js'],
bundle: true,
minify: true,
sourcemap: true,
target: 'es2018',
legalComments: 'linked',
external: external.concat([]),
};

async function build() {
// 1. CommonJS (CJS) - For Node.js `require()`
await esbuild.build({
...sharedConfig,
outfile: 'dist/graphlib.cjs.js',
format: 'cjs',
platform: 'node',
});

// 2. ES Module (ESM) - For modern bundlers/native ES imports
await esbuild.build({
...sharedConfig,
outfile: 'dist/graphlib.esm.js',
format: 'esm',
platform: 'neutral',
});

const iifeConfig = {
...sharedConfig,
format: 'iife',
globalName: 'graphlib',
platform: 'browser',
};

// 3. IIFE/UMD - For direct browser script tag
await esbuild.build({
...iifeConfig,
outfile: 'dist/graphlib.min.js',
});

// 4. IIFE/UMD - For direct browser script tag, unminified
await esbuild.build({
...iifeConfig,
outfile: 'dist/graphlib.js',
minify: false,
});

fs.copyFileSync('index.d.ts', 'dist/graphlib.d.ts');

console.log('Build complete! 🚀');
}

build().catch(() => process.exit(1));
Loading