0.3.8 • Published 28 days ago

@localnerve/web-component-build v0.3.8

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
28 days ago

Web Component Build

Assembles a web component from css, html, and js parts, assists user build flexibility

npm version Verify Coverage Status

Assembles a web component from its parts, allows developers to author the component's parts in separate files.
The parts are processed and written to an output directory, then exposed to a calling build process.

Why This Exists

  1. Author web components in separate JS, CSS, and HTML files
  2. Expose CSS for the web component to builds for computing CSP hashes
  3. Expose HTML for the web component to builds for companion templates and/or DSD for SSR builds
  4. Enable/ease paying these conveniences forward in web component distribution packages

Processing Map

The following is a table of some of the possible input, processing, and output combos. See options for detailed explanations.

inputprocessingoutput
javascriptminify javascriptjavascript
cssminify csscss
htmlminify htmlhtml
css, htmlminify css, prepend style tag to html, minify htmlcss, html
css, html, cssHrefminfy css, prepend style tag to html, prepend link tag to html, minify htmlcss, html
cssHref, htmlprepend link tag to html, minify htmlhtml
javascript, cssminify css, merge style tag into javascript, minify javascriptcss, javascript
javascript, css, htmlminify css, prepend style tag to html, minify html, merge into javascript, minify javascriptcss, html, javascript
javascript, css, html, cssHrefminify css, prepend style tag to html, prepend link tag to html, minify html, merge into javascript, minify javascriptcss, html, javascript
javascript, htmlminify html, merge into javascript, minify javascripthtml, javascript
javascript, html, cssHrefprepend link tag to html, minify html, merge into javascript, minify javascripthtml, javascript
javascript, cssHrefadd link tag to javascript, minify javascriptjavascript

By default, html minification minifies any css found therein.

Usage

  // Sample usage, all options specified
  import {build} from '@localnerve/web-component-build';
  const outputDir = 'some/path/output';

  const result = await build(outputDir, {
    cssPath: '/some/path/file.css',
    cssLinkHref: '//some/path/file.css',
    jsPath: '/some/path/file.js',
    htmlPath: '/some/path/file.html',
    jsReplacement: '__REPLACEMENT_IN_JS__',
    terserOptions: { /* terser options */ },
    htmlminOptions: { /* html-minifier options */ },
    cleancssOptions: { /* clean-css options */ },
    minifySkip: false
  });
  // html, js, and css written to `outputDir`
  
  // Retrieve processed content
  const [js, css, html] = await Promise.all([
    result.getJs(), result.getCss(), result.getHtml()
  ]);

  // Retrieve output paths
  const [jsPath, cssPath, htmlPath] = [result.jsPath, result.cssPath, result.htmlPath];

API

This library exports a single function that takes an output directory and processing options, returns a result object.

build (outputDir, options): Result

outputDir {String}, required

Full path to the output directory where css, html, and javascript output are written.

Options {Object}, optional*

* Not really. One or more of cssPath, jsPath, and/or htmlPath must be supplied. They have no default, so if no options are supplied, this library throws an exception.

  • cssPath {String} - Full path to the input css file
    If supplied:

    • css will be minified using cleancssOptions
    • css will be wrapped in a style tag
    • css will be inserted into the javascript file if jsReplacement and jsPath are supplied and no htmlPath supplied
    • css will be prepended to the html file if htmlPath is supplied
  • cssLinkHref {String} - link href to a stylesheet resource to be referenced by the web component
    If supplied:

    • href will be wrapped in a link tag
    • resulting link will be prepended to the html file if htmlPath supplied
    • resulting link will be inserted into the javascript file if no htmlPath supplied and jsReplacement and jsPath supplied
  • htmlPath {String} - Full path to the input html file
    If supplied:

    • css will be prepended in a style tag
    • cssLinkHref will be prepended in a link tag
    • html will be inserted into the javascript file if jsReplacement and jsPath is supplied
  • jsPath {String} - Full path to the input javascript file

  • jsReplacement {String|RegExp} - The replacement pattern for the css or html in the javascript file. See pattern for full documentation
    If supplied:

    • A replacement will be attempted in the javascript file, jsPath must also be supplied
    • If not supplied or falsy, No replacement will be attempted and all assets are just copied to outputDir
  • terserOptions {Object} - The javascript minifier options object
    Defaults:

    {
      ecma: 2022
    }
  • htmlminOptions {Object} - The html minifier options object
    Defaults:

    {
      minifyJS: true,
      minifyCSS: true,
      collapseWhitespace: true,
      removeAttributeQuotes: true,
      removeComments: true
    }
  • cleancssOptions {Object} - The css minifier options object
    Defaults (same as clean-css defaults)

  • minifySkip {Boolean} - True to skip all minifications, defaults to false

Result {Object}

The output of the build process. Allows access to the output paths and full output content. Format:

  • cssPath {String}, The full path to the output css

  • htmlPath {String}, The full path to the output html

  • jsPath {String}, The full path to the output javascript

  • getCss {asyncFunction}, gets the output css

  • getHtml {asyncFunction}, gets the output html

  • getJs {asyncFunction}, gets the output javascript

License

0.3.8

28 days ago

0.3.7

2 months ago

0.3.6

2 months ago

0.3.5

3 months ago

0.3.4

4 months ago

0.3.3

5 months ago

0.3.2

5 months ago

0.3.1

5 months ago

0.3.0

5 months ago

0.2.4

5 months ago

0.2.3

5 months ago

0.2.2

5 months ago