Component Build
component-build is a JavaScript package from the "component" ecosystem designed to bundle client-side web assets (scripts, styles, files) for browser deployment. Released around 2014, its stable version is 1.2.2. It acts as a wrapper around `component-builder2` and integrates with `component-resolver` to manage dependencies. Key features include transpilation of ES6 modules in source code, JSON parsing, template handling (as strings), automatic CSS autoprefixing, and URL rewriting for styles. For JavaScript, it supports optional UMD wrapping and autorequires the bundle entry point. The package primarily uses a callback-based API for its build processes. While it provided a structured way to build client-side applications within the 'component' framework, the 'component' package manager and its associated tools are largely obsolete, making `component-build` suitable mainly for maintaining legacy projects built within that specific ecosystem rather than new development. Its release cadence is effectively abandoned.
Common errors
-
TypeError: Build is not a function
cause Attempting to import `Build` using ES module syntax (e.g., `import Build from 'component-build';`).fixUse CommonJS `require` syntax: `const Build = require('component-build');` -
Error: Cannot find module 'component-resolver'
cause The `component-resolver` package, a peer dependency, is not installed or cannot be resolved by the `component` tooling.fixEnsure `component-resolver` is correctly installed as a dependency within your project's `component.json` or equivalent setup, as per the `component` ecosystem's requirements. -
ReferenceError: require is not defined
cause Attempting to execute `component-build` or a script using `require()` in an ES module context (e.g., a file with `"type": "module"` in `package.json`) or a browser environment without a CommonJS loader.fixEnsure your build script that invokes `component-build` is run in a Node.js CommonJS environment. This package is designed to be run server-side to build browser assets. -
[CSS] autoprefixer: No browsers config specified
cause The `autoprefixer` plugin (used for styles) requires a `browsers` option to specify target browser versions, which was not provided in the `options` object passed to `Build`.fixAdd a `browsers` array or string to the `options` object, for example: `const options = { browsers: ['last 2 versions', '> 1%'] };`
Warnings
- breaking The `component` package manager ecosystem, which `component-build` is a part of, is largely abandoned and incompatible with modern `npm`, `yarn`, or `pnpm` workflows. This package is primarily for maintaining legacy projects.
- gotcha This package is CommonJS-only and relies on `require()` for module imports. Attempting to use ES module `import` syntax will result in errors.
- gotcha All asynchronous operations within `component-build` utilize an error-first callback pattern. There is no built-in Promise-based or `async/await` compatible API.
- gotcha The internal dependencies, such as `component-builder2` and `component-resolver`, are also likely unmaintained, potentially leading to security vulnerabilities or compatibility issues with newer Node.js versions or underlying build tools.
- gotcha While the README mentions "ES6 Module support," this refers to the ability to transpile ES6 modules found *within* your source code, not that the `component-build` package itself is distributed as an ES module or fully supports the modern ES module loading specification.
Install
-
npm install component-build -
yarn add component-build -
pnpm add component-build
Imports
- Build
import Build from 'component-build';
const Build = require('component-build'); - build.scripts
build.scripts(function (err, string) { /* ... */ }); - Build.scriptPlugins
Build.scriptPlugins = function (build, options) { /* ... */ };
Quickstart
const resolve = require('component-resolver');
const Build = require('component-build');
const fs = require('fs');
const write = fs.writeFileSync;
const options = {
development: true,
install: true,
// Example for autoprefixer, if needed
browsers: ['last 2 versions', '> 1% in US'],
// Example for UMD wrap
umd: 'myBundleName'
};
resolve(process.cwd(), options, function (err, tree) {
if (err) {
console.error('Resolution Error:', err);
throw err;
}
const build = Build(tree, options);
build.scripts(function (err, string) {
if (err) {
console.error('Scripts Build Error:', err);
throw err;
}
if (string) {
console.log('Writing build.js');
write('build.js', string);
} else {
console.log('No scripts to build.');
}
});
build.styles(function (err, string) {
if (err) {
console.error('Styles Build Error:', err);
throw err;
}
if (string) {
console.log('Writing build.css');
write('build.css', string);
} else {
console.log('No styles to build.');
}
});
build.files(function (err) {
if (err) {
console.error('Files Build Error:', err);
throw err;
}
console.log('Files copied successfully.');
});
});