Polymer Build
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
-
TypeError: project.sources is not a function
cause The `PolymerProject` class was not correctly instantiated, or the variable `project` refers to the module itself rather than an instance of `PolymerProject`.fixEnsure you create an instance of `PolymerProject` using `const project = new PolymerProject(config);` before attempting to call its methods like `project.sources()`. -
ENOENT: no such file or directory, open 'polymer.json'
cause The `polymer.json` configuration file, which is often loaded via `require('./polymer.json')` or `readFileSync`, does not exist at the specified path.fixVerify the path to your `polymer.json` file. If you don't have one, pass the configuration object directly to the `PolymerProject` constructor instead of attempting to load from `polymer.json`. -
Error: No matching shell found.
cause The `shell` path configured in `PolymerProject` (or `polymer.json`) does not point to a valid HTML file intended as the application shell, or the file is not reachable within the project's source/dependency graph.fixCheck the `shell` property in your `PolymerProject` configuration. Ensure it correctly points to the main application shell HTML file and that the file exists and is accessible relative to your project root or defined `sources`.
Warnings
- deprecated The Polymer project ecosystem, including `polymer-build`, is deprecated and has been succeeded by Lit for modern web component development. No new features are expected, and maintenance is minimal.
- gotcha Polymer CLI uses `polymer-build` internally. Using `polymer-build` directly provides greater customization but means you are responsible for integrating all build steps (e.g., optimization, bundling, service worker generation) that the CLI might handle automatically.
- gotcha `PolymerProject` configuration can be loaded from `polymer.json` or passed directly as an object. Mismatching paths or missing `entrypoint`, `shell`, or `fragments` can lead to incomplete builds or errors.
Install
-
npm install polymer-build -
yarn add polymer-build -
pnpm add polymer-build
Imports
- PolymerProject
const PolymerProject = require('polymer-build').PolymerProject;import { PolymerProject } from 'polymer-build'; - HtmlSplitter
const HtmlSplitter = require('polymer-build').HtmlSplitter;import { HtmlSplitter } from 'polymer-build'; - ProjectOptions
import type { ProjectOptions } from 'polymer-build';
Quickstart
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.