Skip to main content

js_utils

BuildMode

from dara.core.js_tooling.js_utils import BuildMode

class BuildMode(Enum)

AUTO_JS

AutoJS mode - use pre-bundled UMDs

PRODUCTION

Production mode - vite build from generated entry file (optionally with custom JS)

JsConfig

from dara.core.js_tooling.js_utils import JsConfig

class JsConfig(BaseModel)

Attributes

  • local_entry: str
  • extra_dependencies: Dict[str, str]
  • package_manager: Literal[]

Methods

local_entry

Relative path to the local entrypoint from the package root

extra_dependencies

Extra dependencies to add to package.json before running install

package_manager

Package manager to use, defaults to npm

from_file

@staticmethod
def from_file(path: str = 'dara.config.json') -> Optional['JsConfig']

Read from a config file

Arguments:

  • path: path to the config file, defaults to 'dara.config.json'

BuildConfig

from dara.core.js_tooling.js_utils import BuildConfig

class BuildConfig(BaseModel)

Represents the build configuration used

Attributes

  • mode: BuildMode
  • dev: bool
  • js_config: Optional[JsConfig]
  • npm_registry: Optional[str]
  • npm_token: Optional[str]

mode

Build mode set based on CLI settings

dev

Whether dev (HMR) mode is enabled

js_config

Custom JS configuration from dara.config.json file

npm_registry

Optional npm registry url to pull packages from

npm_token

Optional npm token for the registry url added above

BuildCacheDiff

from dara.core.js_tooling.js_utils import BuildCacheDiff

class BuildCacheDiff(BaseModel)

Represents the diff between two BuildCaches. Contains a list of keys that have changed.

Attributes

  • keys: Set[BuildCacheKey]

Methods

should_rebuild_js

def should_rebuild_js() -> bool

Returns True if a JS rebuild is required. This is the case if:

  • static_files_dir changed - the output folder is different, we need to install into a different output folder
  • package_map changed - required packages changed, we need to install new packages
  • build_config changed - build config changed, we need to install with a different config

full_diff

@staticmethod
def full_diff() -> 'BuildCacheDiff'

Return a full diff - as if everything changed

BuildCache

from dara.core.js_tooling.js_utils import BuildCache

class BuildCache(BaseModel)

Represents the build configuration cache. Contains complete information required to determine how to handle the frontend assets and statics.

Attributes

  • static_folders: List[str]
  • static_files_dir: str
  • package_map: Dict[str, str]
  • build_config: BuildConfig
  • FILENAME: ClassVar[str]

Methods

static_folders

List of static folders registered

static_files_dir

Static files output folder

package_map

Map of py_module_name to js_module_name

build_config

Build configuration used to generate this cache

from_config

@staticmethod
def from_config(config: Configuration,
build_config: Optional[BuildConfig] = None)

Create a BuildCache from a Configuration

Arguments:

  • config: Configuration to use
  • build_config: BuildConfig to use, defaults to BuildConfig.from_env()

migrate_static_assets

def migrate_static_assets()

Migrate data from registered static folders into the static_files_dir.

find_favicon

def find_favicon() -> Optional[str]

Find the favicon in the static files directories, looks for any .ico file

Returns:

path to favicon if found, None otherwise

def symlink_js()

Symlink:

  • the custom js source folder into the static files directory
  • the node_modules folder into the custom js source folder

get_importers

def get_importers() -> Dict[str, str]

Get the importers map for this BuildCache. Includes self.package_map and a local entry if it exists.

get_py_modules

def get_py_modules() -> List[str]

Get a list of all py modules used in this BuildCache

get_package_json

def get_package_json() -> Dict[str, Any]

Generate a package.json file for this BuildCache

get_diff

def get_diff(
other: Optional[Union['BuildCache', str]] = None) -> BuildCacheDiff

Get the diff between this BuildCache and another.

Returns a list of keys that have changed.

Arguments:

  • other: the other BuildCache to diff against, or a path to a BuildCache; if none provided, parses diff from file in configured static_files_dir

setup_js_scaffolding

def setup_js_scaffolding()

Create a dara custom js config

rebuild_js

def rebuild_js(build_cache: BuildCache,
build_diff: BuildCacheDiff = BuildCacheDiff.full_diff())

Generic 'rebuild' function which bundles/prepares assets depending on the build mode chosen

Arguments:

  • build_cache: current build configuration cache
  • build_diff: the difference between the current build cache and the previous build cache

bundle_js

def bundle_js(build_cache: BuildCache, copy_js: bool = False)

Bundle the JS (and CSS) in production mode using Vite

Arguments:

  • build_cache: the build cache
  • copy_js: whether to copy JS instead of symlinking it

prepare_autojs_assets

def prepare_autojs_assets(build_cache: BuildCache)

Prepare the JS (and CSS) assets to use in autoJS mode.

Copies over UMD pre-bundled files from loaded packages into static_files_dir directory, ready to be served directly without bundling.

Arguments:

  • config: the main app configuration

build_autojs_template

def build_autojs_template(html_template: str, build_cache: BuildCache,
config: Configuration) -> str

Build the autojs html template by replacing $$assets$$ with required tags based on packages loaded

and including the startup script

Arguments:

  • html_template: html template to fill out
  • build_cache: build cache
  • config: app configuration