Skip to content

Commit 65b9c35

Browse files
committed
add Variabel + Sourcemaps + server --> with webpack
1 parent ebf79de commit 65b9c35

File tree

6 files changed

+145
-6
lines changed

6 files changed

+145
-6
lines changed

es6-features/modulos.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,130 @@ const webpack = require('webpack');
211211
]
212212
}
213213
```
214-
## Variáveis de Ambiente no Webpack
214+
## Variáveis de Ambiente no Webpack
215+
216+
> Podemos criar variáveis para que de acordo com o seu valor a biblioteca pode ser mais ou menos comprimida.
217+
218+
1. Para exemplificar melhor, executamos no terminal o comando `npm --save install react react-dom` para importarmos as bibliotecas react e react-dom.
219+
2. No arquivo `app.js` adicionamos as linhas abaixo para importar as duas bibliotecas.
220+
```JS
221+
import react from 'react';
222+
import reactDom from 'react-dom';
223+
```
224+
3. No arquivo `webpack.config` criamos uma variável chamada `nodeEnv` e dentro de `plugins` defiimos como ela irá se comportar, o arquivo final ficou assim:
225+
```JS
226+
const webpack = require('webpack');
227+
const nodeEnv = process.env.NODE_ENV || 'production';
228+
229+
module.exports = {
230+
entry: {
231+
filename: './app.js'
232+
},
233+
output: {
234+
filename: './build.js'
235+
},
236+
module: {
237+
loaders: [
238+
{
239+
test: /\.js$/,
240+
exclude: /node_modules/,
241+
loader: 'babel-loader',
242+
query: {
243+
presets: [
244+
['es2015', {modules: false}]
245+
]
246+
}
247+
}
248+
]
249+
},
250+
plugins: [
251+
new webpack.optimize.UglifyJsPlugin({
252+
compress: { warnings: false},
253+
output: { comments: false }
254+
}),
255+
new webpack.DefinePlugin({
256+
'process.env': { NODE_ENV: JSON.stringify(nodeEnv) }
257+
})
258+
]
259+
}
260+
```
261+
262+
## Sourcemaps
263+
264+
> Serve para mapear todo o código fonte e informar ponto a ponto o que cada elemento referencia, ou seja, faz uma referência entre o arquivo que tinhámos antes com o arquivo já compilado, isso é muito útil no desevolvimento até para depuração e descoberta de bugs. Uma observação importante é nunca subir o arquivo `build.js.map` para produção, porque ele é muito extenso e só é útil para facilitar no desenvolvimento.
265+
266+
1. Para usar o Sourcemaps é necessário adicionar a variável `devtool: 'source-map'` dentro de `module.exports`
267+
2. Em `UglifyJsPlugin` adicione `sourceMap: true`, o arquivo final ficou assim:
268+
```JS
269+
const webpack = require('webpack');
270+
const nodeEnv = process.env.NODE_ENV || 'production';
271+
272+
module.exports = {
273+
devtool: 'source-map',
274+
entry: {
275+
filename: './app.js'
276+
},
277+
output: {
278+
filename: './build.js'
279+
},
280+
module: {
281+
loaders: [
282+
{
283+
test: /\.js$/,
284+
exclude: /node_modules/,
285+
loader: 'babel-loader',
286+
query: {
287+
presets: [
288+
['es2015', {modules: false}]
289+
]
290+
}
291+
}
292+
]
293+
},
294+
plugins: [
295+
new webpack.optimize.UglifyJsPlugin({
296+
compress: { warnings: false},
297+
output: { comments: false },
298+
sourceMap: true
299+
}),
300+
new webpack.DefinePlugin({
301+
'process.env': { NODE_ENV: JSON.stringify(nodeEnv) }
302+
})
303+
]
304+
}
305+
```
306+
307+
## Inicializando um server com Webpack
308+
309+
> Possibilita criar um servidor via `webpack-dev-server` que é útil para servir os arquivos estáticos no ambiente de desenvolvimento.
310+
311+
1. Execute no terminal o comando `npm install --save-dev webpack-dev-server` para instalar o webpack-dev-server.
312+
2. No arquivo `package.json` crie a task `"server": "./node_modules/.bin/webpack-dev-server --inline --open"`, o arquivo final ficará assim:
313+
```JS
314+
{
315+
"name": "modulos-exemplos",
316+
"version": "1.0.0",
317+
"description": "",
318+
"main": "app.js",
319+
"scripts": {
320+
"build": "./node_modules/.bin/webpack --colors --progress",
321+
"watch": "npm run build -- --watch",
322+
"server": "./node_modules/.bin/webpack-dev-server --inline --open"
323+
},
324+
"author": "",
325+
"license": "ISC",
326+
"devDependencies": {
327+
"babel-core": "^6.24.1",
328+
"babel-loader": "^6.4.1",
329+
"babel-preset-es2015-native-modules": "^6.9.4",
330+
"ramda": "^0.23.0",
331+
"webpack": "^2.2.0",
332+
"webpack-dev-server": "^2.4.2"
333+
},
334+
"dependencies": {
335+
"react": "^15.5.4",
336+
"react-dom": "^15.5.4"
337+
}
338+
}
339+
```
340+
3. Para testar, execute no terminal `npm run server`.

modulos-exemplos/app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import * as R from 'ramda';
66

77
import sum, { sub, multiplicacao, div as dividir, PI } from './utils';
88

9+
import react from 'react';
10+
import reactDom from 'react-dom';
11+
912
const arr1 = [1, 1, 1, 2, 2, 2, 3, 4, 5, 6];
1013
const arr2 = [5, 6, 6, 6, 7, 7, 8, 9, 10, 6, 1];
1114

@@ -17,6 +20,6 @@ console.log(arr1);
1720

1821
console.log(sum(20, 10))
1922
console.log(sub(20, 10))
20-
console.log(multiplicacao(20, 10))
23+
console.log(multiplicacao(20, 2))
2124
console.log(dividir(20, 10))
2225
console.log(PI)

modulos-exemplos/build.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modulos-exemplos/build.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modulos-exemplos/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "app.js",
66
"scripts": {
77
"build": "./node_modules/.bin/webpack --colors --progress",
8-
"watch": "npm run build -- --watch"
8+
"watch": "npm run build -- --watch",
9+
"server": "./node_modules/.bin/webpack-dev-server --inline --open"
910
},
1011
"author": "",
1112
"license": "ISC",
@@ -14,7 +15,8 @@
1415
"babel-loader": "^6.4.1",
1516
"babel-preset-es2015-native-modules": "^6.9.4",
1617
"ramda": "^0.23.0",
17-
"webpack": "^2.2.0"
18+
"webpack": "^2.2.0",
19+
"webpack-dev-server": "^2.4.2"
1820
},
1921
"dependencies": {
2022
"react": "^15.5.4",

modulos-exemplos/webpack.config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const webpack = require('webpack');
2+
const nodeEnv = process.env.NODE_ENV || 'production';
23

34
module.exports = {
5+
devtool: 'source-map',
46
entry: {
57
filename: './app.js'
68
},
@@ -24,7 +26,11 @@ const webpack = require('webpack');
2426
plugins: [
2527
new webpack.optimize.UglifyJsPlugin({
2628
compress: { warnings: false},
27-
output: { comments: false }
29+
output: { comments: false },
30+
sourceMap: true
31+
}),
32+
new webpack.DefinePlugin({
33+
'process.env': { NODE_ENV: JSON.stringify(nodeEnv) }
2834
})
2935
]
3036
}

0 commit comments

Comments
 (0)