Skip to content

Commit

Permalink
feat: First frontend library with environment
Browse files Browse the repository at this point in the history
- Use esbuild for bundle.
- Use biome for linting/formatting.
- Chechk biome on pre-commit.
- Bundle generated file into python-package.
  • Loading branch information
attakei committed May 15, 2024
1 parent 746443d commit 7a6debe
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
6 changes: 6 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"formatter": {
"indentStyle": "space"
}
}
11 changes: 11 additions & 0 deletions esbuild.mjs
Original file line number Diff line number Diff line change
@@ -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"],
});
4 changes: 4 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Entrypoint for browser works library of atsphinx-mini18n.
*/

226 changes: 226 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
11 changes: 11 additions & 0 deletions src/atsphinx/mini18n/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/atsphinx/mini18n/_static/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
1 change: 1 addition & 0 deletions tests/test_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 7a6debe

Please sign in to comment.