y-src-pack Resource Bundler
y-src-pack is a JavaScript utility designed to bundle various resource files (like media, configuration, or data) into a single JavaScript file, making them synchronously accessible via a generated API. It supports bundling for web applications, Node.js environments, and integration with Webpack. The current stable version is 1.0.15. Unlike traditional bundlers focused primarily on JavaScript and CSS, y-src-pack emphasizes consolidating arbitrary application assets, exposing them through a customizable JavaScript variable or Node.js module. Its primary release cadence is not explicitly stated, but the package is actively maintained with considerations for future major upgrades.
Common errors
-
Error: At least one of dir or glob must be set
cause The `dir` or `glob` option (or both) were not provided in the options object to `YSrcPack.process`.fixEnsure that the `options` object passed to `YSrcPack.process` includes at least one non-empty `dir` or `glob` property. -
ReferenceError: YSrcPack is not defined
cause The `y-src-pack` module was not correctly required or imported before use.fixAdd `const YSrcPack = require('y-src-pack');` at the top of your file to import the module. -
TypeError: YSrcPack.process is not a function
cause Attempted to call `process` on an instance of `YSrcPack` (e.g., `new YSrcPack().process(...)`) instead of directly on the `YSrcPack` class.fixCall `process` as a static method directly on the class: `YSrcPack.process({...});`.
Warnings
- breaking The instance-based `collect()` and `glob()` methods (referred to as 'with commands' in the documentation) are deprecated and will be removed in the next major version. This affects code using `new YSrcPack().collect(...)` or `new YSrcPack().glob(...)`.
- gotcha The `typescript` option is marked as 'beta'. Its behavior, stability, and API generation might change in future versions without being classified as a major breaking change.
- gotcha Incorrect or relative paths for `dir`, `glob.dir`, and `tgtFile` options can lead to unexpected behavior, such as files not being found or output being written to unintended locations.
Install
-
npm install y-src-pack -
yarn add y-src-pack -
pnpm add y-src-pack
Imports
- YSrcPack
import YSrcPack from 'y-src-pack';
const YSrcPack = require('y-src-pack'); - YSrcPack.process
new YSrcPack().process({...});const YSrcPack = require('y-src-pack'); YSrcPack.process({...}); - YSrcPack.WebpackPlugin
const YSrcPack = require('y-src-pack'); plugins: [ new YSrcPack.WebpackPlugin({...}) ];
Quickstart
const YSrcPack = require('y-src-pack');
const path = require('path');
const projectRoot = process.cwd();
const jsname = 'myBundledResources'; // Or true for JSON, falsy for Node.js module
YSrcPack.process({
// Collect files from a single directory
dir: path.resolve(projectRoot, 'src/assets'),
// Or use glob patterns for more specific file selection
glob: [
{
dir: path.resolve(projectRoot, 'src/data'),
glob: ['*.json', '**/*.txt']
},
{
dir: path.resolve(projectRoot, 'src/images'),
glob: '**/*.{png,jpg,svg}'
}
],
tgtFile: path.resolve(projectRoot, 'dist/bundled-resources.js'),
jsName: jsname
});
console.log('Bundling process initiated. Check dist/bundled-resources.js');