Skip to content

Commit f3fa2dc

Browse files
fix: allow importing the bundle without a DOM (#210)
The webpack build picked the browser build of react-textarea-autosize, which reads document at import time. Bundle its default build instead, which checks for document first. Fixes #149 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 83b80c8 commit f3fa2dc

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎test/tests/js/Bundle-test.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import fs from 'fs'
2+
import os from 'os'
3+
import path from 'path'
4+
import vm from 'vm'
5+
import webpack from 'webpack'
6+
import { expect } from 'chai'
7+
8+
import webpackConfig from './../../../webpack/webpack.config'
9+
10+
function build (outputPath) {
11+
const config = {
12+
...webpackConfig,
13+
output: { ...webpackConfig.output, path: outputPath },
14+
optimization: { minimize: false }
15+
}
16+
17+
return new Promise((resolve, reject) => {
18+
webpack(config, (error, stats) => {
19+
if (error) return reject(error)
20+
if (stats.hasErrors()) { return reject(new Error(stats.toString('errors-only'))) }
21+
resolve(path.join(outputPath, config.output.filename))
22+
})
23+
})
24+
}
25+
26+
describe('dist bundle', function () {
27+
this.timeout(60000)
28+
29+
it('can be imported without a DOM (SSR)', async function () {
30+
const outputPath = fs.mkdtempSync(path.join(os.tmpdir(), 'rjv-bundle-'))
31+
try {
32+
const file = await build(outputPath)
33+
const code = fs.readFileSync(file, 'utf8')
34+
const module = { exports: {} }
35+
// a fresh context has no `window` or `document`, like Node during SSR
36+
const context = vm.createContext({
37+
module,
38+
exports: module.exports,
39+
require
40+
})
41+
42+
vm.runInContext(code, context, { filename: file })
43+
44+
expect(module.exports.default).to.be.a('function')
45+
} finally {
46+
fs.rmSync(outputPath, { recursive: true, force: true })
47+
}
48+
})
49+
})

‎webpack/webpack.config.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ const config = {
3737
},
3838
plugins: [],
3939
resolve: {
40-
extensions: ['.js', '.json', '.css', '.scss']
40+
extensions: ['.js', '.json', '.css', '.scss'],
41+
alias: {
42+
// The browser build reads `document` on import, which breaks SSR.
43+
// The default build checks for `document` first, so bundle that one.
44+
'react-textarea-autosize$': require.resolve('react-textarea-autosize')
45+
}
4146
},
4247
module: {
4348
rules: [

0 commit comments

Comments
 (0)