定制 goldenplanetdk/symfony-webpack 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

goldenplanetdk/symfony-webpack

Composer 安装命令:

composer require goldenplanetdk/symfony-webpack

包简介

Bundle to Integrate Webpack to Symfony

README 文档

README

Build Status Coverage Status


Installation

Usage

Run

Documentation

Installation

composer require goldenplanetdk/symfony-webpack

Add to AppKernel:

new GoldenPlanet\WebpackBundle\GoldenPlanetWebpackBundle(),

Generate webpack.symfony.config.js and install dependencies:

app/console webpack:setup

Usage

Twig function and tag

You can choose between webpack_asset function and webpack tag

webpack_asset function

webpack_asset(resource, type = null)

type is js or css, leave null to guess the type. For css this function could return null if no CSS would be extracted from provided entry point. If you are sure that there will be some CSS, you could just ignore this. Otherwise, you could use webpack tag as it handles this for you (omits the <link/> tag entirely in that case).

webpack tag

{% webpack [js|css] [named] [group=...] resource [resource, ...] %}
    Content that will be repeated for each compiled resource.
    {{ asset_url }} - inside this block this variable holds generated URL for current resource
{% end_webpack %}

As with webpack_asset function, provide js, css or leave it out to guess the type.

See usage with named and group in Commons chunk section.

Keep in mind that you must provide hard-coded asset paths in both tag and function. This is to find all available assets in compile-time.

Scripts and Stylesheets

Single entry point (.js, .ts, .coffee etc.) in twig templates:

<link rel="stylesheet" href="{{ webpack_asset('@acmeHello/script.js', 'css') }}">
<script defer src="{{ webpack_asset('@acmeHello/script.js') }}"></script>

Note: here @acmeHello is equal to @AcmeHelloBundle/Resources/assets

Multiple entry points:

{% webpack js
    '@acmeHello/main.js'
    '@acmeHello/another-entry-point.js'
%}
    <script defer src="{{ asset_url }}"><script>
{% end_webpack %}
{% webpack css
    '@acmeHello/main.js'
    '@acmeHello/another-entry-point.js'
%}
    <link rel="stylesheet" href="{{ asset_url }}"><script>
{% end_webpack %}

To avoid having a link element with an empty href in the DOM when the script may possibly not emit a stylesheet, test the value returned from webpack_asset before inserting the link element:

{% set cssUrl = webpack_asset('@acmeHello/script.js', 'css') %}
{% if cssUrl %}
    <link rel="stylesheet" href="{{ cssUrl }}">
{% endif %}

Commons chunk

This bundle supports both single and several commons chunks, but you have to configure this explicitly.

In your webpack.config.js:

config.plugins.push(
    new webpack.optimize.CommonsChunkPlugin({
        name: 'commons'
    })
);

In your base template:

{% webpack named css 'commons' %}
    <link rel="stylesheet" href="{{ asset_url }}"/>
{% end_webpack %}
{# ... #}
{% webpack named js 'commons' %}
    <script src="{{ asset_url }}"></script>
{% end_webpack %}

You can also use webpack_named_asset twig function instead of webpack tags.

Named commons chunk

In webpack configuration it is allowed to put commonly used libraries (shared dependencies) in a separate file, while still having reference to the same singleton library when using require. For example, to put jquery and lodash to a separate file (a commons chunk) add following to your webpack.symfony.config.js:

module.exports = function makeWebpackConfig(symfonyOptions) {

	config.entry = symfonyOptions.entry;
	config.entry['jquery-and-lodash'] = ['jquery', 'lodash'];
	
	// ...
		
	config.plugins.push(
		new webpack.optimize.CommonsChunkPlugin({
			names: [
				'jquery-and-lodash', // match entry point name(s)
			],
		}),		
	)
}

Then add the script that will load the common libs before any other script that may depend on it. Use the webpack_named_asset function to inject the actual compiled asset path:

<script defer src="{{ webpack_named_asset('jquery-and-lodash') }}"><script>

Commons chunk may contain other type of assets:

<link rel="stylesheet" href="{{ webpack_named_asset('shared', 'css') }}">

The rendered output of above in production mode will be something like:

<script src="/compiled/jquery-and-lodash.64ff80bf.c95f999344d5b2777843.js"></script>
<link rel="stylesheet" href="/compiled/shared.0a8efeb2b0832928e773.css">

Webpack can also be configured to determine the commonly used libraries in multiple entry points automatically. Support for these is planned.

Other resource types

You can pass any kind of resources to webpack with webpack_asset function for single entry point:

<img src="{{ webpack_asset('@AcmeHelloBundle/Resources/public/images/background.jpg') }}">

Or with webpack tag for multiple entry points:

<ul class="nav nav-pills social-icons">
	{% webpack
		'@AcmeHelloBundle/Resources/public/images/facebook.jpg'
		'@AcmeHelloBundle/Resources/public/images/twitter.jpg'
		'@AcmeHelloBundle/Resources/public/images/youtube.jpg'
	%}
		<li>
			<img src="{{ asset_url }}">
		</li>
	{% end_webpack %}
</ul>

Requiring within scripts and stylesheets

Inside script.js:

import URI from 'urijs';
import {Person} from './models/person';

require('./other-script.ts');

Inside stylesheet.css, less, sass or stylus:

body {
    background: url('~@AcmeBundle/Resources/images/bg.jpg');
}

Compile

Using Symfony app/console to run webpack commands

Compile for dev environment:

app/console webpack:compile

Watch for changes and compile

app/console webpack:watch

Watch for changes, compile and automatically reload browser tab(s)

app/console webpack:dev-server

Compile as part of deployment in production environment:

app/console webpack:compile --env=prod

Documentation

Full documentation is available at Wiki pages of this repository

goldenplanetdk/symfony-webpack 适用场景与选型建议

goldenplanetdk/symfony-webpack 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 48.02k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「assets」 「assetic」 「amd」 「commonjs」 「webpack」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 goldenplanetdk/symfony-webpack 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 goldenplanetdk/symfony-webpack 我们能提供哪些服务?
定制开发 / 二次开发

基于 goldenplanetdk/symfony-webpack 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 48.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 12
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-23