2017 © Pedro Peláez
 

project yii2-app-basic-npm

How to use NPM with Yii 2 Basic Application Template

image

tanakahisateru/yii2-app-basic-npm

How to use NPM with Yii 2 Basic Application Template

  • Thursday, September 7, 2017
  • by tanakahisateru
  • Repository
  • 1 Watchers
  • 6 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 2 Open issues
  • 2 Versions
  • 25 % Grown

The README.md

How to use NPM with Yii 2

This is an example how to use NPM with Yii 2., (*1)

composer create-project --stability=dev tanakahisateru/yii2-app-basic-npm

You can install Gulp or such as seamlessly via NPM now!, (*2)

Modification points

composer.json

provide section added to skip them while Composer process. fxp-composer-asset-plugin is expected to be uninstalled., (*3)

{
    "provide": {
        "bower-asset/jquery": "*",
        "bower-asset/jquery.inputmask": "*",
        "bower-asset/bootstrap": "*",
        "bower-asset/punycode": "*",
        "bower-asset/typeahead.js": "*",
        "bower-asset/yii2-pjax": "*"
    },
    "scripts": {
        "post-install-cmd": [
            "yii\\composer\\Installer::postInstall",
            "npm install"
        ],
        "post-create-project-cmd": [
            "yii\\composer\\Installer::postCreateProject",
            "yii\\composer\\Installer::postInstall",
            "npm install"
        ]
    }
}

package.json

NPM alternatives., (*4)

{
  "dependencies": {
    "bootstrap": "^3.3.7",
    "jquery": "^2.2.4",
    "jquery.inputmask": "^3.3.4",
    "punycode": "^2.1.0",
    "typeahead.js": "^0.11.1",
    "yii2-pjax": "^2.0.6"
  },
  "devDependencies": {},
  "private": true
}

config/web.php

<?php
$config = [
    // standard paths for Bower and NPM
    'aliases' => [
        '@bower' => '@app/components',
        '@npm'   => '@app/node_modules',
    ],
    'components' => [    
        // assetManager settings added
        'assetManager' => [
            'bundles' => array_merge(
                require(__DIR__ . '/assets-default.php'),
                require(__DIR__ . '/assets-extended.php')
            ),
        ],
    ],
];

config/assets-default.php

AssetManager's bundles setting can modify AssetBundle's default properties., (*5)

<?php
return [
    'yii\web\JqueryAsset' => [
        'sourcePath' => '@npm/jquery/dist',
        'js' => [
            'jquery.js',
        ],
    ],
    'yii\bootstrap\BootstrapAsset' => [
        'sourcePath' => '@npm/bootstrap/dist',
        'css' => [
            'css/bootstrap.css',
        ],
    ],
    'yii\bootstrap\BootstrapPluginAsset' => [
        'sourcePath' => '@npm/bootstrap/dist',
        'js' => [
            'js/bootstrap.js',
        ],
    ],
    'yii\bootstrap\BootstrapThemeAsset' => [
        'sourcePath' => '@npm/bootstrap/dist',
        'css' => [
            'css/bootstrap-theme.css',
        ],
    ],
    'yii\widgets\MaskedInputAsset' => [
        'sourcePath' => '@npm/jquery.inputmask/dist',
        'js' => [
            'jquery.inputmask.bundle.js',
        ],
    ],
    'yii\validators\PunycodeAsset' => [
        'sourcePath' => '@npm/punycode/dist',
        'js' => [
            'punycode.js',
        ],
    ],
    'yii\gii\TypeAheadAsset' => [
        'sourcePath' => '@npm/typeahead.js/dist',
        'js' => [
            'typeahead.bundle.js',
        ],
    ],
    'yii\widgets\PjaxAsset' => [
        'sourcePath' => '@npm/yii2-pjax',
        'js' => [
            'jquery.pjax.js',
        ],
    ],
];

(simple form:), (*6)

<?php
return [
    'yii\web\JqueryAsset'                => ['sourcePath' => '@npm/jquery/dist'],
    'yii\bootstrap\BootstrapAsset'       => ['sourcePath' => '@npm/bootstrap/dist'],
    'yii\bootstrap\BootstrapPluginAsset' => ['sourcePath' => '@npm/bootstrap/dist'],
    'yii\bootstrap\BootstrapThemeAsset'  => ['sourcePath' => '@npm/bootstrap/dist'],
    'yii\widgets\MaskedInputAsset'       => ['sourcePath' => '@npm/jquery.inputmask/dist'],
    'yii\validators\PunycodeAsset'       => ['sourcePath' => '@npm/punycode/dist'],
    'yii\gii\TypeAheadAsset'             => ['sourcePath' => '@npm/typeahead.js/dist'],
    'yii\widgets\PjaxAsset'              => ['sourcePath' => '@npm/yii2-pjax'],
];

Known Issue

Gii and Debug modules are replaced to app\modules\* classes because they reset configured AssetManager., (*7)

HINT

To use your compiled assets instead of source files, you can create link-skipped version of asset-*.php., (*8)

<?php
return [
    'foo\bar\BarAsset' => [
        'sourcePath' => '@app/assets/dist/bar',  // built resources
        'js' => [],   // No JS
        'css' => [],  // No CSS
    ],
];

The Versions

06/09 2017

dev-master

9999999-dev

How to use NPM with Yii 2 Basic Application Template

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework yii2 project template npm basic