Skip to content

Commit ef22782

Browse files
committed
Use rollup to build the code and generate CommonJS and ESM versions
1 parent 05ba8d0 commit ef22782

File tree

9 files changed

+135
-73
lines changed

9 files changed

+135
-73
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
build/
22
coverage/
33
node_modules/
4+
src/charMap.json
45
.idea
56
npm-debug.log
67
package-lock.json

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ node_js:
66
- 10
77
- 8
88
- 6
9-
- 4
109

1110
after_script:
1211
- npm run test:cov

build.js

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,23 @@
1-
21
var fs = require('fs')
3-
var charmap = require('./charmap')
4-
2+
var charmap = require('./charmap.json')
53

6-
exports.sort = function () {
4+
function sortCharmap () {
75
var sorted = charmap.sort(function (a, b) {
86
return a.key > b.key ? 1 : a.key < b.key ? -1 : 0
97
})
108
fs.writeFileSync('charmap.json', JSON.stringify(sorted, null, 2), 'utf8')
119
}
1210

13-
exports.duplicates = function () {
14-
var cache = {}
15-
var duplicates = []
16-
charmap.forEach(function (pair) {
17-
if (!cache[pair.key]) {
18-
cache[pair.key] = pair
19-
}
20-
else {
21-
duplicates.push(pair)
22-
}
23-
})
24-
return {
25-
cache: cache,
26-
duplicates: duplicates
27-
}
28-
}
29-
30-
exports.replace = function () {
11+
function replaceCharmap () {
3112
var obj = charmap.reduce(function (obj, pair) {
3213
obj[pair.key] = pair.value
3314
return obj
3415
}, {})
3516

36-
var json = JSON.stringify(obj).replace(/'/g, '\\\'')
37-
38-
var source =
39-
fs.readFileSync('index.js', 'utf8')
40-
.replace(
41-
/var charMap = JSON\.parse\(.*\)/,
42-
'var charMap = JSON.parse(\'' + json + '\')'
43-
)
17+
var json = JSON.stringify(obj)
4418

45-
fs.writeFileSync('index.js', source, 'utf8')
19+
fs.writeFileSync('src/charMap.json', json, 'utf8')
4620
}
21+
22+
sortCharmap();
23+
replaceCharmap();

index.cjs.js

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 13 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,25 @@
2424
"eslint-plugin-promise": "^3.7.0",
2525
"eslint-plugin-standard": "^3.0.1",
2626
"istanbul": "^0.4.5",
27-
"mocha": "^5.1.1"
27+
"mocha": "^5.1.1",
28+
"rollup": "^0.67.1",
29+
"rollup-plugin-json": "^3.1.0"
2830
},
29-
"main": "./index.js",
31+
"main": "./index.cjs.js",
3032
"files": [
3133
"LICENSE",
3234
"README.md",
35+
"index.cjs.js",
3336
"index.d.ts",
3437
"index.js"
3538
],
3639
"types": "index.d.ts",
3740
"scripts": {
38-
"lint": "eslint index.js test.js && echo Lint Passed",
41+
"build": "node build.js && rollup -c",
42+
"lint": "eslint src/index.js test.js && echo Lint Passed",
3943
"test": "npm run lint && npm run test:ci && echo Tests Passed",
40-
"test:ci": "mocha",
41-
"test:cov": "istanbul cover _mocha"
44+
"test:ci": "npm run build && mocha",
45+
"test:cov": "npm run build && istanbul cover _mocha"
4246
},
4347
"engines": {
4448
"node": ">=4.0.0"

rollup.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pkg from './package.json';
2+
import json from 'rollup-plugin-json';
3+
4+
export default [
5+
{
6+
input: 'src/index.js',
7+
output: [
8+
{ file: pkg.main, format: 'cjs' },
9+
{ file: 'index.js', format: 'umd', name: 'slugify' }
10+
],
11+
plugins: [
12+
json({
13+
compact: true,
14+
namedExports: false
15+
})
16+
]
17+
}
18+
];

src/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import charMap from './charMap.json'
2+
3+
function replace (string, options) {
4+
if (typeof string !== 'string') {
5+
throw new Error('slugify: string argument expected')
6+
}
7+
8+
options = (typeof options === 'string')
9+
? {replacement: options}
10+
: options || {}
11+
12+
var slug = string.split('')
13+
.reduce(function (result, ch) {
14+
return result + (charMap[ch] || ch)
15+
// allowed
16+
.replace(options.remove || /[^\w\s$*_+~.()'"!\-:@]/g, '')
17+
}, '')
18+
// trim leading/trailing spaces
19+
.trim()
20+
// convert spaces
21+
.replace(/[-\s]+/g, options.replacement || '-')
22+
23+
return options.lower ? slug.toLowerCase() : slug
24+
}
25+
26+
replace.extend = function (customMap) {
27+
for (var key in customMap) {
28+
charMap[key] = customMap[key]
29+
}
30+
}
31+
32+
export default replace

0 commit comments

Comments
 (0)