All Projects → webpack-contrib → Expose Loader

webpack-contrib / Expose Loader

Licence: mit
Expose Loader

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Expose Loader

typed-css-modules-loader
💠 Webpack loader for typed-css-modules auto-creation
Stars: ✭ 62 (-88.32%)
Mutual labels:  webpack-loader
Speedy.js
Accelerate JavaScript Applications by Compiling to WebAssembly
Stars: ✭ 300 (-43.5%)
Mutual labels:  webpack-loader
Css Loader
CSS Loader
Stars: ✭ 4,067 (+665.91%)
Mutual labels:  webpack-loader
React Proxy Loader
Wraps a react component in a proxy component to enable Code Splitting.
Stars: ✭ 258 (-51.41%)
Mutual labels:  webpack-loader
Source Map Loader
extract sourceMappingURL comments from modules and offer it to webpack
Stars: ✭ 294 (-44.63%)
Mutual labels:  webpack-loader
Script Loader
[deprecated] Script Loader
Stars: ✭ 327 (-38.42%)
Mutual labels:  webpack-loader
stylos
Webpack plugin to automatically generate and inject CSS utilities to your application
Stars: ✭ 60 (-88.7%)
Mutual labels:  webpack-loader
Svg Inline Loader
Inline SVG loader with cleaning-up functionality
Stars: ✭ 490 (-7.72%)
Mutual labels:  webpack-loader
Extract Loader
webpack loader to extract HTML and CSS from the bundle
Stars: ✭ 297 (-44.07%)
Mutual labels:  webpack-loader
Sass Loader
Compiles Sass to CSS
Stars: ✭ 3,718 (+600.19%)
Mutual labels:  webpack-loader
Istanbul Instrumenter Loader
Istanbul Instrumenter Loader
Stars: ✭ 272 (-48.78%)
Mutual labels:  webpack-loader
Graphql Let
A layer to start/scale the use of GraphQL code generator.
Stars: ✭ 282 (-46.89%)
Mutual labels:  webpack-loader
Markdown Loader
markdown loader for webpack
Stars: ✭ 335 (-36.91%)
Mutual labels:  webpack-loader
sass-to-string
webpack loader that transform your SCSS file in a javascript string
Stars: ✭ 17 (-96.8%)
Mutual labels:  webpack-loader
Json Loader
json loader module for webpack - UNMAINTAINED
Stars: ✭ 431 (-18.83%)
Mutual labels:  webpack-loader
web-components-loader
Webpack loader that makes it incredibly easy to import HTML-centric Web Components into your project.
Stars: ✭ 34 (-93.6%)
Mutual labels:  webpack-loader
Css Hot Loader
This is a css hot loader, which support hot module replacement for an extracted css file.
Stars: ✭ 317 (-40.3%)
Mutual labels:  webpack-loader
Imports Loader
Imports Loader
Stars: ✭ 500 (-5.84%)
Mutual labels:  webpack-loader
Inject Loader
💉📦 A Webpack loader for injecting code into modules via their dependencies.
Stars: ✭ 474 (-10.73%)
Mutual labels:  webpack-loader
Awesome Cms Core
Awesome CMS Core is an open source CMS built using ASP.Net Core & ReactJS with module seperation concern in mind and provide lastest trend of technology like .Net Core, React, Webpack, SASS, Background Job, Message Queue.
Stars: ✭ 352 (-33.71%)
Mutual labels:  webpack-loader

npm node deps tests coverage chat size

expose-loader

The expose-loader loader allows to expose a module (in whole or in part) to global object (self, window and global).

For further hints on compatibility issues, check out Shimming of the official docs.

Getting Started

To begin, you'll need to install expose-loader:

$ npm install expose-loader --save-dev

