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

node-require-extended

v1.1.0

Published

Extend Node.js 'require' function in order to be compatible with webpack.

Downloads

2

Readme

node-require-extended

In our projects, we have some parts that can be distributed and integrated through npm packages or included and bundled by webpack. Supporting webpack would need to use some specific methods which are not implemented or supported by Node.js:

  • require.context
  • require.resolveWeak
  • require.include
  • aliases
  • read an 'xml' from require (ugly implem but it works ;-)

The idea is add in Node.js these features in order to have the same source code, behaving the same with or without webpack bundl'ification.

Node.js Implementation in typescript of the 'require.context' webpack function https://webpack.js.org/guides/dependency-management/#requirecontext

Node.js support of alias https://webpack.js.org/configuration/resolve/#resolve

Installation

npm install node-require-extended

require.context

Usage

The syntax is as follows:

require.context(directory, useSubdirectories = false, regExp = /^\.\/.*$/, mode = 'sync')

Examples

require.context("./test", false, /\.test\.js$/);
// a context with files from the test directory that can be required with a request endings with `.test.js`.
require.context("../", true, /\.stories\.js$/);
// a context with all files in the parent folder and descending folders ending with `.stories.js`.

| Name | Type | Default | Description | | - | - | - | - | | path | string | none (required) | Specifies the path to look for modules in. | | recursive | boolean | true | If true, will recurse through subdirectorys in path. | | regExp | RegExp | /^\.\/.*$/ | Specifies a filter that files must match. | | mode | string | sync | 'sync' | 'eager' | 'weak' | 'lazy' | 'lazy-once'. |

Setup

In order to add the 'context' function, we need to enhance the current 'require' of the module, you have to call this function before any usage of require.resolve

import { InjectRequireContext } from 'node-require-extended';

InjectRequireContext(require);

require alias

Inspired from https://www.npmjs.com/package/better-module-alias

const constants = require("@@common/constants.js");

package.json

"_moduleAliases": {
    "@common": "lib/statics/common"
}

We tried different kind of prefixes and ended with '@@'

| Prefix | pifall | | - | - | | $ | if use in 'scripts' section of the package.json, considered on Mac/Linux as a var env ! | | ~ | already an alias for Max/Linux to Home directory | | @ | may conflict with NPM private package prefixed with a @ | | ! | does not work with webpack as used for loader syntax |

Setup

import { InjectRequireAlias } from 'node-require-extended';

InjectRequireAlias(dirname, {
    '@common': "lib/statics/common"
});

Performance

We redirect the module loading in order to check if the path contains an alias. It may have a performance impact.
Would suggest to activate the feature as late as possible. The alias search is optimized in order to search first the common pattern to all your aliases (in our case '@@') rather than testing each alias one by one.

Webpack

We suggest to surround some method calls by '{ /* webpack_ignore_start /' / '/ webpack_ignore_end */ }' comments. In order to skip them when webpack generate the bundle.
/!\ notice the '{' and '}' usage in comment in order to workaround TypeScript limitation: https://github.com/microsoft/TypeScript/issues/32813

{ /* webpack_ignore_start */
InjectRequireContext(require);
InjectRequireAlias(__dirname, {
    '@@config-manager-ui': 'ui'
});
/* webpack_ignore_end */ }

You have to keep your comment in the transpiled TypeScript code

{
    "compilerOptions": {
        "removeComments": false,
npm install --save-dev webpack-strip-block
const packageJson = require('./package.json')
 
const webpackConfig = {
  // ...
    resolve: {
        alias: {
        ...packageJson._moduleAliases,
        },
    },
  // ...
    rules: [
        {
            test: /\.js$/,
            enforce: 'pre',
            use: [
                {
                    loader: 'webpack-strip-block',
                    options: {
                        start: 'webpack_ignore_start',
                        end: 'webpack_ignore_end'
                    }
                }
            ]
        },
    }
  // ...

MIT License

Copyright (c) 2023 Emmanuel Kimmerlin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.