Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions JavaScript/AppModulesConventions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Wikia App JavaScript modules conventions

This styleguide defines the JavaScript modules strucutre conventions for Wikia App (https://github.com/Wikia/app).

## TOC

* [Files/directories structure](#filesdirectories-structure)
* [Entry point files](#entry-point-files)
* [Modules](#modules)
* [Tracking](#tracking)


## Files/directories structure

### Keep all JS in scripts folder

Keep all JavaScript files in `scripts` folder which should be located in extension's root directory (`extensions/wikia/MyExtensions/scripts`)

### Entry-point files

All entry points file-names should start with `index.`. If there is only one entry point file it should be named `index.js`

```javascript
// bad
foo.js
entry.js
start.js
moduleName.js

// good
index.js

// good only if there are more than one entry point files
index.example.js
index.foo.js
```

## Entry point files

### Keep them small

Keep entry point files as small as possible. They should usually just execute some initialization functions from other files.
```
TODO: what if we want to have just one js file in extension?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would be the case when we 'want' such solution?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no need to create multiple files if we want to for example add just one button click handler.

```

## Modules

### Name should start with extension name

Each defined Modil module name should start with extension name prefix.

```javascript
// bad
define('tracking', ['dependency'], function (dependency) {});

// good
define('MyExtensionName.tracking', ['dependency'], function (dependency) {});
```

Read more here: https://github.com/Wikia/guidelines/blob/master/JavaScript/CodingConventions.md#amd-modules

### Define module as class

JS module should be define as class. Currently we use ES5 so should define them using `function` keyword.

```javascript
// good
define('MyExtensionName.tracking', ['dependency'], function (dependency) {
function Tracking () {
// constructor
}

Tracking.prototype.myInstanceMethod = function () {
// public non-static method
};

Tracking.myStaticFunction = function () {
// public static method
};

function _myPrivateFunction () {
// private function
}

return Tracking;
});
```
Copy link
Contributor

@dianafa dianafa May 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case we have static methods only do we want to have convention regarding

Tracking.myStaticFunction1 = function () {
  // public non-static method
};

Tracking.myStaticFunction2 = function () {
  // public static method
};

return Tracking;

over

myStaticFunction1 = function () {
  // public non-static method
};

myStaticFunction2 = function () {
  // public static method
};

return {
  myStaticFunction1: myStaticFunction1,
  myStaticFunction2: myStaticFunction2
};

as well?


### Private function names should start with underscore

All private function should start with underscore (`_`).

```javascript
// bad
function myPrivateFunction () {}

// good
function _myPrivateFunction () {}
```

## Tracking

Keep all tracking in separated file(s). Tracking file-names should start with `tracking.`. If there is only one file that contains tracking functionality it should be named `tracking.js`.