Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

entry-dir-loader

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

entry-dir-loader

Load directories dynamically in Webpack

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

entry-loader

Based on https://github.com/sleexyz/dir-loader

Build Status

dir-loader lets you dynamically require a directory in webpack.

In other words, dir-loader is a code-generation tool; it obviates the need to hard-code require's for a given directory's modules and subdirectories.

contents

install

npm install --save-dev dir-loader

use

Suppose you have a webpack project with a semantic directory structure. You want to require your content but still preserve the hierarchical information inherent to the filesystem.

.
├── website
│   ├── intro.md
|   |
│   ├── travel
│   │   ├── post1.md
│   │   ├── post2.md
│   │   └── post3.md
|   |
│   ├── food
│   │   └── post1.md
|   |
│   └──ignore-me.js
|
|
|
├── blog.config.js
└── entry.js

In a js file, specify the configuration for dir-loader:

// ./blog.config.js

module.exports = {
  path: "./website",
  filter: /\.md$/
}

And then just require that configuration with dir! in your application code!

// ./entry.js

var blog = require("dir!./blog.config.js");
...

This is equivalent to the following javascript:

// (equivalent to ./entry.js)

var blog = {
  "food": require("./website/food/post1.md"),
  "intro.md": require("./website/intro.md"),
  "travel": require("./website/travel/post1.md")
};
...

api

// ./entry.js
var blog = require("dir!./blog.config.js");
...
// ./blog.config.js

module.exports = {

  // path :: String
  // Path to directory. Can be absolute or relative path.
  path: "./website",
  
  // filter :: RegExp
  // (optional)
  // Regular expression to test entry filenames.
  filter: /\.md$/,
  
  // dirFilter :: RegExp
  // (optional)
  // Regular expression to test directory names.
  dirFilter: /^(?!__private__).*/,
  
  // pathTransform :: (String) -> String
  // (optional)
  // Function to transform each generated require statement.
  pathTransform: (_) => "bundle!" + _
  
}

examples

Code here.

To run it:

git clone https://github.com/sleep/dir-loader
cd dir-loader
npm install
npm run example

alternatives

dir-loader was created due to insuffciencies with require.context, webpack's built-in solution for dynamic requires.

require.context provides a flat array of matched modules. This is the easiest way to dynamically require modules if your modules are non-hierarchical. But in the case you want to use the hierarchical information implicit in the filesystem structure, require.context falls short.

Keywords

FAQs

Package last updated on 03 Dec 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc