-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from redwoodjs/pp-add-import-all-macro
Add our own `importAll.macro`
- Loading branch information
Showing
16 changed files
with
245 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import path from 'path' | ||
|
||
const BASE_PATH = path.resolve(__dirname, '../__tests__/fixtures') | ||
|
||
export const getPaths = () => ({ | ||
api: { | ||
services: path.resolve(BASE_PATH, './api/src/services'), | ||
graphql: path.resolve(BASE_PATH, './api/src/graphql'), | ||
} | ||
}) |
35 changes: 35 additions & 0 deletions
35
packages/core/src/__tests__/__snapshots__/importAll.macro.test.js.snap
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,35 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`macros api.graphql: api.graphql 1`] = ` | ||
import importAll from '../importAll.macro.js' | ||
const graphQLImports = importAll('api', 'graphql') | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
import * as _UsersPeterpPersonalRedwoodjsRedwoodPackagesCoreSrc__tests__FixturesApiSrcGraphqlPostsSdlTs from '/Users/peterp/Personal/redwoodjs/redwood/packages/core/src/__tests__/fixtures/api/src/graphql/posts.sdl.ts' | ||
import * as _UsersPeterpPersonalRedwoodjsRedwoodPackagesCoreSrc__tests__FixturesApiSrcGraphqlUsersSdlJs from '/Users/peterp/Personal/redwoodjs/redwood/packages/core/src/__tests__/fixtures/api/src/graphql/users.sdl.js' | ||
const graphQLImports = { | ||
posts: _UsersPeterpPersonalRedwoodjsRedwoodPackagesCoreSrc__tests__FixturesApiSrcGraphqlPostsSdlTs, | ||
users: _UsersPeterpPersonalRedwoodjsRedwoodPackagesCoreSrc__tests__FixturesApiSrcGraphqlUsersSdlJs, | ||
} | ||
`; | ||
|
||
exports[`macros api.services: api.services 1`] = ` | ||
import importAll from '../importAll.macro.js' | ||
const serviceImports = importAll('api', 'services') | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
import * as _UsersPeterpPersonalRedwoodjsRedwoodPackagesCoreSrc__tests__FixturesApiSrcServicesPostsTs from '/Users/peterp/Personal/redwoodjs/redwood/packages/core/src/__tests__/fixtures/api/src/services/posts.ts' | ||
import * as _UsersPeterpPersonalRedwoodjsRedwoodPackagesCoreSrc__tests__FixturesApiSrcServicesUsersJs from '/Users/peterp/Personal/redwoodjs/redwood/packages/core/src/__tests__/fixtures/api/src/services/users.js' | ||
const serviceImports = { | ||
posts: _UsersPeterpPersonalRedwoodjsRedwoodPackagesCoreSrc__tests__FixturesApiSrcServicesPostsTs, | ||
users: _UsersPeterpPersonalRedwoodjsRedwoodPackagesCoreSrc__tests__FixturesApiSrcServicesUsersJs, | ||
} | ||
`; |
File renamed without changes.
File renamed without changes.
Empty file.
Empty file.
Empty file.
Empty file.
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,24 @@ | ||
import pluginTester from 'babel-plugin-tester' | ||
import plugin from 'babel-plugin-macros' | ||
|
||
const MACRO_IMPORT_PATH = '../importAll.macro.js' | ||
|
||
jest.mock('../paths') | ||
|
||
pluginTester({ | ||
plugin, | ||
snapshot: true, | ||
babelOptions: { | ||
filename: __filename, | ||
}, | ||
tests: { | ||
'api.services': ` | ||
import importAll from '${MACRO_IMPORT_PATH}' | ||
const serviceImports = importAll('api', 'services') | ||
`, | ||
'api.graphql': ` | ||
import importAll from '${MACRO_IMPORT_PATH}' | ||
const graphQLImports = importAll('api', 'graphql') | ||
`, | ||
}, | ||
}) |
4 changes: 2 additions & 2 deletions
4
packages/core/__tests__/paths.test.ts → packages/core/src/__tests__/paths.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import path from 'path' | ||
|
||
import { createMacro } from 'babel-plugin-macros' | ||
import glob from 'glob' | ||
|
||
import { getPaths } from './paths' | ||
|
||
// The majority of this code is copied from `importAll.macro`: https://github.com/kentcdodds/import-all.macro | ||
// And was modified to work with our `getPaths`. | ||
|
||
/** | ||
* This macro runs during build time. | ||
* @example | ||
* ```js | ||
* import importAll from '@redwoodjs/core/importAll.macro | ||
* const typeDefs = importAll('api', 'graphql') | ||
* ``` | ||
*/ | ||
function prevalMacros({ references, state, babel }) { | ||
references.default.forEach((referencePath) => { | ||
if (referencePath.parentPath.type === 'CallExpression') { | ||
importAll({ referencePath, state, babel }) | ||
} else { | ||
throw new Error( | ||
`This is not supported: \`${referencePath | ||
.findParent(babel.types.isExpression) | ||
.getSource()}\`. Please use "importAll('target', 'directory')"` | ||
) | ||
} | ||
}) | ||
} | ||
|
||
const getGlobPattern = (callExpressionPath) => { | ||
const redwoodPaths = getPaths() | ||
const args = callExpressionPath.parentPath.get('arguments') | ||
const target = args[0].evaluate().value | ||
const dir = args[1].evaluate().value | ||
return `${redwoodPaths[target][dir]}/*.{ts,js}` | ||
} | ||
|
||
function importAll({ referencePath, state, babel }) { | ||
const t = babel.types | ||
const globPattern = getGlobPattern(referencePath) | ||
// Grab a list of the files | ||
const importSources = glob.sync(globPattern) | ||
|
||
const { importNodes, objectProperties } = importSources.reduce( | ||
(all, source) => { | ||
const id = referencePath.scope.generateUidIdentifier(source) | ||
all.importNodes.push( | ||
t.importDeclaration( | ||
[t.importNamespaceSpecifier(id)], | ||
t.stringLiteral(source) | ||
) | ||
) | ||
|
||
// Turn `./relativePath/a.js` into `a`. | ||
const objectKey = path | ||
.basename(source, path.extname(source)) | ||
.replace('.sdl', '') | ||
all.objectProperties.push( | ||
t.objectProperty(t.stringLiteral(objectKey), id) | ||
) | ||
return all | ||
}, | ||
{ importNodes: [], objectProperties: [] } | ||
) | ||
|
||
const program = state.file.path | ||
program.node.body.unshift(...importNodes) | ||
|
||
const objectExpression = t.objectExpression(objectProperties) | ||
referencePath.parentPath.replaceWith(objectExpression) | ||
} | ||
|
||
export default createMacro(prevalMacros) |
File renamed without changes.
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