-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assets / packages management #8
Comments
What does "process" do? Can you show a detailed example? Putting anonymous functions in configuration array is not a very good approach because anonymous functions cannot be serialized and thus the whole configuration array cannot be serialized and cached. |
Process takes the content of the source file, converts it and gives it back. It can be minification or preprocessing (LESS, TypeScript). Since we can't predict what processing is required we should provide a way customizing it. Another variant I have in mind is to introduce IAssetProcessor interface and implement basic stuff, like CSS and JavaScript minification, out of the box. In config files there will just an array of such class aliases. |
Most likely minification/preprocessing requires some external tools/commands, and writing such anonymous functions means redundant code and unnecessary dependency. Some predefined processor classes seem fine to me. But then we need to decide: what are these processors? should they be applied by default on certain script files? If not, what will happen? |
The most useful ones are:
Also it would be handy to have:
I don't think any of these should be applied by default. If no processing is applied file contents are used as is. |
I think compressing CSS/JS should be done outside rather than via processor. |
Well, this "outside" thing should be configurable as well and I thought about using the same package config for it. What's bad in handling compression in the same way btw.? I see no difference in translating LESS into CSS or translating CSS into compressed CSS. |
After compressing, you still need combining to make it truly meaningful. |
For this purpose there will be array(
'*.ts' => array(
'class' => '\yii\asset\processors\TypeScript',
'enabled' => !YII_DEBUG,
),
), By default To do the same for combining scripts we can introduce |
These are global settings, a different story. What about settings per package that we are talking about? Do you still think compressing should be a processor? IMO, the definition of processor is to turn non-standard scripts into standard ones (js/css). Then we rely on external tools (depend on some global settings) to combine/compress them. |
Yes, it should be a processor. There are lots of ways you can compress CSS and JavaScript. Some give better compression rates, some are safer, some are with options. Processor is just something that converts input into something different. I think compressor fits there very well. I don't think we should write processor routine itself but use existing ones so the implementation of |
btw., processing isn't just CSS and JavaScript. We will not provide it out of the box but this processing can probably be applied to image resources (i.e. stripping down metadata, recompressing PNGs etc.) |
I thought more about the above proposal. One thing I'm still not clear is how we will call the processors: should it be done on-the-fly or offline? Also, the proposal above mainly applies to native application code. For extensions, it has two crucial problems:
Since script and asset management is mainly meaningful for extension, we need to introduce more rules to resolve the above problems. Here is I'm thinking about:
|
I think there are good arguments for both on-the-fly processing and offline processing of assets. During development, it's super comfortable to hack on your LESS/SCSS files or CoffeeScript sources, reload the browser and see the changes. On my live server, I definitely don't want that, though. Especially not, if the processing includes any exec calls. They might even be disabled. So I wouldn't restrict the use of processors to either one or the other approach. |
Why? What if this some complex extension?
Why separate file? |
The reason is not to instantiate all the extensions while building combined script / css.
Any examples? |
Probably modules. If you think of them as "embedded applications" like say a wiki, a forum or an image gallery, then I guess they might very well grow large enough to justify several packages. Basically, if any application is considered to use several packages, then extensions should have the same freedom. And what's wrong if they define packages like:
And I wouldn't consider those package names "internal". If an extension is released, it should be documented what packages it defines and what purpose they serve. They're part of the extension's API. I'm not sure about the thing with package configurations in a separate file. After all, the configuration is data, and where that data comes from shouldn't matter. Could also come from app config or user input. If we're only talking about defaults, then I think a separate file is good. Oh, and you don't plan to implement some fancy async lazy on demand loading of scripts, do you? |
@bwoester modules are a special case where no such problem exists since we know for sure which modules are enabled. How files are combined will be configurable. |
Extension with 5 widgets for example. So it needs 5 packages. This is not compatible with
|
You can implement it as a set of extensions. Each with one widget. |
Why the restriction at all? What's the problem with extensions defining multiple packages? |
If it will never be used and there's a better way we prefer not to allow doing it. |
It would be nice, if you could setup a small glossary about the terminology which is used for Yii2. Some words I'd like to see clarified: application, assets, configuration, installation, extension, namespace, alias, package, module, |
We will use the name asset bundle instead of package. Other than this, there're very few new terms introduced in Yii 2 as we want to make the transition from Yii 1 to Yii 2 as smooth as possible. |
@schmunk42 https://github.com/yiisoft/yii2/wiki/Glossary feel free to add new terms and edit / request descriptions. |
Finally I have done the design and implementation of asset and script management, except the implementation of concrete asset processor classes. I'd like to share with you some of the designs so that you may have better idea about the new extension strategy. First, we will embrace Composer as a way to install extensions in Yii 2. By default, all extensions will be installed under the "@app/vendor" directory. Here "@app" stands for the application directory. Each extension has a unique name, in the format of "CompanyName/ProductName", following the convention recommended by Composer. We will introduce the new concept called asset bundle, which is similar to the package concept in 1.1. Each asset bundle is represented in terms of an
For application code, asset bundles are declared by To use a bundle, simply call Conversion from special types of asset files (e.g. LESS, Sass, TypeScript) is supported via the processor design that Alex proposed. In particular, Script combining and compression is done via an offline command that will be included in the core release. The command will do the actual job and generate a new list of bundle declarations. By setting |
Done. |
btw: 💯 👍 for the stuff I see so far |
Publishing a code snippet
Packages
There should be no way to publish a file without defining a package for it in order
to support combining and processing assets. All the core classes, extensions and
modules should use packages.
Package definition
Asset mappings
The idea of mappings is to be able to map one or more source files into same of less
number of destination files. Typically we have a number of source assets such as
a.js
,b.js
,c.js
,d.js
. We need to be able to map:*.js
toall.js
.a.js
andb.js
toab.js
whilec.js
andd.js
tocd.js
.If all these from point 2 are separate packages, then if
a
is requiredab.js
will be included in the page source.
By default each asset maps to the same named file.
Processing
Adding package on the fly
Adding package to the page
When doing it:
process file.
file from 'map' or the the same named file if not defined.
Package processing
Common tasks needed for resources:
internal or external. Should be customizable per resource. Should support wildcards.
writing.
(i.e. define a place on the page where it should be written).
Extensions
If extension registers any resources it should be specified in application's
extensions
section of the config. Each record will point to extension's config.The whole process should be described in the extension's
readme.md
. Will be automatedin future in case of installing via Composer.
Modules
We have a list of currently enabled modules in the application config (
modules
)so we can get package definitions from module config file located at
moduleName/config/packages.php
.The text was updated successfully, but these errors were encountered: