-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathhatch_build.py
More file actions
97 lines (75 loc) · 3.05 KB
/
hatch_build.py
File metadata and controls
97 lines (75 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Copyright (c) 2021-2023 Datalayer, Inc.
#
# MIT License
import glob
import os
import re
from subprocess import check_call
import shutil
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
here = os.path.abspath(os.path.dirname(__file__))
def patch_package_json_requires(file_path):
"""Patch built JS files to replace require('../package.json') patterns.
This is similar to the patch for @jupyter-widgets/controls that replaces:
require('../package.json').version -> hardcoded version string
This prevents RequireJS from intercepting these calls at runtime.
"""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
original_content = content
# Pattern to match require("../package.json") or require('../package.json')
# and similar patterns like require("./package.json")
patterns = [
# require("../package.json").version or require('../package.json').version
(r'require\(["\']\.\.?/package\.json["\']\)\.version', '"0.0.0"'),
# require("../package.json") or require('../package.json') standalone
(r'require\(["\']\.\.?/package\.json["\']\)', '{version:"0.0.0"}'),
]
for pattern, replacement in patterns:
content = re.sub(pattern, replacement, content)
if content != original_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"Patched package.json requires in: {file_path}")
def clean_dist():
"""Remove the contents of the dist folder and tsconfig.tsbuildinfo."""
dist_path = os.path.join(here, 'dist')
if os.path.exists(dist_path):
shutil.rmtree(dist_path)
print(f"Cleaned dist folder: {dist_path}")
# Also remove tsconfig.tsbuildinfo if it exists
tsbuildinfo_path = os.path.join(here, 'tsconfig.tsbuildinfo')
if os.path.exists(tsbuildinfo_path):
os.remove(tsbuildinfo_path)
print(f"Removed tsconfig.tsbuildinfo: {tsbuildinfo_path}")
def build_javascript():
# Install deps
check_call(
['npm', 'i'],
cwd=here,
)
clean_dist()
vite_env = os.environ.copy()
vite_env['VITE_BASE_URL'] = '/static/jupyter_lexical/'
check_call(
['npm', 'run', 'build:vite'],
cwd=here,
env=vite_env,
)
# Copy built files recursively to static folder
dist_path = os.path.join(here, 'dist')
static_path = os.path.join(here, 'jupyter_lexical', 'static')
if os.path.exists(static_path):
shutil.rmtree(static_path)
shutil.copytree(dist_path, static_path)
# Patch JS files to remove package.json requires
for file in glob.glob(os.path.join(static_path, '**', '*.js'), recursive=True):
patch_package_json_requires(file)
class JupyterBuildHook(BuildHookInterface):
def initialize(self, version, build_data):
if self.target_name == 'editable':
build_javascript()
elif self.target_name == 'wheel':
build_javascript()
elif self.target_name == 'sdist':
build_javascript()