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

feat(lib): export useFieldPlugin react hook #224

Merged
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"build:lib": "yarn workspace @storyblok/field-plugin build",
"build:cli": "yarn workspace @storyblok/field-plugin-cli build",
"build:container": "yarn build:lib && yarn workspace container build",
"build:helpers": "yarn workspaces foreach --include \"helper-*\" run build",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Starting the package names with helper- is useful in this case.

"test": "jest",
"check:types": "yarn workspaces foreach run check:types",
"lint": "eslint .",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/templates/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@vitejs/plugin-react": "^3.1.0",
"eslint": "latest",
"eslint-plugin-react": "7.30.0",
"typescript": "^4.9.3",
"typescript": "5.1.6",
johannes-lindgren marked this conversation as resolved.
Show resolved Hide resolved
"vite": "^4.2.2",
"vite-plugin-css-injected-by-js": "3.1.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/templates/react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import FieldPlugin from './components/FieldPlugin'
import FieldPluginExample from './components/FieldPluginExample'
import { FunctionComponent } from 'react'
import { FieldPluginProvider } from './FieldPluginProvider'
import { FieldPluginProvider } from '@storyblok/field-plugin/react'

const App: FunctionComponent = () => {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FunctionComponent } from 'react'
import { useFieldPlugin } from '../useFieldPlugin'
import { useFieldPlugin } from '@storyblok/field-plugin/react'

const FieldPlugin: FunctionComponent = () => {
const plugin = useFieldPlugin()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FunctionComponent, useState } from 'react'
import { useFieldPlugin } from '../../useFieldPlugin'
import { useFieldPlugin } from '@storyblok/field-plugin/react'
import { Asset } from '@storyblok/field-plugin'

const AssetSelector: FunctionComponent = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FunctionComponent } from 'react'
import { useFieldPlugin } from '../../useFieldPlugin'
import { useFieldPlugin } from '@storyblok/field-plugin/react'

const Counter: FunctionComponent = () => {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FunctionComponent } from 'react'
import { useFieldPlugin } from '../../useFieldPlugin'
import { useFieldPlugin } from '@storyblok/field-plugin/react'