(If you're using WebPack 4, install [email protected] and follow the corresponding instructions instead.)

Then you can use the expose-loader using two approaches.

Inline

The | or %20 (space) allow to separate the globalName, moduleLocalName and override of expose. The documentation and syntax examples can be read here.

%20 is space in a query string, because you can't use spaces in URLs

import $ from "expose-loader?exposes=$,jQuery!jquery";
//
// Adds the `jquery` to the global object under the names `$` and `jQuery`
import { concat } from "expose-loader?exposes=_.concat!lodash/concat";
//
// Adds the `lodash/concat` to the global object under the name `_.concat`
import {
  map,
  reduce,
} from "expose-loader?exposes=_.map|map,_.reduce|reduce!underscore";
//
// Adds the `map` and `reduce` method from `underscore` to the global object under the name `_.map` and `_.reduce`

Using Configuration

src/index.js

import $ from "jquery";

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: require.resolve("jquery"),
        loader: "expose-loader",
        options: {
          exposes: ["$", "jQuery"],
        },
      },
      {
        test: require.resolve("underscore"),
        loader: "expose-loader",
        options: {
          exposes: [
            "_.map|map",
            {
              globalName: "_.reduce",
              moduleLocalName: "reduce",
            },
            {
              globalName: ["_", "filter"],
              moduleLocalName: "filter",
            },
          ],
        },
      },
    ],
  },
};

The require.resolve call is a Node.js function (unrelated to require.resolve in webpack processing). require.resolve gives you the absolute path to the module ("/.../app/node_modules/jquery/dist/jquery.js"). So the expose only applies to the jquery module. And it's only exposed when used in the bundle.

And run webpack via your preferred method.

Options

Name Type Default Description
exposes {String|Object|Array<String|Object>} undefined List of exposes

exposes

Type: String|Object|Array<String|Object> Default: undefined

List of exposes.

String

Allows to use a string to describe an expose.

Syntax

The | or %20 (space) allow to separate the globalName, moduleLocalName and override of expose.

String syntax - [[globalName] [moduleLocalName] [override]] or [[globalName]|[moduleLocalName]|[override]], where:

  • globalName - the name in the global object, for example window.$ for a browser environment (required)
  • moduleLocalName - the name of method/variable/etc of the module (the module must export it) (may be omitted)
  • override - allows to override existing value in the global object (may be omitted)

If moduleLocalName is not specified, it exposes the entire module to the global object, otherwise it exposes only the value of moduleLocalName.

src/index.js

import _ from "underscore";

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: require.resolve("jquery"),
        loader: "expose-loader",
        options: {
          // For `underscore` library, it can be `_.map map` or `_.map|map`
          exposes: "jquery",
        },
      },
    ],
  },
};

Object

Allows to use an object to describe an expose.

globalName

Type: String|Array<String> Default: undefined

The name in the global object. (required).

src/index.js

import _ from "underscore";

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: require.resolve("underscore"),
        loader: "expose-loader",
        options: {
          exposes: {
            // Can be `['_', 'filter']`
            globalName: "_.filter",
            moduleLocalName: "filter",
          },
        },
      },
    ],
  },
};
moduleLocalName

Type: String Default: undefined

The name of method/variable/etc of the module (the module must export it). If moduleLocalName is specified, it exposes only the value of moduleLocalName.

src/index.js

import _ from "underscore";

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: require.resolve("underscore"),
        loader: "expose-loader",
        options: {
          exposes: {
            globalName: "_.filter",
            moduleLocalName: "filter",
          },
        },
      },
    ],
  },
};
override

Type: Boolean Default: false

By default loader does not override the existing value in the global object, because it is unsafe. In development mode, we throw an error if the value already present in the global object. But you can configure loader to override the existing value in the global object using this option.

To force override the value that is already present in the global object you can set the override option to the true value.

src/index.js

import $ from "jquery";

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: require.resolve("jquery"),
        loader: "expose-loader",
        options: {
          exposes: {
            globalName: "$",
            override: true,
          },
        },
      },
    ],
  },
};

Array

src/index.js

import _ from "underscore";

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: require.resolve("underscore"),
        loader: "expose-loader",
        options: {
          exposes: [
            "_.map map",
            {
              globalName: "_.filter",
              moduleLocalName: "filter",
            },
            {
              globalName: ["_", "find"],
              moduleLocalName: "myNameForFind",
            },
          ],
        },
      },
    ],
  },
};

It will expose only map, filter and find (under myNameForFind name) methods to the global object.

In a browser these methods will be available under windows._.map(..args), windows._.filter(...args) and windows._.myNameForFind(...args) methods.

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].