Skip to content

Commit ace7170

Browse files
committed
Add Yarn
1 parent a19b1ae commit ace7170

File tree

31 files changed

+116
-197
lines changed

31 files changed

+116
-197
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# JavaScript Stack from Scratch
22

3-
![JS](/img/js.png)
3+
![JS](/img/yarn.png)
44
![React](/img/react.png)
55
![Gulp](/img/gulp.png)
66
![Redux](/img/redux.png)
@@ -20,7 +20,7 @@ Since the goal of this tutorial is to assemble various tools, I do not go into d
2020

2121
A big chunk of the stack described in this tutorial uses React. A lot of React tutorials completely skip the setup part, which makes newcomers build their learning on weak foundations. Instead of giving beginners a "black box" configuration, the approach I'm taking here is to set up the foundations in the simplest possible way, for a thorough understanding.
2222

23-
Code examples are available for each section, and you can run them all with `npm install && npm start`. I recommend writing everything from scratch yourself by following the **step-by-step instructions** of each chapter.
23+
Code examples are available for each chapter, and you can run them all with `yarn && yarn start` or `npm install && npm start`. I recommend writing everything from scratch yourself by following the **step-by-step instructions** of each chapter.
2424

2525
**Every chapter contains the code of previous chapters**, so if you are simply looking for a boilerplate project containing everything, just clone the last chapter and you're good to go.
2626

@@ -30,9 +30,9 @@ The code of this tutorial works on Linux, macOS, and Windows.
3030

3131
## Table of contents
3232

33-
[1 - Node, NPM, and package.json](/tutorial/1-npm-and-package-json)
33+
[1 - Node, NPM, Yarn, and package.json](/tutorial/1-node-npm-yarn-package-json)
3434

35-
[2 - Installing and using an NPM package](/tutorial/2-packages)
35+
[2 - Installing and using a package](/tutorial/2-packages)
3636

3737
[3 - Setting up ES6 with Babel and Gulp](/tutorial/3-es6-babel-gulp)
3838

@@ -58,4 +58,4 @@ Coming up: React Router, Server-Side Rendering, Styling, Enzyme, Yarn, Minificat
5858

