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-async-require-loader

v2.0.0

Published

Transparently require() remote contents (node moudles) in webpack !

Downloads

15,409

Readme

node-async-require-loader

Build Status

Transparently require() remote contents (node moudles) in webpack !

npm install --save node-async-require-loader

Concept

Fetch the remote contnets (node module) by http GET and build codes in webpack.
I made up the file with .ajs extention.

  • Only for the loader to recognize the file.
  • The file contents is a node moudle that provides a remote url.
module.exports = {
   remoteUrl : "http://xxx.xxx.xxx/api/"
}
  • webpack will fetch the contents by the remote url to build codes.

Highlight

  • Provid the PreParser config for parsing remote contents before webpack compiles.
  • Privde the queryString config for fetching diffrent remote contents.
  • Provide the PreParser for react-templates!
  • How about isomorphic require() on server-side ? We got node-async-require for it !

Basic Usage

Fetch the remote contnets (node module) by http GET and build codes in webpack.

  • Intsall this module
npm install --save node-async-require-loader
  • Use directly in the js file. (Not recommed)
require("node-async-require-loader!remote-content.ajs");
  • Or Set up in the webpack.config.js
    module: {
        loaders: [{
            test: /\.ajs$/,
            loader: "node-async-require-loader"
        }]
    }

You may wondering what '.ajs' file is. The file contents of the .ajs file is a node moudle providing a remote url. The remote url that provides contents(node moudle). The .ajs extention is only for the loader to recognize the file.

Example for Basic Usage

Step 1. Provide an .ajs file

The file contents of the .ajs file is a node moudle providing a remote url.
Write down the remote url that provides contents(node moudle).
The following is the exmaple of .ajs file.

remote-content.ajs

module.exports = {
	remoteUrl: "https://jaydenlin.github.io/fake-remote-contents-for-test/contents/pure-js/"
}

the contnets from the remote url are:

module.exports=function(){ console.log("Hello World From Web"); };

It's a node moudle.

Step 2. Set up the webpack.config.js

We set up the config so that the loader will load the .ajs file and fetch the remote node moudle.

    module: {
        loaders: [{
            test: /\.ajs$/,
            loader: "node-async-require-loader"
        }]
    }
Step 3. Done

Then the webpack will fetch the remote contents and build the codes for you! You can see examples/example01 in codes for more detials.

Usage with queryString

In some cases, the fixed remote url is not good. You may need to add queryString to fetch diffrent remote contents (node moudle).

  • Use queryString directly in the js file. (Not recommed)
    Use the sigle quote for the querString that you added.
require("node-async-require-loader!remote-content.ajs?queryString='en'");
  • Or Set up in the webpack.config.js
    Use the sigle quote for the querString that you added.
    module: {
        loaders: [{
            test: /\.ajs$/,
            loader: "node-async-require-loader?queryString='en'"
        }]
    }

Example for using queryString

Step 1. Provide an .ajs file

The file contents of the .ajs file is a node moudle providing a remote url.
Write down the remote url that provides contents(node moudle).
The following is the exmaple of .ajs file.

remote-content.ajs

module.exports = {
    remoteUrl: "https://jaydenlin.github.io/fake-remote-contents-for-test/contents/pure-js/"
}

When webpack start to run, the queryString you set will automatically append to the end of the url. With the queryString, the actual url we fetch is

//the queryString `en` is appended
https://jaydenlin.github.io/fake-remote-contents-for-test/contents/pure-js/en/

So the contents will be the new url's contents

module.exports=function(){ console.log("Hello USA From Web"); };

It's a node moudle.

Step 2. Set up the webpack.config.js

Use the sigle quote for the querString that you added.

    module: {
        loaders: [{
            test: /\.ajs$/,
            loader: "node-async-require-loader?queryString='en'"
        }]
    }
Step 3. Done

Then the webpack will fetch the remote contents and build the codes for you!
You can see examples/example03 in codes for more detials.

Usage with preParser

In some cases, the remote contents you fetch may not be a pure node moudle. You need a parser to do some stuffs before webpack compiles it. So you can set up a preParser for the remote contents.

webpack.config.js

