Code Monkey home page Code Monkey logo

link-media-html-webpack-plugin's Introduction

Link Media HTML Webpack Plugin

Build Status codecov

This is an extension plugin for the webpack plugin html-webpack-plugin.

It automatically adds the media attribute to generated <link> HTML elements, inferred from the CSS filename according to the pattern media_{base64MediaString}:

style.media_KG1pbi13aWR0aDogNzAwcHgpLCBoYW5kaGVsZCBhbmQgKG9yaWVudGF0aW9uOiBsYW5kc2NhcGUp.css

will be injected into the HTML template as:

<link src="styles.media_KG1pbi13aWR0aDogNzAwcHgpLCBoYW5kaGVsZCBhbmQgKG9yaWVudGF0aW9uOiBsYW5kc2NhcGUp.css" rel="stylesheet" media="(min-width: 700px), handheld and (orientation: landscape)" />

This is useful for print stylesheets or desktop/mobile-specific styles that the browser should only load depending on the @media match.

Installation

You must be running webpack on node 4.x or higher

Install the plugin with npm:

$ npm install --save-dev link-media-html-webpack-plugin

Basic Usage

Load the plugin

const LinkMediaHtmlWebpackPlugin = require('link-media-html-webpack-plugin');

and add it to your webpack config as follows:

plugins: [
    new HtmlWebpackPlugin(),
    new LinkMediaHtmlWebpackPlugin(),
]

you'll probably want to use this in conjunction with extract-text-webpack-plugin to split up the loaded assets so the browser can do its thing. Here's a sample webpack 2.x config:

const path = require('path');
const webpack = require('webpack');
const HTMLPlugin = require('html-webpack-plugin');
const LinkMediaHTMLPlugin = require('link-media-html-webpack-plugin');

const ExtractPlugin = require('extract-text-webpack-plugin');

// a utility provided by this plugin for easily forming the requisite filename syntax
const getMediaFilename = require('link-media-html-webpack-plugin/get-media-filename');

const getFilePath = (filename) => path.join(__dirname, 'src', filename);

const mainStyleExtractor = new ExtractPlugin('style.css');
const printStyleExtractor = new ExtractPlugin(getMediaFilename(getFilePath('style.print.css')));

webpack({
    entry: getFilePath('entry.js'),
    module: {
      loaders: [
        {
            test: /print\.css$/,
            use: printStyleExtractor.extract('css-loader'),
        }, {
            test: /\.css$/,
            exclude: /print\.css$/,
            use: mainStyleExtractor.extract('css-loader'),
        },
      ],
    },
    output: {
        path: OUTPUT_DIR,
    },
    plugins: [
        mainStyleExtractor,
        printStyleExtractor,
        new HTMLPlugin({
            minify: {
                collapseWhitespace: true,
            },
        }),
        new LinkMediaHTMLPlugin(),
    ],
});

link-media-html-webpack-plugin's People

Contributors

ccorcos avatar greenkeeper[bot] avatar quantizor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

link-media-html-webpack-plugin's Issues

An in-range update of eslint is breaking the build ๐Ÿšจ

Version 4.14.0 of eslint was just published.

Branch Build failing ๐Ÿšจ
Dependency eslint
Current Version 4.13.1
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

eslint is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • โŒ continuous-integration/travis-ci/push The Travis CI build failed Details

