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

source-loader

v1.0.0

Published

Webpack loader that exports the source directly

Downloads

448

Readme

Webpack Source Loader

npm tests dependencies coverage

Webpack loader that exports the source directly

Note: This project is in early development, and versioning is a little different. Read this for more details.

Why should you care?

If you need to load up a file's source directly and have it available within your javascript, this loader is what you're after. For example, you could load up a svg file, parse out a path you want, and manipulate that with your javascript somehow. Or you could load up a json or text file. Or some other type of file that you came up with yourself. Whatever it is, as long as it's contents would be ok after being run through JSON.stringify (read: text files, not images or binary files), it will work just great with the source loader. And if it is a binary file, it will work great as well, but you won't be able to require it in your client-side js, just manipulate it through a plugin.

Installation

npm install source-loader -S

Usage

Just load it up in your webpack config like this:

module.exports = {
  module: {
    loaders: [
      { test: /\.foo$/, loader: 'source-loader' }
    ]
  }
}

Then you can require it up in your main entry:

const fooFile = require('testing.foo')
console.log(fooFile) // wow it's the contents!

As an added bonus, this loader makes the buffered raw source available on the loaderContext object so that plugins can manipulate it in any way necessary.

Let's break down how this could be done. Inside any plugin hook, you have a compilation object. You can get the loaderContext for any of the modules that webpack is processing through compilation.modules -- just find the one(s) you want by name. Now you have a large object which is an instance of the DependenciesBlock class, with a bunch of great information on it. You can find the raw buffered source under the _src property if the file was loaded with the source-loader.

Additionally, if you have a specific source that is valid javascript and you'd like this loader not to output it as a string that would need to be eval'd in order to use the javascript, there's a hack for that. Within a plugin or another loader, if you add _jsSource as a truthy property to the module, it will skip the extra stringification and output the source raw. Note that you will get an error if it's not valid javascript, so make sure you are only setting the _jsSource property if you are positive that what is coming out of your loader chain is going to be js. To set from a loader, this._module._jsSource = true will do it, and from a plugin, you can do the same as is described above with the _src property.

Wondering what sets this loader apart from raw-loader? This is it. Both loaders expose the file's contents to be required by webpack, but this loader also exposes the raw source for plugin processing, and allows raw js to be passed through conditionally. It also does not try to stringify binary files (which can cause bugs), has tests, and is actively maintained, as a bonus.

License & Contributing