webpack для реальных задач · 31 feature description shorthands iteratee shorthands...

52
webpack для реальных задач

Upload: others

Post on 19-Aug-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

webpack для реальных задач

Page 2: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

webpack — это сборщик фронтенда

2@iamakulov

Page 3: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

// cow.js

export function cow() {

console.log("Moo!");

}) function cow() {

console.log("Moo!");

};

cow();// main.js

import { cow } from './cow.js';

cow();

Сборщик фронтенда

3@iamakulov

Page 4: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

План— Собрать фронтенд

— Добавить компиляцию

— Уменьшить размер фронтенда

— Улучшить кеширование

— Ускорить процесс билда

— Сделать процесс разработки приятнее

4@iamakulov

Page 5: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

План— Собрать фронтенд

— Добавить компиляцию

— Уменьшить размер фронтенда

— Улучшить кеширование

— Ускорить процесс билда

— Сделать процесс разработки приятнее

5@iamakulov

Page 6: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Задача: уменьшить размер фронтенда

6

Page 7: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Минификация

UglifyJS

7@iamakulov

Page 8: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Минификация// webpack.config.jsconst webpack = require('webpack');

module.exports = {plugins: [new webpack.optimize.UglifyJsPlugin()

]};

8@iamakulov

Page 9: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

// comments.js

import './comments.css';

export function render(data, target) {

console.log('Rendered!');

}

// bundle.js (part of)

"use strict";

Object.defineProperty(__webpack_exports__, "__esModule", { value: true });

var __WEBPACK_IMPORTED_MODULE_0__comments_css__ = __webpack_require__(4);

var __WEBPACK_IMPORTED_MODULE_0__comments_css___default =

__webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__comments_css__);

__webpack_exports__["render"] = render;

function render(data, target) {

console.log('Rendered!');

}

9@iamakulov

Page 10: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

}

// bundle.js (part of)

"use strict";

Object.defineProperty(__webpack_exports__, "__esModule", { value: true });

var __WEBPACK_IMPORTED_MODULE_0__comments_css__ = __webpack_require__(4);

var __WEBPACK_IMPORTED_MODULE_0__comments_css___default =

__webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__comments_css__);

__webpack_exports__["render"] = render;

function render(data, target) {

console.log('Rendered!');

}

10

// bundle.js (part of)

"use strict";function

r(e,t){console.log("Rendered!")}Object.defineProperty(t,"__esModule",{value:!0});var

o=n(4);n.n(o);t.render=r

@iamakulov

Page 11: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Минификация

UglifyJS { minimize: true }

11@iamakulov

Page 12: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Минификация// comments.css

.comment {

color: black;

}

// bundle.js (part of)

exports = module.exports = __webpack_require__(1)();

exports.push([module.i, ".comment {\r\n color: black;\r\n}", ""]);

12@iamakulov

Page 13: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

// webpack.config.js

module.exports = {

module: {

rules: [

{

test: /\.css$/,

use: [

'style-loader',

{ loader: 'css-loader', options: { minimize: true } }

]

}

]

}

};

13@iamakulov

Page 14: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Подводные камни— Webpack → Babili

14@iamakulov

Page 15: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

NODE_ENV=production// …

if (process.env.NODE_ENV !== 'production') {validateTypeDef(Constructor, propTypes, 'prop');

}

// …

15@iamakulov

Page 16: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

NODE_ENV=production// webpack.config.js

const webpack = require('webpack');

module.exports = {

plugins: {

new webpack.DefinePlugin({

'process.env.NODE_ENV': JSON.stringify('production')

})

}

};

16@iamakulov

Page 17: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

NODE_ENV=production// …

if ('production' !== 'production') {validateTypeDef(Constructor, propTypes, 'prop');

}

// …

17@iamakulov

Page 18: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

NODE_ENV=production// …

if ('production' !== 'production') {validateTypeDef(Constructor, propTypes, 'prop');

}

// …

18@iamakulov

Page 19: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

ES-импорты// comments.jsexport const commentRestEndpoint = '/rest/comments';export const render = () => { return 'Rendered!'; };

// index.jsimport { render } from './a.js';

