@lwrjs/shared-utils
Advanced tools
Comparing version 0.0.2-alpha26 to 0.0.2-alpha27
@@ -6,4 +6,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.slugify = exports.moduleSpecifierToKebabCase = exports.kebabcaseToCamelcase = exports.parseSyntheticSpecifier = exports.getVersionedSpecifier = exports.getSpecifier = exports.explodeSpecifier = exports.normalizeVersionFromUri = exports.normalizeVersionToUri = exports.ASSETS_CACHE_DIR = exports.IMMUTABLE_ASSET_PREFIX = exports.VERSION_SIGIL = exports.MAPPING_NAMESPACE = exports.JSON_NAMESPACE = exports.CONFIG_NAMESPACE = exports.BOOTSTRAP_NAMESPACE = void 0; | ||
exports.getCacheKeyFromJson = exports.slugify = exports.moduleSpecifierToKebabCase = exports.kebabcaseToCamelcase = exports.parseSyntheticSpecifier = exports.getVersionedSpecifier = exports.getSpecifier = exports.explodeSpecifier = exports.normalizeVersionFromUri = exports.normalizeVersionToUri = exports.ASSETS_CACHE_DIR = exports.IMMUTABLE_ASSET_PREFIX = exports.LATEST_SIGNATURE = exports.VERSION_SIGIL = exports.MAPPING_NAMESPACE = exports.JSON_NAMESPACE = exports.CONFIG_NAMESPACE = exports.BOOTSTRAP_NAMESPACE = void 0; | ||
const slugify_1 = __importDefault(require("slugify")); | ||
const fast_json_stable_stringify_1 = __importDefault(require("fast-json-stable-stringify")); | ||
exports.getCacheKeyFromJson = fast_json_stable_stringify_1.default; | ||
const WEBRUNTIME_NAMESPACE = '@lwrjs'; | ||
@@ -15,2 +17,3 @@ exports.BOOTSTRAP_NAMESPACE = `${WEBRUNTIME_NAMESPACE}/bootstrap`; | ||
exports.VERSION_SIGIL = '/v/'; | ||
exports.LATEST_SIGNATURE = 'latest'; | ||
exports.IMMUTABLE_ASSET_PREFIX = '/_immutable/'; | ||
@@ -41,8 +44,2 @@ exports.ASSETS_CACHE_DIR = 'assetsCache'; | ||
exports.normalizeVersionFromUri = normalizeVersionFromUri; | ||
/** | ||
* Create a partial Module Identifier from a specifier | ||
* The Module Identifier is partial because the specifier may not contain a version | ||
* This function is the opposite of 'getSpecifier()' | ||
* @param rawSpecifier | ||
*/ | ||
function explodeSpecifier(rawSpecifier) { | ||
@@ -53,10 +50,10 @@ const decodedSpecifier = decodeURIComponent(rawSpecifier); | ||
const version = versionMatch ? versionMatch[2] : undefined; | ||
const specifier = versionMatch ? versionMatch[1] : decodedSpecifier; | ||
const importee = versionMatch ? versionMatch[1] : decodedSpecifier; | ||
// Pull out the namespace and name | ||
const [rawNamespace, rawName, ...remaining] = specifier.split('/'); | ||
const [rawNamespace, rawName, ...remaining] = importee.split('/'); | ||
const namespace = rawName ? rawNamespace : undefined; | ||
const name = rawName ? [rawName, ...remaining].join('/') : rawNamespace; | ||
// Return a partial ModuleIdentifier | ||
// Return an abstract ModuleIdentifier | ||
return { | ||
specifier, | ||
specifier: importee, | ||
namespace, | ||
@@ -63,0 +60,0 @@ name, |
@@ -18,15 +18,46 @@ "use strict"; | ||
function explodeMode(mode) { | ||
const devMode = { | ||
mode, | ||
bundle: false, | ||
format: 'esm', | ||
minify: false, | ||
compat: false, | ||
watchFiles: true, | ||
stripAsserts: true, | ||
}; | ||
switch (mode) { | ||
case 'dev': | ||
return { mode, format: 'esm', minify: false, compat: false, stripAsserts: true }; | ||
return devMode; | ||
case 'prod': | ||
return { | ||
mode, | ||
bundle: true, | ||
format: 'esm', | ||
minify: true, | ||
compat: false, | ||
watchFiles: false, | ||
stripAsserts: true, | ||
}; | ||
case 'prod-compat': | ||
return { | ||
mode, | ||
bundle: false, | ||
format: 'amd', | ||
minify: true, | ||
compat: true, | ||
watchFiles: false, | ||
stripAsserts: true, | ||
}; | ||
case 'compat': | ||
return { | ||
mode, | ||
bundle: false, | ||
format: 'amd', | ||
minify: false, | ||
compat: true, | ||
watchFiles: true, | ||
stripAsserts: true, | ||
}; | ||
default: | ||
return { mode, format: 'esm', minify: false, compat: false, stripAsserts: true }; | ||
return devMode; | ||
} | ||
@@ -54,4 +85,4 @@ } | ||
// Given a Module Identifier, return a JSON entry | ||
async function createJsonModule(moduleId, moduleRegistry, context) { | ||
const { ownHash, moduleEntry: { version }, } = await moduleRegistry.getModuleSource(moduleId, context); | ||
async function createJsonModule(moduleId, moduleRegistry, runtimeEnvironment) { | ||
const { ownHash, moduleEntry: { version }, } = await moduleRegistry.getModule(moduleId); | ||
return { | ||
@@ -62,3 +93,3 @@ specifier: moduleId.specifier, | ||
links: { | ||
self: moduleRegistry.resolveModuleUri(moduleId, context, ownHash), | ||
self: moduleRegistry.resolveModuleUri(moduleId, runtimeEnvironment, ownHash), | ||
}, | ||
@@ -73,8 +104,8 @@ }; | ||
* @param moduleRegistry | ||
* @param context - runtime context | ||
* @returns - Promise to the JSON serialization of the module | ||
*/ | ||
async function serializeModuleToJson(code = '', { specifier, version, ownHash, moduleDefinition: { format, moduleRecord: { imports = [] }, }, }, moduleRegistry, context) { | ||
async function serializeModuleToJson(code = '', { specifier, version, ownHash, runtimeEnvironment, moduleRecord: { imports = [] }, }, moduleRegistry) { | ||
const { format, minify } = runtimeEnvironment; | ||
// Build the dependencies array | ||
const dependencies = imports.map((dep) => createJsonModule(dep, moduleRegistry, context)); | ||
const dependencies = imports.map((dep) => createJsonModule(dep, moduleRegistry, runtimeEnvironment)); | ||
return { | ||
@@ -86,3 +117,3 @@ specifier, | ||
format, | ||
minified: context.minify, | ||
minify, | ||
code, | ||
@@ -89,0 +120,0 @@ }; |
import slugifyText from 'slugify'; | ||
import getCacheKeyFromJson from 'fast-json-stable-stringify'; | ||
const WEBRUNTIME_NAMESPACE = '@lwrjs'; | ||
@@ -8,2 +9,3 @@ export const BOOTSTRAP_NAMESPACE = `${WEBRUNTIME_NAMESPACE}/bootstrap`; | ||
export const VERSION_SIGIL = '/v/'; | ||
export const LATEST_SIGNATURE = 'latest'; | ||
export const IMMUTABLE_ASSET_PREFIX = '/_immutable/'; | ||
@@ -32,8 +34,2 @@ export const ASSETS_CACHE_DIR = 'assetsCache'; | ||
} | ||
/** | ||
* Create a partial Module Identifier from a specifier | ||
* The Module Identifier is partial because the specifier may not contain a version | ||
* This function is the opposite of 'getSpecifier()' | ||
* @param rawSpecifier | ||
*/ | ||
export function explodeSpecifier(rawSpecifier) { | ||
@@ -44,10 +40,10 @@ const decodedSpecifier = decodeURIComponent(rawSpecifier); | ||
const version = versionMatch ? versionMatch[2] : undefined; | ||
const specifier = versionMatch ? versionMatch[1] : decodedSpecifier; | ||
const importee = versionMatch ? versionMatch[1] : decodedSpecifier; | ||
// Pull out the namespace and name | ||
const [rawNamespace, rawName, ...remaining] = specifier.split('/'); | ||
const [rawNamespace, rawName, ...remaining] = importee.split('/'); | ||
const namespace = rawName ? rawNamespace : undefined; | ||
const name = rawName ? [rawName, ...remaining].join('/') : rawNamespace; | ||
// Return a partial ModuleIdentifier | ||
// Return an abstract ModuleIdentifier | ||
return { | ||
specifier, | ||
specifier: importee, | ||
namespace, | ||
@@ -170,2 +166,3 @@ name, | ||
} | ||
export { getCacheKeyFromJson }; | ||
//# sourceMappingURL=identity.js.map |
@@ -12,15 +12,46 @@ import fs from 'fs'; | ||
export function explodeMode(mode) { | ||
const devMode = { | ||
mode, | ||
bundle: false, | ||
format: 'esm', | ||
minify: false, | ||
compat: false, | ||
watchFiles: true, | ||
stripAsserts: true, | ||
}; | ||
switch (mode) { | ||
case 'dev': | ||
return { mode, format: 'esm', minify: false, compat: false, stripAsserts: true }; | ||
return devMode; | ||
case 'prod': | ||
return { | ||
mode, | ||
bundle: true, | ||
format: 'esm', | ||
minify: true, | ||
compat: false, | ||
watchFiles: false, | ||
stripAsserts: true, | ||
}; | ||
case 'prod-compat': | ||
return { | ||
mode, | ||
bundle: false, | ||
format: 'amd', | ||
minify: true, | ||
compat: true, | ||
watchFiles: false, | ||
stripAsserts: true, | ||
}; | ||
case 'compat': | ||
return { | ||
mode, | ||
bundle: false, | ||
format: 'amd', | ||
minify: false, | ||
compat: true, | ||
watchFiles: true, | ||
stripAsserts: true, | ||
}; | ||
default: | ||
return { mode, format: 'esm', minify: false, compat: false, stripAsserts: true }; | ||
return devMode; | ||
} | ||
@@ -45,4 +76,4 @@ } | ||
// Given a Module Identifier, return a JSON entry | ||
async function createJsonModule(moduleId, moduleRegistry, context) { | ||
const { ownHash, moduleEntry: { version }, } = await moduleRegistry.getModuleSource(moduleId, context); | ||
async function createJsonModule(moduleId, moduleRegistry, runtimeEnvironment) { | ||
const { ownHash, moduleEntry: { version }, } = await moduleRegistry.getModule(moduleId); | ||
return { | ||
@@ -53,3 +84,3 @@ specifier: moduleId.specifier, | ||
links: { | ||
self: moduleRegistry.resolveModuleUri(moduleId, context, ownHash), | ||
self: moduleRegistry.resolveModuleUri(moduleId, runtimeEnvironment, ownHash), | ||
}, | ||
@@ -64,8 +95,8 @@ }; | ||
* @param moduleRegistry | ||
* @param context - runtime context | ||
* @returns - Promise to the JSON serialization of the module | ||
*/ | ||
export async function serializeModuleToJson(code = '', { specifier, version, ownHash, moduleDefinition: { format, moduleRecord: { imports = [] }, }, }, moduleRegistry, context) { | ||
export async function serializeModuleToJson(code = '', { specifier, version, ownHash, runtimeEnvironment, moduleRecord: { imports = [] }, }, moduleRegistry) { | ||
const { format, minify } = runtimeEnvironment; | ||
// Build the dependencies array | ||
const dependencies = imports.map((dep) => createJsonModule(dep, moduleRegistry, context)); | ||
const dependencies = imports.map((dep) => createJsonModule(dep, moduleRegistry, runtimeEnvironment)); | ||
return { | ||
@@ -77,3 +108,3 @@ specifier, | ||
format, | ||
minified: context.minify, | ||
minify, | ||
code, | ||
@@ -80,0 +111,0 @@ }; |
@@ -1,2 +0,3 @@ | ||
import { ModuleIdentifier, PublicModuleRegistry } from '@lwrjs/types'; | ||
import getCacheKeyFromJson from 'fast-json-stable-stringify'; | ||
import { AbstractModuleId, PublicModuleRegistry } from '@lwrjs/types'; | ||
export declare const BOOTSTRAP_NAMESPACE: string; | ||
@@ -7,9 +8,6 @@ export declare const CONFIG_NAMESPACE: string; | ||
export declare const VERSION_SIGIL = "/v/"; | ||
export declare const LATEST_SIGNATURE = "latest"; | ||
export declare const IMMUTABLE_ASSET_PREFIX = "/_immutable/"; | ||
export declare const ASSETS_CACHE_DIR = "assetsCache"; | ||
declare type ModuleIdentifierPartial = Partial<ModuleIdentifier>; | ||
declare type VersionlessModuleIdentifier = Partial<ModuleIdentifier> & { | ||
specifier: string; | ||
name: string; | ||
}; | ||
declare type ModuleIdentifierPartial = Partial<AbstractModuleId>; | ||
/** | ||
@@ -33,3 +31,6 @@ * Turn the dots in a version into underscores | ||
*/ | ||
export declare function explodeSpecifier(rawSpecifier: string): VersionlessModuleIdentifier; | ||
interface PartialModuleId extends AbstractModuleId { | ||
name: string; | ||
} | ||
export declare function explodeSpecifier(rawSpecifier: string): PartialModuleId; | ||
/** | ||
@@ -87,3 +88,3 @@ * Create a specifier with a namespace (optional), name and version (optional) | ||
export declare function slugify(name: string): string; | ||
export {}; | ||
export { getCacheKeyFromJson }; | ||
//# sourceMappingURL=identity.d.ts.map |
/// <reference types="node" /> | ||
import { ApplicationMode, ModeConfig, Watcher, ModuleDefinition, ModuleJsonDefinition, ModuleRegistry, RuntimeContext, ViewSource, ResourcePaths } from '@lwrjs/types'; | ||
import { ApplicationMode, ModeConfig, Watcher, LinkedModuleDefinition, ModuleJsonDefinition, ModuleRegistry, ViewSource, ResourcePaths } from '@lwrjs/types'; | ||
/** | ||
@@ -27,6 +27,5 @@ * Given a mode, return a config set | ||
* @param moduleRegistry | ||
* @param context - runtime context | ||
* @returns - Promise to the JSON serialization of the module | ||
*/ | ||
export declare function serializeModuleToJson(code: string | undefined, { specifier, version, ownHash, moduleDefinition: { format, moduleRecord: { imports }, }, }: ModuleDefinition, moduleRegistry: ModuleRegistry, context: RuntimeContext): Promise<ModuleJsonDefinition>; | ||
export declare function serializeModuleToJson(code: string | undefined, { specifier, version, ownHash, runtimeEnvironment, moduleRecord: { imports }, }: LinkedModuleDefinition, moduleRegistry: ModuleRegistry): Promise<ModuleJsonDefinition>; | ||
/** | ||
@@ -33,0 +32,0 @@ * Given a filepath, ensure it has a file extension by checking for the file on the fs |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.0.2-alpha26", | ||
"version": "0.0.2-alpha27", | ||
"homepage": "https://lwr.dev/", | ||
@@ -27,2 +27,3 @@ "repository": { | ||
"chokidar": "^3.4.0", | ||
"fast-json-stable-stringify": "^2.1.0", | ||
"parse5-sax-parser": "^6.0.1", | ||
@@ -45,3 +46,3 @@ "slugify": "^1.4.5" | ||
}, | ||
"gitHead": "7d101ce35ce090e3dc77c7cb466c319ee646f6dd" | ||
"gitHead": "913e1857772c46d14e177d4f2254cf4e26565c51" | ||
} |
50814
1296
4
+ Addedfast-json-stable-stringify@2.1.0(transitive)