Skip to content

Commit 8af3228

Browse files
authored
Merge pull request #9 from epranka/feature#rollup-bundler
Feature#rollup bundler
2 parents 605f869 + da185fb commit 8af3228

File tree

7 files changed

+165
-14
lines changed

7 files changed

+165
-14
lines changed

cli.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ cli
3131
"-s, --silent",
3232
"Silent mode. Create package without user interaction"
3333
)
34+
.option("--umd <GlobalName>", "Build UMD module with <GlobalName>")
35+
.option("--no-es", "Don't build ES Module")
3436
.option("--no-travis", "Don't use travis ci.")
3537
.option("--no-tests", "Don't use tests")
3638
.option("--no-semantic-release", "Don't use semantic release")
@@ -58,4 +60,4 @@ cli.help();
5860

5961
cli.version(version);
6062

61-
cli.parse();
63+
global["parsedArgs"] = cli.parse();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@semantic-release/git": "^7.0.18",
4949
"@semantic-release/release-notes-generator": "^7.3.4",
5050
"cz-conventional-changelog": "^3.0.2",
51+
"js-beautify": "^1.10.2",
5152
"semantic-release": "^15.13.31"
5253
},
5354
"config": {

prompts.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
const getRepositoryURL = require("./utils/repository");
22
const { random } = require("superb");
33

4-
const args = process.argv;
5-
const useTravis = !args.includes("--no-travis");
6-
const useTests = !args.includes("--no-tests");
7-
const useSemanticRelease = !args.includes("--no-semantic-release");
8-
const useNPM = args.includes("--npm");
4+
const { options } = global["parsedArgs"];
5+
const umdBuild = !!options.umd;
6+
const umdName = options.umd;
7+
const esBuild = options.es;
8+
const useTravis = options.travis;
9+
const useTests = options.tests;
10+
const useSemanticRelease = options["semantic-release"];
11+
const useNPM = options["npm"];
912

1013
module.exports = [
1114
{
@@ -28,6 +31,28 @@ module.exports = [
2831
type: "list",
2932
default: useNPM ? "npm" : "yarn"
3033
},
34+
{
35+
name: "umd",
36+
message: "Build to UMD module for browsers (CommonJS is default) ?",
37+
type: "confirm",
38+
default: umdBuild
39+
},
40+
{
41+
name: "umd_name",
42+
message:
43+
"What is a global name of your package in browser (e.g. React, ReactDOM) ?",
44+
default: umdName,
45+
when: answers => {
46+
if (answers["umd"]) return true;
47+
return false;
48+
}
49+
},
50+
{
51+
name: "es",
52+
message: "Build to ES Module ?",
53+
type: "confirm",
54+
default: esBuild
55+
},
3156
{
3257
name: "tests",
3358
message: "Use unit tests ?",

saofile.js

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const spawn = require("cross-spawn");
22
const validate = require("validate-npm-package-name");
33
const serializePackage = require("./utils/serializePackage");
44
const serializeTSConfig = require("./utils/serializeTSConfig");
5+
const serializeRollupConfig = require("./utils/serializeRollupConfig");
56
const prompts = require("./prompts");
67

78
module.exports = {
@@ -35,7 +36,6 @@ module.exports = {
3536
description: this.answers.description,
3637
version: "0.0.1",
3738
main: "lib/index.js",
38-
module: "lib/index.es.js",
3939
types: "lib/index.d.ts",
4040
files: ["lib"],
4141
publishConfig: { access: "public" },
@@ -44,10 +44,7 @@ module.exports = {
4444
contributors: [],
4545
repository: "",
4646
license: "MIT",
47-
scripts: [
48-
{ build: "rm -rf ./lib/** && tsx" },
49-
{ watch: "tsc -- --watch" }
50-
],
47+
scripts: [{ build: "rollup -c" }, { watch: "rollup -cw" }],
5148
dependencies: [],
5249
devDependencies: [
5350
{ "@babel/cli": "^7.2.3" },
@@ -67,11 +64,73 @@ module.exports = {
6764
{ "tslint-react": "^3.6.0" },
6865
{ typescript: "^3.3.3333" },
6966
{ react: "*" },
70-
{ "react-dom": "*" }
67+
{ "react-dom": "*" },
68+
{ rollup: "^1.27.5" },
69+
{ "rollup-plugin-babel-minify": "^9.1.1" },
70+
{ "rollup-plugin-cleanup": "^3.1.1" },
71+
{ "rollup-plugin-commonjs": "^10.1.0" },
72+
{ "rollup-plugin-delete": "^1.1.0" },
73+
{ "rollup-plugin-progress": "^1.1.1" },
74+
{ "rollup-plugin-typescript2": "^0.25.2" }
7175
],
7276
peerDependencies: [{ react: "*" }, { "react-dom": "*" }]
7377
};
7478

79+
const rollupConfig = {
80+
input: "./src/index.tsx",
81+
output: [],
82+
plugins: [
83+
`progress({clearLines: false})`,
84+
`del({targets: [
85+
"lib/*"
86+
]})`,
87+
`commonjs({
88+
namedExports: {}
89+
})`,
90+
`typescript()`,
91+
`minify()`,
92+
`cleanup()`
93+
],
94+
external: [
95+
"...Object.keys(pkg.dependencies || {})",
96+
"...Object.keys(pkg.peerDependencies || {})"
97+
]
98+
};
99+
100+
if (this.answers.umd) {
101+
if (!this.answers.umd_name || !this.answers.umd_name.trim()) {
102+
console.error(
103+
this
104+
.chalk`{red No global UMD name defined while UMD module build is enabled}`
105+
);
106+
process.exit(1);
107+
}
108+
const umdOutput = `{
109+
file: pkg.main,
110+
format: "umd",
111+
name: "${this.answers.umd_name}",
112+
globals: {
113+
react: "React"
114+
}
115+
}`;
116+
rollupConfig.output.push(umdOutput);
117+
} else {
118+
const cjsOutput = `{
119+
file: pkg.main,
120+
format: "cjs"
121+
}`;
122+
rollupConfig.output.push(cjsOutput);
123+
}
124+
125+
if (this.answers.es) {
126+
package.module = "lib/index.es.js";
127+
const esOutput = `{
128+
file: pkg.module,
129+
format: "es"
130+
}`;
131+
rollupConfig.output.push(esOutput);
132+
}
133+
75134
if (this.answers.tests) {
76135
tsconfig.includes.push("./__tests__");
77136
package.scripts.push({ test: "jest" });
@@ -144,6 +203,7 @@ module.exports = {
144203
return {
145204
tsconfig: serializeTSConfig(tsconfig),
146205
package: serializePackage(package),
206+
rollupConfig: serializeRollupConfig(rollupConfig),
147207
pmRun
148208
};
149209
},
@@ -184,6 +244,7 @@ module.exports = {
184244
babelrc: ".babelrc",
185245
travis_yml: ".travis.yml",
186246
jest_config_js: "jest.config.js",
247+
rollup_config_js: "rollup.config.js",
187248
releaserc: ".releaserc",
188249
_package_json: "package.json",
189250
_tsconfig_json: "tsconfig.json",

templates/rollup_config_js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%- rollupConfig %>

utils/serializeRollupConfig.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const beautify = require("js-beautify").js;
2+
3+
const serializeRollupConfig = rollupConfig => {
4+
const rollupResult = `
5+
import typescript from "rollup-plugin-typescript2";
6+
import commonjs from "rollup-plugin-commonjs";
7+
import progress from "rollup-plugin-progress";
8+
import minify from "rollup-plugin-babel-minify";
9+
import cleanup from "rollup-plugin-cleanup";
10+
import del from "rollup-plugin-delete";
11+
import pkg from "./package.json";
12+
13+
export default {
14+
input: ${JSON.stringify(rollupConfig.input)},
15+
output: [
16+
${rollupConfig.output.join(",\n")}
17+
],
18+
plugins: [
19+
${rollupConfig.plugins.join(",\n")}
20+
],
21+
external: [
22+
${rollupConfig.external.join(",\n")}
23+
]
24+
}
25+
`;
26+
27+
const formatted = beautify(rollupResult, {
28+
indent_with_tabs: true,
29+
indent_size: 4
30+
});
31+
32+
return formatted;
33+
};
34+
35+
module.exports = serializeRollupConfig;

yarn.lock

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
10431043
dependencies:
10441044
delayed-stream "~1.0.0"
10451045

1046-
commander@~2.20.3:
1046+
commander@^2.19.0, commander@~2.20.3:
10471047
version "2.20.3"
10481048
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
10491049
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
@@ -1608,6 +1608,16 @@ editor@~1.0.0:
16081608
resolved "https://registry.yarnpkg.com/editor/-/editor-1.0.0.tgz#60c7f87bd62bcc6a894fa8ccd6afb7823a24f742"
16091609
integrity sha1-YMf4e9YrzGqJT6jM1q+3gjok90I=
16101610

1611+
editorconfig@^0.15.3:
1612+
version "0.15.3"
1613+
resolved "https://registry.yarnpkg.com/editorconfig/-/editorconfig-0.15.3.tgz#bef84c4e75fb8dcb0ce5cee8efd51c15999befc5"
1614+
integrity sha512-M9wIMFx96vq0R4F+gRpY3o2exzb8hEj/n9S8unZtHSvYjibBp/iMufSzvmOcV/laG0ZtuTVGtiJggPOSW2r93g==
1615+
dependencies:
1616+
commander "^2.19.0"
1617+
lru-cache "^4.1.5"
1618+
semver "^5.6.0"
1619+
sigmund "^1.0.1"
1620+
16111621
ejs@^2.5.7:
16121622
version "2.7.4"
16131623
resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.7.4.tgz#48661287573dcc53e366c7a1ae52c3a120eec9ba"
@@ -3026,6 +3036,17 @@ joycon@^2.1.2:
30263036
resolved "https://registry.yarnpkg.com/joycon/-/joycon-2.2.5.tgz#8d4cf4cbb2544d7b7583c216fcdfec19f6be1615"
30273037
integrity sha512-YqvUxoOcVPnCp0VU1/56f+iKSdvIRJYPznH22BdXV3xMk75SFXhWeJkZ8C9XxUWt1b5x2X1SxuFygW1U0FmkEQ==
30283038

3039+
js-beautify@^1.10.2:
3040+
version "1.10.2"
3041+
resolved "https://registry.yarnpkg.com/js-beautify/-/js-beautify-1.10.2.tgz#88c9099cd6559402b124cfab18754936f8a7b178"
3042+
integrity sha512-ZtBYyNUYJIsBWERnQP0rPN9KjkrDfJcMjuVGcvXOUJrD1zmOGwhRwQ4msG+HJ+Ni/FA7+sRQEMYVzdTQDvnzvQ==
3043+
dependencies:
3044+
config-chain "^1.1.12"
3045+
editorconfig "^0.15.3"
3046+
glob "^7.1.3"
3047+
mkdirp "~0.5.1"
3048+
nopt "~4.0.1"
3049+
30293050
js-tokens@^4.0.0:
30303051
version "4.0.0"
30313052
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
@@ -3463,7 +3484,7 @@ lowercase-keys@^1.0.0:
34633484
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-1.0.1.tgz#6f9e30b47084d971a7c820ff15a6c5167b74c26f"
34643485
integrity sha512-G2Lj61tXDnVFFOi8VZds+SoQjtQC3dgokKdDG2mTm1tx4m50NUHBOZSBwQQHyy0V12A0JTG4icfZQH+xPyh8VA==
34653486

3466-
lru-cache@^4.0.1:
3487+
lru-cache@^4.0.1, lru-cache@^4.1.5:
34673488
version "4.1.5"
34683489
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.5.tgz#8bbe50ea85bed59bc9e33dcab8235ee9bcf443cd"
34693490
integrity sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==
@@ -5225,6 +5246,11 @@ shelljs@0.7.6:
52255246
interpret "^1.0.0"
52265247
rechoir "^0.6.2"
52275248

5249+
sigmund@^1.0.1:
5250+
version "1.0.1"
5251+
resolved "https://registry.yarnpkg.com/sigmund/-/sigmund-1.0.1.tgz#3ff21f198cad2175f9f3b781853fd94d0d19b590"
5252+
integrity sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=
5253+
52285254
signal-exit@^3.0.0, signal-exit@^3.0.2:
52295255
version "3.0.2"
52305256
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"

0 commit comments

Comments
 (0)