# AbstractNpmAsset

> Provides local or CDN delivery for asset bundles backed by an npm package installed through Foxy.

[API reference](/docs/api/index.md) › [Bootstrap 6](/docs/extensions/bootstrap6/reference/index.md) › [asset](/docs/extensions/bootstrap6/reference/asset/index.md) › AbstractNpmAsset

| abstract class | `yii2\extensions\bootstrap6\asset\AbstractNpmAsset` |
| --- | --- |
| Extends | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Source | [`src/asset/AbstractNpmAsset.php`](https://github.com/yii2-extensions/bootstrap6/blob/main/src/asset/AbstractNpmAsset.php#L32) |

3 subclasses:

- [`BootstrapAsset`](/docs/extensions/bootstrap6/reference/asset/bootstrapasset/index.md)
- [`BootstrapIconAsset`](/docs/extensions/bootstrap6/reference/asset/bootstrapiconasset/index.md)
- [`BootstrapPluginAsset`](/docs/extensions/bootstrap6/reference/asset/bootstrappluginasset/index.md)

Provides local or CDN delivery for asset bundles backed by an npm package installed through Foxy.

In local mode the bundle publishes [`directory()`](/docs/extensions/bootstrap6/reference/asset/abstractnpmasset/index.md#method-directory) of the package from the `@npm` alias. In CDN mode it links
the same files from [`$cdnUrl`](/docs/extensions/bootstrap6/reference/asset/abstractnpmasset/index.md#property-cdnUrl), pinned to the installed version, so both delivery modes serve the same release.

Each bundle publishes its own sub-directory because the asset manager publishes a source directory only once: two
bundles sharing one source path with different `publishOptions['only']` filters would leave the files of the second
bundle unpublished.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-basePath"></span>[`$basePath`](/docs/api/yii/web/assetbundle/index.md#property-basePath) | `string\|null` | The Web-accessible directory that contains the asset files in this bundle. Default: `null`. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| <span id="property-baseUrl"></span>[`$baseUrl`](/docs/api/yii/web/assetbundle/index.md#property-baseUrl) | `string\|null` | The base URL for the relative asset files listed in [`$js`](/docs/api/yii/web/assetbundle/index.md#property-js) and [`$css`](/docs/api/yii/web/assetbundle/index.md#property-css). Default: `null`. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| <span id="property-cdn"></span>`$cdn` | `bool` | Whether to link the files from the CDN instead of publishing the local package. Default: `false`. | `AbstractNpmAsset` |
| <span id="property-cdnUrl"></span>`$cdnUrl` | `string` | Base URL of the CDN serving npm packages as `{cdnUrl}/{package}@{version}/{directory}/{file}`. Default: `'https://cdn.jsdelivr.net/npm'`. | `AbstractNpmAsset` |
| <span id="property-css"></span>[`$css`](/docs/api/yii/web/assetbundle/index.md#property-css) | `array<string\|array<array-key, mixed>>` | List of CSS files that this bundle contains. Default: `[]`. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| <span id="property-cssOptions"></span>[`$cssOptions`](/docs/api/yii/web/assetbundle/index.md#property-cssOptions) | `yii\web\RegisterCssFileOptions` | The options that will be passed to [`View::registerCssFile()`](/docs/api/yii/web/view/index.md#method-registerCssFile) when registering the CSS files in this bundle. Default: `[]`. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| <span id="property-depends"></span>[`$depends`](/docs/api/yii/web/assetbundle/index.md#property-depends) | `array<class-string>` | List of bundle class names that this bundle depends on. Default: `[]`. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| <span id="property-js"></span>[`$js`](/docs/api/yii/web/assetbundle/index.md#property-js) | `array<string\|array<array-key, mixed>>` | List of JavaScript files that this bundle contains. Default: `[]`. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| <span id="property-jsOptions"></span>[`$jsOptions`](/docs/api/yii/web/assetbundle/index.md#property-jsOptions) | `yii\web\RegisterJsFileOptions` | The options that will be passed to [`View::registerJsFile()`](/docs/api/yii/web/view/index.md#method-registerJsFile) when registering the JS files in this bundle. Default: `[]`. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| <span id="property-minified"></span>`$minified` | `bool\|null` | Whether to serve the minified files, or `null` to serve them outside debug mode. Default: `null`. | `AbstractNpmAsset` |
| <span id="property-publishOptions"></span>[`$publishOptions`](/docs/api/yii/web/assetbundle/index.md#property-publishOptions) | `yii\web\PublishOptions` | The options to be passed to [`AssetManager::publish()`](/docs/api/yii/web/assetmanager/index.md#method-publish) when the asset bundle is being published. Default: `[]`. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| <span id="property-sourcePath"></span>[`$sourcePath`](/docs/api/yii/web/assetbundle/index.md#property-sourcePath) | `string\|null` | The directory that contains the source asset files for this asset bundle. Default: `null`. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`dependencies()`](#method-dependencies) static | Returns the asset bundle classes this bundle depends on. | `AbstractNpmAsset` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`init()`](#method-init) | Initializes the bundle file lists for the configured delivery mode. | `AbstractNpmAsset` |
| <span id="method-publish"></span>[`publish()`](/docs/api/yii/web/assetbundle/index.md#method-publish) | Publishes the asset bundle if its source code is not under Web-accessible directory. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| <span id="method-register"></span>[`register()`](/docs/api/yii/web/assetbundle/index.md#method-register) static | Registers this asset bundle with a view. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| <span id="method-registerAssetFiles"></span>[`registerAssetFiles()`](/docs/api/yii/web/assetbundle/index.md#method-registerAssetFiles) | Registers the CSS and JS files with the given view. | [`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md) |
| [`registerWith()`](#method-registerWith) static | Registers the bundle, and its npm-backed dependencies, with the given delivery mode. | `AbstractNpmAsset` |
| [`directory()`](#method-directory) protected | Returns the package sub-directory the bundle publishes, without leading or trailing slashes. | `AbstractNpmAsset` |
| [`package()`](#method-package) protected abstract | Returns the npm package name that ships the files. | `AbstractNpmAsset` |
| [`publishPatterns()`](#method-publishPatterns) protected | Returns additional publish patterns, relative to [`directory()`](/docs/extensions/bootstrap6/reference/asset/abstractnpmasset/index.md#method-directory), for files referenced by the stylesheets. | `AbstractNpmAsset` |
| [`scripts()`](#method-scripts) protected | Returns the script paths relative to [`directory()`](/docs/extensions/bootstrap6/reference/asset/abstractnpmasset/index.md#method-directory), without the `.min` infix. | `AbstractNpmAsset` |
| [`styles()`](#method-styles) protected | Returns the stylesheet paths relative to [`directory()`](/docs/extensions/bootstrap6/reference/asset/abstractnpmasset/index.md#method-directory), without the `.min` infix. | `AbstractNpmAsset` |

## Method details

### dependencies() <span id="method-dependencies"></span>

```php
public static function dependencies(): list<class-string<yii\web\AssetBundle>>
```

Returns the asset bundle classes this bundle depends on.

**Returns**

`list<class-string<`[`yii\web\AssetBundle`](/docs/api/yii/web/assetbundle/index.md)`>>`: Dependency bundle classes.

### init() <span id="method-init"></span>

```php
public function init(): void
```

Initializes the bundle file lists for the configured delivery mode.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the installed package version cannot be resolved in CDN mode.

### registerWith() <span id="method-registerWith"></span>

```php
public static function registerWith(yii\web\View $view, bool $cdn = false): static
```

Registers the bundle, and its npm-backed dependencies, with the given delivery mode.

Bundles already configured in the asset manager, or already registered, keep their current mode.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$view` | [`yii\web\View`](/docs/api/yii/web/view/index.md) | View that renders the asset files. |
| `$cdn` | `bool` | Whether to link the files from the CDN. |

**Returns**

`static`: Registered bundle instance.

### directory() <span id="method-directory"></span>

```php
protected function directory(): string
```

Returns the package sub-directory the bundle publishes, without leading or trailing slashes.

**Returns**

`string`: Sub-directory relative to the package root, or an empty string to publish the whole package.

### package() <span id="method-package"></span>

```php
abstract protected function package(): string
```

Returns the npm package name that ships the files.

**Returns**

`string`: Npm package name.

### publishPatterns() <span id="method-publishPatterns"></span>

```php
protected function publishPatterns(): list<string>
```

Returns additional publish patterns, relative to [`directory()`](/docs/extensions/bootstrap6/reference/asset/abstractnpmasset/index.md#method-directory), for files referenced by the stylesheets.

**Returns**

`list<string>`: Patterns appended to the default `publishOptions['only']` list.

### scripts() <span id="method-scripts"></span>

```php
protected function scripts(): list<string>
```

Returns the script paths relative to [`directory()`](/docs/extensions/bootstrap6/reference/asset/abstractnpmasset/index.md#method-directory), without the `.min` infix.

**Returns**

`list<string>`: Script paths.

### styles() <span id="method-styles"></span>

```php
protected function styles(): list<string>
```

Returns the stylesheet paths relative to [`directory()`](/docs/extensions/bootstrap6/reference/asset/abstractnpmasset/index.md#method-directory), without the `.min` infix.

**Returns**

`list<string>`: Stylesheet paths.