Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
feat: add entry.basePath helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
thecotne committed Nov 19, 2016
1 parent ca18111 commit bfcf989
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,25 @@ module.exports = {
entry: entry(filePath => path.basename(filePath), 'bar/*.js', 'baz/*.js')
}
```

you can also use `entry.basePath` function as first argument like this

```javascript

module.exports = {
entry: entry(entry.basePath(), 'bar/*.js', 'baz/*.js')
}

# or like this

module.exports = {
entry: entry(entry.basePath('src'), 'src/bar/*.js', 'src/baz/*.js')
}

# or like this

module.exports = {
entry: entry(entry.basePath('src', '.js'), 'src/bar/*.some.js', 'src/baz/*.js')
}

```
73 changes: 73 additions & 0 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,77 @@ describe('entry', () => {

expect(() => entry(1)).toThrow()
})

it('must thorw if first argument is not string or function', () => {
mockFs({
'src/main.entry.js': '',
'src/vendor.entry.js': '',
'src/foo/bar.js': ''
})

expect(() => entry(1)).toThrow()
})
})

describe('entry.basePath', () => {
afterEach(() => {
mockFs.restore()
})

it('should modify entry name (case 1)', () => {
mockFs({
'src/main.entry.js': '',
'src/vendor.entry.js': '',
'src/admin/vendor.foo.entry.js': '',
'src/foo/bar.js': ''
})

var expectedResult = {
'main': 'src/main.entry.js',
'vendor': 'src/vendor.entry.js',
'admin/vendor': 'src/admin/vendor.foo.entry.js'
}

var result = entry(entry.basePath('src'), 'src/**/*.entry.js')

expect(result).toEqual(expectedResult)
})

it('should modify entry name (case 2)', () => {
mockFs({
'src/main.entry.js': '',
'src/vendor.entry.js': '',
'src/admin/vendor.entry.js': '',
'src/foo/bar.js': ''
})

var expectedResult = {
'main.entry': 'src/main.entry.js',
'vendor.entry': 'src/vendor.entry.js',
'admin/vendor.entry': 'src/admin/vendor.entry.js'
}

var result = entry(entry.basePath('src', '.js'), 'src/**/*.entry.js')

expect(result).toEqual(expectedResult)
})

it('should modify entry name (case 3)', () => {
mockFs({
'src/main.entry.js': '',
'src/vendor.entry.js': '',
'src/admin/vendor.entry.js': '',
'src/foo/bar.js': ''
})

var expectedResult = {
'src/main': 'src/main.entry.js',
'src/vendor': 'src/vendor.entry.js',
'src/admin/vendor': 'src/admin/vendor.entry.js'
}

var result = entry(entry.basePath(), 'src/**/*.entry.js')

expect(result).toEqual(expectedResult)
})
})
24 changes: 24 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import glob from 'glob'
import {basename, dirname, relative} from 'path'

const assoc = (key, value, object) => do {
object[key] = value
Expand All @@ -13,6 +14,29 @@ const entry = (...patterns) => patterns
}))
.reduce((a, b) => Object.assign(a, b), {})
entry.basePath = (basePath = '.', ext = null) => fullPath => {
const directory = do {
const relativePath = relative(basePath, fullPath)
const relativeDirName = dirname(relativePath)
if (relativeDirName === '.') {
''
} else {
`${relativeDirName}/`
}
}
const fileName = do {
if (ext) {
basename(fullPath, ext)
} else {
entryName(fullPath)
}
}
return directory + fileName
}
const entryName = path => path
.split('/')
.pop()
Expand Down

0 comments on commit bfcf989

Please sign in to comment.