import normalizePath from "normalize-path" ; import { Entrypoint } from "./entrypoint" ; import { Package } from "./package" ; import * as nodePath from "path" ; import { FatalError } from "./errors" ; export function getNameForDistForEntrypoint ( entrypoint ) { return getDistName ( entrypoint . package , entrypoint . name ) ; } let fields = [ "version" , "description" , "main" , "module" , "umd:main" , "browser" , ] ; export function setFieldInOrder < Obj extends { [ key ] : any ; } , Key extends "main" | "module" | "umd:main" | "browser" , Val extends any > ( obj , field , value ) { if ( field in obj ) { let newObj = { ... obj } ; ( newObj as any ) [ field ] = value ; return newObj ; } let fieldIndex = fields . indexOf ( field ) ; let idealField = fields . slice ( 0 , fieldIndex ) . reverse ( ) . find ( ( key ) => { return key in obj ; } ) ; if ( idealField === undefined ) { return { ... obj , [ field ] : value } ; } let newObj = { } ; for ( let key in obj ) { newObj [ key ] = obj [ key ] ; if ( key === idealField ) { newObj [ field ] = value ; } } return newObj as any ; } export function getEntrypointName ( pkg , entrypointDir ) { return normalizePath ( nodePath . join ( pkg . name , nodePath . relative ( pkg . directory , nodePath . resolve ( pkg . directory , entrypointDir ) ) ) ) ; } type DistFilenameStrategy = "full" | "unscoped-package-name" ; function getDistNameWithStrategy ( pkg , entrypointName , strategy ) { if ( strategy === "full" ) { return entrypointName . replace ( "@" , "" ) . replace ( /\//g , "-" ) ; } return pkg . name . replace ( /.*\// , "" ) ; } function getDistName ( pkg , entrypointName , forceStrategy ? ) { if ( forceStrategy ) { return getDistNameWithStrategy ( pkg , entrypointName , forceStrategy ) ; } if ( "distFilenameStrategy" in pkg . project . json . preconstruct ) { if ( pkg . project . json . preconstruct . distFilenameStrategy !== "full" && pkg . project . json . preconstruct . distFilenameStrategy !== "unscoped-package-name" ) { throw new FatalError ( `distFilenameStrategy is defined in your Preconstruct config as ${ JSON . stringify ( pkg . project . json . preconstruct . distFilenameStrategy ) } but the only accepted values are "full" and "unscoped-package-name"` , pkg . project . name ) ; } if ( pkg . project . json . preconstruct . distFilenameStrategy === "unscoped-package-name" ) { return getDistNameWithStrategy ( pkg , entrypointName , "unscoped-package-name" ) ; } } return getDistNameWithStrategy ( pkg , entrypointName , "full" ) ; } export const validFieldsFromPkg = { main ( pkg , entrypointName , forceStrategy ? ) { let safeName = getDistName ( pkg , entrypointName , forceStrategy ) ; return `dist/${ safeName }.cjs.js` ; } , module ( pkg , entrypointName , forceStrategy ? ) { let safeName = getDistName ( pkg , entrypointName , forceStrategy ) ; return `dist/${ safeName }.esm.js` ; } , "umd:main" ( pkg , entrypointName , forceStrategy ? ) { let safeName = getDistName ( pkg , entrypointName , forceStrategy ) ; return `dist/${ safeName }.umd.min.js` ; } , browser ( pkg , hasModuleBuild , entrypointName , forceStrategy ? ) { let safeName = getDistName ( pkg , entrypointName , forceStrategy ) ; let obj = { [ `./dist/${ safeName }.cjs.js` ] : `./dist/${ safeName }.browser.cjs.js` , } ; if ( hasModuleBuild ) { obj [ `./dist/${ safeName }.esm.js` ] = `./dist/${ safeName }.browser.esm.js` ; } return obj ; } , } ; export const validFields = { main ( entrypoint , forceStrategy ? ) { return validFieldsFromPkg . main ( entrypoint . package , entrypoint . name , forceStrategy ) ; } , module ( entrypoint , forceStrategy ? ) { return validFieldsFromPkg . module ( entrypoint . package , entrypoint . name , forceStrategy ) ; } , "umd:main" ( entrypoint , forceStrategy ? ) { return validFieldsFromPkg [ "umd:main" ] ( entrypoint . package , entrypoint . name , forceStrategy ) ; } , browser ( entrypoint , forceStrategy ? ) { return validFieldsFromPkg . browser ( entrypoint . package , entrypoint . json . module !== undefined , entrypoint . name , forceStrategy ) ; } , } ; export function flowTemplate ( hasDefaultExport , relativePath ) { const escapedPath = JSON . stringify ( relativePath ) ; return `// @flow
export * from ${ escapedPath };${ hasDefaultExport ? `\nexport { default } from ${ escapedPath };` : "" }\n` ; } export function tsTemplate ( hasDefaultExport , relativePath ) { const escapedPath = JSON . stringify ( relativePath ) ; return `export * from ${ escapedPath };${ hasDefaultExport ? `\nexport { default } from ${ escapedPath };` : "" }\n` ; } export type JSONValue = string | number | boolean | null | Array < JSONValue > | { [ key ] : JSONValue | undefined ; } ;