Skip to content

Commit

Permalink
Update: React project base with Refresh and Linaria support 🎊 (#75)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: React projects must install Refresh and Linaria packages
  • Loading branch information
DHedgecock committed Jun 5, 2021
1 parent e1b116e commit a0a01ed
Show file tree
Hide file tree
Showing 10 changed files with 9,026 additions and 9,049 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,25 @@ module.exports = function babelConfigs() {
target: 'node|react',
})

/* Optionally override the base configs as needed */
/* Optionally override the base configs as needed... */

return baseConfigs
}
```

**3. Setup polyfills**
## React projects

React projects should also setup polyfills for unsupported language features.
**1. Install packages**

```sh
# Install polyfills included in application bundle
npm i core-js regenerator-runtime -E

# Install Linaria preset as a dev dependency
npm i @linaria/babel-preset react-refresh -DE
```

ℹ️ Note that because these packages will be included in the application bundle
they should be installed as `dependencies`.
**2. Import polyfills**

```javascript
// src/index.js
Expand Down
17,935 changes: 8,933 additions & 9,002 deletions package-lock.json

Large diffs are not rendered by default.

33 changes: 16 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,26 @@
"clean": "rm -rf dist types"
},
"dependencies": {
"@babel/core": "7.12.7",
"@babel/plugin-proposal-class-properties": "7.12.1",
"@babel/plugin-proposal-private-methods": "7.12.1",
"@babel/plugin-transform-runtime": "7.12.1",
"@babel/preset-env": "7.12.7",
"@babel/preset-react": "7.12.7",
"@babel/preset-typescript": "7.12.7",
"@babel/core": "7.14.3",
"@babel/plugin-proposal-class-properties": "7.13.0",
"@babel/plugin-proposal-private-methods": "7.13.0",
"@babel/plugin-transform-runtime": "7.14.3",
"@babel/preset-env": "7.14.4",
"@babel/preset-react": "7.13.13",
"@babel/preset-typescript": "7.13.0",
"babel-plugin-transform-import-aliases": "1.2.0",
"babel-plugin-transform-react-remove-prop-types": "0.4.24"
},
"devDependencies": {
"@babel/cli": "7.12.7",
"@crystal-ball/commitizen-base": "2.9.0",
"@crystal-ball/prettier-base": "2.4.0",
"@crystal-ball/semantic-release-base": "2.23.0",
"@types/jest": "26.0.15",
"@types/node": "14.14.9",
"eslint-config-eloquence": "16.7.1",
"husky": "4.3.0",
"jest": "26.6.3",
"typescript": "4.1.2"
"@babel/cli": "7.14.3",
"@crystal-ball/commitizen-base": "2.11.0",
"@crystal-ball/prettier-base": "2.7.0",
"@crystal-ball/semantic-release-base": "2.32.0",
"@types/jest": "26.0.23",
"@types/node": "15.12.1",
"eslint-config-eloquence": "19.1.0",
"jest": "27.0.4",
"typescript": "4.3.2"
},
"config": {
"commitizen": {
Expand Down
42 changes: 35 additions & 7 deletions src/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

exports[`babelBase when called with target node, then node babel configs are returned 1`] = `
Object {
"env": Object {},
"plugins": Array [
Array [
"@babel/plugin-transform-runtime",
Object {
"useESModules": false,
"version": "7.12.5",
"version": "7.14.0",
},
],
Array [
Expand Down Expand Up @@ -37,19 +38,28 @@ Object {
exports[`babelBase when called with target react for dev, then development react configs are returned 1`] = `
Object {
"env": Object {
"development": Object {},
"development": Object {
"plugins": Array [
"react-refresh/babel",
],
},
"production": Object {
"plugins": Array [
"transform-react-remove-prop-types",
],
},
"test": Object {
"presets": Array [
"@linaria",
],
},
},
"plugins": Array [
Array [
"@babel/plugin-transform-runtime",
Object {
"useESModules": true,
"version": "7.12.5",
"version": "7.14.0",
},
],
Array [
Expand Down Expand Up @@ -86,19 +96,28 @@ Object {
exports[`babelBase when called with target react for prod, then production react configs are returned 1`] = `
Object {
"env": Object {
"development": Object {},
"development": Object {
"plugins": Array [
"react-refresh/babel",
],
},
"production": Object {
"plugins": Array [
"transform-react-remove-prop-types",
],
},
"test": Object {
"presets": Array [
"@linaria",
],
},
},
"plugins": Array [
Array [
"@babel/plugin-transform-runtime",
Object {
"useESModules": true,
"version": "7.12.5",
"version": "7.14.0",
},
],
Array [
Expand Down Expand Up @@ -135,19 +154,28 @@ Object {
exports[`babelBase when called with target react for test, then test react configs are returned 1`] = `
Object {
"env": Object {
"development": Object {},
"development": Object {
"plugins": Array [
"react-refresh/babel",
],
},
"production": Object {
"plugins": Array [
"transform-react-remove-prop-types",
],
},
"test": Object {
"presets": Array [
"@linaria",
],
},
},
"plugins": Array [
Array [
"@babel/plugin-transform-runtime",
Object {
"useESModules": false,
"version": "7.12.5",
"version": "7.14.0",
},
],
Array [
Expand Down
8 changes: 4 additions & 4 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ jest.mock('path')
import { babelBase } from '.'

describe('babelBase', () => {
test('when called with target node, then node babel configs are returned', () => {
it('when called with target node, then node babel configs are returned', () => {
expect(babelBase({ env: 'development', target: 'node' })).toMatchSnapshot()
})

test('when called with target react for dev, then development react configs are returned', () => {
it('when called with target react for dev, then development react configs are returned', () => {
expect(babelBase({ env: 'development', target: 'react' })).toMatchSnapshot()
})

test('when called with target react for test, then test react configs are returned', () => {
it('when called with target react for test, then test react configs are returned', () => {
expect(babelBase({ env: 'test', target: 'react' })).toMatchSnapshot()
})

test('when called with target react for prod, then production react configs are returned', () => {
it('when called with target react for prod, then production react configs are returned', () => {
expect(babelBase({ env: 'production', target: 'react' })).toMatchSnapshot()
})
})
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
* Babel entry
*/

import { TransformOptions } from '@babel/core'

import { Env } from './types'
import nodeConfigs from './node'
import reactConfigs from './react'
import { BaseTransformOptions, Env } from './types'

type Options = {
env: Env
target: 'node' | 'react'
}

export function babelBase({ env, target }: Options): TransformOptions {
export function babelBase({ env, target }: Options): BaseTransformOptions {
return target === 'node' ? nodeConfigs() : reactConfigs(env)
}
5 changes: 3 additions & 2 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs'
import path from 'path'

import { TransformOptions } from '@babel/core'
import { BaseTransformOptions } from './types'

export default function nodeConfigs(): TransformOptions {
export default function nodeConfigs(): BaseTransformOptions {
return {
presets: [
[
Expand Down Expand Up @@ -57,5 +57,6 @@ export default function nodeConfigs(): TransformOptions {
{ aliases: { '@': path.resolve(fs.realpathSync(process.cwd()), 'src') } },
],
],
env: {},
}
}
20 changes: 15 additions & 5 deletions src/react.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import fs from 'fs'
import path from 'path'

import { TransformOptions } from '@babel/core'

import { Env } from './types'
import { BaseTransformOptions, Env } from './types'
import { testEnv } from './test-env'

/**
Expand Down Expand Up @@ -47,7 +45,7 @@ import { testEnv } from './test-env'
* compilation) Note that enabling this requires adding `@babel/runtime-corejs3`
* as a dependency.
*/
export default function reactConfigs(env: Env): TransformOptions {
export default function reactConfigs(env: Env): BaseTransformOptions {
return {
// --------------------------------------------------------
// Presets
Expand Down Expand Up @@ -114,16 +112,28 @@ export default function reactConfigs(env: Env): TransformOptions {
],
],

// --------------------------------------------------------
// Environments
env: {
development: {
/* Namespace created for easy assingment of dev options by consumers */
plugins: [
// Babel transform for "Fast Refresh"
'react-refresh/babel',
],
},
production: {
plugins: [
// Strip component prop types in production builds
'transform-react-remove-prop-types',
],
},
test: {
presets: [
// The linaria webpack-loader includes this preset under the hood, provide it for
// test envs to prevent crashes
'@linaria',
],
},
},
}
}
6 changes: 3 additions & 3 deletions src/test-env.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { testEnv } from './test-env'

describe('babelBase', () => {
test('returns true for a single environment', () => {
it('returns true for a single environment', () => {
expect(testEnv('development', ['development'])).toBe(true)
})

test('returns true for a multiple environments', () => {
it('returns true for a multiple environments', () => {
expect(testEnv('development', ['development', 'test'])).toBe(true)
})

test('returns false when unmatched', () => {
it('returns false when unmatched', () => {
expect(testEnv('development', ['production'])).toBe(false)
})
})
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
import { TransformOptions } from '@babel/core'

/** Supported environment values */
export type Env = 'development' | 'production' | 'test'

/** Package transform options includes an env map by default */
export type BaseTransformOptions = TransformOptions & {
env: { [environment: string]: TransformOptions }
}

0 comments on commit a0a01ed

Please sign in to comment.