Skip to content

Commit e055f96

Browse files
authored
Rework build process & add "module" to package.json. (#441)
* fix(build): add module build This allows us to export a cjs build that is more ES5-like to avoid issues with JSDOM, PhantomJS, etc. * test(phantom): separate out phantom test to use cjs build opts This ensures we're testing both build types * refactor(build): remove index.js mess, rename to cjs.js Finishes refactor. Dev, test, lint, and build all working. * fix(build): separate module options & es6 compat
1 parent f024205 commit e055f96

17 files changed

+88
-66
lines changed

.babelrc.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
'use strict';
22

3-
const targets = process.env.IS_WEBPACK === "1" ?
4-
"> 0.25%, not dead" :
5-
"maintained node versions"
3+
// If set, we put Babel in "esmMode", i.e. leave import/export intact.
4+
// Good for webpack and for an esm build.
5+
const esmMode = process.env.BABEL_MODULE_TYPE === "module";
6+
const es6Compat = process.env.BABEL_ES_COMPAT === "6";
67

78
module.exports = {
89
"presets": [
910
[
1011
"@babel/preset-env",
1112
{
12-
targets
13-
}
13+
// Don't transpile import/export in esmMode.
14+
modules: esmMode ? false : "auto",
15+
targets: es6Compat ? "maintained node versions" : undefined
16+
},
1417
],
1518
"@babel/react",
1619
"@babel/preset-flow"
1720
],
1821
"plugins": [
19-
"@babel/transform-flow-comments",
22+
"@babel/plugin-transform-flow-comments",
2023
"@babel/plugin-proposal-class-properties",
2124
],
2225
"env": {

.browserslistrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
> 0.25%
2+
ie 11
3+
not dead

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
*.iml
33
node_modules/
44
build/
5-
web/

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ env:
99
addons:
1010
firefox: latest
1111
script:
12-
- npm run lint
13-
- npm run test
12+
- make lint
13+
- make test
14+
- make test-phantom
1415
email:
1516
on_failure: change
1617
on_success: never

Makefile

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# Mostly lifted from https://andreypopp.com/posts/2013-05-16-makefile-recipes-for-node-js.html
22
# Thanks @andreypopp
33

4+
# Make it parallel
5+
MAKEFLAGS += j4
46
export BIN := $(shell yarn bin)
5-
.PHONY: test dev lint build clean install link
7+
.PHONY: test dev lint build build-cjs build-esm build-web clean install link publish
68
.DEFAULT_GOAL := build
79

810
clean:
911
rm -rf build
12+
mkdir -p build
1013

1114
lint:
1215
@$(BIN)/flow
1316
@$(BIN)/eslint lib/* lib/utils/* specs/*
1417
@$(BIN)/tsc -p typings
1518

16-
build: clean
17-
$(BIN)/babel --out-dir ./build ./lib
19+
build: build-cjs build-esm build-web
20+
21+
build-cjs: $(BIN)
22+
$(BIN)/babel --out-dir ./build/cjs ./lib
23+
24+
build-esm: $(BIN)
25+
env BABEL_MODULE_TYPE="module" BABEL_ES_COMPAT="6" $(BIN)/babel --out-dir ./build/module ./lib
26+
27+
build-web: $(BIN)
1828
$(BIN)/webpack --mode=production --display-modules
1929

2030
# Allows usage of `make install`, `make link`
2131
install link:
2232
@yarn $@
2333

2434
test: $(BIN)
25-
@NODE_ENV=test $(BIN)/karma start --single-run
35+
@NODE_ENV=test $(BIN)/karma start
36+
37+
test-phantom: $(BIN)
38+
@NODE_ENV=test $(BIN)/karma start karma-phantomjs.conf.js
2639

2740
dev: $(BIN) clean
2841
DRAGGABLE_DEBUG=true $(BIN)/webpack-dev-server

example/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Draggable = window.ReactDraggable;
1+
const {ReactDraggable: Draggable, React, ReactDOM} = window;
22

33
class App extends React.Component {
44
state = {

example/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<div id="container"></div>
5858
<script src="//unpkg.com/react@16/umd/react.development.js"></script>
5959
<script src="//unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
60-
<script src="../web/react-draggable.js"></script>
60+
<script src="../build/web/react-draggable.min.js"></script>
6161
<!-- for imported script -->
6262
<script src="//unpkg.com/@babel/standalone/babel.min.js"></script>
6363
<script type="text/babel" src="../example/example.js"></script>

index.js

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

karma-phantomjs.conf.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const baseConfig = require('./karma.conf.js');
4+
// Phantom build can't handle the modern JS in the module build
5+
process.env.BABEL_MODULE_TYPE = 'cjs';
6+
7+
module.exports = function(config) {
8+
// Set base config options.
9+
baseConfig(config);
10+
// Then set some of our own, to run PhantomJS. It's a bit older, which is the idea.
11+
// We want to make sure our CJS build still works on old environments.
12+
config.set({
13+
// Shim required for phantom
14+
frameworks: ['phantomjs-shim', 'jasmine'],
15+
browsers: ['PhantomJS_custom'],
16+
// Includes Map/Set
17+
files: [
18+
'specs/draggable-phantom.spec.jsx'
19+
],
20+
preprocessors: {
21+
'specs/draggable-phantom.spec.jsx': ['webpack']
22+
},
23+
customLaunchers: {
24+
PhantomJS_custom: {
25+
base: 'PhantomJS',
26+
options: {
27+
viewportSize: {width: 1024, height: 768}
28+
}
29+
}
30+
},
31+
});
32+
};

karma.conf.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const webpack = require('webpack');
43
const _ = require('lodash');
54
process.env.NODE_ENV = 'test';
65
process.env.CHROME_BIN = require('puppeteer').executablePath();
@@ -10,25 +9,23 @@ module.exports = function(config) {
109

1110
basePath: '',
1211

13-
frameworks: ['phantomjs-shim', 'jasmine'],
12+
frameworks: [ 'jasmine'],
1413

1514
files: [
16-
'specs/main.js'
15+
'specs/draggable.spec.jsx'
1716
],
1817

1918
exclude: [
2019
],
2120

2221
preprocessors: {
23-
'specs/main.js': ['webpack']
22+
'specs/draggable.spec.jsx': ['webpack']
2423
},
2524

2625
webpack: _.merge(
2726
require('./webpack.config.js')({}, {}),
2827
{
2928
mode: 'production',
30-
// Remove minified build & separate compile of index-src
31-
entry: '',
3229
// Remove source maps: *speeeeeed*
3330
devtool: 'none',
3431
module: {
@@ -60,16 +57,7 @@ module.exports = function(config) {
6057

6158
autoWatch: false,
6259

63-
browsers: ['PhantomJS_custom', 'Firefox', 'ChromeHeadless'],
64-
65-
customLaunchers: {
66-
PhantomJS_custom: {
67-
base: 'PhantomJS',
68-
options: {
69-
viewportSize: {width: 1024, height: 768}
70-
}
71-
}
72-
},
60+
browsers: ['Firefox', 'ChromeHeadless'],
7361

7462
singleRun: true,
7563
});

0 commit comments

Comments
 (0)