5959
Created by [@verekia](https://twitter.com/verekia)[verekia.com](http://verekia.com/).
6060

61-
License: ISC
61+
License: MIT

img/yarn.png

2.13 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"description": "JavaScript Stack from Scratch - Step-by-step tutorial to build a modern JavaScript stack",
55
"scripts": {
6-
"test": "cd tutorial/1-npm-and-package-json && yarn && yarn run tutorial-test && cd ../2-packages && yarn && yarn run tutorial-test && cd ../3-es6-babel-gulp && yarn && yarn run tutorial-test && cd ../4-es6-syntax-class && yarn && yarn run tutorial-test && cd ../5-es6-modules-syntax && yarn && yarn run tutorial-test && cd ../6-eslint && yarn && yarn run tutorial-test && cd ../7-client-webpack && yarn && yarn run tutorial-test && cd ../8-react && yarn && yarn run tutorial-test && cd ../9-redux && yarn && yarn run tutorial-test && cd ../10-immutable-redux-improvements && yarn && yarn run tutorial-test && cd ../11-testing-mocha-chai-sinon && yarn && yarn run tutorial-test && cd ../12-flow && yarn && yarn run tutorial-test"
6+
"test": "cd tutorial/1-node-npm-yarn-package-json && yarn && yarn run tutorial-test && cd ../2-packages && yarn && yarn run tutorial-test && cd ../3-es6-babel-gulp && yarn && yarn run tutorial-test && cd ../4-es6-syntax-class && yarn && yarn run tutorial-test && cd ../5-es6-modules-syntax && yarn && yarn run tutorial-test && cd ../6-eslint && yarn && yarn run tutorial-test && cd ../7-client-webpack && yarn && yarn run tutorial-test && cd ../8-react && yarn && yarn run tutorial-test && cd ../9-redux && yarn && yarn run tutorial-test && cd ../10-immutable-redux-improvements && yarn && yarn run tutorial-test && cd ../11-testing-mocha-chai-sinon && yarn && yarn run tutorial-test && cd ../12-flow && yarn && yarn run tutorial-test"
77
},
88
"devDependencies": {
99
"yarn": "^0.16.1"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 1 - Node, NPM, Yarn, and package.json
2+
3+
In this section we will set up Node, NPM, Yarn, and a basic `package.json` file.
4+
5+
First, we need to install Node, which is not only used for back-end JavaScript, but all the tools we need to build a modern Front-End stack.
6+
7+
Head to the [download page](https://nodejs.org/en/download/current/) for macOS or Windows binaries, or the [package manager installations page](https://nodejs.org/en/download/package-manager/) for Linux distributions.
8+
9+
For instance, on **Ubuntu / Debian**, you would run the following commands to install Node:
10+
11+
```bash
12+
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
13+
sudo apt-get install -y nodejs
14+
```
15+
You want any version of Node > 6.5.0.
16+
17+
`npm`, the default package manager for Node, comes automatically with Node, so you don't have to install it yourself.
18+
19+
**Note**: If Node is already installed, install `nvm` ([Node Version Manager](https://github.com/creationix/nvm)), make `nvm` install and use the latest version of Node for you.
20+
21+
[Yarn](https://yarnpkg.com/) is another package manager which is must faster than NPM, caches packages offline, and fetches dependencies more predictably. Since it [came out](https://code.facebook.com/posts/1840075619545360) in October 2016, it received a very quick adoption and is becoming the new package manager of choice of the JavaScript community. We are going to use Yarn in this tutorial. If you want to stick to NPM you can simply replace all `yarn add` and `yarn add --dev` commands of this tutorial by `npm install --save` and `npm install --dev`.
22+
23+
- Install Yarn by following the [instructions](https://yarnpkg.com/en/docs/install). You can likely install it with `npm install -g yarn` or `sudo npm install -g yarn` (yeah, we're using NPM to install Yarn, much like you would use Internet Explorer or Safari to install Chrome!).
24+
25+
- Create a new folder to work in, and `cd` in it.
26+
- Run `yarn init` and answer the questions (`yarn init -y` to skip all questions), to generate a `package.json` file automatically.
27+
- Create an `index.js` file containing `console.log('Hello world')`.
28+
- Run `node .` in this folder (`index.js` is the default file Node looks for in the current folder). It should print "Hello world".
29+
30+
Running `node .` to execute our program is a bit too low-level. We are going to use an NPM/Yarn script to trigger the execution of that code instead. That will give us a nice abstraction to be able to always use `yarn start`, even when our program gets more complicated.
31+
32+
- In `package.json`, add `"start": "node ."` in the `scripts` object.
33+
34+
`package.json` must be a valid JSON file, which means that you cannot have trailing commas. So be careful when editing manually your `package.json` file.
35+
36+
- Run `yarn start`. It should print `Hello world`.
37+
38+
- Create a `.gitignore` file and add the following to it:
39+
40+
```
41+
npm-debug.log
42+
yarn-error.log
43+
```
44+
45+
**Note**: If you take a look at the `package.json` files I provide, you will see a `tutorial-test` script in every chapter. Those scripts let me test that the chapter works fine when running `yarn && yarn start`. You can delete them in your own projects.
46+
47+
Next section: [2 - Installing and using a package](/tutorial/2-packages)
48+
49+
Back to the [table of contents](https://github.com/verekia/js-stack-from-scratch).

tutorial/1-npm-and-package-json/README.md

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
npm-debug.log
3+
yarn-error.log
34
lib
45
dist/client-bundle.js

tutorial/10-immutable-redux-improvements/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ obj.set('a', 2); // Returns a new object without mutating `obj`
1818

1919
This approach follows the **functional programming** paradigm, which works really well with Redux. Your reducer functions actually *have* to be pure functions that don't alter the state passed as parameter, but return a brand new state object instead. Let's use Immutable to enforce this.
2020

21-
- Run `npm install --save immutable`
21+
- Run `yarn add immutable`
2222

2323
We are going to use `Map` in our codebase, but ESLint and the Airbnb config will complain about using a capitalized name without it being a class. Add the following to your `package.json` under `eslintConfig`:
2424

@@ -35,7 +35,7 @@ We are going to use `Map` in our codebase, but ESLint and the Airbnb config will
3535
]
3636
}
3737
```
38-
This makes `Map` and `List` (the 2 Immutable objects you'll use all the time) exceptions to that ESLint rule. This verbose JSON formatting is actually done automatically by NPM, so we cannot make it more compact unfortunately.
38+
This makes `Map` and `List` (the 2 Immutable objects you'll use all the time) exceptions to that ESLint rule. This verbose JSON formatting is actually done automatically by Yarn/NPM, so we cannot make it more compact unfortunately.
3939

4040
Anyway, back to Immutable:
4141

@@ -78,7 +78,7 @@ The app should still behave exactly the way it did before.
7878
As you can see from the code snippet above, our state object still contains a plain old `dog` object attribute, which isn't immutable. It is fine this way, but if you want to only manipulate immutable objects, you could install the `redux-immutable` package to replace Redux's `combineReducers` function.
7979

8080
**Optional**:
81-
- Run `npm install --save redux-immutable`
81+
- Run `yarn add redux-immutable`
8282
- Replace your `combineReducers` function in `app.jsx` to use the one imported from `redux-immutable` instead.
8383
- In `bark-message.js` replace `state.dog.get('hasBarked')` by `state.getIn(['dog', 'hasBarked'])`.
8484

@@ -95,7 +95,7 @@ export const makeBark = createAction(MAKE_BARK, () => true);
9595

9696
`redux-actions` implement the [Flux Standard Action](https://github.com/acdlite/flux-standard-action) model, just like the action we previously wrote, so integrating `redux-actions` is seamless if you follow this model.
9797

98-
- Don't forget to run `npm install --save redux-actions`.
98+
- Don't forget to run `yarn add redux-actions`.
9999

100100
Next section: [11 - Testing with Mocha, Chai, and Sinon](/tutorial/11-testing-mocha-chai-sinon)
101101

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
npm-debug.log
3+
yarn-error.log
34
lib
45
dist/client-bundle.js

tutorial/11-testing-mocha-chai-sinon/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
We are going to use [Mocha](http://mochajs.org/) as our main testing framework. Mocha is easy to use, has tons of features, and is currently the [most popular JavaScript testing framework](http://stateofjs.com/2016/testing/). It is very flexible and modular. In particular, it lets you use any assertion library you want. [Chai](http://chaijs.com/) is a great assertion library that has a lot of [plugins](http://chaijs.com/plugins/) available and lets you choose between different assertion styles.
1010

11-
- Let's install Mocha and Chai by running `npm install --save-dev mocha chai`
11+
- Let's install Mocha and Chai by running `yarn --dev mocha chai`
1212

1313
In `state-test.js`, write the following:
1414

@@ -70,7 +70,7 @@ gulp.task('test', ['build'], () =>
7070
);
7171
```
7272

73-
- Run `npm install --save-dev gulp-mocha` of course.
73+
- Run `yarn add --dev gulp-mocha` of course.
7474

7575
As you can see, tests are run on transpiled code in `lib`, which is why `build` is a prerequisite task of `test`. `build` also has a prerequisite, `lint`, and finally, we are making `test` a prerequisite of `main`, which gives us the following task cascade for the `default` task: `lint` > `build` > `test` > `main`.
7676

@@ -80,9 +80,9 @@ As you can see, tests are run on transpiled code in `lib`, which is why `build`
8080
gulp.task('main', ['test'], () => /* ... */ );
8181
```
8282

83-
- In `package.json`, replace the current `"test"` script by: `"test": "gulp test"`. This way you can use `npm test` to just run your tests. `npm test` is also the standard command that will be automatically called by tools like continuous integration services for instance, so you should always bind your test task to it. `npm start` will run the tests before building the Webpack client bundle as well, so it will only build it if all tests pass.
83+
- In `package.json`, replace the current `"test"` script by: `"test": "gulp test"`. This way you can use `yarn test` to just run your tests. `test` is also the standard script that will be automatically called by tools like continuous integration services for instance, so you should always bind your test task to it. `yarn start` will run the tests before building the Webpack client bundle as well, so it will only build it if all tests pass.
8484

85-
- Run `npm test` or `npm start`, and it should print the result for our test, hopefully green.
85+
- Run `yarn test` or `yarn start`, and it should print the result for our test, hopefully green.
8686

8787
## Sinon
8888

@@ -144,7 +144,7 @@ describe('Shared', () => {
144144

145145
Here, we are using *stubs* from Sinon, and a Chai plugin to be able to use Chai assertions on Sinon stubs and such.
146146

147-
- Run `npm install --save-dev sinon sinon-chai` to install these libraries.
147+
- Run `yarn add --dev sinon sinon-chai` to install these libraries.
148148

149149
So what is new here? Well first of all, we call `chai.use(sinonChai)` to activate the Chai plugin. Then, all the magic happens in the `it()` statement: `stub(console, 'log')` is going to neutralize `console.log` and monitor it. When `new Dog('Test Toby').barkInConsole()` is executed, a `console.log` is normally supposed to happen. We test this call to `console.log` with `console.log.should.have.been.calledWith()`, and finally, we `restore` the neutralized `console.log` to make it work normally again.
150150

tutorial/11-testing-mocha-chai-sinon/yarn-error.log

Lines changed: 0 additions & 107 deletions
This file was deleted.

tutorial/12-flow/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
npm-debug.log
3+
yarn-error.log
34
lib
45
dist/client-bundle.js

0 commit comments

Comments
 (0)