Skip to content

Commit c152156

Browse files
committed
Update to use Rollup to build both umd and cjs bundles
1 parent 56ad0d7 commit c152156

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
"src",
3737
"types",
3838
"umd",
39-
"index.cjs",
40-
"Timecode.cjs",
41-
"utils.cjs"
39+
"index.cjs"
4240
],
4341
"directories": {
4442
"lib": "/src"
@@ -48,7 +46,7 @@
4846
"jsnext:main": "src/index.js",
4947
"types": "types",
5048
"scripts": {
51-
"clean": "rimraf index.cjs utils.cjs es umd",
49+
"clean": "rimraf index.cjs es umd",
5250
"prebuild": "yarn clean",
5351
"build": "node ./tools/build.mjs",
5452
"watch": "babel ./src -d . --ignore __mocks__,__tests__,**/*.test.js --watch",

rollup.config.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,22 @@ import resolve from '@rollup/plugin-node-resolve'
55
import terser from '@rollup/plugin-terser'
66
import pkg from './package.json' assert { type: 'json' }
77

8-
const config = {
8+
const cjsConfig = {
9+
input: 'src/index.js',
10+
output: {
11+
exports: 'named',
12+
name: pkg.name,
13+
file: './index.cjs',
14+
format: 'cjs',
15+
globals: {
16+
react: 'React',
17+
},
18+
banner: `/*! ${pkg.name} !*/`,
19+
footer: `/* Copyright 2018 - ${(new Date()).getFullYear()} - ${pkg.author} */`,
20+
},
21+
}
22+
23+
const umdConfig = {
924
input: 'src/index.js',
1025
output: {
1126
exports: 'named',
@@ -18,10 +33,15 @@ const config = {
1833
banner: `/*! ${pkg.name} !*/`,
1934
footer: `/* Copyright 2018 - ${(new Date()).getFullYear()} - ${pkg.author} */`,
2035
},
36+
}
37+
38+
const config = {
39+
...(process.env.BABEL_ENV === 'cjs' ? cjsConfig : umdConfig),
2140
external: [
2241
'react',
2342
],
2443
plugins: [
44+
json(),
2545
babel({
2646
exclude: 'node_modules/**',
2747
babelHelpers: 'runtime',
@@ -30,7 +50,6 @@ const config = {
3050
commonjs({
3151
include: /node_modules/,
3252
}),
33-
json(),
3453
],
3554
}
3655

tools/babel-preset.cjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
const BABEL_ENV = process.env.BABEL_ENV
2-
const building = BABEL_ENV != undefined && BABEL_ENV !== 'cjs'
3-
41
const plugins = [
52
'@babel/plugin-proposal-class-properties',
63
'@babel/plugin-proposal-object-rest-spread',
@@ -23,7 +20,7 @@ module.exports = function () {
2320
[
2421
'@babel/preset-env',
2522
{
26-
modules: building ? false : 'commonjs',
23+
modules: false,
2724
},
2825
],
2926
'@babel/preset-react',

tools/build.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ const exec = (command, extraEnv) => {
1313

1414
console.log('Building CommonJS modules ...')
1515

16-
exec('babel src -d . --out-file-extension .cjs --ignore src/__mocks__,__tests__,**/*.test.js', {
16+
exec(`rollup -c -f cjs -o index.cjs`, {
1717
BABEL_ENV: 'cjs',
18+
NODE_ENV: 'development',
1819
})
1920

2021
console.log('\nBuilding ES modules ...')
2122

22-
exec('babel src -d es --ignore src/__mocks__,__tests__,**/*.test.js', {
23+
exec('babel src -d es --ignore src/__mocks__,__snapshots__,__tests__,**/*.test.js', {
2324
BABEL_ENV: 'es',
2425
})
2526

0 commit comments

Comments
 (0)