const ModalToggle: FunctionComponent = () => {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Counter from './Counter'
import ModalToggle from './ModalToggle'
import AssetSelector from './AssetSelector'
import { FunctionComponent } from 'react'
import { useFieldPlugin } from '../../useFieldPlugin'
import { useFieldPlugin } from '@storyblok/field-plugin/react'

const FieldPlugin: FunctionComponent = () => {
const { data, actions } = useFieldPlugin()
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/templates/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"moduleResolution": "bundler",
johannes-lindgren marked this conversation as resolved.
Show resolved Hide resolved
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
Expand Down
11 changes: 9 additions & 2 deletions packages/field-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
"exports": {
".": {
"import": "./dist/field-plugin.js",
"require": "./dist/field-plugin.umd.cjs"
"require": "./dist/field-plugin.umd.cjs",
"types": "./dist/index.d.ts"
},
"./react": {
"import": "./dist/react/index.js",
"require": "./dist/react/index.umd.cjs",
"types": "./dist/react/index.d.ts"
johannes-lindgren marked this conversation as resolved.
Show resolved Hide resolved
}
},
"repository": {
Expand All @@ -25,7 +31,8 @@
"check:types": "tsc --noEmit",
"dev": "vite build --watch",
"test": "jest",
"build": "vite build"
"build": "rm -rf dist && vite build && yarn build:helpers && yarn copy-helpers",
"copy-helpers": "./scripts/copy-helpers.sh"
},
"devDependencies": {
"@types/jest": "29.0.3",
Expand Down
4 changes: 4 additions & 0 deletions packages/field-plugin/scripts/copy-helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

mkdir ./dist/react
cp -r ../helper-react/dist/* ./dist/react/
3 changes: 2 additions & 1 deletion packages/field-plugin/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default defineConfig({
lib: {
// Could also be a dictionary or array of multiple entry points
entry: fileURLToPath(new URL('./src/index.ts', import.meta.url)),
name: 'field-plugin',
name: 'FieldPlugin',
fileName: 'field-plugin',
johannes-lindgren marked this conversation as resolved.
Show resolved Hide resolved
},
emptyOutDir: false,
},
Expand Down
14 changes: 14 additions & 0 deletions packages/helper-react/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': 'warn',
},
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is generated by yarn create vite. We can always revisit this if we want something different. Now the tooling for the helper is isolated within this package :)

24 changes: 24 additions & 0 deletions packages/helper-react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
29 changes: 29 additions & 0 deletions packages/helper-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "helper-react",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0 --resolve-plugins-relative-to ."
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something I've learned:

I added --resolve-plugins-relative-to . to the eslint command because eslint was traveling up to the root of this repository to gather all the eslint configs. By applying this parameter, it forces eslint to only look for the configs and plugins within this folder.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be possible to set "root": true in the eslint config file? https://eslint.org/docs/latest/use/configure/configuration-files#cascading-and-hierarchy

},
"dependencies": {
"@storyblok/field-plugin": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.37",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"@vitejs/plugin-react": "^4.0.0",
"eslint": "^8.38.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"typescript": "^5.0.2",
"vite": "^4.3.9",
"vite-plugin-dts": "3.0.3"
}
}
6 changes: 6 additions & 0 deletions packages/helper-react/src/context.ts
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we rename this to FieldPluginContext.ts?

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { FieldPluginResponse } from '@storyblok/field-plugin'
import { createContext } from 'react'

export const FieldPluginContext = createContext<
Extract<FieldPluginResponse, { type: 'loaded' }> | undefined
>(undefined)
3 changes: 3 additions & 0 deletions packages/helper-react/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './context'
export * from './provider'
export * from './useFieldPlugin'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we rename this to FieldPluginProvider.ts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import {
FunctionComponent,
createContext,
useEffect,
useState,
ComponentType,
} from 'react'
import { FunctionComponent, useEffect, useState, ComponentType } from 'react'
import {
type FieldPluginResponse,
createFieldPlugin,
} from '@storyblok/field-plugin'
import { FieldPluginContext } from './context'
import { ReactNode } from 'react'

export const FieldPluginContext = createContext<
Extract<FieldPluginResponse, { type: 'loaded' }> | undefined
>(undefined)

type Props = {
Error?: ComponentType<{ error: Error }>
Loading?: ComponentType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FieldPluginResponse } from '@storyblok/field-plugin'
import { useContext } from 'react'
import { FieldPluginContext } from './FieldPluginProvider'
import { FieldPluginContext } from './context'

export const useFieldPlugin = (): Extract<
FieldPluginResponse,
Expand Down
25 changes: 25 additions & 0 deletions packages/helper-react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
johannes-lindgren marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions packages/helper-react/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
johannes-lindgren marked this conversation as resolved.
Show resolved Hide resolved
27 changes: 27 additions & 0 deletions packages/helper-react/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { PluginOption, defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import dts from 'vite-plugin-dts'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig({
// there is a typing problem with the dts package
plugins: [dts() as unknown as PluginOption, react()],
johannes-lindgren marked this conversation as resolved.
Show resolved Hide resolved
build: {
lib: {
entry: resolve(__dirname, 'src/index.ts'),
name: 'react-helper',
fileName: 'index',
},
rollupOptions: {
external: ['react', 'react-dom', '@storyblok/field-plugin'],
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'@storyblok/field-plugin': 'FieldPlugin',
},
},
Comment on lines +17 to +24
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although we're depending on these 3 libraries, we're excluding them in the final bundle output.

Copy link
Contributor

Choose a reason for hiding this comment

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

this part is a bit unclear to me...are we excluding the dependencies because we are bundling it together with the field plugin package and therefore they are already bundled there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll show you this on Wednesday. Don't forget to ask! :)

},
},
})
Loading