If you do not have access to node_modules, use createDefaultMapFromCDN to fetch the library definition files from a CDN (like unpkg).
Key Features:
- Caching: If
shouldCache is set to true, the library files are stored in localStorage. The cache automatically purges items from different ArkTS versions to save space. - Compression: You can pass an
lz-string instance to compress/decompress the files in the cache, significantly reducing the storage footprint.
Note: This process involves downloading approximately 1.5MB of data.
import { createDefaultMapFromCDN } from '@arkts/vfs'
import lzstring from 'lz-string'
import ts from 'ohos-typescript'
async function start() {
const shouldCache = true
const arktsVersion = '3.7.3'
// Standard usage with localStorage caching
const fsMap = await createDefaultMapFromCDN({ target: ts.ScriptTarget.ES2015 }, arktsVersion, shouldCache, ts)
// Usage with lz-string for compressed storage
const otherMap = await createDefaultMapFromCDN({ target: ts.ScriptTarget.ES2015 }, arktsVersion, shouldCache, ts, lzstring)
fsMap.set('index.ts', "const hello = 'hi'")
}
start()