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-fallback-directory-resolver-plugin

v0.1.4

Published

Webpack Resolver plugin to resolve modules and files through a fallback chain at compile time.

Downloads

21

Readme

webpack-fallback-directory-resolver-plugin

Webpack Resolver plugin to resolve modules and files through a fallback chain at compile time.

Description

This is a Resolver-Plugin for webpack. It enables resolving modules by looking up files in an fallback directory chain.

Let's look at a simple example.

Suppose you have an application greeter to greet users in different languages. The file structure of this application looks something like this:

/greeter
  - /src
    - /de 
      - translations.js
    - /en 
      - translations.js
      - ordnial.js
    - ordnial.js
    - index.js
    

The translations.js files contain the translated strings, which are used by index.js.

// /greeter/src/en/translations.js

export default {
    "hello": "Hello",
    "nth_visitor_start": "You are the",
    "visitor": "visitor"
    // ...
};
// /greeter/src/de/translations.js

export default {
    "hello": "Hallo",
    "nth_visitor_start": "Du bist der",
    "visitor": "Besucher"
    // ...
};

There are also two ordnial.js files containing a function which returns a number with its ordinal postfix.

// /greeter/src/ordnial.js

export default (num) => {
    return `${num}.`;
};
// /greeter/src/en/ordnial.js
// different ordnial postfixes for en

export default (num) => {
    switch (num) {
        case 0:
           return num;
         
        case 1:
            return `${num}st`;
         
        case 2:
            return `${num}nd`;
            
        case 3:
            return `${num}rd`;
            
        default:
            return `${num}th`;
    }
};

Now suppose you want to create two separate webpack builds for each language. The resulting build should only contain the translations strings and the ordinal function for its language.

To do this, you can set up webpack-fallback-directory-resolver-plugin to resolve these files based on a language parameter (Environment variable) on build time.

// webpack.config.js

const FallbackDirectoryResolverPlugin = require('webpack-fallback-directory-resolver-plugin');
const path = require('path');

// get the language from the environment
const language = process.env.LANGUAGE || 'en';

module.exports = {
    context: __dirname,
    entry: "./src/index.js",
    output: {
        path: __dirname + "/dist",
        filename: `script.${language}.js`
    },
    resolve: {
        plugins: [
            new FallbackDirectoryResolverPlugin(
                {
                    prefix: 'language-resolve',
                    directories: [
                        // this is the fallback directory chain. The plugin tries to resolve the file first 
                        // in the `src/${language}` folder. If it can't be found there, it will try to resolve it in the next directory in the chain, and so on...
                        path.resolve(__dirname, `src/${language}`),
                        path.resolve(__dirname, `src`)
                    ]
                }
            )
        ]
    }
};
// /greeter/src/index.js

import translations from "#language-resolve#/translations.js"; // translations is dynamically resolved to
import ordinal from "#language-resolve#/ordnial.js"; 

const greet = (name) => {
    console.log(`${translations.hello}`)
};

const greetNthVisitor = (num) => {
    // this will output "You are the 1st Visitor" in the en build, 
    // and "Du bist der 1. Besucher" in the de  build.
    console.log(`${translations.nth_visitor_start} ${ordinal(num)} ${translations.visitor}`)
};

export { 
    greet,
    greetNthVisitor
};

Another usage example would be a react application, which has multiple themes with different JSX-Markups.

For more information, take a look at the example application.

Installation

Install this plugin via yarn or npm alongside with webpack.

yarn add --dev webpack-fallback-directory-resolver-plugin

or

npm install --save-dev webpack-fallback-directory-resolver-plugin

Usage

Add webpack-fallback-directory-resolver-plugin as a resolve-Plugin in your webpack.config.js-File

Example

// webpack.config.js

const FallbackDirectoryResolverPlugin = require('webpack-fallback-directory-resolver-plugin');

// ...

module.exports = {
    // ...
    
    resolve: {
        plugins: [
            new FallbackDirectoryResolverPlugin(
                {
                    prefix: 'fallback',
                    directories: [
                        // this is the fallback directory chain. The plugin tries to resolve the file first 
                        // in the `js/dir2` folder. If it can't be found there, it will try to resolve it in the next directory in the chain, and so on...
                        path.resolve(__dirname, 'js/dir2'),
                        path.resolve(__dirname, 'js/dir1')
                    ]
                }
            )
        ]
    }
};

Options

The FallbackDirectoryResolverPlugin takes an option object as its argument.

Possible options are:

prefix

The prefix is used to make sure this resolver is only used explicitly.

Also, you could add multiple instances of this plugin with different prefixes.

Usage: prefix the module path in the import statement with #<prefix>#/<file-to-resolve>, where <prefix> is the prefix you specified in options.prefix.

Default: fallback

directories

An array of directory paths the resolver should try to resolve the imported files in.

The plugin tries to resolve the file first in the first directory. If it can't find it there, it tries the next directory and so on.

Bugs

If you encounter any bugs, please consider opening an issue on GitHub.