mirror of
https://github.com/cypress-io/cypress.git
synced 2026-05-01 04:20:23 -05:00
b0c0eaa508
Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com> Co-authored-by: Zach Bloomquist <git@chary.us> Co-authored-by: Tyler Biethman <tbiethman@users.noreply.github.com> Co-authored-by: Matt Henkes <mjhenkes@gmail.com> Co-authored-by: Chris Breiding <chrisbreiding@users.noreply.github.com> Co-authored-by: Matt Schile <mschile@cypress.io> Co-authored-by: Mark Noonan <mark@cypress.io> Co-authored-by: Zachary Williams <ZachJW34@gmail.com> Co-authored-by: Ben M <benm@cypress.io> Co-authored-by: Zachary Williams <zachjw34@gmail.com> Co-authored-by: astone123 <adams@cypress.io> Co-authored-by: Bill Glesias <bglesias@gmail.com> Co-authored-by: Emily Rohrbough <emilyrohrbough@yahoo.com> Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com> Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net> Co-authored-by: Adam Stone <adams@cypress.io> Co-authored-by: Blue F <blue@cypress.io> Co-authored-by: GitStart <1501599+gitstart@users.noreply.github.com> Co-authored-by: Mike Plummer <mike-plummer@users.noreply.github.com> Co-authored-by: Jordan <jordan@jpdesigning.com> Co-authored-by: Sam Goodger <turbo@tailz.dev> Co-authored-by: Colum Ferry <cferry09@gmail.com> Co-authored-by: Stokes Player <stokes@cypress.io> Co-authored-by: Vilhelm Melkstam <vilhelm.melkstam@gmail.com> Co-authored-by: amehta265 <65267668+amehta265@users.noreply.github.com>
79 lines
2.7 KiB
TypeScript
79 lines
2.7 KiB
TypeScript
import path from 'path'
|
|
|
|
import type { Metafile } from 'esbuild'
|
|
import type { CreateBundle } from './types'
|
|
import { getMetadata } from './get-metadata'
|
|
|
|
const packherd = require('../package.json').name
|
|
|
|
export type PathsMapper = (s: string) => string
|
|
|
|
export const identityMapper: PathsMapper = (s: string) => s
|
|
|
|
/**
|
|
* The EntryGenerator creates an entry file which includes all dependencies reachable from the [entryFilePath] but
|
|
* flattened. This is helpful while diagnosing problems as it is easier to exclude a dependency simply by commenting it
|
|
* out in the generated entry file. This entry file is then provided to packherd instead of the main app file when
|
|
* generating the bundle.
|
|
* Combined with the `nodeModulesOnly` options allows to control which files are included in the bundle in the end that
|
|
* way.
|
|
*
|
|
* @category Bundle
|
|
*/
|
|
export class EntryGenerator {
|
|
private readonly entryDirectory: string
|
|
|
|
/**
|
|
* Creates an instance of the EntryGenerator.
|
|
*
|
|
* @param createBundle create bundle function which calls into esbuild
|
|
* @param entryFilePath file that is the apps main entry and (in)directly references all app and node_modules
|
|
* @param nodeModulesOnly if `true` only `node_modules` are included in the entry file
|
|
* @param pathsMapper if provided the module paths are mapped with it
|
|
*
|
|
* @category Bundle
|
|
*/
|
|
constructor (
|
|
private readonly createBundle: CreateBundle,
|
|
private readonly entryFile: string,
|
|
private readonly nodeModulesOnly: boolean = true,
|
|
private readonly pathsMapper: PathsMapper = identityMapper,
|
|
) {
|
|
this.entryDirectory = path.dirname(entryFile)
|
|
}
|
|
|
|
async createEntryScript () {
|
|
const meta = await this._getMetadata()
|
|
|
|
// Make paths relative to the entry dir and sort them by name
|
|
const relToCwdPaths = this._resolveRelativePaths(meta)
|
|
|
|
relToCwdPaths.sort()
|
|
|
|
const fullPaths = relToCwdPaths.map((x) => path.join(process.cwd(), x))
|
|
const paths = fullPaths.map((x) => path.relative(this.entryDirectory, x)).map((x) => x.split(path.sep).join(path.posix.sep))
|
|
|
|
const entry = ['// vim: set ft=text:']
|
|
.concat(paths.map((x) => `exports['./${x}'] = require('./${x}')`))
|
|
.join('\n')
|
|
|
|
return { paths, entry }
|
|
}
|
|
|
|
private _getMetadata (): Promise<Metafile> {
|
|
return getMetadata(this.createBundle, this.entryFile, this.entryDirectory)
|
|
}
|
|
|
|
private _resolveRelativePaths (meta: Metafile) {
|
|
let relPaths = Object.keys(meta.inputs).filter((x) => !x.includes(packherd))
|
|
|
|
if (this.nodeModulesOnly) {
|
|
relPaths = relPaths.filter((x) => x.includes('node_modules'))
|
|
}
|
|
|
|
return relPaths
|
|
.map((x) => x.replace(/^node_modules\//, './node_modules/'))
|
|
.map(this.pathsMapper)
|
|
}
|
|
}
|