skip to Main Content

I need to create a "plugin" that contains a user interface which will be displayed on many different vendor websites. This is a CMS agnostic plugin. I cannot use an iframe for SEO reasons. I need to isolate the plugin’s css (and maybe js) from the rest of the website, and stop the rest of the website’s css from getting to this plugin. How can I do this?

Update:

Ok, so I’ve asked a question that’s a little too specific to my setup/tech. The question should have been: How do I isolate an html element from the rest of the document styles? This is answered here;

New Question: How do I scope Vue CSS so that it doesn’t propagate up, but propagates to child components?

E.g I have the main Vue component which includes bootstrap.scss, i need that to apply to all child components, but I don’t want it to leak into the main website. Adding scoped to style stops the leak upward, but I want it to apply to child classes as well.

3

Answers


  1. Chosen as BEST ANSWER

    Ok, I've figured it out.

    Pretty simple really, combined with this answer to prevent parent -> child inheritance. I scoped all Vue css into #app { /*styles*/ } INCLUDING the bootstrap import. E.g.

    <style type="text/scss" lang="scss">
        #app {
            @import '../node_modules/bootstrap/scss/bootstrap';
    
            // rest of vue global styles go here.
            // child components may use scoped
        }
    </style>
    

    Note: I am NOT using scoped attribute on the root vue component.


  2. I think this is what you’re looking for. In your .vue file you can add style tags to the template, then Vue will create Shadow DOM styles that only apply to your application. In the final product the styles are rendered via a data-v attribute to prevent class name conflicts.

    https://vue-loader.vuejs.org/en/features/scoped-css.html

    (Copied from my reddit answer)
    https://www.reddit.com/r/vuejs/comments/76ss46/how_to_isolate_a_vue_application_from_the_rest_of/?st=J8UMA1JQ&sh=c3ebf5b1

    Login or Signup to reply.
  3. I solved this as follows, using the postcss-plugin-namespace plugin:

    1. Add an extra wrapper in the /src/App.vue file, e.g. if your main index.html app file has <div id="myapp">, in /src/App.vue add <div id="myapp-inner"> and </div> to your <template>. This will mean the actual HTML remains the same, but the generated HTML structure (viewed in the browser console’s inspector) has this additional wrapper div just inside the main one.

    2. Using PostCSS, in /postcss.config.js, add require('postcss-plugin-namespace')('#myapp') where the parameter matches the outer div. This will mean all CSS directives get prefixed with #myapp. (You may need to change other plugins already listed there from the object key-value pairs format {'...': '', '...': ''} to the array list requires format: [require(...), require(...)] format.)

    3. If you have any existing CSS in components that are defined as top-level, you will need to adjust these to reference the inner div, e.g. #myapp {-webkit-font-smoothing: antialiased;} needs to become #myapp-inner {-webkit-font-smoothing: antialiased;} – as these will then resolve eventually to #myapp #myapp-inner {-webkit-font-smoothing: antialiased;}.

    4. Then yarn build as normal.

    This approach feels architecturally cleanest, because the postcss-plugin-namespace basically does the prefixing of the CSS at the very end of the build process.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search