From 7a6debe2eab69f3cd218bbc2b67cbd6d95069a5a Mon Sep 17 00:00:00 2001 From: Kazuya Takei Date: Thu, 16 May 2024 01:49:54 +0900 Subject: [PATCH] feat: First frontend library with environment - Use esbuild for bundle. - Use biome for linting/formatting. - Chechk biome on pre-commit. - Bundle generated file into python-package. --- .github/workflows/main.yml | 8 + .pre-commit-config.yaml | 7 + biome.json | 6 + esbuild.mjs | 11 ++ main.js | 4 + package-lock.json | 226 ++++++++++++++++++++++++ package.json | 12 +- pyproject.toml | 3 +- src/atsphinx/mini18n/__init__.py | 11 ++ src/atsphinx/mini18n/_static/.gitignore | 2 + tests/test_it.py | 1 + 11 files changed, 287 insertions(+), 4 deletions(-) create mode 100644 biome.json create mode 100644 esbuild.mjs create mode 100644 main.js create mode 100644 package-lock.json create mode 100644 src/atsphinx/mini18n/_static/.gitignore diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6df2a2d..b233fd0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -35,6 +35,11 @@ jobs: - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} + - uses: volta-cli/action@v4 + - name: Build static files + run: | + npm i + npm run build - name: Configure venv run: | pip install uv @@ -69,8 +74,11 @@ jobs: - uses: actions/setup-python@v5 with: python-version-file: '.python-version' + - uses: volta-cli/action@v4 - name: Build package run: | + npm i + npm run build pip install hatch hatch build ls -l dist diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 65c11db..4e5309b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,3 +13,10 @@ repos: - id: doc8 args: - --max-line-length=119 + - repo: local + hooks: + - id: local-biome-check + name: biome + entry: npx biome check --apply --files-ignore-unknown=true --no-errors-on-unmatched + language: system + types: [text] diff --git a/biome.json b/biome.json new file mode 100644 index 0000000..770b8cf --- /dev/null +++ b/biome.json @@ -0,0 +1,6 @@ +{ + "$schema": "./node_modules/@biomejs/biome/configuration_schema.json", + "formatter": { + "indentStyle": "space" + } +} diff --git a/esbuild.mjs b/esbuild.mjs new file mode 100644 index 0000000..895eeee --- /dev/null +++ b/esbuild.mjs @@ -0,0 +1,11 @@ +#!/usr/bin/env node +import * as esbuild from "esbuild"; + +await esbuild.build({ + entryPoints: ["main.js"], + bundle: true, + minify: true, + sourcemap: true, + outfile: "src/atsphinx/mini18n/_static/atsphinx-mini18n.min.js", + target: ["chrome58", "firefox57", "safari11", "edge16"], +}); diff --git a/main.js b/main.js new file mode 100644 index 0000000..ed2431c --- /dev/null +++ b/main.js @@ -0,0 +1,4 @@ +/** + * Entrypoint for browser works library of atsphinx-mini18n. + */ + diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a9b2431 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,226 @@ +{ + "name": "mini18n", + "version": "0.4.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "mini18n", + "version": "0.4.0", + "license": "Apache-2.0", + "devDependencies": { + "@biomejs/biome": "1.7.3", + "esbuild": "0.21.2" + } + }, + "node_modules/@biomejs/biome": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/@biomejs/biome/-/biome-1.7.3.tgz", + "integrity": "sha512-ogFQI+fpXftr+tiahA6bIXwZ7CSikygASdqMtH07J2cUzrpjyTMVc9Y97v23c7/tL1xCZhM+W9k4hYIBm7Q6cQ==", + "dev": true, + "hasInstallScript": true, + "bin": { + "biome": "bin/biome" + }, + "engines": { + "node": ">=14.21.3" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/biome" + }, + "optionalDependencies": { + "@biomejs/cli-darwin-arm64": "1.7.3", + "@biomejs/cli-darwin-x64": "1.7.3", + "@biomejs/cli-linux-arm64": "1.7.3", + "@biomejs/cli-linux-arm64-musl": "1.7.3", + "@biomejs/cli-linux-x64": "1.7.3", + "@biomejs/cli-linux-x64-musl": "1.7.3", + "@biomejs/cli-win32-arm64": "1.7.3", + "@biomejs/cli-win32-x64": "1.7.3" + } + }, + "node_modules/@biomejs/cli-darwin-arm64": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-darwin-arm64/-/cli-darwin-arm64-1.7.3.tgz", + "integrity": "sha512-eDvLQWmGRqrPIRY7AIrkPHkQ3visEItJKkPYSHCscSDdGvKzYjmBJwG1Gu8+QC5ed6R7eiU63LEC0APFBobmfQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-darwin-x64": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-darwin-x64/-/cli-darwin-x64-1.7.3.tgz", + "integrity": "sha512-JXCaIseKRER7dIURsVlAJacnm8SG5I0RpxZ4ya3dudASYUc68WGl4+FEN03ABY3KMIq7hcK1tzsJiWlmXyosZg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-linux-arm64": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-linux-arm64/-/cli-linux-arm64-1.7.3.tgz", + "integrity": "sha512-phNTBpo7joDFastnmZsFjYcDYobLTx4qR4oPvc9tJ486Bd1SfEVPHEvJdNJrMwUQK56T+TRClOQd/8X1nnjA9w==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-linux-arm64-musl": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-linux-arm64-musl/-/cli-linux-arm64-musl-1.7.3.tgz", + "integrity": "sha512-c8AlO45PNFZ1BYcwaKzdt46kYbuP6xPGuGQ6h4j3XiEDpyseRRUy/h+6gxj07XovmyxKnSX9GSZ6nVbZvcVUAw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-linux-x64": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-linux-x64/-/cli-linux-x64-1.7.3.tgz", + "integrity": "sha512-vnedYcd5p4keT3iD48oSKjOIRPYcjSNNbd8MO1bKo9ajg3GwQXZLAH+0Cvlr+eMsO67/HddWmscSQwTFrC/uPA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-linux-x64-musl": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-linux-x64-musl/-/cli-linux-x64-musl-1.7.3.tgz", + "integrity": "sha512-UdEHKtYGWEX3eDmVWvQeT+z05T9/Sdt2+F/7zmMOFQ7boANeX8pcO6EkJPK3wxMudrApsNEKT26rzqK6sZRTRA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-win32-arm64": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-win32-arm64/-/cli-win32-arm64-1.7.3.tgz", + "integrity": "sha512-unNCDqUKjujYkkSxs7gFIfdasttbDC4+z0kYmcqzRk6yWVoQBL4dNLcCbdnJS+qvVDNdI9rHp2NwpQ0WAdla4Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@biomejs/cli-win32-x64": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/@biomejs/cli-win32-x64/-/cli-win32-x64-1.7.3.tgz", + "integrity": "sha512-ZmByhbrnmz/UUFYB622CECwhKIPjJLLPr5zr3edhu04LzbfcOrz16VYeNq5dpO1ADG70FORhAJkaIGdaVBG00w==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=14.21.3" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.21.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.21.2.tgz", + "integrity": "sha512-giZ/uOxWDKda44ZuyfKbykeXznfuVNkTgXOUOPJIjbayJV6FRpQ4zxUy9JMBPLaK9IJcdWtaoeQrYBMh3Rr4vQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/esbuild": { + "version": "0.21.2", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.21.2.tgz", + "integrity": "sha512-LmHPAa5h4tSxz+g/D8IHY6wCjtIiFx8I7/Q0Aq+NmvtoYvyMnJU0KQJcqB6QH30X9x/W4CemgUtPgQDZFca5SA==", + "dev": true, + "hasInstallScript": true, + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=12" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.21.2", + "@esbuild/android-arm": "0.21.2", + "@esbuild/android-arm64": "0.21.2", + "@esbuild/android-x64": "0.21.2", + "@esbuild/darwin-arm64": "0.21.2", + "@esbuild/darwin-x64": "0.21.2", + "@esbuild/freebsd-arm64": "0.21.2", + "@esbuild/freebsd-x64": "0.21.2", + "@esbuild/linux-arm": "0.21.2", + "@esbuild/linux-arm64": "0.21.2", + "@esbuild/linux-ia32": "0.21.2", + "@esbuild/linux-loong64": "0.21.2", + "@esbuild/linux-mips64el": "0.21.2", + "@esbuild/linux-ppc64": "0.21.2", + "@esbuild/linux-riscv64": "0.21.2", + "@esbuild/linux-s390x": "0.21.2", + "@esbuild/linux-x64": "0.21.2", + "@esbuild/netbsd-x64": "0.21.2", + "@esbuild/openbsd-x64": "0.21.2", + "@esbuild/sunos-x64": "0.21.2", + "@esbuild/win32-arm64": "0.21.2", + "@esbuild/win32-ia32": "0.21.2", + "@esbuild/win32-x64": "0.21.2" + } + } + } +} diff --git a/package.json b/package.json index a6b9e44..53db72d 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,17 @@ "doc": "doc", "test": "tests" }, - "scripts": {}, + "scripts": { + "build": "node ./esbuild.mjs" + }, "keywords": [], "author": "Kazuya Takei", "license": "Apache-2.0", - "dependencies": { - "typescript": "^5.4.5" + "devDependencies": { + "@biomejs/biome": "1.7.3", + "esbuild": "0.21.2" + }, + "volta": { + "node": "20.13.1" } } diff --git a/pyproject.toml b/pyproject.toml index 8b4b17f..97040f1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,10 +53,11 @@ dev-dependencies = [ ] [tool.rye.scripts] -setup = {chain = ["setup:sync", "setup:pre-commit", "setup:playwright"]} +setup = {chain = ["setup:sync", "setup:pre-commit", "setup:playwright", "setup:frontend"]} "setup:sync" = "rye sync --no-lock --all-features" "setup:pre-commit" = "pre-commit install" "setup:playwright" = "rye run playwright install firefox" +"setup:frontend" = "npm i" doc = "make -C doc" age = "python tools/run_age.py" diff --git a/src/atsphinx/mini18n/__init__.py b/src/atsphinx/mini18n/__init__.py index bd575ff..272a30d 100644 --- a/src/atsphinx/mini18n/__init__.py +++ b/src/atsphinx/mini18n/__init__.py @@ -108,12 +108,23 @@ def get_template_dir() -> str: # noqa: D103 return str(package_root / "templates") +def register_extra_static_files(app: Sphinx): + """Append JavaScript file for package.""" + if app.builder.format != "html": + return + _static_path = getattr(app.config, "html_static_path", []) + _static_path.append(str(package_root / "_static")) + _js_files = getattr(app.config, "html_js_files", []) + _js_files.append("atsphinx-mini18n.min.js") + + def setup(app: Sphinx): # noqa: D103 app.add_config_value("mini18n_default_language", None, "env") app.add_config_value("mini18n_support_languages", [], "env") app.add_config_value("mini18n_basepath", "/", "env") app.add_config_value("mini18n_select_lang_label", "Language:", "env") app.connect("config-inited", autocomplete_config) + app.connect("builder-inited", register_extra_static_files) # IMPORTANT!! # This is verty dirty hack to work it for any builders of third-party extensions. diff --git a/src/atsphinx/mini18n/_static/.gitignore b/src/atsphinx/mini18n/_static/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/src/atsphinx/mini18n/_static/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/tests/test_it.py b/tests/test_it.py index 3844515..3297e61 100644 --- a/tests/test_it.py +++ b/tests/test_it.py @@ -11,6 +11,7 @@ def test__it(app: SphinxTestApp): """Test to pass.""" app.build() + assert (app.outdir / "_static/atsphinx-mini18n.min.js").exists() @pytest.mark.sphinx("mini18n-html")