Skip to content

Commit a7ba5af

Browse files
authored
Replace CJS-only build with ESM and CJS w/o NODE_ENV (#66)
* Remove last remaining NODE_ENV switch * Replace build outputs with .es.js + .js NOTE: This can't safely use package.json:exports since react isn't an ESM build and this would lead to issues as per the standard ESM module resolution which must start only resolving .mjs by default once it hits an ESM package. * Add script to move typings to dist output * Update copy-typings.js
1 parent 71a2812 commit a7ba5af

File tree

8 files changed

+202
-235
lines changed

8 files changed

+202
-235
lines changed

index.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

package.json

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22
"name": "react-ssr-prepass",
33
"version": "1.3.0",
44
"description": "A custom partial React SSR renderer for prefetching and suspense",
5-
"main": "index.js",
5+
"main": "dist/react-ssr-prepass.js",
6+
"module": "dist/react-ssr-prepass.es.js",
7+
"types": "dist/react-ssr-prepass.d.ts",
68
"author": "Phil Plückthun <phil.pluckthun@formidable.com>",
79
"license": "MIT",
810
"repository": "git@github.com:FormidableLabs/react-ssr-prepass.git",
911
"bugs": {
1012
"url": "https://github.com/FormidableLabs/react-ssr-prepass/issues"
1113
},
1214
"files": [
13-
"index.js",
14-
"index.js.flow",
15-
"dist",
16-
"index.d.ts"
15+
"dist"
1716
],
1817
"sideEffects": false,
19-
"types": "index.d.ts",
2018
"scripts": {
2119
"prepublishOnly": "run-s flow test build",
2220
"build": "rollup -c rollup.config.js",
21+
"postbuild": "node ./scripts/copy-typings.js",
2322
"test": "jest",
2423
"flow": "flow check"
2524
},
@@ -54,17 +53,16 @@
5453
},
5554
"devDependencies": {
5655
"@ampproject/rollup-plugin-closure-compiler": "^0.26.0",
57-
"@babel/core": "^7.13.1",
56+
"@babel/core": "^7.13.10",
5857
"@babel/plugin-transform-flow-strip-types": "^7.13.0",
5958
"@babel/plugin-transform-object-assign": "^7.12.13",
60-
"@babel/preset-env": "^7.13.5",
59+
"@babel/preset-env": "^7.13.10",
6160
"@babel/preset-flow": "^7.12.13",
6261
"@babel/preset-react": "^7.12.13",
6362
"@rollup/plugin-babel": "^5.3.0",
6463
"@rollup/plugin-buble": "^0.21.3",
6564
"@rollup/plugin-commonjs": "^17.1.0",
6665
"@rollup/plugin-node-resolve": "^11.2.0",
67-
"@rollup/plugin-replace": "^2.3.4",
6866
"babel-plugin-closure-elimination": "^1.3.2",
6967
"babel-plugin-transform-async-to-promises": "^0.8.15",
7068
"codecov": "^3.8.1",
@@ -76,7 +74,7 @@
7674
"prettier": "^2.2.1",
7775
"react": "^17.0.1",
7876
"react-dom": "^17.0.1",
79-
"rollup": "^2.39.1",
77+
"rollup": "^2.41.2",
8078
"rollup-plugin-babel": "^4.4.0",
8179
"rollup-plugin-terser": "^7.0.2"
8280
}

rollup.config.js

Lines changed: 48 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import commonjs from '@rollup/plugin-commonjs'
22
import resolve from '@rollup/plugin-node-resolve'
33
import buble from '@rollup/plugin-buble'
44
import babel from '@rollup/plugin-babel'
5-
import replace from '@rollup/plugin-replace'
65
import { terser } from 'rollup-plugin-terser'
76
import compiler from '@ampproject/rollup-plugin-closure-compiler'
87

@@ -26,48 +25,7 @@ const externalTest = (id) => {
2625
return externalPredicate.test(id)
2726
}
2827

