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

[Core] Convert custom webpack cell-loader into Babel Plugin #512

Merged
merged 6 commits into from
May 9, 2020
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
28 changes: 28 additions & 0 deletions packages/core/config/__tests__/__fixtures__/commented/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const QUERY = gql`
query {
posts {
id
title
body
}
}
`

// export const beforeQuery = () => ({})
export const afterQuery = () => ({})

export function Loading() {
return 'Loading'
}

// export function Empty() {
// return 'Empty'
// }

export function Failure({ error }) {
return error.message
}

export const Success = ({ posts }) => {
return JSON.stringify(posts, null, 2)
}
31 changes: 31 additions & 0 deletions packages/core/config/__tests__/__fixtures__/commented/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { withCell } from '@redwoodjs/web'
export const QUERY = gql`
query {
posts {
id
title
body
}
}
` // export const beforeQuery = () => ({})

export const afterQuery = () => ({})
export function Loading() {
return 'Loading'
} // export function Empty() {
// return 'Empty'
// }

export function Failure({ error }) {
return error.message
}
export const Success = ({ posts }) => {
return JSON.stringify(posts, null, 2)
}
export default withCell({
QUERY,
afterQuery,
Loading,
Failure,
Success,
})
28 changes: 28 additions & 0 deletions packages/core/config/__tests__/__fixtures__/complete-cell/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const QUERY = gql`
query {
posts {
id
title
body
}
}
`

export const beforeQuery = () => ({})
export const afterQuery = () => ({})

export function Loading() {
return 'Loading'
}

export function Empty() {
return 'Empty'
}

export function Failure({ error }) {
return error.message
}

export const Success = ({ posts }) => {
return JSON.stringify(posts, null, 2)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { withCell } from '@redwoodjs/web'
export const QUERY = gql`
query {
posts {
id
title
body
}
}
`
export const beforeQuery = () => ({})
export const afterQuery = () => ({})
export function Loading() {
return 'Loading'
}
export function Empty() {
return 'Empty'
}
export function Failure({ error }) {
return error.message
}
export const Success = ({ posts }) => {
return JSON.stringify(posts, null, 2)
}
export default withCell({
QUERY,
beforeQuery,
afterQuery,
Loading,
Empty,
Failure,
Success,
})
11 changes: 11 additions & 0 deletions packages/core/config/__tests__/babel-plugin-redwood-cell.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import path from 'path'

import pluginTester from 'babel-plugin-tester'

import redwoodCellPlugin from '../babel-plugin-redwood-cell'

pluginTester({
plugin: redwoodCellPlugin,
pluginName: 'Redwood Cell',
fixtures: path.join(__dirname, '__fixtures__'),
})
82 changes: 82 additions & 0 deletions packages/core/config/babel-plugin-redwood-cell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// This is supposed to wrap a file that's has a suffix of `Cell` in Redwood's `withCell` higher order component.
// The HOC is responsible for the lifecycle methods during a graphQL query.
// The end result of this plugin is something like:
// ```js
// import { withCell } from '@redwoodjs/web'
// export default withCell({ QUERY, Loading, Succes, Failure, Empty, beforeQuery, afterQuery })
// ```
module.exports = function ({ types: t }) {
RobertBroersma marked this conversation as resolved.
Show resolved Hide resolved
// These are the expected named exports from a Cell file.
const EXPECTED_EXPORTS_FROM_CELL = [
'QUERY',
'Loading',
'Success',
'Failure',
'Empty',
'beforeQuery',
'afterQuery',
]

// This array will
// - collect exports from the Cell file during ExportNamedDeclaration
// - collected exports will then be passed to `withCell`
// - be cleared after Program exit to prepare for the next file
let exportNames = []

return {
visitor: {
ExportNamedDeclaration(path) {
const { declaration } = path.node

let name
if (declaration.type === 'VariableDeclaration') {
name = declaration.declarations[0].id.name
}

if (declaration.type === 'FunctionDeclaration') {
name = declaration.id.name
}

if (EXPECTED_EXPORTS_FROM_CELL.includes(name)) {
exportNames.push(name)
}
},
Program: {
exit(path) {
// import { withCell } from '@redwoodjs/web'
path.node.body.unshift(
t.importDeclaration(
[
t.importSpecifier(
t.identifier('withCell'),
t.identifier('withCell')
),
],
t.stringLiteral('@redwoodjs/web')
)
)

// export default withCell({ QUERY?, Loading?, Succes?, Failure?, Empty?, beforeQuery?, afterQuery? })
path.node.body.push(
t.exportDefaultDeclaration(
t.callExpression(t.identifier('withCell'), [
t.objectExpression(
exportNames.map((name) =>
t.objectProperty(
t.identifier(name),
t.identifier(name),
false,
true
)
)
),
])
)
)

exportNames = []
RobertBroersma marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
}
}
4 changes: 4 additions & 0 deletions packages/core/config/babel-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,9 @@ module.exports = () => ({
],
],
},
{
test: /.+Cell.(js|tsx)$/,
plugins: [require('./babel-plugin-redwood-cell')],
},
],
})
9 changes: 0 additions & 9 deletions packages/core/config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,6 @@ module.exports = (webpackEnv) => {
),
},
},
{
// "Create" an `index.js` file adjacent to a user's Cell.js|tsx file
// so that the Cell exports are run through the `withCell` HOC
test: /.+Cell.(js|tsx)$/,
include: path.join(redwoodPaths.base, 'web/src/components'),
use: {
loader: path.resolve(__dirname, '../dist/loaders/cell-loader.js'),
},
},
],
},
optimization: {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/*.test.[jt]s?(x)'],
testPathIgnorePatterns: ['fixtures', '__fixtures__'],
}
7 changes: 6 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
"scripts": {
"build": "yarn cross-env NODE_ENV=production babel src --out-dir dist --extensions \".ts\" --delete-dir-on-start",
"prepublishOnly": "yarn build",
"build:watch": "nodemon --watch src --ext 'js,ts,tsx' --ignore dist --exec 'yarn build'"
"build:watch": "nodemon --watch src --ext 'js,ts,tsx' --ignore dist --exec 'yarn build'",
"test": "jest",
"test:watch": "yarn test --watch"
},
"devDependencies": {
"babel-plugin-tester": "^9.0.1"
}
}