npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webpack-custom-directory-default-file-plugin

v1.1.0

Published

Teach webpack to use the anything as the main file for a module

Downloads

21

Readme

webpack-custom-directory-default-file-plugin

Teach webpack to use anything you want instead of index.ext file (ext being .js, but doesnt have to be)

This allows you to write:

require('lib/ReallyImportantModule')

in order to require:

lib/ReallyImportantModule/whatever_you_want_goes_here.js

This module is designed to be used as strategy for a webpack.ResolverPlugin.

It comes with a prebuilt resolver called 'regexp' that let you specify any index file with a regexp.

DirectoryDefaultFilePlugin.resolver.regexp

This resolver can be used to make any filename that matches indexRegexp be the index file of that directory. There can only ever be 0 or 1 such files per dir, if there is more than one, the build will fail (unless ignoreMultpleMatches is true)

includeDirRegexp can be used to limit the dirs where this behavior is used. The default value for this param will include everything under the current working dir except anything in node_modules.

Options object contains: @param indexRegexp @param includeDirRegexp @param ignoreMultipleMatches

Example use

var CustomDefaultFilePlugin = require('webpack-custom-directory-default-file-plugin');

plugins: [
     new webpack.ResolverPlugin([
         // this will use any file ending in .index.js as the index file for that dir
         // unless there is an index.js file there already.
         new CustomDefaultFilePlugin(CustomDefaultFilePlugin.resolver.regexp(
             // 'index' file must match this
             /\.index.js$/,

             // directory where that file lives must match this (has sane default)
             new RegExp("^(" + process.cwd() + "/(?!node_modules)|" + process.cwd() + "/node_modules/my_other_module)"))
         )
     ]),

Writing your own resolver

The plugin takes a fn with 3 args: resolver, req, done (the first 2 are webpack objects with useful stuff on them).

The done callback must be called with undefined to fallback on default webpack behavior, or full path to the file that will be the index file.

var CustomDefaultFilePlugin = require('webpack-custom-directory-default-file-plugin');
var glob = require('glob');

var webpackConfig = {
  entry: ...,

  plugins: [
    new webpack.ResolverPlugin([
        new CustomDefaultFilePlugin(function(resolver, req, done){
            var directory = resolver.join(req.path, req.request);
            var files = glob.sync(directory+'/*.idx.js');
            if(files.length == 1){
                // important: remove ext from path!
                return done(files[0].replace(/\.\w+$/, ''));
            }
        })
    ]),
    ...
  ]
};

The example above lets you require('lib/ReallyImportantModule') and it will load lib/ReallyImportantModule/anything_goes_here.idx.js