Polymer Build

3.1.4 · deprecated · verified Tue Apr 21

Polymer Build (current version 3.1.4) is a Node.js library designed for building Polymer web component projects. It provides a highly customizable, stream-based pipeline for processing project source files and their dependencies, using Vinyl file objects. While the Polymer CLI utilizes `polymer-build` internally for its `build` command, this library offers greater flexibility for developers who require custom build steps, specific optimizers, or advanced stream manipulations not available through the CLI's pre-configured options. `polymer-build` enables direct interaction with build streams, allowing integration with other Node.js stream-based tools like Gulp. The Polymer ecosystem, including `polymer-build`, has largely been superseded by Lit for modern web component development, meaning this library is in a deprecated state with no new feature development expected.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `PolymerProject` instance using either a `polymer.json` file or a default configuration. It then combines the project's source and dependency streams using `merge-stream` for a basic Gulp-based build process, outputting files to a `build/default` directory. It highlights the stream-centric nature of `polymer-build` and its integration with Gulp.

import { PolymerProject } from 'polymer-build';
import gulp from 'gulp';
import mergeStream from 'merge-stream';
import { resolve } from 'path';
import { readFileSync, existsSync } from 'fs';

// This example demonstrates a basic Polymer build pipeline using Gulp.
// Ensure 'gulp', 'merge-stream', and 'polymer-build' are installed.
// For a real project, replace 'dummy-app' with your project structure.

const polymerJsonPath = resolve(process.cwd(), 'polymer.json');
let projectConfig = {};

if (existsSync(polymerJsonPath)) {
  projectConfig = JSON.parse(readFileSync(polymerJsonPath, 'utf-8'));
} else {
  // Fallback to a minimal example config if polymer.json is not found
  console.warn('polymer.json not found. Using a default example configuration.');
  projectConfig = {
    entrypoint: 'index.html',
    shell: 'src/my-app.html', // Adjust based on your app shell
    fragments: ['src/my-view1.html', 'src/my-view2.html'], // Example fragments
    sources: ['src/**/*', 'index.html', '!node_modules/**/*'], // Include all sources, exclude node_modules
    extraDependencies: ['node_modules/@webcomponents/webcomponentsjs/**/*'], // Example polyfills
  };
}

// Create a PolymerProject instance with the determined configuration
const project = new PolymerProject(projectConfig);

gulp.task('build', () => {
  console.log('Starting Polymer build...');
  // Merge project sources and dependencies into a single stream
  const mergedStreams = mergeStream(project.sources(), project.dependencies());

  return mergedStreams
    // You can add custom build steps here, e.g., linting, optimization, minification
    // .pipe(myCustomOptimizer())
    .pipe(gulp.dest('build/default')) // Output to a 'build/default' directory
    .on('end', () => console.log('Polymer build completed. Output in build/default/.'));
});

// To run this Gulp task:
// 1. Install dependencies: `npm install --save-dev gulp merge-stream polymer-build`
// 2. Create a `gulpfile.ts` (or `.js`) in your project root with this code.
// 3. Create a basic `index.html` and `src/my-app.html` (or other configured entrypoints).
// 4. Run `npx gulp build` from your terminal.

view raw JSON →