render();

19@iamakulov

Page 20: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

ES-импорты// comments.jsexport const commentRestEndpoint = '/rest/comments';export const render = () => { return 'Rendered!'; };

// index.jsimport { render } from './comments.js';

render();

20@iamakulov

Page 21: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

ES-импорты// bundle.js (part of)"use strict";__webpack_exports__["a"] = commentRestEndpoint;__webpack_exports__["b"] = render;

var commentRestEndpoint = '/rest/comments';var render = function () { return 'Rendered!'; }

21@iamakulov

Page 22: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

ES-импорты"use strict";r.d(n,"a",function(){return t});var t=function(){return 'Rendered!'}

22@iamakulov

Page 23: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Подводные камни// webpack.config.jsconst webpack = require('webpack');

module.exports = {plugins: {

new webpack.optimize.UglifyJsPlugin()}

};

23@iamakulov

Page 24: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

// webpack.config.jsmodule.exports = {

module: {rules: [{test: /\.js$/,use: [{'babel-loader', options: {presets: [['es2015', { modules: false }]]

}]}

]}

};

24@iamakulov

Page 25: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Подводные камни// article.jsexport { default as renderComments } from './comments.js';

webpack/webpack#2867

25@iamakulov

Page 26: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

moment.jsimport moment from 'moment'; // +243 Кб

// moment.jsrequire('./locale/' + name);

26@iamakulov

Page 27: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

moment.js// webpack.config.js

const webpack = require('webpack');

module.exports = {

plugins: {

new webpack.ContextReplacementPlugin(

/moment[\/\\]locale/,

/(en-gb|ru)\.js/

)

}

};

27@iamakulov

Page 28: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

lodashimport _ from 'lodash'; // +70 Кб_.get({ a: { b: 5 } }, 'a.b');

import get from 'lodash/get';get({ a: { b: 5 } }, 'a.b');

28@iamakulov

Page 29: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

babel-plugin-lodash// До babel-plugin-lodashimport _ from 'lodash';_.get({ a: { b: 5 } }, 'a.b');

// После babel-plugin-lodashimport _get from 'lodash/get';_get({ a: { b: 5 } }, 'a.b');

73 Кб

29

11 Кб@iamakulov

Page 30: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

lodash-webpack-plugin// До lodash-webpack-pluginimport _ from 'lodash';_.get({ a: { b: 5 } }, 'a.b'); // 5

// lodash-webpack-plugin с {paths: false}import _ from 'lodash';_.get({ a: { b: 5 } }, 'a.b'); // undef.

11 Кб

30

<1 Кб@iamakulov

Page 31: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

31

Feature Descriptionshorthands Iteratee shorthands for _.property,

_.matches, & _.matchesProperty.

cloning Support “clone” methods & cloning

source objects.

currying Support “curry” methods.

caching Caches for methods like

_.cloneDeep, _.isEqual, & _.uniq.

collections Support objects in “Collection”

methods.

exotics Support objects like buffers, maps,

sets, symbols, typed arrays, etc.

guards Guards for host objects, sparse

arrays, & other edge cases.

metadata Metadata to reduce wrapping of

bound, curried, & partially applied

functions.

(requires currying)

Feature Descriptiondeburring Support deburring letters.

unicode Support Unicode symbols.

chaining Components to support chain

sequences.

memoizing Support _.memoize &

memoization.

coercions Support for coercing values to

integers, numbers, & strings.

flattening Support “flatten” methods &

flattening rest arguments.

paths Deep property path support for

methods like _.get, _.has, & _.set.

placeholders Argument placeholder support for

“bind”, “curry”, & “partial”

methods.

(requires currying)

@iamakulov

Page 32: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

32@iamakulov

Page 33: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

externals// webpack.config.jsmodule.exports = {

externals: {'react': 'React','react-dom': 'ReactDOM',

}};

33@iamakulov

Page 34: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

externals// webpack.config.jsmodule.exports = {

output: { libraryTarget: 'amd' },

externals: {'react': { amd: '/libraries/react.min.js' },'react-dom': { amd: '/libraries/react-dom.min.js' },

}};

34@iamakulov

