Skip to content

Commit e9617c7

Browse files
committed
Use rollup to build the code and generate CommonJS and ESM versions
1 parent 77b5346 commit e9617c7

File tree

7 files changed

+108
-36
lines changed

7 files changed

+108
-36
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

build.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,9 @@ function replaceCharmap () {
1515
return obj
1616
}, {})
1717

18-
var json = JSON.stringify(obj).replace(/'/g, '\\\'')
18+
var json = JSON.stringify(obj)
1919

20-
var source =
21-
fs.readFileSync('index.js', 'utf8')
22-
.replace(
23-
/var charMap = JSON\.parse\(.*\)/,
24-
'var charMap = JSON.parse(\'' + json + '\')'
25-
)
26-
27-
fs.writeFileSync('index.js', source, 'utf8')
20+
fs.writeFileSync('src/charMap.json', json, 'utf8')
2821
}
2922

3023
sortCharmap()

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: 14 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,22 @@
2424
"eslint-plugin-promise": "^4.0.1",
2525
"eslint-plugin-standard": "^4.0.0",
2626
"istanbul": "^0.4.5",
27-
"mocha": "^5.1.1"
27+
"mocha": "^5.1.1",
28+
"rollup": "^0.67.4",
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-
"build": "node build.js && echo Build completed",
39-
"lint": "eslint index.js test.js build.js && echo Lint Passed",
41+
"build": "node build.js && rollup -c",
42+
"lint": "eslint src/index.js test.js build.js && echo Lint Passed",
4043
"test": "npm run lint && npm run test:ci && echo Tests Passed",
4144
"test:ci": "npm run build && mocha",
4245
"test:cov": "npm run build && istanbul cover _mocha"

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)