Skip to content

Commit 12e4d78

Browse files
authored
fix(build): fix exports in UMD build (#3540)
* fix(build): fix exports in UMD build * build umd before test
1 parent bf504d3 commit 12e4d78

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

.circleci/config.yml

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ jobs:
4646
- run:
4747
name: Test JavaScript
4848
command: yarn test
49+
- run:
50+
name: Test UMD bundle
51+
command: yarn test:umd
4952
- run:
5053
name: Report coverage
5154
command: bash <(curl -s https://codecov.io/bash)

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"pretest": "yarn satisfied && gulp build:docs:docgen",
4040
"test": "cross-env NODE_ENV=test node -r @babel/register ./node_modules/karma/bin/karma start karma.conf.babel.js",
4141
"test:watch": "yarn test --no-single-run",
42+
"test:umd": "gulp build:dist:umd && node test/umd.js",
4243
"tsd:lint": "tslint \"./index.d.ts\" \"./src/**/*.d.ts\" \"./src/**/*.tsx\" \"./test/**/*.d.ts\" \"./test/**/*.tsx\"",
4344
"tsd:lint:fix": "yarn tsd:lint --fix",
4445
"tsd:test": "gulp build:dist:commonjs:tsd && tsc -p ./ && rm test/typings.js"

src/umd.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@
33
// Do not replace this with named exports.
44
// We need to export an object here for browser builds.
55
// Otherwise, we end up with every component on the window.
6-
import * as semanticUIReact from './index'
7-
8-
module.exports = {
9-
...semanticUIReact,
10-
}
6+
export * from './index'

test/umd.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const assert = require('assert')
2+
const fs = require('fs')
3+
const http = require('http')
4+
const puppeteer = require('puppeteer')
5+
6+
const config = require('../config')
7+
8+
const html = `
9+
<!DOCTYPE html>
10+
<html>
11+
<body>
12+
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.5/umd/react.production.min.js"></script>
13+
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.5/umd/react-dom.production.min.js"></script>
14+
<script src="/umd.js"></script>
15+
16+
<div id="root"></div>
17+
<script>
18+
ReactDOM.render(
19+
React.createElement(semanticUIReact.Button, null, "Foo"),
20+
document.getElementById("root"),
21+
)
22+
</script>
23+
</body>
24+
`
25+
26+
const assertInnerHtml = async () => {
27+
const browser = await puppeteer.launch()
28+
const server = http
29+
.createServer((req, response) => {
30+
if (req.url === '/umd.js') {
31+
fs.readFile(config.paths.dist('umd', 'semantic-ui-react.min.js'), (err, data) => {
32+
response.setHeader('Content-type', 'text/javascript')
33+
response.end(data)
34+
})
35+
return
36+
}
37+
38+
response.setHeader('Content-type', 'text/html')
39+
response.end(html)
40+
})
41+
.listen(9000)
42+
43+
const page = await browser.newPage()
44+
await page.goto('http://localhost:9000')
45+
46+
const innerHtml = await page.evaluate(() => document.querySelector('#root').innerHTML)
47+
48+
await browser.close()
49+
server.close()
50+
51+
assert.equal(
52+
innerHtml,
53+
'<button class="ui button">Foo</button>',
54+
'UMD bundle test: Something wrong with UMD build, please check!',
55+
)
56+
}
57+
58+
assertInnerHtml().catch((e) => {
59+
// eslint-disable-next-line no-console
60+
console.error(e)
61+
process.exit(1)
62+
})

webpack.umd.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const TerserPlugin = require('terser-webpack-plugin')
12
const webpack = require('webpack')
23

34
const config = require('./config')
@@ -47,6 +48,17 @@ const webpackUMDConfig = {
4748
maxEntrypointSize: 750000,
4849
maxAssetSize: 750000,
4950
},
51+
optimization: {
52+
minimizer: [
53+
new TerserPlugin({
54+
terserOptions: {
55+
output: {
56+
comments: false,
57+
},
58+
},
59+
}),
60+
],
61+
},
5062
}
5163

5264
module.exports = webpackUMDConfig

0 commit comments

Comments
 (0)