From 85a2156134e6c1cbcff3d89c69f2c250917d91c2 Mon Sep 17 00:00:00 2001 From: Michael Ciniawsky Date: Fri, 25 Nov 2016 02:35:57 +0100 Subject: [PATCH 1/2] docs(README): refactor for webpack v2 --- README.md | 331 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 229 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index 55674fc6..e4c71a5c 100644 --- a/README.md +++ b/README.md @@ -1,66 +1,127 @@ -# css loader for webpack - -## installation +[![npm][npm]][npm-url] +[![node][node]][node-url] +[![deps][deps]][deps-url] +[![tests][tests]][tests-url] +[![coverage][cover]][cover-url] +[![chat][chat]][chat-url] + +
+ + + + +

CSS Loader

+
+ +

Install

+ +```bash +npm install --save-dev css-loader +``` -`npm install css-loader --save-dev` +

Usage

-## Usage +**require** +```js +const css = require('css!./file.css') +``` -[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html) +**webpack.config.js** +```js +const css = require('file.css') +``` -``` javascript -var css = require("css-loader!./file.css"); -// => returns css code from file.css, resolves imports and url(...) +```js +module.exports = { + module: { + rules: [ + { + test: /\.css$/, + use: [ 'style-loader', 'css-loader' ] + } + ] + } +}; ``` -`@import` and `url(...)` are interpreted like `require()` and will be resolved by the css-loader. +`@import` and `url()` are interpreted like `require()` and will be resolved by the css-loader. Good loaders for requiring your assets are the [file-loader](https://github.com/webpack/file-loader) and the [url-loader](https://github.com/webpack/url-loader) which you should specify in your config (see below). To be compatible with existing css files (if not in CSS Module mode): + * `url(image.png)` => `require("./image.png")` * `url(~module/image.png)` => `require("module/image.png")` -### Example config +

Options

