Giter Club home page Giter Club logo

parcel-plugin-inliner's Introduction

parcel-plugin-inliner

A Parcel plugin to inline CSS and JS code in your HTML file.

This plugin uses PostHTML and posthtml-inline-assets to inline your HTML files.

IMPORTANT NOTICE:

Due to a change in Parcel 2.0 API, plugins whose package name start with parcel-plugin- don't work on it. This plugin needs Parcel 1.x.

If you want inlining in Parcel 2.0, check their documentation: Inline script and style tags :: v2.parceljs.org.

Usage

Just install the plugin as a dependency using yarn or npm and build normally with Parcel. There are no configuration options.

Installation

yarn add parcel-plugin-inliner

or

npm install parcel-plugin-inliner

Caveats

This library only inlines files that are referenced directly in your entry point HTML file. It doesn't inline dependencies referenced inside JS and CSS files. It also doesn't inline files loaded asyncronously using Javascript. It doesn't affect code-splitting.

It only inlines the entry point of your app, and only works with HTML files.

It does not delete the JS and CSS files.

Example

Original HTML file:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Project</title>
    <link rel="style.css" type="stylesheet">
  </head>
  <body>
    <main></main>
    <script src="main.jsx"></script>
  </body>
</html>

Resulting HTML file:

<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>Project</title> <style> html{-webkit-text-size-adjust:100%;line-height:1.15}body{margin:0} ... .h-100{height:100%}</style></head> <body> <main></main> <script>parcelRequire=function(e,t,n,r){function o(n,r){ ... },{preact:"WHr8",tachyons:"kuxM"}]},{},["WoLR"]);</script> </body> </html>

CSS and JS sections were shortened for example.

License


MIT License

Copyright (c) 2018 Silvio Henrique Ferreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

parcel-plugin-inliner's People

Contributors

dependabot-preview[bot] avatar hudochenkov avatar shff avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

parcel-plugin-inliner's Issues

Plugin does not inline resources

Hello!

I've tried to use your plugin but unfortunately it does not inline all css and js resources. I've installed the plugin and then typed "parcel build index.html". It produced normal set of parcel files instead of one html. What's more, even the html file consisted of only html, not both html, css and js.

I use node 8.11.3, npm 6.2.0.

what about favicon.ico?

I want the favicon to turn into a data-url.
but I am getting just a .ico file next to the compiled html file

Unable to configure the posthtml-inline-assets module

Hi!

There is currently no way to add custom transformations for the posthtml-inline-assets module.
As you wrote in the readme: "There are no configuration options".
I don't know if this is a function that will come in the future, or if you didn't want to make it configurable at all.
Either way, it would be great if the module was configurable for the poshtml plugin! :)

Does not work on Windows

I am currently trying to build an web application using Windows.

I did the following things:

npm install --save parcel-plugin-inliner
$env:INLINE_ASSETS = 'true'
$env:ENV = 'development'
parcel .\web\index.html

I checked out the index.html file and its still links to the index.hash.css and index.hash.js instead of inlining the content.

My environment is:

npm -v
7.14.0

node -v
v16.2.0

parcel --version
2.0.0-beta.3.1

Unable to resolve local CSS files

I had an index.html that references an adjacent index.css file. inliner was unable to resolve the CSS file; it just couldn't find it.

I think this stems from the need for this feature:

remy/inliner#171

Since the Parcel plugin is unable to thread the correct location to resolve those relative references from on the file system through to Inliner.

For reference, the log from Inliner looks something like this:

  inliner html: 1 +0ms
  inliner inlining file +1ms
  inliner inliner.get file: /Users/alafroscia/Code/netflix/launcher-page-html/dist/index.html +1ms
  inliner processing HTML +1ms
  inliner { images: true,
  inliner   compressCSS: true,
  inliner   compressJS: true,
  inliner   collapseWhitespace: true,
  inliner   nosvg: false,
  inliner   skipAbsoluteUrls: false,
  inliner   preserveComments: false,
  inliner   iesafe: false } +0ms
  inliner loaded DOM +5ms
  inliner links: 1 +4ms
  inliner start 1 links +0ms
  inliner inliner.get file: /src.b1a1520c.css +0ms
  inliner skipping videos +0ms
  inliner ENOENT: src.b1a1520c.css, trying decodeURI +2ms
  inliner adding 0 CSS assets +2ms
  inliner images: 0 +0ms
  inliner collapsing whitespace +2ms
  inliner completed: 432 bytes +1ms

so it seems like it identifies the CSS file to resolve, but just fails to do so.

changes to assets during `parcel watch` don't propagate to inlined copies

Steps to reproduce

  1. Set up a simple index.html file referencing an index.ts file.

  2. Put something in the index.ts file that affects page display.

  3. Without parcel-plugin-inliner installed, run parcel watch --public-url . index.html.

  4. Observe the thing from index.ts that affects page display, such as:

    document.body.append('XXX');
  5. Comment out that thing in index.ts.

  6. Reload the page, and observe that the thing is no longer visible.

  7. Terminate the parcel watch process.

  8. Reinstate the thing in index.ts.

  9. With parcel-plugin-inliner installed, run parcel watch --public-url . index.html .

  10. Observe the thing from index.ts that affects page display.

  11. Comment out that thing in index.ts.

  12. Observe that the page reloads, but the thing remains visible.

Expected result

Inlined assets update as the parcel watch process rebuilds them.

Only inline certain types of resources

I like the ability to inline certain things, but often it makes little sense to inline everything, such as:

  • favicon images (let the browser download the one it needs, it's not a critical item to render the page)
  • script tags with the defer attribute

Is there a way to control the list of resources to inline ?

How to use with scss?

I tried to use this plugin for my project that uses scss, but using scss requires doing an import in index.js file as so: import './index.scss';, I cannot simply link in index.html

Is there a way to use this plugin with scss?

Cannot read property 'type' of null

I'm getting this error when trying to run it:

Cannot read property 'type' of null
    at Bundler.bundler.on (/home/ole/projects/dilbert/node_modules/parcel-plugin-inliner/index.js:6:69)
    at Bundler.emit (events.js:182:13)
    at Bundler.bundle (/home/ole/projects/dilbert/node_modules/parcel-bundler/src/Bundler.js:324:12)
    at process.internalTickCallback (internal/process/next_tick.js:77:7)

I have a pretty simple setup with one .less file and one .js file referenced in index.html. I remembered to add public-url as well.

Comments are removed, overriding .terserrc file

I would like to retain comments in my index.js file. I do this using a .terserrc file:
{ "output": { "comments": true } }

However, when using the plugin the comments are stripped from the inline file. Is there a way to preserve them?

Allow to conditionally inline ressources

This plugin is great.

I would like to utilize some more options of posthtml-inline-assets to inline only certain resources.

My use case is that I need to inline CSS for a splash screen or above the fold content only.

Proposal

  • Either allow to configure filters e.g. with resolve option
  • Or use a declarative approach at the HTML node, e.g. with a data-inline boolean attribute
    <--! will be inlined -->
    <link rel="stylesheet" href="./foo.css" data-inline />
    <script src="./foo.js" data-inline></script>
    <--! will not be inlined -->
    <link rel="stylesheet" href="./bar.css" />
    <script src="./bar.js"></script>

Alternatively if it would work the same as in Parcel V2, that would be great too.
https://parceljs.org/languages/html/#inline-script-and-style-tags

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.