Progressive Development
Egg provides both Plugin and Framework, and the former has two loading modes which are path
and package
. Then how should we choose?
Step-by-step example will be provided to demonstrate how to start coding development progressively.
Find detail codes on eggjs/examples/progressive.
# Getting Started
Assume that we are writing a code to analyze UA to implement the function below:
ctx.isAndroid
ctx.isIOS
You can easily write it down after previous tutorials, let's have a quick review:
Codes refer to step1.
Directory structure:
example-app |
Core code:
// app/extend/context.js |
# Prototype of Plugin
Obviously, the logic is universal that can be written as a plugin.
But since function might not be perfect at the beginning, it might be difficult to maintain if encapsulated into a plugin directly.
We can write the code as the format of plugin, but not separate out.
Codes refer to step2.
New directory structure:
example-app |
Core code:
-
app/extend/context.js
move tolib/plugin/egg-ua/app/extend/context.js
. -
lib/plugin/egg-ua/package.json
declares plugin.
{ |
config/plugin.js
usespath
to mount the plugin.
// config/plugin.js |
# Extract to Independent Plugin
The functions of module become better after a period of developing so we could extract it out as an independent plugin.
We extract an egg-ua plugin and have a quick review as below. Details refer to Plugin Development.
Directory structure:
egg-ua |
Codes refer to step3/egg-ua.
Then modify the application, details refer to step3/example-app.
- Remove directory
lib/plugin/egg-ua
. - Declare dependencies
egg-ua
inpackage.json
. - Change type to
package
inconfig/plugin.js
.
// config/plugin.js |
Note:We can use npm link
for local test before releasing the plugin. Details refer to npm-link.
$ cd example-app |
# Finally: A Framework
After repeating the process above, we accumulate a few plugins and configurations, and might find that most of our team projects are using them.
At that time, you can consider abstracting them as a framework which is suitable for business scenarios.
Firstly, abstract the example-framework as below. Let's have a quick review, details refer to Framework.
Directory structure:
example-framework |
- Codes refer to example-framework.
- Remove the dependencies of plugins such as
egg-ua
and remove it from example-app, then configure them into thepackage.json
andconfig/plugin.js
of the framework.
Then modify the application, details refer to step4/example-app.
- Remove
egg-ua
inconfig/plugin.js
. - Remove
egg-ua
inpackage.json
. - declare
example-framework
inpackage.json
and configure theegg.framework
.
{ |
Note:We can use npm link
for local test before releasing the framework npm-link.
$ cd example-app |
# In the End
In conclusion, we can see how to make the framework evolution step by step which benefits from Egg's powerful plugin mechanism, code co-build, reusability and modularity.
-
In general, put codes into
lib/plugin
if they can be reused in the application. -
Separate it into a
node module
when plugin becomes stable. -
Application with relatively reusable codes will work as a separate plugin.
-
Abstract it as framework to release after application become certain solutions of specified business scenario.
-
It would be a great improvement in the efficiency of teamwork after plugins were extracted, modularized and finally became a framework, because other projects could reuse codes by just using
npm install
. -
Note:Whether it's the application/plugin/framework, unittest is necessary and try to reach 100% coverage