Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ module.exports = {
}
```

## Custom export names

In some cases, you may want to customize the formatting of the generated export names.
In this case, you can pass in a custom `formatExports` function to the plugin constructor:

```js
plugins: [
new DotIndexPlugin({
path: path.join(__dirname, '../src'),
formatExports: (filename, rootPath) => filename.toUpperCase()
})
],

// ... will result in ...

export { default as MYCOMPONENT } from './MyComponent'
```

## Limitations

This plugin hijacks the default "watch" functionality in order to watch for newly added files.
Expand Down
6 changes: 3 additions & 3 deletions src/dot-index-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ function DotIndexPlugin (options={}) {
}

DotIndexPlugin.prototype.apply = function (compiler) {
const rootPath = this.options.path
const { path: rootPath, ...options } = this.options
if (!rootPath) throw new Error('DotIndexPlugin requires "path" option')
function recompile () {
return compiler.run(() => {})
}
compiler.hooks.watchRun.tapAsync('DotIndexPlugin', (compilation, callback) => {
createDotIndexFiles(rootPath)
createDotIndexFiles(rootPath, options)
// Only initialize once
if (this.initialized) return callback()
createMonitor(rootPath, monitor => {
Expand All @@ -26,7 +26,7 @@ DotIndexPlugin.prototype.apply = function (compiler) {
})
})
compiler.hooks.run.tapAsync('DotIndexPlugin', (compilation, callback) => {
createDotIndexFiles(rootPath)
createDotIndexFiles(rootPath, options)
callback()
})
}
Expand Down
6 changes: 2 additions & 4 deletions src/generate-dot-index-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

const createDotIndexFiles = require('./utils/create-dot-index-files')

function main (args=process.argv.slice(2)) {
const rootPath = args[0]
function main ([ rootPath, options ]=process.argv.slice(2)) {
if (!rootPath) throw new Error('Must provide "path" argument')
// console.log(rootPath)
return createDotIndexFiles(rootPath)
return createDotIndexFiles(rootPath, options)
}

module.exports = main
Expand Down
6 changes: 2 additions & 4 deletions src/utils/create-dot-index-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ const path = require('path')
const createIndexFileContent = require('./create-index-file-content')
const getFilesAndDirectories = require('./get-files-and-directories')

function createDotIndexFiles (rootPath) {
function createDotIndexFiles (rootPath, options={}) {
const to = relPath => path.join(rootPath, relPath)
const { files, directories } = getFilesAndDirectories(rootPath)
// console.log(files, directories)
// return
if (files.includes('index.js')) {
if (files.includes('.index.js')) fs.removeSync(to('.index.js'))
} else {
const existingContent = attemptRead(to('.index.js'))
const newContent = createIndexFileContent([ ...files, ...directories ])
const newContent = createIndexFileContent([ ...files, ...directories ], rootPath, options)
if (newContent !== existingContent) fs.writeFileSync(to('.index.js'), newContent)
}
directories.forEach(dir => createDotIndexFiles(to(dir)))
Expand Down
10 changes: 5 additions & 5 deletions src/utils/create-index-file-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ function isKebabCase (fileName) {
return fileName.includes('-')
}

function toExportName (fileName) {
const stripped = stripExtension(fileName)
return isKebabCase(stripped) ? camelcase(stripped) : stripped
function defaultFormatExports (fileName, rootPath) {
return isKebabCase(fileName) ? camelcase(fileName) : fileName
}

function createIndexFileContent (files=[]) {
function createIndexFileContent (files=[], rootPath, { formatExports=defaultFormatExports }={}) {
let content = `// This index file was auto-generated by dot-index-webpack-plugin.\n// To overwrite it, simply add an index.js file of your own.\n\n`
files.forEach(file => {
if (file === '.index.js') return
content += `export { default as ${ toExportName(file) } } from './${ stripExtension(file) }'\n`
const strippedFileName = stripExtension(file)
content += `export { default as ${ formatExports(strippedFileName, rootPath) } } from './${ strippedFileName }'\n`
})
return content
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Accepts formatExports argument 1`] = `
"// This index file was auto-generated by dot-index-webpack-plugin.
// To overwrite it, simply add an index.js file of your own.

export { default as MYCOMPONENT } from './MyComponent';
export { default as MYKEBABCASEDFUNCTION } from './my-kebab-cased-function';
export { default as MYFUNCTION } from './myFunction';"
`;

exports[`Generates dot index files 1`] = `
"// This index file was auto-generated by dot-index-webpack-plugin.
// To overwrite it, simply add an index.js file of your own.
Expand Down
37 changes: 36 additions & 1 deletion test/dot-index-plugin/dot-index-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,42 @@ test('Generates dot index files', end => {

})

afterAll(() => {
test('Accepts formatExports argument', end => {
const toUpperCase = filename => filename.replace(/-/g, '').toUpperCase()
const config = {
entry: to('./test-input'),
output: {
path: to('./test-output'),
filename: '[name].js'
},
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
},
],
},
resolve: {
mainFiles: ['index', '.index'],
},
plugins: [
new DotIndexPlugin({ path: to('./test-input'), formatExports: toUpperCase })
]
}

webpack(config, (err, stats) => {
expect(err).toEqual(null)
const modules = stats.toJson().modules
expect(modules.length).toEqual(4)
const indexFileContent = modules.pop().source
expect(indexFileContent).toMatchSnapshot()
end()
})

})

afterEach(() => {
fs.removeSync(to('./test-input/.index.js'))
fs.removeSync(to('./test-output'))
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Accepts formatExports argument 1`] = `
"// This index file was auto-generated by dot-index-webpack-plugin.
// To overwrite it, simply add an index.js file of your own.

export { default as MYCOMPONENT } from './MyComponent'
export { default as MYKEBABCASEDFUNCTION } from './my-kebab-cased-function'
export { default as MYFUNCTION } from './myFunction'
"
`;

exports[`Generates dot index file correctly 1`] = `
"// This index file was auto-generated by dot-index-webpack-plugin.
// To overwrite it, simply add an index.js file of your own.
Expand Down
10 changes: 9 additions & 1 deletion test/generate-dot-index-files/generate-dot-index-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ test('Generates dot index file correctly', () => {
expect(indexFileContent).toMatchSnapshot()
})

afterAll(() => {
test('Accepts formatExports argument', () => {
const toUpperCase = filename => filename.replace(/-/g, '').toUpperCase()
generateDotIndexFiles([ to('./test-input'), { formatExports: toUpperCase } ])
expect(fs.existsSync(to('./test-input/.index.js'))).toEqual(true)
const indexFileContent = fs.readFileSync(to('./test-input/.index.js')).toString()
expect(indexFileContent).toMatchSnapshot()
})

afterEach(() => {
fs.removeSync(to('./test-input/.index.js'))
})