29-
const terserPretty = terser({
30-
warnings: true,
31-
ecma: 5,
32-
keep_fnames: true,
33-
ie8: false,
34-
compress: {
35-
pure_getters: true,
36-
toplevel: true,
37-
booleans_as_integers: false,
38-
keep_fnames: true,
39-
keep_fargs: true,
40-
if_return: false,
41-
ie8: false,
42-
sequences: false,
43-
loops: false,
44-
conditionals: false,
45-
join_vars: false
46-
},
47-
mangle: false,
48-
output: {
49-
beautify: true,
50-
braces: true,
51-
indent_level: 2
52-
}
53-
})
54-
55-
const terserMinified = terser({
56-
warnings: true,
57-
ecma: 5,
58-
ie8: false,
59-
toplevel: true,
60-
compress: {
61-
keep_infinity: true,
62-
pure_getters: true,
63-
passes: 10
64-
},
65-
output: {
66-
comments: false
67-
}
68-
})
69-
70-
const makePlugins = (isProduction = false) => [
28+
const plugins = [
7129
babel({
7230
babelrc: false,
7331
babelHelpers: 'bundled',
@@ -113,54 +71,58 @@ const makePlugins = (isProduction = false) => [
11371
]
11472
]
11573
}),
116-
isProduction &&
117-
replace({
118-
'process.env.NODE_ENV': JSON.stringify('production')
119-
}),
120-
isProduction &&
121-
compiler({
122-
compilation_level: 'SIMPLE_OPTIMIZATIONS'
123-
}),
124-
isProduction ? terserMinified : terserPretty
74+
compiler({
75+
compilation_level: 'SIMPLE_OPTIMIZATIONS'
76+
}),
77+
terser({
78+
warnings: true,
79+
ecma: 5,
80+
keep_fnames: true,
81+
ie8: false,
82+
compress: {
83+
pure_getters: true,
84+
toplevel: true,
85+
booleans_as_integers: false,
86+
keep_fnames: true,
87+
keep_fargs: true,
88+
if_return: false,
89+
ie8: false,
90+
sequences: false,
91+
loops: false,
92+
conditionals: false,
93+
join_vars: false
94+
},
95+
mangle: false,
96+
output: {
97+
beautify: true,
98+
braces: true,
99+
indent_level: 2
100+
}
101+
})
125102
]
126103

127-
const config = {
104+
export default {
128105
input: './src/index.js',
129106
onwarn: () => {},
130107
external: externalTest,
131108
treeshake: {
132109
propertyReadSideEffects: false
133-
}
134-
}
135-
136-
const name = 'react-ssr-prepass'
137-
138-
export default [
139-
{
140-
...config,
141-
plugins: makePlugins(false),
142-
output: [
143-
{
144-
sourcemap: true,
145-
legacy: true,
146-
freeze: false,
147-
esModule: false,
148-
file: `./dist/${name}.development.js`,
149-
format: 'cjs'
150-
}
151-
]
152110
},
153-
{
154-
...config,
155-
plugins: makePlugins(true),
156-
output: [
157-
{
158-
sourcemap: true,
159-
legacy: true,
160-
freeze: false,
161-
file: `./dist/${name}.production.min.js`,
162-
format: 'cjs'
163-
}
164-
]
165-
}
166-
]
111+
plugins,
112+
output: [
113+
{
114+
sourcemap: true,
115+
freeze: false,
116+
// NOTE: *.mjs files will lead to issues since react is still a non-ESM package
117+
// the same goes for package.json:exports
118+
file: './dist/react-ssr-prepass.es.js',
119+
format: 'esm'
120+
},
121+
{
122+
sourcemap: true,
123+
freeze: false,
124+
file: './dist/react-ssr-prepass.js',
125+
format: 'cjs'
126+
}
127+
]
128+
}

scripts/copy-typings.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env node
2+
3+
const path = require('path')
4+
const fs = require('fs')
5+
6+
fs.copyFileSync(
7+
path.resolve(__dirname, 'react-ssr-prepass.d.ts'),
8+
path.resolve(__dirname, '../dist/react-ssr-prepass.d.ts')
9+
)
10+
11+
fs.copyFileSync(
12+
path.resolve(__dirname, 'react-ssr-prepass.js.flow'),
13+
path.resolve(__dirname, '../dist/react-ssr-prepass.js.flow')
14+
)
15+
16+
fs.copyFileSync(
17+
path.resolve(__dirname, 'react-ssr-prepass.js.flow'),
18+
path.resolve(__dirname, '../dist/react-ssr-prepass.es.js.flow')
19+
)
File renamed without changes.
File renamed without changes.

src/visitor.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ const {
7979
// yielding behavior that gives the event loop a chance to continue
8080
// running when the prepasses would otherwise take too long
8181
export const SHOULD_YIELD = typeof setImmediate === 'function'
82-
8382
// Time in ms after which the otherwise synchronous visitor yields so that
8483
// the event loop is not interrupted for too long
85-
const YIELD_AFTER_MS = process.env.NODE_ENV !== 'production' ? 20 : 5
84+
const YIELD_AFTER_MS = 5
8685

8786
const render = (
8887
type: ComponentType<DefaultProps> & ComponentStatics,

0 commit comments

Comments
 (0)