Skip to content

Commit

Permalink
fix: bring back support for jest
Browse files Browse the repository at this point in the history
closes #137
  • Loading branch information
jasonkuhrt committed Sep 18, 2021
1 parent e94dad3 commit 1705a54
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ dist-*
# TernJS port file
.tern-port

# Facades
# Simulated Package Exports
scalars.d.ts
scalars.js
generator.d.ts
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"license": "MIT",
"files": [
"dist-cjs",
"dist-esm"
"dist-esm",
"scalars.d.ts",
"scalars.js",
"generator.d.ts",
"generator.js"
],
"exports": {
".": {
Expand Down
55 changes: 55 additions & 0 deletions scripts/simulatePackageExports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Simualated Module Exports
*
* This script builds the modules that will be consumed publically. They front the actual code inside ./dist.
* The problem being solved here is that it allows consumers to do e.g. this:
*
* Import { ... } from 'nexus/testing'
*
* Instead of:
*
* Import { ... } from 'nexus/dist/testing'
*
* This is needed to support Jest: https://github.com/prisma/nexus-prisma/issues/137
*
* Whatever modules are written here should be:
*
* 1. ignored in .gitignore. <-- YOU MUST DO THIS MANUALLY
* 2. added to the package.json files array <-- Automated for you
*/

// TODO automate gitignore step

import * as fs from 'fs-jetpack'
import * as lo from 'lodash'
import * as os from 'os'
import * as path from 'path'
import { PackageJson } from 'type-fest'

simulatePackageExports([
['scalars.d.ts', "export * from './dist-cjs/entrypoints/scalars'"],
['scalars.js', "module.exports = require('./dist-cjs/entrypoints/scalars')"],
['generator.d.ts', "export * from './dist-cjs/entrypoints/generator'"],
['generator.js', "module.exports = require('./dist-cjs/entrypoints/generator')"],
])

function simulatePackageExports(facades: ModuleFacade[]) {
// Write facade files

for (const facade of facades) {
fs.write(facade[0], facade[1] + os.EOL)
}

// Handle package.json files array

const packageJsonPath = path.join(__dirname, '..', 'package.json')
const packageJson = fs.read(packageJsonPath, 'json') as PackageJson

packageJson.files = lo.uniq([...(packageJson.files ?? []), ...facades.map((facade) => facade[0])])

const packageJsonString = JSON.stringify(packageJson, null, 2) + os.EOL

fs.write(packageJsonPath, packageJsonString)
}

type ModuleFacade = [filePath: string, fileContents: string]

1 comment on commit 1705a54

@vercel
Copy link

@vercel vercel bot commented on 1705a54 Sep 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.