Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
closure-loader
Advanced tools
This is a webpack loader which resolves goog.provide()
and goog.require()
statements in webpack
just like if they were regular CommonJS modules.
npm install --save-dev closure-loader
NOTE: This loader is mainly meant for building (probably older) closure library 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()
goog.require()
goog.provide()
it creates the given namespace in the scope of that modulegoog.provide()
statement and loads it (see configuration below)In the simplest way you can just use those two statements like you usually would with the google closure library.
NOTE: Usually the closure lib simply creates all namespaces on the global scope (i.e. the window object). This is not the case if you use this loader. Every file ("module") has its own scope just like it would have if you used CommonJS syntax.
You can use closure library dependencies in conjunction with CommonJS syntax. You can load any module that uses
goog.provide()
with require()
, but not the other way round.
// module.js
goog.provide('my.app.module');
my.app.module = function () {
console.log('my module was loaded');
}
// index.js
var module = require('./module.js').my.app.module;
module(); // will output 'my module was loaded' to the console
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
Here is an example webpack config for this loader:
module.exports = {
entry: {
app: './src/index.js'
},
output: {
path: './build',
filename: '[name].js'
},
module: {
loaders: [
{
test: /\/src\/.*\.js$/,
loaders: [
'closure-loader'
],
exclude: [/node_modules/, /test/]
}
]
},
closureLoader: {
paths: [
__dirname + '/src'
],
es6mode: true,
watch: true
}
};
Here are the configuration options specific for this loader:
*.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.goog.provide()
as default export for usage with babel. For this reason it will also export the corresponding flag
module.exports.__esModule = true
In the hopes of clarifying the usage of the loader a bit I have provided a couple of examples which
you can find in the examples
directory.
To run an example please follow these steps:
npm install
in the closure-loader root directorynpm install
in the directory of the examplenpm start
or npm run build
in the directory of the exampleThe following examples are available:
goog.provide()
and
goog.require()
via commonJs require()
calls.require()
calls.goog.provide()
and
goog.require()
via babel and es6 import
calls.import
calls.goog.require()
calls. This is not advised. If you are using webpack you should think about using a proper module loader,
preferably es6 as this is now the standard.NOTE: This loader does in no way include or wrap the actual google closure library. If you want to use the closure library you will have to include it yourself and ensure correct shimming. See the above examples on how this can be done.
FAQs
Webpack loader for google closure library dependencies
The npm package closure-loader receives a total of 20 weekly downloads. As such, closure-loader popularity was classified as not popular.
We found that closure-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.