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

closure-global-loader

v0.0.1

Published

Webpack loader for google closure library dependencies

Downloads

1

Readme

closure-global-loader (Fork of closure-loader)

npm deps test

This is a Webpack loader which resolves goog.provide() and goog.require() statements in Webpack as if they were regular CommonJS modules.

Installation

npm install --save-dev closure-global-loader

Usage

Documentation: Using loaders

NOTE: This loader is mainly meant for building (probably older) Closure Compiler projects with Webpack and to make a transition to other module systems (CommonJS or ES6) easier.

There are two parts to this loader:

  • goog.provide()
    • Basically just creates the given namespace in the codebase if it doesn't already exist.
    • Any file containing this statement will be added to a map for require lookups
  • goog.require()
    • Like goog.provide() it creates the given namespace in the codebase if it doesn't already exist.
    • It finds the corresponding file with the goog.provide() statement and loads it (see configuration below)

In the simplest way you can just use those two statements like you usually would with Google Closure Tools.

NOTE: Usually the Closure compiler simply creates all namespaces on the global scope (i.e. the window object). The original closure-loader changed this behaviour by creating a local namespace for every file. This fork restores the original closure compiler behaviour by creating a single shared namespace. This shared namespace is not exposed on the window by default, unless it's already defined, in which case it simply extends the existing namespace as you'd expect with goog.provide().

You can use Closure dependencies in conjunction with CommonJS syntax. You can load any module that uses goog.provide() with require(), but not the other way round.

// moduleA.js
goog.provide('my.app.moduleA');

my.app.moduleA = function () {
    console.log('module A was loaded');
}

// moduleB.js
goog.require('my.app.moduleA')
goog.provide('my.app.moduleB');

my.app.moduleB = function () {
    console.log('module B was loaded');
}

// index.js
require('my.app.moduleB');

my.app.moduleB(); // will output 'module B was loaded' to the console

my.app.moduleA(); // will output 'module A was loaded' to the console

var moduelA = require('./moduleA.js').my.app.moduleA;
moduleA(); // will output 'module A was loaded' to the console

var moduleB = require('./moduleB.js').my.app.moduleB;
moduleB(); // will output 'module B was loaded' to the console

require('./moduleA.js').my.app === require('./moduleB.js').my.app; // true
my.app === require('./moduleA.js').my.app // true

ES6 Modules

If you use babel you can even use ES6 import syntax. If you have enabled the es6mode in the loader config the first goog.provide() of each file will be exported as "default" in addition to its full namespace.

// module.js
goog.provide('my.app.module');

my.app.module = function () {
    console.log('my module was loaded');
}

// index.js
import module from './module.js';
// is the same as
var module = require('./module.js').default;
// or
var module = require('./module.js').my.app.module;

module(); // will output 'my module was loaded' to the console

Configuration

Here is an example Webpack config for this loader:

module.exports = {
    entry: {
        app: './src/index.js'
    },
    output: {
        path: './build',
        filename: '[name].js'
    },
    module: {
        rules: [
            {
                test: /\/src\/.*\.js$/,
                loader: 'closure-loader',
                options: {
                    paths: [
                        __dirname + '/src',
                    ],
                    es6mode: true,
                    watch: true,
                    fileExt: '.js',
                },
                exclude: [/node_modules/, /test/]
            }
        ]
    },
};

Here are the configuration options specific for this loader:

  • paths (array): An array of path strings. The loader will search all *.js files within theses paths for goog.provide() statements when resolving a goog.require(). You can only goog.require() dependencies that can be found under one of these paths.
  • es6mode (boolean, default: false): If enabled it will add the value of the first goog.provide() as default export for usage with babel. For this reason it will also export the corresponding flag module.exports.__esModule = true
  • watch (boolean, default: true): If true, the loader will intitialise watchers which check for changes in the mapped files. This is neccesary to be able to delete the internal map cache. But it also makes problems with CI sytstems and build scripts, because the watcher will prevent the process from beeing exited.
  • fileExt (string, default: '.js'): Files extension which will be searched for dependency resolving. Support glob pattern syntax.

NOTE: This loader in no way includes or wraps the actual Google Closure Library. If you want to use the Closure Library you will have to include it yourself and ensure correct shimming:

module: {
    rules: [
        {
            test: /google-closure-library\/closure\/goog\/base/,
            use: [
                'imports-loader?this=>{goog:{}}&goog=>this.goog',
                'exports-loader?goog',
            ],
        },
    ],
},
plugins: [
    new webpack.ProvidePlugin({
        goog: 'google-closure-library/closure/goog/base',
    }),
]

Authors

  • Joel Kornbluh - Created this fork - Joel Kornbluh
  • Steven Weingärtner - Original author & maintainer - eXaminator
  • Matt Mulder - Current maintainer - mxmul

See also the list of contributors who participated in this project.

License

MIT (http://www.opensource.org/licenses/mit-license.php)