Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add typescript definitions #146

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0-development",
"description": "Bundle Cypress specs using esbuild",
"main": "src",
"typings": "src/index.d.ts",
"files": [
"src"
],
Expand Down
41 changes: 41 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// <reference types="node" />
import { BuildOptions } from 'esbuild';

export interface CypressFileObject {
filePath: string;
outputPath: string;
shouldWatch: boolean;
}
export declare type CypressPlugin = (file: CypressFileObject & NodeJS.EventEmitter) => Promise<string> | string;

export interface BundleOnceParams {
JoaoTMDias marked this conversation as resolved.
Show resolved Hide resolved
filePath: string;
outputPath: string;
esBuildUserOptions: BuildOptions;
}

/**
* Bundles the files only once.
*
* @param {BundleOnceParams} params
* @returns {Promise<void>}
*/
export declare const bundleOnce: ({ filePath, outputPath, esBuildUserOptions, }: BundleOnceParams) => Promise<void>;


/**
* Pass ESBuild options to be applied to each spec file
*
* @example
*
* const bundler = createBundler({
* define: {
* "process.env.NODE_ENV": '"development"'
* }
* });
* on('file:preprocessor', bundler)
*
* @param {BuildOptions} esBuildUserOptions
* @returns
*/
export declare const createBundler: (esBuildUserOptions?: BuildOptions) => CypressPlugin;
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const esbuild = require('esbuild')
const debug = require('debug')('cypress-esbuild-preprocessor')

/**
* @typedef {import("./index.d").BundleOnceParams} BundleOnceParams
* @typedef {import("./index.d").CypressPlugin} CypressPlugin
* @typedef {import("esbuild").BuildOptions} BuildOptions
*/

// bundled[filename] => promise
const bundled = {}

/**
* Bundles the files only once.
*
* @param {BundleOnceParams} params
* @returns {Promise<void>}
*/
const bundleOnce = ({ filePath, outputPath, esBuildUserOptions }) => {
debug('bundleOnce %s', filePath)
const started = +new Date()
Expand All @@ -22,6 +34,21 @@ const bundleOnce = ({ filePath, outputPath, esBuildUserOptions }) => {
})
}

/**
* Pass ESBuild options to be applied to each spec file
*
* @example
*
* const bundler = createBundler({
* define: {
* "process.env.NODE_ENV": '"development"'
* }
* });
* on('file:preprocessor', bundler)
*
* @param {BuildOptions} esBuildUserOptions
* @returns {CypressPlugin}
*/
const createBundler = (esBuildUserOptions = {}) => {
debug('ESBuild user options %o', esBuildUserOptions)

Expand Down