-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
242 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { resolve } from 'path'; | ||
|
||
import loader, { getFunctionLoader } from './function'; | ||
|
||
describe('getFunctionLoader', () => { | ||
it('returns a loader definition', () => { | ||
const fn = jest.fn(); | ||
expect( | ||
getFunctionLoader(fn, { | ||
foo: 'bar', | ||
}), | ||
).toStrictEqual({ | ||
loader: resolve(__dirname, 'function.ts'), | ||
options: { | ||
fn, | ||
foo: 'bar', | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('loader', () => { | ||
it('executes the function', async () => { | ||
const fn = jest.fn(); | ||
|
||
await loader.call( | ||
// @ts-expect-error - Partial `this` object. | ||
{ | ||
getOptions: () => ({ | ||
fn, | ||
}), | ||
}, | ||
'test', | ||
); | ||
|
||
expect(fn).toHaveBeenCalledWith('test'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { LoaderDefinitionFunction } from 'webpack'; | ||
|
||
/** | ||
* Options for the function loader. | ||
*/ | ||
export type FunctionLoaderOptions = { | ||
/** | ||
* The function to execute. This is bound to the loader context, so it can | ||
* access the loader options and other properties. | ||
*/ | ||
fn: LoaderDefinitionFunction; | ||
}; | ||
|
||
/** | ||
* A loader that executes a function. See {@link getFunctionLoader} for more | ||
* information. | ||
* | ||
* @param content - The input file contents as a `Uint8Array`. | ||
* @returns The output of the function. | ||
*/ | ||
const loader: LoaderDefinitionFunction<FunctionLoaderOptions> = function ( | ||
content, | ||
) { | ||
const { fn } = this.getOptions(); | ||
return fn.bind(this)(content); | ||
}; | ||
|
||
export default loader; | ||
|
||
/** | ||
* Get a loader that executes the given function. This is useful for executing | ||
* loaders without needing to pass a file to Webpack. | ||
* | ||
* @param fn - The function to execute. | ||
* @param options - The options to pass to the loader. | ||
* @returns The loader definition. | ||
*/ | ||
export function getFunctionLoader<Options>( | ||
fn: LoaderDefinitionFunction<Options>, | ||
options: Options, | ||
) { | ||
return { | ||
// We use `__filename` as the loader, so Webpack will execute the loader in | ||
// this file, with the actual function in the options. | ||
loader: __filename, | ||
options: { | ||
fn, | ||
...options, | ||
}, | ||
}; | ||
} | ||
|
||
// When running as CJS, we need to export the loader as a default export, since | ||
// `tsup` exports it as `loader_default`. | ||
// istanbul ignore next 3 | ||
// eslint-disable-next-line n/no-process-env | ||
if (typeof module !== 'undefined' && process?.env?.NODE_ENV !== 'test') { | ||
module.exports = loader; | ||
module.exports.getFunctionLoader = getFunctionLoader; | ||
module.exports.raw = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './function'; | ||
export { default as browserify } from './browserify'; | ||
export { default as wasm } from './wasm'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.