Release Notes v4.14.0
  • be2f57e Update: support separate requires in one-var. (fixes #6175) (#9441) (่–›ๅฎš่ฐ”็š„็Œซ)
  • 370d614 Docs: Fix typos (#9751) (Jed Fox)
  • 8196c45 Chore: Reorganize CLI options and associated docs (#9758) (Kevin Partington)
  • 75c7419 Update: Logical-and is counted in complexity rule (fixes #8535) (#9754) (Kevin Partington)
  • eb4b1e0 Docs: reintroduce misspelling in valid-typeof example (#9753) (Teddy Katz)
  • ae51eb2 New: Add allowImplicit option to array-callback-return (fixes #8539) (#9344) (James C. Davis)
  • e9d5dfd Docs: improve no-extra-parens formatting (#9747) (Rich Trott)
  • 37d066c Chore: Add unit tests for overrides glob matching. (#9744) (Robert Jackson)
  • 805a94e Chore: Fix typo in CLIEngine test name (#9741) (@scriptdaemon)
  • 1c2aafd Update: Improve parser integrations (fixes #8392) (#8755) (Toru Nagashima)
  • 4ddc131 Upgrade: debug@^3.1.0 (#9731) (Kevin Partington)
  • f252c19 Docs: Make the lint message source property a little more subtle (#9735) (Jed Fox)
  • 5a5c23c Docs: fix the link to contributing page (#9727) (Victor Hom)
  • f44ce11 Docs: change beginner to good first issue label text (#9726) (Victor Hom)
  • 14baa2e Chore: improve arrow-body-style error message (refs #5498) (#9718) (Teddy Katz)
  • f819920 Docs: fix typos (#9723) (Thomas Broadley)
  • 43d4ba8 Fix: false positive on rulelines-between-class-members (fixes #9665) (#9680) (sakabar)
Commits

The new version differs by 19 commits.

  • 8d166b4 4.14.0
  • 5a29612 Build: changelog update for 4.14.0
  • be2f57e Update: support separate requires in one-var. (fixes #6175) (#9441)
  • 370d614 Docs: Fix typos (#9751)
  • 8196c45 Chore: Reorganize CLI options and associated docs (#9758)
  • 75c7419 Update: Logical-and is counted in complexity rule (fixes #8535) (#9754)
  • eb4b1e0 Docs: reintroduce misspelling in valid-typeof example (#9753)
  • ae51eb2 New: Add allowImplicit option to array-callback-return (fixes #8539) (#9344)
  • e9d5dfd Docs: improve no-extra-parens formatting (#9747)
  • 37d066c Chore: Add unit tests for overrides glob matching. (#9744)
  • 805a94e Chore: Fix typo in CLIEngine test name (#9741)
  • 1c2aafd Update: Improve parser integrations (fixes #8392) (#8755)
  • 4ddc131 Upgrade: debug@^3.1.0 (#9731)
  • f252c19 Docs: Make the lint message source property a little more subtle (#9735)
  • 5a5c23c Docs: fix the link to contributing page (#9727)

There are 19 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those donโ€™t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot ๐ŸŒด

Cannot read property 'tap' of undefined

I'm getting

TypeError: Cannot read property 'tap' of undefined
    at .../node_modules/link-media-html-webpack-plugin/index.js:44:63

My webpack plugins configuration:

plugins: [
    new LinkMediaHtmlWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: helpers.root('client/public/index.html'),
      inject: 'body',
    }),
    ...
]

webpack version: 4.34.0

Figure out why @import CSS can't be extracted

It's kind of annoying to have to require each CSS file separately in your entry JS file for this to work... it'd be a lot nicer to declare them as @import statements inside primary CSS file and have ExtractTextPlugin handle it.

An in-range update of webpack is breaking the build ๐Ÿšจ

Version 4.17.3 of webpack was just published.

Branch Build failing ๐Ÿšจ
Dependency webpack
Current Version 4.17.2
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

webpack is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • โŒ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Release Notes v4.17.3

Bugfixes

  • Fix exit code when multiple CLIs are installed
  • No longer recommend installing webpack-command, but still support it when installed
Commits

The new version differs by 7 commits.

  • ee27d36 4.17.3
  • 4430524 Merge pull request #7966 from webpack/refactor-remove-webpack-command-from-clis
  • b717aad Show only webpack-cli in the list
  • c5eab67 Merge pull request #8001 from webpack/bugfix/exit-code
  • 943aa6b Fix exit code when multiple CLIs are installed
  • 691cc94 Spelling
  • 898462d refactor: remove webpack-command from CLIs

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those donโ€™t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot ๐ŸŒด

plugin does not prevent loading the files

The README says

This is useful for print stylesheets or desktop/mobile-specific styles that the browser should only load depending on the @media match.

But the whole plugin depends on <link rel="stylesheet" href="example.css" media="example query"> to do the job โ€“ what it does not!

The media attribute only controls when the CSS gets applied. But it gets loaded always. I've created a codepen to demonstrate this https://codepen.io/KaiTheGreat/pen/VdRagb
check the network tab in the dev tools โ€“ both, foundation and bootstrap get loaded always

@probablyup have you been aware of this?

An in-range update of codecov is breaking the build ๐Ÿšจ

The devDependency codecov was updated from 3.1.0 to 3.2.0.

๐Ÿšจ View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

codecov is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • โŒ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 3 commits.

  • e427d90 feat(services): add azure pipelines (#114)
  • 023d204 Use small HTTP dependency (#110)
  • 500f308 Update Readme

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those donโ€™t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot ๐ŸŒด

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.