-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Braydon Hall <40751395+nobrayner@users.noreply.github.com> Co-authored-by: Jacob M-G Evans <27247160+JacobMGEvans@users.noreply.github.com> Co-authored-by: Juhana Jauhiainen <juhana.jauhiainen@gmail.com> Co-authored-by: Peter Hozak <peter.hozak@gmail.com>
- Loading branch information
1 parent
7b0e710
commit 3887e25
Showing
17 changed files
with
549 additions
and
360 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import preval from './dist/macro' | ||
export default preval |
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,2 @@ | ||
import preval from '../../macro' | ||
export default preval |
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 @@ | ||
// this is just here because the import needs to end in "macro" with no extension. | ||
// and if we don't do this, then babel-plugin-macros cannot find the macro.ts file (it's looking for a .js file). | ||
module.exports = require('../../macro.ts') |
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 was deleted.
Oops, something went wrong.
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,79 @@ | ||
import p from 'path' | ||
import requireFromStringOfCode from 'require-from-string' | ||
import type babelCore from '@babel/core' | ||
import objectToAST from './object-to-ast' | ||
|
||
type PrevalModuleExport = unknown | ((...args: Array<any>) => unknown) | ||
|
||
type CompiledESModule = { | ||
__esModule: boolean | ||
default: PrevalModuleExport | ||
} | ||
|
||
function isCompiledESModule(module: unknown): module is CompiledESModule { | ||
return typeof module === 'object' && module !== null && '__esModule' in module | ||
} | ||
|
||
// istanbul ignore next because I don't know how to reproduce a situation | ||
// where the filename doesn't exist, but TypeScript gets mad when I don't handle that case. | ||
const getFilename = (fileOpts: babelCore.TransformOptions): string => | ||
fileOpts.filename ?? '"unknown"' | ||
|
||
type RequireFromStringOptions = { | ||
string: string | Buffer | ||
fileOpts: babelCore.TransformOptions | ||
args?: any[] | ||
} | ||
export function requireFromString({ | ||
string: stringToPreval, | ||
fileOpts, | ||
args = [], | ||
}: RequireFromStringOptions) { | ||
const filename = getFilename(fileOpts) | ||
let module = requireFromStringOfCode(String(stringToPreval), filename) as | ||
| CompiledESModule | ||
| PrevalModuleExport | ||
| unknown | ||
|
||
if (isCompiledESModule(module)) { | ||
// Allow for es modules (default export) | ||
module = module.default | ||
} | ||
|
||
if (typeof module === 'function') { | ||
module = module(...args) | ||
} else if (args.length) { | ||
throw new Error( | ||
`\`preval.require\`-ed module (${p.relative( | ||
process.cwd(), | ||
filename, | ||
)}) cannot accept arguments because it does not export a function. You passed the arguments: ${args.join( | ||
', ', | ||
)}`, | ||
) | ||
} | ||
|
||
return module | ||
} | ||
|
||
type GetReplacementOptions = { | ||
string: string | Buffer | ||
fileOpts: babelCore.TransformOptions | ||
args?: any[] | ||
babel: typeof babelCore | ||
} | ||
|
||
export function getReplacement({ | ||
string, | ||
fileOpts, | ||
args, | ||
babel, | ||
}: GetReplacementOptions) { | ||
const module = requireFromString({string, fileOpts, args}) | ||
return objectToAST(module, {babel, fileOptions: fileOpts}) | ||
} | ||
|
||
/* | ||
eslint | ||
@typescript-eslint/no-explicit-any: "off", | ||
*/ |
Oops, something went wrong.