Code Monkey home page Code Monkey logo

eleventy-plugin-add-web-component-definitions's Introduction

eleventy-plugin-add-web-component-definitions

This plugin will automatically add Web Component definitions to your HTML pages

Given a page with this structure:

<html>
  <body>
    <custom-tag></custom-tag>
  </body>
</html>

The plugin will transform it, with default options, into:

<html>
  <body>
    <custom-tag></custom-tag>
    <script type="module" src="/js/components/custom-tag/custom-tag.js"></script>
  </body>
</html>

How to use

First, install it:

npm install --save-dev eleventy-plugin-add-web-component-definitions

Then, in your .eleventy.js configuration file, add:

// Together with your other imports
const addWebComponentDefinitions = require('eleventy-plugin-add-web-component-definitions')

module.exports = function(eleventyConfig) {

  // Inside your eleventy configuration function
  eleventyConfig.addPlugin(addWebComponentDefinitions)
}

Options

name type default description
path Function or String function (tag) { return `/js/components/${tag}/${tag}.js\` Path where your components are published
specifiers Object {} Input with this format {<custom-tag>: <Function or String>} to override a specific tag path, see below an example
position String beforeend Position where the script tag will be put in regards to the body element, the other options being afterbegin
verbose Boolean false It will console log each step, for debug purposes
quiet Boolean false It won't console log anything. By default, a log of each Web Component definition is log out with this format: [add-web-component-definitions] Adding definition for tag: custom-tag
singleScript Boolean false If true, only one script with import statements will be output: <script type="module">import "js/components/custom-tag.js;</script>
modulePreload Boolean false If true (and singleScript is false), it will add modulepreload links to the head of the document using the path function

Example

Say your components live in /components/ with no subfolders for tags and that your published website lives in a sub-folder of the domain (such as what happens in Github Pages or Gitlab Pages), you can do this:

eleventyConfig.addPlugin(addWebComponentDefinitions, {
  path: tag => project.environment === 'production'
      ? `/my-project/components/${tag}.js`
      : `/components/${tag}.js`
  }
)

You can also specify a unique path for any custom-tag, which overrides the path configuration:

eleventyConfig.addPlugin(addWebComponentDefinitions, {
  specifiers: {
    'custom-tag-one': tag => project.environment === 'production'
      ? `/alpha-project/components/${tag}.js`
      : `/components/${tag}.js`,
    'custom-tag-two': 'my-module'
  }
)

For a verbose output, do this:

eleventyConfig.addPlugin(addWebComponentDefinitions, { verbose: true })

Demo

Please find a demo at /demo, to see it working live just:

npm run demo

Questions? Feature requests?

Please open an issue and I'll get back to you ASAP!

eleventy-plugin-add-web-component-definitions's People

Contributors

bennypowers avatar dependabot[bot] avatar gdbaldw avatar jdvivar avatar jdvivar-eb avatar

Stargazers

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

Watchers

 avatar  avatar

eleventy-plugin-add-web-component-definitions's Issues

Deal with `permalink: false`

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch [email protected] for the project I'm working on because templates with permalink: false cause an error when running the package.

Here is the diff that solved my problem:

diff --git a/node_modules/eleventy-plugin-add-web-component-definitions/add-web-component-definitions.js b/node_modules/eleventy-plugin-add-web-component-definitions/add-web-component-definitions.js
index 2fbc2d6..da3ac31 100644
--- a/node_modules/eleventy-plugin-add-web-component-definitions/add-web-component-definitions.js
+++ b/node_modules/eleventy-plugin-add-web-component-definitions/add-web-component-definitions.js
@@ -16,6 +16,9 @@ const addChild = (body, child, position) => {
 }
 
 module.exports = function (options, content, outputPath) {
+  if (typeof outputPath !== 'string')
+    return content;
+    
   if (!!options.path && !!options.specifiers) {
     throw new Error('You may configure a path function or import specifiers, but not both')
   }

This issue body was partially generated by patch-package.

Plugin interferes with RSS feeds

Hello Daniel,

first of all, thank you so much for the plugin. It does exactly what I was looking for and it saved me a lot of work. Great work! Thank you! โ™ฅ๏ธ

Unfortunately, I'm running into a small issue. I use eleventy to also generate RSS feeds and since I enabled the plugin my RSS broke. ๐Ÿ™ˆ

I debugged a little bit and saw that the plugin changes files with the file type html or files that start with <. The 2nd condition is true for RSS feeds and that leads to malformed feed files.

https://github.com/jdvivar/eleventy-plugin-add-web-component-definitions/blob/master/add-web-component-definitions.js#L25

To solve the issue I see two options:

  1. add an ignore glob config option
  2. change the 2nd condition to content.toLowerCase().startsWith('<!doctype html') (not sure if that's a breaking change though).

Thanks so much for the hard work, and I'm also happy to hand in a PR if you prefer that.

Would you take a PR to replace JSDom with parse5?

Howdy

I put together an alternate implementation that uses parse5 and hast instead of JSDom, and is quite performant.

Rather than publish under some other name, would you be interested in taking a PR?

passing eleventyConfig to path functions

What do you think of passing in the config object?

eleventyConfig.addPlugin(addWebComponentDefinitions, {
  path: (tag, config) => config.environment === 'production'
      ? `/my-project/components/${tag}.js`
      : `/components/${tag}.js`
  }
)
eleventyConfig.addPlugin(addWebComponentDefinitions, {
  specifiers: {
    'custom-tag-one': (tag, config) => `/${config.pathPrefix}/assets/${tag}.js`,
    'custom-tag-two': 'my-module'
  }
)

Support for the `is` attribute for customized built-in elements

Web components can also be customized built-in elements using the is attribute as long as the web component specifically extends that element. The plugin adds web component definitions based on the tag name but doesn't check for built-in elements using is.

I've actually been messing around with this locally and think I have it working, but didn't want to open a pull request before discussing the feature and checking for contribution requirements.

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.