Skip to content

Commit beba26b

Browse files
committed
feat: TypeScript
1 parent 7ab961a commit beba26b

File tree

5 files changed

+152
-9
lines changed

5 files changed

+152
-9
lines changed
File renamed without changes.

package.json

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
"name": "lib-boilerplate",
33
"version": "0.0.0",
44
"description": "Some awesome description",
5-
"main": "dist/lib-boilerplate.js",
5+
"main": "dist/lib-boilerplate.common.js",
6+
"module": "dist/lib-boilerplate.esm.js",
7+
"unpkg": "dist/lib-boilerplate.js",
8+
"browser": "dist/lib-boilerplate.esm.js",
9+
"types": "dist/lib-boilerplate/src",
10+
"sideEffects": false,
611
"author": {
712
"name": "Eduardo San Martin Morote",
813
"email": "[email protected]"
@@ -13,7 +18,8 @@
1318
"unit": "jest",
1419
"dev": "npm run unit -- --watchAll",
1520
"test": "npm run lint && npm run unit",
16-
"prepublishOnly": "rollit"
21+
"build": "rollup -c rollup.config.js",
22+
"prepublishOnly": "yarn run build"
1723
},
1824
"files": [
1925
"src",
@@ -25,13 +31,16 @@
2531
"keywords": [],
2632
"license": "MIT",
2733
"devDependencies": {
28-
"@babel/core": "^7.2.0",
29-
"@babel/preset-env": "^7.2.0",
30-
"babel-jest": "^24.8.0",
31-
"codecov": "^3.1.0",
32-
"eslint": "^6.0.1",
33-
"eslint-config-posva": "^3.0.1",
34-
"jest": "^24.8.0"
34+
"pascalcase": "^1.0.0",
35+
"rimraf": "^3.0.0",
36+
"rollup": "^1.21.4",
37+
"rollup-plugin-alias": "^2.0.0",
38+
"rollup-plugin-commonjs": "^10.1.0",
39+
"rollup-plugin-node-resolve": "^5.2.0",
40+
"rollup-plugin-replace": "^2.2.0",
41+
"rollup-plugin-terser": "^5.1.2",
42+
"rollup-plugin-typescript2": "^0.24.2",
43+
"typescript": "^3.6.3"
3544
},
3645
"repository": {
3746
"type": "git",

rollup.config.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import replace from 'rollup-plugin-replace'
2+
import resolve from 'rollup-plugin-node-resolve'
3+
import commonjs from 'rollup-plugin-commonjs'
4+
import ts from 'rollup-plugin-typescript2'
5+
import alias from 'rollup-plugin-alias'
6+
import { terser } from 'rollup-plugin-terser'
7+
import path from 'path'
8+
import rimraf from 'rimraf'
9+
import pascalcase from 'pascalcase'
10+
11+
const cwd = process.cwd()
12+
// eslint-disable-next-line
13+
const pkg = require(path.join(cwd, 'package.json'))
14+
15+
rimraf.sync(path.join(cwd, './dist'))
16+
17+
const banner = `/*!
18+
* ${pkg.name} v${pkg.version}
19+
* (c) ${new Date().getFullYear()} Eduardo San Martin Morote
20+
* @license MIT
21+
*/`
22+
23+
const exportName = pascalcase(pkg.name)
24+
25+
function createEntry(
26+
{
27+
format, // Rollup format (iife, umd, cjs, es)
28+
external, // Rollup external option
29+
input = 'src/index.ts', // entry point
30+
env = 'development', // NODE_ENV variable
31+
minify = false,
32+
isBrowser = false, // produce a browser module version or not
33+
} = {
34+
input: 'src/index.ts',
35+
env: 'development',
36+
minify: false,
37+
isBrowser: false,
38+
}
39+
) {
40+
// force production mode when minifying
41+
if (minify) env = 'production'
42+
43+
const config = {
44+
input,
45+
plugins: [
46+
replace({
47+
__VERSION__: pkg.version,
48+
'process.env.NODE_ENV': `'${env}'`,
49+
}),
50+
alias({
51+
resolve: ['.ts', '.js'],
52+
// entries: [{ find: 'firebase', replacement: path.join(__dirname, './stub') }],
53+
}),
54+
],
55+
output: {
56+
banner,
57+
file: `dist/${pkg.name}.UNKNOWN.js`,
58+
format,
59+
},
60+
}
61+
62+
if (format === 'iife') {
63+
config.output.file = pkg.unpkg
64+
config.output.name = exportName
65+
} else if (format === 'es') {
66+
config.output.file = isBrowser ? pkg.browser : pkg.module
67+
} else if (format === 'cjs') {
68+
config.output.file = pkg.main
69+
}
70+
71+
if (!external) {
72+
config.plugins.push(resolve(), commonjs())
73+
} else {
74+
config.external = external
75+
}
76+
77+
config.plugins.push(
78+
ts({
79+
// only check once, during the es version with browser (it includes external libs)
80+
check: format === 'es' && isBrowser && !minify,
81+
tsconfigOverride: {
82+
exclude: ['__tests__'],
83+
compilerOptions: {
84+
// same for d.ts files
85+
declaration: format === 'es' && isBrowser && !minify,
86+
target: format === 'es' && !isBrowser ? 'esnext' : 'es5',
87+
},
88+
},
89+
})
90+
)
91+
92+
if (minify) {
93+
config.plugins.push(
94+
terser({
95+
module: format === 'es',
96+
output: {
97+
preamble: banner,
98+
},
99+
})
100+
)
101+
config.output.file = config.output.file.replace(/\.js$/i, '.min.js')
102+
}
103+
104+
return config
105+
}
106+
107+
const builds = [
108+
createEntry({ format: 'cjs' }),
109+
createEntry({ format: 'es', isBrowser: true }),
110+
]
111+
112+
if (pkg.unpkg)
113+
builds.push(
114+
createEntry({ format: 'iife' }),
115+
createEntry({ format: 'iife', minify: true }),
116+
createEntry({ format: 'es', isBrowser: true, minify: true })
117+
)
118+
119+
export default builds
File renamed without changes.

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "esnext",
5+
"noEmit": true,
6+
"strict": true,
7+
"composite": true,
8+
"esModuleInterop": true,
9+
"moduleResolution": "node",
10+
11+
"rootDir": ".",
12+
"baseUrl": "."
13+
},
14+
"include": ["src", "__tests__"]
15+
}

0 commit comments

Comments
 (0)