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

legacy-loader

v0.0.2

Published

Webpack loader that prevents scripts from extending the window object

Downloads

5,279

Readme

legacy-loader

Webpack loader that prevents scripts from extending the window object

Build Status Dependency Status Coverage Status

Use this loader to cope with legacy scripts that extend the window object instead of using AMD or CommonJS.

Installation

npm status

Usage

Basic example

Imagine you've downloaded some-legacy-script from npm which looks like

window.someLegacyScript = function () {
    console.log("Yay!");
};

Now just run npm i legacy-loader --save and configure your webpack.config.js like this

module.exports = {
    module: {
        loaders: [
            {
                test: /[\/\\]node_modules[\/\\]some-legacy-script[\/\\]index\.js$/,
                loader: "legacy"
            }
        ]
    }
};

then you can do

var someLegacyScript = require("some-legacy-script");

someLegacyScript(); // prints 'Yay!'
window.someLegacyScript; // undefined

Auto export

The legacy-loader exports a single value via module.exports when your legacy script did only add one property to the window object. If it added two or more, an object is returned instead:

// node_modules/some-legacy-script/index.js

window.propertyA = true;
window.propertyB = false;
// app.js

var someLegacyScript = require("some-legacy-script");

someLegacyScript.propertyA; // true
someLegacyScript.propertyB; // false

window.propertyA; // undefined
window.propertyB; // undefined

Specific exports

When your legacy script adds two or more properties, but you're still just interested in one particular property, you can also pass a property name:

// webpack.config.js

    ...
    {
        test: /[\/\\]node_modules[\/\\]some-legacy-script[\/\\]index\.js$/,
        loader: "legacy?exports=propertyA"
    }
    ...
// app.js

var someLegacyScript = require("some-legacy-script");

someLegacyScript; // true -> propertyA

Publish

Sometimes other libraries are relying on a particular global variable (like jQuery plugins rely on $). Then you should first consider using the imports-loader to inject that variable into the private module scope. If this is not an option for you (e.g. because you're not loading this module via webpack), you can decide to publish a single property back to the window object.

// webpack.config.js

    ...
    {
        test: /[\/\\]node_modules[\/\\]some-legacy-script[\/\\]index\.js$/,
        loader: "legacy?publish=propertyB"
    }
    ...
// app.js

var someLegacyScript = require("some-legacy-script");

someLegacyScript.propertyA; // true
someLegacyScript.propertyB; // false

window.propertyA; // undefined
window.propertyB; // false

Under the hood

The legacy-loader creates a window shim by inheriting from window via Object.create(window). Thus the legacy script receives a window-like object, without being able to extend it. Of course, this approach has the usual limitations implied by the prototype inheritance (such as iterating over window while checking for hasOwnProperty).

Contributing

From opening a bug report to creating a pull request: every contribution is appreciated and welcome. If you're planing to implement a new feature or change the api please create an issue first. This way we can ensure that your precious work is not in vain.

All pull requests should have 100% test coverage (with notable exceptions) and need to pass all tests.

  • Call npm test to run the unit tests
  • Call npm run coverage to check the test coverage (using istanbul)

License

Unlicense