Giter Club home page Giter Club logo

style-inject's Introduction

style-inject

NPM version NPM downloads CircleCI donate chat

Inject style tag to document head.

Installation

npm install style-inject

Example

import styleInject from 'style-inject';
const css = `
  body {
    margin: 0;
  }
`;
styleInject(css, options);

Options

insertAt

Type: string
Possible values: top
Default: undefined

Insert style tag to specific position of head element.

License

MIT © EGOIST

style-inject's People

Contributors

egoist avatar elasim avatar t4t5 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

Watchers

 avatar  avatar  avatar

style-inject's Issues

Document undefined

Hi,

Going from 0.1.2 to 0.2.0 made verification of document variable definition disappeared.

if (typeof document === 'undefined') { return returnValue; }

Would it be possible to put it back so my module can work on server-side without throwing an exception?

Thanks

Typescript not supported

When trying to use in a TS project, getting an error:

Could not find a declaration file for module 'style-inject'. '/Users/anton/Documents/EasyKnock/git/ek-ui-components/node_modules/style-inject/dist/style-inject.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/style-inject` if it exists or add a new declaration (.d.ts) file containing `declare module 'style-inject';`ts(7016)

tsconfig:

"compilerOptions": {
    "jsx": "react",
    "lib": ["dom", "esnext"],
    "module": "esnext",
    "moduleResolution": "node",
    "outDir": "dist",
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "esModuleInterop": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "suppressImplicitAnyIndexErrors": true
  },

style-loader insert hook

We need an option to hook onto the created stylesheet element, similar to style-loader option. Correct me if I'm wrong, but right not it isn't possible and the only way is overriding this function, which is used in Rollup PostCSS plugin.

Feature Request: More control over injection

I would like to be able to pass options to style-inject that allows me to place meta tags on the <style> tags injected into the head.

For example, I could use the CSS Module name like: <style data-meta="MyModule" />.

This would allow me to add context to the style, and additionally target this specific <style> tag for updates during development (with things such as hot-reloading).

insertAt: 'top' is broken as it reverses the order of injected stylesheets

style-inject is used by rollup-plugin-postcss and many other packages. When used in these packages there is an issue with insertAt behavior.

Given the following imports

// A.js
import B from './B';

import styles from './a.css';
...
// B.js
import styles from './b.css'`
...

Without insertAt: 'top' the resulting injected styles are

<style>
// contents of b.css
</style>
<style>
// contents of a.css
</style>

With insertAt: 'top' the resulting injected styles are

<style>
// contents of a.css
</style>
<style>
// contents of b.css
</style>

This is incorrect as a's styles were imported after b, so should override the styles from b naturally by being imported second.

What this plugin really needs in an option to preserve the order of stylesheets relative to each other but still insert them at the beginning of the head

The CDN and Github source is different but same package version?

Hi I think there's a problem with the release.

  1. There's an issue with the yarnpkg CDN source that you've already fixed here in GitHub (three years ogo)
  2. The CDN and the GitHub package versions are the same, yet the source is different.
  3. Unrelated but confusing: GitHub says the latest releast is v0.2.0, yet CDN it's v0.3.0 and the package here in GitHub is v0.3.0?

Different Code - Same v0.3.0 package

yarn add --dev style-inject gets you this version
https://registry.yarnpkg.com/style-inject/-/style-inject-0.3.0.tgz#d21c477affec91811cc82355832a700d22bf8dd3

function styleInject(css, ref) {
  if ( ref === void 0 ) ref = {};
  var insertAt = ref.insertAt;

This has the issue of ref.insertAt can be an uncaught inexistant property, which I see you've fixed already in the current GitHub version.

yarn add --dev https://github.com/egoist/style-inject.git gets you also v0.3.0 but it's source is different;

export default function styleInject(css, { insertAt } = {}) {
  if (!css || typeof document === 'undefined') return

Summary

So basically yarn add --dev style-inject or yarn add --dev rollup-plugin-postcss gets the user an old version of the code which in my case failed Closure Compiler tests (I'll submit another issue about that shortly).

More

I also checked other repos and they have the same difference.
https://www.jsdelivr.com/package/npm/style-inject
https://registry.npmjs.org/style-inject/-/style-inject-0.3.0.tgz

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.