Yii2 使用 npm 安装的包

转载自: yii2.0.15 使用 npm 替换 bower,加速 composer 安装速度 [ 2.0 版本 ]

  1. 修改 ommon/config/main.php
1
2
3
4
5
6
7
<?php
return [
'aliases' => [
'@bower' => dirname(dirname(__DIR__)) . '/node_modules',
'@npm' => dirname(dirname(__DIR__)) . '/node_modules',
],
];

这样类似 BootstrapPluginAsset 的 public $sourcePath = ‘@bower/bootstrap/dist’; 就会正确定位到 path/to/project/node_modules。

新增、修改 package.json,将 yii2 的 composer 依赖 bower-asset/* 转换为对应的 npm 包:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"private": true,
"dependencies": {
"jquery": "^2.2.4",
"bootstrap": "3.3.7",
"inputmask": "^3.3.11",
"jquery-treegrid": "^0.3.0",
"jquery-ui": "^1.12.1",
"punycode": "^2.1.0",
"typeahead.js": "^0.11.1",
"yii2-pjax": "^2.0.7"
},
"devDependencies": {},
"license": "BSD-3-Clause"
}

再修改 composer.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"provide": {
"bower-asset/jquery": "*",
"bower-asset/bootstrap": "*",
"bower-asset/inputmask": "*",
"bower-asset/punycode": "*",
"bower-asset/typeahead.js": "*",
"bower-asset/yii2-pjax": "*"
},
"scripts": {
"post-install-cmd": [
"yii\\composer\\Installer::postInstall",
"yarn install"
],
"post-create-project-cmd": [
"yii\\composer\\Installer::postCreateProject",
"yii\\composer\\Installer::postInstall",
"yarn install"
]
}

删除项目根目录下的 vendor 和 node_modules 文件夹后,将 composer 和 npm 都设置为使用国内的镜像源,执行:

1
2
rm composer.lock # remove composer.lock if exist
composer install

composer.json

1
2
3
4
5
6
7
8
9
"config": {
"process-timeout": 1800,
"fxp-asset":{
"installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
},

删除composer全局安装的包

How to remove globally a package from Composer?

1
composer global remove <packagename>

如:

1
composer global remove fxp/composer-asset-plugin