Page 35: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Итого: уменьшить размер фронтенда— Настроить минификацию

— Передавать NODE_ENV=production

— Использовать ES-импорты и экспорты

— Выбрасывать locales в moment.js

— Выбрасывать ненужные методы в lodash

— Использовать externals

35@iamakulov

Page 36: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Задача: улучшить кеширование

36@iamakulov

Page 37: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Использовать хеш<script src="./index.js?version=1">

<script src="./index.js?version=2">

37@iamakulov

Page 38: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Использовать хеш// webpack.config.jsmodule.exports = {

entry: './index.js',output: {

filename: 'bundle.[chunkhash].js// → bundle.8e0d62a03.js

}};

38@iamakulov

Page 39: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Узнать название бандла

HtmlWebpackPlugin

<!doctype html>

<!-- -->

<script src="bundle.8e0d62a03.js">

</script>

WebpackManifestPlugin

{

"bundle.js": "bundle.8e0d62a03.js"

}

39@iamakulov

Page 40: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Подводные камни// webpack.config.jsmodule.exports = {

entry: './index.js',output: {filename: 'bundle.[chunkhash].js

},plugins: [new WebpackChunkHash() // пакет webpack-chunk-hash

]};

40@iamakulov

Page 41: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

41

Page 42: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

module.exports = {

entry: {

homepage: './index.js',

article: './article.js'

},

output: {

filename: '[name].[chunkhash].js'

},

plugins: [

new WebpackManifestPlugin(),

new WebpackChunkHash(),

// …

]

};

42@iamakulov

Page 43: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

module.exports = {

plugins: [

// …

new webpack.optimize.CommonsChunkPlugin({

name: 'vendor',

minChunks: m => m.context && m.context.includes('node_modules'),

}),

new webpack.optimize.CommonsChunkPlugin({

name: 'runtime',

chunks: ['vendor'],

minChunks: Infinity,

}),

// …

]

};43@iamakulov

Page 44: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

module.exports = {

plugins: [

// …

new ChunkManifestPlugin({ // chunk-manifest-webpack-plugin package

filename: 'chunk-manifest.json',

manifestVariable: 'webpackManifest'

}),

new webpack.HashedModuleIdsPlugin()

]

};

44@iamakulov

Page 45: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Разбить код на модулиhomepage.a68cd93e1a43281ecaf0.jsarticle.d07a1a5e55dbd86d572b.jsvendor.1ebfd76d9dbc95deaed0.jsruntime.d41d8cd98f00b204e980.jsmanifest.jsonchunk-manifest.json

45@iamakulov

Page 46: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods
Page 47: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Загружать по частям// article.jsimport { renderArticle } from './components/article';import { renderComments } from './components/comments';import { renderSidebar } from './components/sidebar';

renderArticle();renderComments();renderSidebar();

47@iamakulov

Page 48: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Загружать по частям// article.jsimport { renderArticle } from './components/article';renderArticle();

import('./comments.js').then((m) => { m.renderComments(); });import('./sidebar.js').then((m) => { m.renderSidebar(); });

48@iamakulov

Page 49: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Подводные камни// article.jsimport { renderArticle } from './components/article';renderArticle();

import('./comments.js').then((m) => { m.renderComments(); });import('./sidebar.js').then((m) => { m.renderSidebar(); });

babel-plugin-syntax-dynamic-import

49@iamakulov

Page 50: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Предзагрузить все чанки// webpack.config.jsmodule.exports = {plugins: [new OfflinePlugin() // offline-plugin package

]};

// index.jsimport runtime from 'offline-plugin/runtime';runtime.install();

50@iamakulov

Page 51: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Итого: улучшить кеширование— Использовать хэш

— Разбить код на чанки

— Загружать чанки по частям

— Предзагрузить все чанки

51@iamakulov

Page 52: webpack для реальных задач · 31 Feature Description shorthands Iteratee shorthands for _.property, _.matches, & _.matchesProperty. cloning Support “clone” methods

Спасибо!Иван Акулов

EPAM

Текстовая версия: iamakulov.com/webpack

Подписывайтесь в Твитере: @iamakulov

52@iamakulov