Use file structure to namespace your code.
npm install babel-plugin-filenamespaceUse file structure to namespace your code



[![Watch on GitHub][github-watch-badge]][github-watch]
[![Star on GitHub][github-star-badge]][github-star]
[![Tweet][twitter-badge]][twitter]
Directories and filenames are typically descriptive by nature so let's use them to namespace our code.
> Think about unit test [describe][jestDescribe] blocks, Storybook [stories][storyNesting] and Redux [action types][reduxActionType] (DDD).
Node provides [__dirname][__dirname] and [__filename][__filename] which are perfect for this, but with code bundling and client-side JS they lose their meaning. The purpose of this plugin is to give that meaning back.
``javascript`
// src/components/Button.test.js
describe(__filenamespace, () => {})`javascript`
// is transformed into
describe('src/components/Button', () => {})
``
npm install --save-dev babel-plugin-filenamespace
`json
{
"plugins": [
"filenamespace"
]
}
`Via
.eslintrc`json
{
"globals": {
"__filenamespace": false
}
}
`Then in any file you want a filename based namespace generated use the placeholder __filenamespace.
โ๏ธ Options
Use Babel's plugin options by replacing the plugin string with an array of the plugin name and an object with the options:
-
root: (Default: project root)
- Specify root directory relative to project src (e.g. src).
- separator: (Default: "/")
- Specify directory separator.
- dropAllFilenames: (Default: false)
- Setting to true will exclude all filenames and use directory structure only.
- _Note: files named index OR have a name that match the parent directory (file extention ignored) are always dropped, regardless of this setting, as they do not provide meaning_.
- dropExtensions: (Default: [".spec", ".test", ".story", ".stories"])
- Specify the extensions you want removed.
- _Note: file extension are always removed, these extensions are for removing extensions from common file naming conventions (e.g. path/to/file.test.js will transform to path/to/file)_.
- customPlaceholders: (Default: [])
- Specify custom placeholders, each with their own configuration (all above options) e.g. [{ "placeholder": "__testnamespace", "separator": "." }].โจ Examples
$3
`json
{
"plugins": [
[
"filenamespace",
{
"root": "app",
}
]
]
}
``javascript
// app/container/App/data/file.js
const namespace = __filenamespace;
`
`javascript
// is transformed into
const namespace = 'container/App/data/file';
`$3
`json
{
"plugins": [
[
"filenamespace",
{
"separator": "๐"
}
]
]
}
``javascript
// app/container/App/data/file.js
const namespace = __filenamespace;
`
`javascript
// is transformed into
const namespace = 'app๐container๐App๐data๐file';
`$3
`json
{
"plugins": [
[
"filenamespace",
{
"dropAllFilenames": true
}
]
]
}
``javascript
// app/container/HomePage/Home.js
const namespace = __filenamespace;
`
`javascript
// is transformed into
const namespace = 'app/container/HomePage';
`$3
`json
{
"plugins": [
[
"filenamespace",
{
"dropExtensions": [".test"]
}
]
]
}
``javascript
// app/container/HomePage/Home.test.js
const namespace = __filenamespace;
`
`javascript
// is transformed into
const namespace = 'app/container/HomePage/Home';
`$3
`json
{
"plugins": [
[
"filenamespace",
{
"separator": ".",
"customPlaceholders": [
{
"placeholder": "__dotDot",
"separator": ".."
},
{
"placeholder": "__dotDotDot",
"separator": "...",
"root": "app",
"dropAllFilenames": true
}
]
}
]
]
}
``javascript
// app/container/App/data/file.js
const namespace = __filenamespace;
const dotDotNamespace = __dotDot;
const dotDotDotNamespace = __dotDotDot;
`
`javascript
// is transformed into
const namespace = 'app.container.App.data.file';
const dotDotNamespace = 'app.container..App..data..file';
const dotDotDotNamespace = 'container...App...data';
``Copyright ยฉ 2019 Samuel Sharpe.
This project is MIT licensed.
[__dirname]: https://nodejs.org/api/modules.html#modules_dirname
[__filename]: https://nodejs.org/api/modules.html#modules_filename
[jestDescribe]: https://jestjs.io/docs/en/api#describename-fn
[reduxActionType]: https://redux.js.org/basics/actions#actions
[storyNesting]: https://storybook.js.org/docs/basics/writing-stories/#nesting-stories
[github-watch-badge]: https://img.shields.io/github/watchers/samit4me/babel-plugin-filenamespace.svg?style=social
[github-watch]: https://github.com/samit4me/babel-plugin-filenamespace/watchers
[github-star-badge]: https://img.shields.io/github/stars/samit4me/babel-plugin-filenamespace.svg?style=social
[github-star]: https://github.com/samit4me/babel-plugin-filenamespace/stargazers
[twitter]: https://twitter.com/intent/tweet?text=Check%20out%20babel-plugin-filenamespace!%20https://github.com/samit4me/babel-plugin-filenamespace%20%F0%9F%91%8D
[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/samit4me/babel-plugin-filenamespace.svg?style=social