module.exports = {
...
    NodeAsycRequirePreParser: {
        anyNameYouWant: function(remoteRawContent) {
            // replace by your own contents
            remoteRawContent = "module.exports=function(){ console.log('Replaceed by custom preParser!');}";
            return remoteRawContent;
        }
    }
}

Example with preParser

Step 1. Provide an .ajs file

The file contents of the .ajs file is a node moudle providing a remote url.
Write down the remote url that provides contents(node moudle).
The following is the exmaple of .ajs file.

remote-content.ajs

module.exports = {
    remoteUrl: "https://jaydenlin.github.io/fake-remote-contents-for-test/contents/pure-js/"
}
Step 2. Write down your own the preParser in webpack.config.js

The fetched contents will be passed to the preParser you write (in this example, it will be anyNameYouWant) before webpack compiles it.

webpack.config.js

module.exports = {
...
    NodeAsycRequirePreParser: {
        anyNameYouWant: function(remoteRawContent) {
            // replace by your own contents
            remoteRawContent = "module.exports=function(){ console.log('Replaceed by custom preParser!');}";
            return remoteRawContent;
        }
    }
}

After the fetched contents are parsed by PreParser. The new contents will be

module.exports=function(){ console.log('Replaceed by custom preParser!');}

It's a new node moudle.

Step 3. Set up the preParser in webpack.config.js

To use the preParser that you added, set the preParser=anyNameYouWant

    module: {
        loaders: [{
            test: /\.ajs$/,
            loader: "node-async-require-loader?preParser=anyNameYouWant"
        }]
    }
Step 3. Done

Then the webpack will fetch the remote contents and build the codes for you! You can see examples/example05 in codes for more detials.

Usage with preParser (React templates)

We provide a preparser for pasing react-templates contents. You can use it by setting the preParser=rt.

    module: {
        loaders: [{
            test: /\.ajs$/,
            loader: "node-async-require-loader?preParser=rt"
        }]
    }

Example with preParser (React templates)

Step 1. Provide an .ajs file

The file contents of the .ajs file is a node moudle providing a remote url.
Write down the remote url that provides contents(node moudle).
The following is the exmaple of .ajs file.

remote-content-using-react-templates.ajs

module.exports = {
    remoteUrl: "http://jaydenlin.github.io/fake-remote-contents-for-test/contents/react-template/"
}

the contnets from the remote url are:

<div>
  <h3>Hello World Form Web</h3>
</div>

It is a react-template.

Step 2. Set up the preParser in webpack.config.js

To use React Teamplates parser, set the preParser=rt.

    module: {
        loaders: [{
            test: /\.ajs$/,
            loader: "node-async-require-loader?preParser=rt"
        }]
    }

The react-template preParser will parse the contents to pure node moudle before webpack compiles.

Step 3. Done

Then the webpack will fetch the remote contents and build the codes for you! You can see examples/example02 in codes for more detials.

Example for using local file

Step 1. Provide an new .ajs file

Add local path to ajs file

local-content.ajs

module.exports = {
	remoteUrl: "http://jaydenlin.github.io/fake-remote-contents-for-test/contents/react-template/",
	localPath: './examples/example06--usage-with-local/test.txt'
}

the contnets in the test.txt are:

module.exports=function(){ console.log("Hello World From Local"); }

It's a node moudle.

Step 2. Set up the webpack.config.js

We set up the config ?async=false so that the loader will load the .ajs file and fetch the local node moudle via localPath.

    module: {
        loaders: [{
            test: /\.ajs$/,
            loader: "node-async-require-loader?async=false"
        }]
    }
Step 3. Live reload Settiing

Because this module hijacks the webpack's build proccess, so the local file you used will not be watched by webpack. If you want to let your local file be watched and do live reload by webpack. You need to do the following settings.

npm install watchfile-webpack-plugin --save
var watchFilePlugin = require("watchfile-webpack-plugin");
...
plugins: [
        new watchFilePlugin({watchFolder: "./", watchExtension: "rt"})
    ],
...
Step 4. Done

Then the webpack will get local file content and build the codes for you! You can see examples/example06 and examples/example07 in codes for more detials, it also work fine with react templates.

Test

Use the command to run the mocha test.

npm test

The test tagets are in the examples/ folder.