-This webpack config can load css files, embed small png images as Data Urls and jpg images as files. +|Name|Default|Description| +|:--:|:-----:|:----------| +|**`root`**|`/`|Path to resolve URLs, URLs starting with `/` will not be translated| +|**`modules`**|`false`|Enable/Disable CSS Modules| +|**`import`** |`true`| Enable/Disable @import handling| +|**`url`**|`true`| Enable/Disable `url()` handling| +|**`minimize`**|`true`|Enable/Disable minification| +|**`sourceMap`**|`false`|Enable/Disable Sourcemaps| +|**`camelCase`**|`false`|Export Classnames in CamelCase| +|**`importLoaders`**|`0`|Number of loaders applied before CSS loader| -``` javascript +This webpack config can load css files, embed small png images as Data URLs and jpg images as files. + +**webpack.config.js** +```js module.exports = { module: { - loaders: [ - { test: /\.css$/, loader: "style-loader!css-loader" }, - { test: /\.png$/, loader: "url-loader?limit=100000" }, - { test: /\.jpg$/, loader: "file-loader" } + rules: [ + { + test: /\.css$/, + use: [ 'style-loader', 'css-loader' ] + }, + { + test: /\.png$/, + use: { loader: 'url-loader', options: { limit: 100000 } }, + }, + { + test: /\.jpg$/, + use: [ 'file-loader' ] + } ] } }; ``` -### 'Root-relative' urls +### Root + +For URLs that start with a `/`, the default behavior is to not translate them: -For urls that start with a `/`, the default behavior is to not translate them: * `url(/image.png)` => `url(/image.png)` -If a `root` query parameter is set, however, it will be prepended to the url +If a `root` query parameter is set, however, it will be prepended to the URL and then translated: -With a config like: - -``` javascript - loaders: [ - { test: /\.css$/, loader: "style-loader!css-loader?root=." }, - ... +**webpack.config.js** +```js +rules: [ + { + test: /\.css$/, + use: [ + "style-loader", + { + loader: "css-loader", + options: { root: "." } + } ] + } +] ``` -The result is: - * `url(/image.png)` => `require("./image.png")` Using 'Root-relative' urls is not recommended. You should only use it for legacy CSS files. -### Local scope +### CSS Scope By default CSS exports all class names into a global selector scope. Styles can be locally scoped to avoid globally scoping styles. @@ -70,17 +131,15 @@ With `:local` (without brackets) local mode can be switched on for this selector The loader replaces local selectors with unique identifiers. The choosen unique identifiers are exported by the module. -Example: - -``` css +**app.css** +```css :local(.className) { background: red; } :local .className { color: green; } :local(.className .subClass) { color: green; } :local .className .subClass :global(.global-class-name) { color: blue; } ``` -is transformed to - +**app.bundle.css** ``` css ._23_aKvs-b8bW2Vg3fwHozO { background: red; } ._23_aKvs-b8bW2Vg3fwHozO { color: green; } @@ -88,7 +147,7 @@ is transformed to ._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 .global-class-name { color: blue; } ``` -and the identifiers are exported: +> Note: Identifiers are exported ``` js exports.locals = { @@ -97,49 +156,62 @@ exports.locals = { } ``` -Camelcasing is recommended for local selectors. They are easier to use in the importing javascript module. +CamelCase is recommended for local selectors. They are easier to use in the within the imported JS module. -`url(...)` URLs in block scoped (`:local .abc`) rules behave like requests in modules: - * `./file.png` instead of `file.png` - * `module/file.png` instead of `~module/file.png` +`url()` URLs in block scoped (`:local .abc`) rules behave like requests in modules: +* `./file.png` instead of `file.png` +* `module/file.png` instead of `~module/file.png` You can use `:local(#someId)`, but this is not recommended. Use classes instead of ids. -You can configure the generated ident with the `localIdentName` query parameter (default `[hash:base64]`). Example: `css-loader?localIdentName=[path][name]---[local]---[hash:base64:5]` for easier debugging. +You can configure the generated ident with the `localIdentName` query parameter (default `[hash:base64]`). -You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema. Note that this requires `webpack@2` since to be able to pass function in. For example: + **webpack.config.js** + ```js +{ + test: /\.css$/, + use: [ + { + loader: 'css-loader', + options: { + modules: true, + localIdentName: '[path][name]__[local]--[hash:base64:5]' + } + } + ] +} +``` + +You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema. Note that this requires `webpack >= v2.x.` since to be able to pass function in. For example: ```js { - test: /\.css$/, - loaders: [ - { - loader: 'css-loader', - query: { - modules: true, - importLoaders: 1, - getLocalIdent: function (loaderContext, localIdentName, localName, options) { - return 'whatever_random_class_name' - } - } + test: /\.css$/, + use: [ + { + loader: 'css-loader', + options: { + modules: true, + localIdentName: '[path][name]__[local]--[hash:base64:5]', + getLocalIdent: (context, localIdentName, localName, options) => { + return 'whatever_random_class_name' } - ] -}, + } + } + ] +} ``` - Note: For prerendering with extract-text-webpack-plugin you should use `css-loader/locals` instead of `style-loader!css-loader` **in the prerendering bundle**. It doesn't embed CSS but only exports the identifier mappings. -### CSS Modules +### [CSS Modules](https://github.com/css-modules/css-modules) -See [CSS Modules](https://github.com/css-modules/css-modules). +The query parameter `modules` enables the **CSS Modules** spec. -The query parameter `modules` enables the **CSS Modules** spec. (`css-loader?modules`) +This enables local scoped CSS by default. (You can switch it off with `:global(...)` or `:global` for selectors and/or rules.) -This enables Local scoped CSS by default. (You can switch it off with `:global(...)` or `:global` for selectors and/or rules.) - -### Composing CSS classes +### CSS Composing When declaring a local class name you can compose a local class from another local class name. @@ -157,15 +229,13 @@ When declaring a local class name you can compose a local class from another loc This doesn't result in any change to the CSS itself but exports multiple class names: -``` js +```js exports.locals = { className: "_23_aKvs-b8bW2Vg3fwHozO", subClass: "_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO" } ``` -and CSS is transformed to: - ``` css ._23_aKvs-b8bW2Vg3fwHozO { background: red; @@ -177,7 +247,7 @@ and CSS is transformed to: } ``` -### Importing local class names +### Importing CSS Locals To import a local class name from another module: @@ -208,30 +278,47 @@ To import from multiple modules use multiple `composes:` rules. ### SourceMaps -To include SourceMaps set the `sourceMap` query param. - -`require("css-loader?sourceMap!./file.css")` +To include Sourcemaps set the `sourceMap` query param. I. e. the extract-text-webpack-plugin can handle them. -They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS SourceMap do not). In addition to that relative paths are buggy and you need to use an absolute public path which include the server url. - -### importing and chained loaders +They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS SourceMap do not). In addition to that relative paths are buggy and you need to use an absolute public path which include the server URL. -The query parameter `importLoaders` allow to configure which loaders should be applied to `@import`ed resources. +**webpack.config.js** +```js +{ + test: /\.css$/, + use: [ + { + loader: 'css-loader', + options: { + sourceMap: true + } + } + ] +} +``` -`importLoaders` (int): That many loaders after the css-loader are used to import resources. +### ImportLoaders -Examples: +The query parameter `importLoaders` allow to configure which loaders should be applied to `@import`ed resources. -``` js -require("style-loader!css-loader?importLoaders=1!postcss-loader!...") -// => imported resources are handled this way: -require("css-loader?importLoaders=1!postcss-loader!...") +`importLoaders`: That many loaders after the css-loader are used to import resources. -require("style-loader!css-loader!stylus-loader!...") -// => imported resources are handled this way: -require("css-loader!...") +**webpack.config.js** +```js +{ + test: /\.css$/, + use: [ + { + loader: 'css-loader', + options: { + importLoaders: 1 + } + }, + 'postcss-loader' + ] +} ``` This may change in the future, when the module system (i. e. webpack) supports loader matching by origin. @@ -240,41 +327,81 @@ This may change in the future, when the module system (i. e. webpack) supports l By default the css-loader minimizes the css if specified by the module system. -In some cases the minification is destructive to the css, so you can provide some options to it. cssnano is used for minification and you find a [list of options here](http://cssnano.co/options/). Just provide them as query parameter: i. e. `require("css-loader?-colormin")` to disable making color values as small as possible. +In some cases the minification is destructive to the css, so you can provide some options to it. cssnano is used for minification and you find a [list of options here](http://cssnano.co/options/). You can also disable or enforce minification with the `minimize` query parameter. -`require("css-loader?minimize!./file.css")` (enforced) +**webpack.config.js** +```js +{ + test: /\.css$/, + use: [ + { + loader: 'css-loader', + options: { + minimize: true || {/* CSSNano Options */} + } + } + ] +} +``` -`require("css-loader?-minimize!./file.css")` (disabled) +### CamelCase -### Disable behavior +By default, the exported JSON keys mirror the class names. If you want to camelize class names (useful in JS), pass the query parameter `camelCase` to css-loader. -`css-loader?-url` disables `url(...)` handling. +**webpack.config.js** +```js +{ + test: /\.css$/, + use: [ + { + loader: 'css-loader', + options: { + camelCase: true + } + } + ] +} +``` -`css-loader?-import` disables `@import` handling. +```css +.class-name {} +``` -### Camel case +```js +const className = require('file.css').className +``` -By default, the exported JSON keys mirror the class names. If you want to camelize class names (useful in Javascript), pass the query parameter `camelCase` to the loader. +

Maintainer

-Example: + + + + + + +
+ +
+ Tobias Koppers +
-`css-loader?camelCase` -Usage: -```css -/* file.css */ +[npm]: https://img.shields.io/npm/v/css-loader.svg +[npm-url]: https://npmjs.com/package/css-loader -.class-name { /* ... */ } -``` +[node]: https://img.shields.io/node/v/css-loader.svg +[node-url]: https://nodejs.org -```js -// javascript +[deps]: https://david-dm.org/webpack/css-loader.svg +[deps-url]: https://david-dm.org/webpack/css-loader -require('file.css').className -``` +[tests]: http://img.shields.io/travis/webpack/css-loader.svg +[tests-url]: https://travis-ci.org/webpack/css-loader -## License +[cover]: https://coveralls.io/repos/github/webpack/css-loader/badge.svg +[cover-url]: https://coveralls.io/github/webpack/css-loader -MIT (http://www.opensource.org/licenses/mit-license.php) +[chat]: https://badges.gitter.im/webpack/webpack.svg +[chat-url]: https://gitter.im/webpack/webpack From 5735e5d9a355bac901cdfcad694e7beeccc7b4d8 Mon Sep 17 00:00:00 2001 From: Kees Kluskens Date: Wed, 7 Dec 2016 21:52:17 +0100 Subject: [PATCH 2/2] Implement feedback from other loader readme's --- README.md | 72 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index e4c71a5c..3daedc3e 100644 --- a/README.md +++ b/README.md @@ -23,16 +23,11 @@ npm install --save-dev css-loader

Usage

-**require** -```js -const css = require('css!./file.css') -``` +Use the loader either via your webpack config, CLI or inline. -**webpack.config.js** -```js -const css = require('file.css') -``` +### Via webpack config (recommended) +**webpack.config.js** ```js module.exports = { module: { @@ -43,17 +38,42 @@ module.exports = { } ] } -}; +} +``` + +**In your application** +```js +import css from 'file.css'; +``` + +### CLI + +```bash +webpack --module-bind 'css=style-loader!css-loader' +``` + +**In your application** +```js +import css from 'file.css'; ``` -`@import` and `url()` are interpreted like `require()` and will be resolved by the css-loader. +### Inline + +**In your application** +```js +import css from 'style-loader!css-loader!./file.css'; +``` + +

Options

+ +`@import` and `url()` are interpreted like `import` and will be resolved by the css-loader. Good loaders for requiring your assets are the [file-loader](https://github.com/webpack/file-loader) and the [url-loader](https://github.com/webpack/url-loader) which you should specify in your config (see below). To be compatible with existing css files (if not in CSS Module mode): -* `url(image.png)` => `require("./image.png")` -* `url(~module/image.png)` => `require("module/image.png")` +* `url(image.png)` => `require('./image.png')` +* `url(~module/image.png)` => `require('module/image.png')`

Options

@@ -68,7 +88,7 @@ To be compatible with existing css files (if not in CSS Module mode): |**`camelCase`**|`false`|Export Classnames in CamelCase| |**`importLoaders`**|`0`|Number of loaders applied before CSS loader| -This webpack config can load css files, embed small png images as Data URLs and jpg images as files. +This webpack config can load CSS files, embed small png images as Data URLs and JPG images as files. **webpack.config.js** ```js @@ -107,17 +127,17 @@ rules: [ { test: /\.css$/, use: [ - "style-loader", + 'style-loader', { - loader: "css-loader", - options: { root: "." } + loader: 'css-loader', + options: { root: '.' } } ] } ] ``` -* `url(/image.png)` => `require("./image.png")` +* `url(/image.png)` => `require('./image.png')` Using 'Root-relative' urls is not recommended. You should only use it for legacy CSS files. @@ -151,8 +171,8 @@ The loader replaces local selectors with unique identifiers. The choosen unique ``` js exports.locals = { - className: "_23_aKvs-b8bW2Vg3fwHozO", - subClass: "_13LGdX8RMStbBE9w-t0gZ1" + className: '_23_aKvs-b8bW2Vg3fwHozO', + subClass: '_13LGdX8RMStbBE9w-t0gZ1' } ``` @@ -231,8 +251,8 @@ This doesn't result in any change to the CSS itself but exports multiple class n ```js exports.locals = { - className: "_23_aKvs-b8bW2Vg3fwHozO", - subClass: "_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO" + className: '_23_aKvs-b8bW2Vg3fwHozO', + subClass: '_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO' } ``` @@ -253,14 +273,14 @@ To import a local class name from another module: ``` css :local(.continueButton) { - composes: button from "library/button.css"; + composes: button from 'library/button.css'; background: red; } ``` ``` css :local(.nameEdit) { - composes: edit highlight from "./edit.css"; + composes: edit highlight from './edit.css'; background: red; } ``` @@ -269,8 +289,8 @@ To import from multiple modules use multiple `composes:` rules. ``` css :local(.className) { - composes: edit hightlight from "./edit.css"; - composes: button from "module/button.css"; + composes: edit hightlight from './edit.css'; + composes: button from 'module/button.css'; composes: classFromThisModule; background: red; } @@ -370,7 +390,7 @@ By default, the exported JSON keys mirror the class names. If you want to cameli ``` ```js -const className = require('file.css').className +import { className } from 'file.css'; ```

Maintainer