Loader Back
Description
Loaders are transformations that are applied on a resource file of your app. For example, you can use loaders to tell webpack to load CoffeeScript or JSX
Usage
There are some ways to use loaders:
- explicit in the require statement
- configured via configuration
- configured via CLI
Loader can be passed query parameters via a query string (just like in the web). The query string is appended to the loader with ?
. i.e. url-loader?mimetype=image/png
.
Loaders in require
Note:A void using this, if at all possible, if you intend your scripts to be environment agnostic (node.js and browser). Use the configuration convention for specifying loaders.
Just separate loaders from resource with !
. Each part is resolved relative to the current directory.
require("./loader!./dir/file.txt");
// => uses the file "loader.js" in the current directory to transform
// "file.txt" in the folder "dir".
require("jade!./template.jade");
// => uses the "jade-loader" (that is installed from npm to "node_modules")
// to transform the file "template.jade"
// If configuration has some transforms bound to the file, they will still be applied.
require("!style!css!less!bootstrap/less/bootstrap.less");
// => the file "bootstrap.less" in the folder "less" in the "bootstrap"
// module (that is installed from github to "node_modules") is
// transformed by the "less-loader". The result is transformed by the
// "css-loader" and then by the "style-loader".
// If configuration has some transforms bound to the file, they will not be applied.
require("url-loader?mimetype=image/png!file.png");
Configuration
module.exports = {
entry: './src/app.js',
output: {
path: './bin',
filename: 'app.bundle.js'
},
module: {
loaders: [
{ test: /\.jade$/, loader: "jade" },
// => "jade" loader is used for ".jade" files
{ test: /\.css$/, loader: "style!css" },
// => "style" and "css" loader is used for ".css" files
// Alternative syntax:
{ test: /\.css$/, loaders: ["style", "css"] },
{ test: /\.png$/, loader: "url-loader?mimetype=image/png" },
/** or */
{
test: /\.png$/,
loader: "url-loader",
query: { mimetype: "image/png" }
}
]
}
};
CLI
webpack --module-bind jade --module-bind 'css=style!css' --module-bind "png=url-loader?mimetype=image/png"
Lists of Loaders
basic
json
: Loads file as JSONhson
: Loads HanSON file (JSON for Humans) as JSON objectraw
: Loads raw content of a file (as utf-8)val
: Executes code as module and consider exports as JavaScript codeto-string
: Executes code as a module, casts output to a string and exports itimports
: Imports stuff to the moduleexports
: Exports stuff from the moduleexpose
: Expose exports from a module to the global contextscript
: Executes a JavaScript file once in global context (like in script tag), requires are not parsed.apply
: Executes a exported JavaScript function, optionally with arguments, and exports its return value.callback
: Parses your JS, calls specified functions (which you implement in webpack context) and replaces them with the resultsif-loader
: This is a preprocesser for the webpack module bundler. It support theif
directive,similar to C#ifdef
.ifdef-loader
: Preprocessor for .js/.ts files that allows conditional compilation via// #if
and// #endif
, while preserving sourcemaps.source-map
: ExtractsourceMappingURL
comments from modules and offer it to webpackchecksum
: Computes the checksum of a filenull
: Emits an empty module.cowsay
: Emits a module with a cowsay header.dsv
: Loads csv/tsv files.glsl
: Loads glsl files and support glsl-chunks.render-placement
: Adds React.render to your component for you (not very practical in most cases)xml
: Loads XML as JSON.svg-react
: Load SVG files as JSX-ified React.createClass declarations.svg-url
: Loads SVG file as utf-8 encoded data:URI string.svg-as-symbol
: Wraps content of root element of source SVG file insidesymbol
element and returns resulting markup.base64
: Loads file content as base64 stringng-annotate
: A loader to annotate dependency injections inAngular.js
applications.node
: Loads.node
files that are produced using node-gyp.required
: Require a whole directory of trees in bulk. Require JS, Import CSS and imports stuff in it.icons
Generates iconfont from .svg files (uses gulp-iconfont)markup-inline
Inline SVGs to HTML. It's very useful when applying icon font or applying CSS animation to SVG.block-loader
Generic loader for rewriting only parts of files, based on content start/end delimiters.bundler-configuration
Bundler configuration loader, a tool to include bundler configuration in the resulting build.console
: Prints the resolved require of webpack to the console.solc
: Compiles Solidity code (.sol
) and returns JavaScript objects with Application Binary Interface (ABI) and bytecode ready for deployment to Ethereum.web3
: Deploys Ethereum VM bytecode and returns ready-to-use JS instances of deployed smart contracts. Also returns initialized Web3 object.includes
: Load any text file and support nested includescombine
: Combine results from multiple loaders into one objectregexp-replace
: Replace RegExp matches with a string
packaging
file
: Emits the file into the output folder and returns the (relative) url.url
: The url loader works like the file loader, but can return a Data Url if the file is smaller than a limit.extract
: Prepares HTML and CSS modules to be extracted into a separate file (lean alternative to the ExtractTextWebpackPlugin).worker
: The worker loader creates a WebWorker for the provided file. The bundling of dependencies of the Worker is transparent.shared-worker
: Like the worker loader, but for Shared Workers.serviceworker
: Like the worker loader, but designed for Service Workers.bundle
: Wraps request in arequire.ensure
block (callback)promise
: Wraps request in arequire.ensure
block (promise)async-module
: Same asbundle
, but provides a way to handle script loading errors. Wraps request in arequire.ensure
block (callback, errback)react-proxy
: Code Splitting for react components.react-hot
: Allows to live-edit React components while keeping them mounted and preserving their state.image
: Compresses your images. Ideal to use together withfile
orurl
.img
: Load and compress images with imagemin.base64-image
: Load image as base64 string srcresponsive
: Create multiple resized images for use withsrcset
and CSS media queriessvgo
: Compresses SVG images using svgo librarysvg-sprite
: Like style-loader but for SVG: it creates a single SVG sprite from a set of images, appends it to DOM and returns relativesymbol
url to be used with svg's<use>
.svg-fill
: Changes colors in SVG images. Useful when you embed SVG in CSS as background image and don't want to produce tons of identical files which only differ in theirfill
attributes.line-art
: Inlines SVG files, converting all of its nodes to paths. Useful for line art animations in React components.baggage
: Automatically require any resources related to the required onepolymer
: Process HTML & CSS with preprocessor of choice andrequire()
Web Components like first-class modules.uglify
: Uglify contents of a module. Unlike uglify plugin you can minify with mangling only your application files and not the librarieshtml-minify
: Minifies HTML using minimizevue
: Load single-file Vue.js components as modules, with loader-support for preprocessors.tojson
Serialize module exports as JSON. Cache generated static data as JSON at build time.zip-it
Convert files and directories to zip. Great withfile
.lzstring
Compresses large strings inline using lz-string, and decompresses them at runtimemodernizr
Get your modernizr build bundled with webpacks3
Pull assets from s3 based on filename, path, and desired environment.path-replace
Replace a given base path with another path for dynamic module loadingreact-intl
Loads react-intl locale data viarequire.ensure
font-subset
: Loads a transformed font resource that contains only a specified subset of glyphs with all other glyphs stripped out.w3c-manifest
: Loads a WebApp manifest.json file.web-app-manifest
: Load images referenced in the icons and splash_screens fields in a Web App Manifest.manifest-scope
: Sets the scope of a web app manifest to the public path.
dialects (方言)
coffee
: Loads coffee-script like JavaScriptcoffee-jsx
: Loads coffee-script with JSX like JavaScriptcoffee-redux
: Loads coffee-script like JavaScriptjson5
: Like json, but not so strict.es6
: Loads ES6 modules. (old)esnext
: Transpile ES6 code using esnext.babel
: Turn ES6 code into vanilla ES5 using Babel.regenerator
: Use ES6 generators via Facebook's Regenerator module.livescript
: Loads LiveScript like JavaScriptsweetjs
: Use sweetjs macros.traceur
: Use future JavaScript features with Traceur.ts
: Loads TypeScript like JavaScript.typescript
: Loads TypeScript like JavaScript.awesome-typescript
: Loads TypeScript like JavaScript with watching support. Works with TypeScript 1.5-alfawebpack-typescript
: Loads TypeScript like JavaScript. Supports watch mode and source maps. Works with TypeScript 1.5, 1.6, and nightly builds of TypeScript 1.7 and 1.8.purs
: Loads PureScript like JavaScript.oj
: Loads OJ (an Objective-C like language) files and compiles them to plain JavaScript.elm-webpack
: Loads Elm files and compiles them to plain JavaScript.miel
: Loads Miel syntax and compiles to JavaScript.wisp
: Loads Wisp modules and compiles them to JavaScript.sibilant
: Loads Sibilant files and compiles them to JavaScript.
templating
html
: Exports HTML as string, require references to static resources.dom
: Exports HTML in a DOM element container.riot
: Load RiotJS tags and convert them to javascript.pug
: Loads Pug/Jade template and returns a functionjade-html
: Loads Jade template and returns generated HTMLjade-react
: Uses Jade templates for React rendering instead of JSXvirtual-jade
: Use Jade templates to producevirtual-dom
hyperscript outputtemplate-html
: Loads any template with consolidate.js and returns generated HTMLhandlebars
: Loads handlebars template and returns a functionhandlebars-template-loader
: Loads handlebars template and returns a function (alternative)dust
: Loads dust template and returns a functionractive
: Pre-compiles Ractive templates for interactive DOM manipulationjsx
: Transform jsx code for React to js code.react-templates
: Loads react-template and returns a functionem
: Compiles Emblem to Handlebars.jsejs
: Loads EJS (underscore( templating engine) template and returns a pre-compiled functionejs-html
: Loads EJS templates and returns generated HTML.mustache
: Pre-compiles Mustache templates with Hogan.js and returns a functionyaml
: Converts YAML to JSONyml
: Converts YAML to JavaScript object, optionally omitting blacklisted keysreact-markdown
: Compiles Markdown to a React Component using the markdown-parse parserfront-matter
: Extracts YAML frontmattermarkdown
: Compiles Markdown to HTMLremarkable
: Compiles Markdown to HTML using the Remarkable parsermarkdown-it
: Compiles Markdown to HTML using the markdown-it parsermarkdownattrs
: Compiles Markdown to HTML using the markdown-it-attrs parser that allows you to set classes, identifiers and attributes to your markdown.ng-cache
: Puts HTML partials in the Angular's $templateCachengtemplate
: Bundles your AngularJS templates and Pre-loads the template cache.hamlc
: Compiles haml-coffee templates (.hamlc) and returns a function.haml
: Renders haml-coffee templates (.html.hamlc) and returns a string.jinja
: Precompiles nunjucks and jinja2 templatesnunjucks
: Precompiles nunjucks templatessoy
: Compiles Google Closure templates and returns the namespace with render functionssmarty
: Pre-compiles php smarty templates and returns a functionswagger
: Compiles Swagger/OpenAPI JSON and YAML specifications.template-string
: Use ES6 template strings for html templatesect
: Compile ectjs templatestmodjs
: Load art-template , a template-engine that is widely used in China.layout
: You can use require directly in html now!swig
: Webpack Swig loadertwig
: Webpack Twig.js loader
styling
bootstrap-webpack
: Loads a configuration file for Twitter Bootstrap integration using Less. Allows complete customisation via Less.font-awesome-webpack
: Loads a configuration file for Font Awesome integration using Less. Allows complete customisation via Less.bootstrap-sass
: Deprecated. Seebootstrap
.bootstrap
: Loads a configuration file for Twitter Bootstrap integration using Sass. Allows complete customization via Sass. Supports CSS modules and both Bootstrap 3 and 4.font-awesome
: Easy integration of font-awesome with customization via Sass.style
: Add exports of a module as style to DOMisomorphic-style
: Ad-hoc replacement tostyle-loader
adding support of isomorphic apps and critical path CSScss
: Loads css file with resolved imports and returns css codecess
: Loads and compile cess file (css in es6 js) to cssless
: Loads and compiles a less filesass
: Loads and compiles a scss filestylus
: Loads and compiles a stylus filecsso
: Minifies input CSS with CSSOrework
: Post-process CSS with Rework and returns CSS codepostcss
: Post-process CSS with Autoprefixer and other PostCSS pluginsautoprefixer
: Add vendor prefixes to CSS rules using values from Can I Usenamespace-css
: Namespace your css with a given selector (for encapsulating all rules in one subset of your site)fontgen
: Create your own webfont with proper CSS on-the-fly and include it into WebPack.classnames
: Automatically bind css-modules to classnames.theo
Loads Design Tokens files transformed by theobulma
Customize Bulma in your css-modules.css-to-string
Convert the output of thecss-loader
back to a string.
translation
po
: Loads a PO gettext file and returns JSONpo2mo
: Compiles gettext PO files to MO filesformat-message
: Compiles translations to ICU Message Format strings informatMessage
callsjsxlate
: Transform React source code for use withjsxlate
angular-gettext
Compiles.po
files asAngular.js
module orjson
to be used withangular-gettext
.webpack-angular-translate
: Extracts angular-translate translation id's and default text'sangular-gettext-extract
Extracts strings for translation into a nominated.pot
file.gettext
: Compiles a Gettext PO file from code source.preprocessor
: The preprocessor-loader provides the ability to preprocess source files through user defined regular expressions, macros, and callback routines. All user defined logic can be applied to line scope or source scope.amdi18n-loader
: I18n loader similar to require.js i18n plugin. The language packs support AMD/CommonJS module and can be written in.json
/.js
/.coffee
files.sprockets-preloader
: Translate sprockets require directives into JavaScript module dependencies. Useful for migrating a Rails project to Webpack module bundling.properties
: Loader for .properties files.transifex
: Fetch imported translation files from transifex.
support
mocha
: do tests with mocha in browser or node.jscoverjs
: PostLoader to code coverage with CoverJsistanbul-instrumenter
: Istanbul postLoader to code coverage with karma-webpack and karma-coverageisparta-instrumenter
: Isparta preLoader to code coverage with karma-webpack and douglasduteil/karma-coverage#nextibrik-instrumenter
: Ibrik preLoader to CoffeeScript code coverage with karma-webpack and douglasduteil/karma-coverageeslint
: PreLoader for linting code using ESLint.jshint
: PreLoader for linting code.jscs
: PreLoader for style checking.standard
: Conform to standard code style.inject
: A Webpack loader for injecting code into modules via their dependenciesinjectable
: Allow to inject dependencies into modulestransform
: Use browserify transforms as loader.falafel
: Use falafel AST transforms as a loader.image-size
: Loads an image and returns its dimensions and typecsslint
: PreLoader for linting code using CSSLintcoffeelint
: PreLoader for linting CoffeeScript.tslint
: PreLoader for linting TypeScript using TSLintparker
: Output a stylesheet analysis report using parker.sjsp
: Inject some codes for profiling using node-sjsp.amdcheck
: Uses AST to find and remove unused dependencies in AMD modules using amdextract.manifest
: A loader to generate JSON asset manifests to pass to preloading systems.gulp-rev
: Use in tandem with gulp-rev to replace assets from rev-manifest.html-test
Test your html templates (for example) for analytics.stylelint
Preloader for linting SASS and SCSS with style lintstylefmt
Preloader for fixing SASS/SCSS/PostCSS errors with style lintscsslint
: PreLoader for linting Scss using scss-lint.htmlhint
: PreLoader for linting HTML using htmlhint-loader.documentation
: Javascript documentation with DocumentationJSsassdoc
: Scss documentation with SassDocperformance-loader
: profile module performance