You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*[Building React applications with Gulp and Browserify](https://tylermcginnis.com/react-js-tutorial-pt-2-building-react-applications-with-gulp-and-browserify/)
85
+
## Gulpfile explained
86
+
The process of code transformation is carried out with `gulp`, the instructions
87
+
are found in the `gulpfile.js` file. The tool
88
+
[pump](https://www.npmjs.com/package/pump) is used for easier
89
+
debugging of the `gulpfile.js`.
90
+
91
+
### 'default'
92
+
This is the process executed with the shell command `gulp`, in this proyect
93
+
it is executed also with `npm start`.
94
+
95
+
The tasks executed are:
96
+
1.`'sass'`
97
+
2.`'transform'`
98
+
3.`'replaceHTML-dev`
99
+
4.`'copy-public-dev'`
100
+
5.`'serve'`
101
+
102
+
### 'sass'
103
+
Take all the files in the `app/css` directory with a `.scss` file extension.
104
+
105
+
The files will be compacted and minified, a sourcemap is needed to debug this
106
+
file, otherwise errors will be shown as relative to the minified `.css` file
107
+
which are useless for development intentions. This is done with
108
+
`sourcemaps.init()` and `sourcemaps.write()`.
109
+
110
+
Take all `.scss` files and transform them into readable CSS code.
111
+
112
+
`gulp-clean-css` is used to eliminate unnecesary code in the resulting `.css`
113
+
file, making it lighter and thus faster to load for the browser.
114
+
115
+
Concatenate all the code into a single file called `styles.min.css`. Finally
116
+
place this file in the `dev` folder and reload all browsers with `browsersync`
117
+
to see their effect.
118
+
119
+
### 'transform'
120
+
A `'watcher'` is created with `watchify` and `browserify`. This watcher needs
121
+
only to receive the first `.js` file and will find all the other ones through
122
+
the `import` instructions.
123
+
124
+
All the JSX code is transformed into readable JS with `babelify`. Babelify in
125
+
turn uses the presets `react` and `env` (in `package.json`) to know how to
126
+
behave.
127
+
128
+
Debug is set to `true` and the options `cache`, `packageCache` and `fullPaths`
129
+
are needed for watchify to properly work.
130
+
131
+
When there is an `'update'`, the `bundle` function will be executed. This
132
+
function contains the same code as the `'transform'` gulp task.
133
+
134
+
The `'transform'` task creates a "bundle" of these JS files and writes the
135
+
output inside the `dev` directory, with the filename `build.js`.
136
+
137
+
### 'replaceHTML-dev'
138
+
As JS and CSS files are concatenated into new compact versions, the `index.html`
139
+
file needs to know where they are located and how they are named.
140
+
141
+
The `index.html` file is taken, and the code inside the `<!-- build:js -->`
142
+
and `<!-- build:css -->` tags is changed to point to the correct created files.
143
+
144
+
### 'copy-public-dev'
145
+
This process takes all files inside the `public` directory and copies them
146
+
exactly into the `dev` folder.
147
+
148
+
### 'serve'
149
+
A server is connected with `gulp-connect`. `browser-sync` is initialized.
150
+
151
+
Gulp will watch changes with the `gulp.watch` commands. Changes to SCSS files
152
+
will trigger the transformation of SCSS into the single CSS file and reload
153
+
browserSync. Changes to JS files trigger only the browserSync to reload, as its
154
+
transformation process is handled with the `watcher.on('change', bundle)`
155
+
instruction.
156
+
Changes to the `index.html` file trigger its copy and tag replacement, followed
157
+
by the reload of browserSync.
158
+
159
+
### 'production'
160
+
The production process is very similar to the default.
161
+
162
+
The JS build process is
163
+
different: it will create an uglified version for a lighter file and faster
164
+
browser performance.
165
+
166
+
The CSS does not need sourcemaps for debugging (as debugging is unnecesary in
167
+
the production environment), and so they are not created.
168
+
169
+
The difference of copying the `index.html` and `public` files is only in the
170
+
destination directory `dist`.
171
+
172
+
A server is connected to show files in the `dist` directory and browserSync
173
+
initialized. No watching and reloading tasks are used in production.
174
+
175
+
## App Context
176
+
### Creation
177
+
You can recreate the behavior of this app by installing its corresponding
178
+
packages individually. Packages were managed with `npm`. The app was created
179
+
from scratch in a C9 environment, with a 'blank' template.
*[Gettin started with React](https://reactjs.org/docs/add-react-to-an-existing-app.html#installing-react)
209
+
*[Getting started with Babel](https://babeljs.io/docs/setup/#installation)
210
+
*[Getting started with Gulp](https://css-tricks.com/gulp-for-beginners/)
211
+
*[Easier gulpfile debugging with pump](https://www.npmjs.com/package/pump)
212
+
*[Building React applications with Gulp and Browserify](https://tylermcginnis.com/react-js-tutorial-pt-2-building-react-applications-with-gulp-and-browserify/)
213
+
*[Display and reload the app with Browsersync](https://browsersync.io/)
214
+
*[Getting started with Jest](https://facebook.github.io/jest/docs/en/getting-started.html)
215
+
*[Expand Jest test suite with Enzyme](http://airbnb.io/enzyme/)
216
+
*[Color pallete creation with coolors.co](https://coolors.co/)
0 commit comments