webpack@5ã§å ¥ãPersistent Cachingã«ã¤ãã¦
webpackã¯in-memoryã®ã¿ã§ä»ã¾ã§æ°¸ç¶çãªãã£ãã·ã¥ãå®è£
ãã¦ãã¾ããã§ãããçç±ã¨ãã¦ã¯ãããã©ã¼ãã³ã¹ãããå®å
¨æ§ãåªå
ãã¦ããããã§ãã
cache-loaderã使ã£ããã¨ããã人ã¯ãããããããã¾ãããã確ãã«éããªãä¸æ¹ãå®å
¨æ§ãæãªããã¦ããã®ã¯äºå®ã§ãã
ãã®æ©è½ã¯ãwebpackã¯ããã©ã«ãã§ãã¡ã¤ã«ãã£ãã·ã¥ããªã³ã«ã¯ãã¾ãããããã§ããã«ãã®é度ãä¸ãããå ´åã«ä½¿ãæ©è½ã§ãã
以ä¸ãããã©ã«ãã®æåã¨ãªãã¾ãã
mode | cache |
---|---|
development | memory |
production | false |
å®éã«ä½¿ãã¨ãã®è¨å®
çµè«ã§ãããwebpack.config.jsã¸ä»¥ä¸ã®ããã«æ¸ããã¨ãæ¨å¥¨ããã¾ãã
module.exports = { cache: { type: 'filesystem', buildDependencies: { config: [__filename] } } };
ãã¨ã¯ãåã³ã¼ãã®è¨å®ã«ä¾åããããversion
çã®è¿½å ãå¿
è¦ã«ãªãå¯è½æ§ãããã¾ãã
ããã¥ã¡ã³ã
ä»çµã¿
ãã¡ã¤ã«ãã£ãã·ã¥ã§ã¯ä»¥ä¸ã®ããã«ããã©ã«ãã§ã¯node_modules/.cache/webpack
ã¨ãããã£ã¬ã¯ããªã«ã¹ãããã·ã§ãããçæãã¾ãã
(à¹Ë̵á´Ë̵)Ù ~/D/w/node_modules á tree .cache .cache âââ webpack âââ default-production âââ 0.pack <-- çææ¸ã¿ã³ã¼ãã®è¨é² âââ index.pack <-- ä¾åãã¡ã¤ã«çã®è¨é² 2 directories, 2 files
ãã®ããã«ã·ãªã¢ã«åããããã¼ã¿ãä¿åãã¾ãã
MD4ããã·ã¥ã¢ã«ã´ãªãºã ãç¨ããetagãèå¥åã¨ããããã¨ä¸è´ãããã®ãwebpackã¯ä½¿ç¨ãã¾ãã(å®è£
ãç¥ããã人ã¯createHash.jsã¨PackFileCacheStrategy.jsãèªãã§ãã ãã)
æ¬çªç°å¢ã¨buildDependenciesã«ã¯ã timestamp + hash ã¢ã¼ããããã©ã«ãã§é©å¿ããã¾ãã
使ãã¦ã¼ã¶ã¼ã¯ snapshot
ã®ãªãã·ã§ã³ãè¨å®ãããã¨ã¯ãªãã¨æãã®ã§ããã®è¨äºã§ã¯å²æãã¾ãã
https://webpack.js.org/configuration/other-options/#snapshot
webpackã¯ããã¹ã¦ã®ã¢ã¸ã¥ã¼ã«ããããã«å¯¾ããcompilation.fileDependencies
, compilation.contextDependencies
, compilation.missingDependencies
ããã©ããã³ã°ããã¹ãããã·ã§ãããçæãã¦ããã¾ãã
ä½è«ã§ããããã®ä¸ç¹ã¯ãwebpack@5ããSortable Set
ããSet
ã¨ãªãã並ã³æ¿ããä¸å¯è½ã¨ãªãã¾ããã®ã§ãã©ã°ã¤ã³ä½è
ã¯æ°ãã¤ãã¦ãã ããã
ã¤ã¾ããç¹å®ãã¡ã¤ã«ãå¤æ´ããã¨webpackã¯ãã®ãã¡ã¤ã«ã®ãã£ãã·ã¥(webpackå ã§ã¯ãã£ãã·ã¥ã¨ã³ããªã¨å¼ã°ãã)ãç¡å¹åããå種loaderãå®è¡å¾ã«ãã¡ã¤ã«è§£æãè¡ãåçæãè¡ãã¾ããã¾ããbundle.jsã®ãã£ãã·ã¥ã¨ã³ããªãç¡å¹åãããã®ãã¡ã¤ã«ãåçæããå¯è½æ§ãããã¾ãã
ä¾ãã°ãä¾åãã¡ã¤ã«ãå¤æ´ããã¨ä»¥ä¸ã®ããã«å¤æ´ããã¾ãã
(à¹Ë̵á´Ë̵)Ù ~/D/w/node_modules á tree .cache .cache âââ webpack âââ default-production âââ 0.pack âââ 1.pack âââ index.pack âââ index.pack.old 2 directories, 4 files
1ã®æ¹ãæ°ãããªããã¹ãããã·ã§ããã追å ããã¾ããã
ãã£ãã·ã¥ã¨ã³ããªãç¡å¹åãããã±ã¼ã¹
以ä¸ã®å ´åã«ãã£ãã·ã¥ã¨ã³ããªãç¡å¹åããã¾ãã
- ç£è¦ä¸ã®ãã¡ã¤ã«ãå¤æ´ãããã¨ã
- è¨å®ãå¤æ´ããã¨ã
- webpack.config.jsã®
cache
çã®è¨å®å¤æ´
- webpack.config.jsã®
- loaderãpluginãããã±ã¼ã¸ã¢ãããã¼ããããã¨ã
- ä¾åé¢ä¿(node_modules)ãããã±ã¼ã¸ã¢ãããã¼ããããã¨ã
- cliãããã«ãã«å½±é¿ã®ããå¤ãéã£ãã¨ã
--optimization-minimize
, ã³ã¼ãã§å¤æã§ããªããã®
- ã«ã¹ã¿ã ãªãã«ãã¹ã¯ãªãããå¤æ´ãããã¨ã
cache.version
,cache.name
,cache.buildDependencies
ãã«ãçµæãå¤æ´ããå¯è½æ§ã®ãããã®ã¯ãã£ãã·ã¥ã¨ã³ããªãç¡å¹åããã¾ãã
ä¾ãã°ã--optimization-minimize
ã渡ãã°ãã«ãçµæã«ã¯å½±é¿ããã¾ãããããå
¥åãããã½ã¼ã¹ã³ã¼ãã®å¤æ´ã ãã§ã¯ãããæ¤ç¥ã§ãã¾ãããããã£ãã·ã¥ã¯ãããèæ
®ããå¿
è¦ãããã¾ãã webpackã§ã¯ããã«å¯¾ãã¦ãcache.version
, cache.name
, cache.buildDependencies
ã使ãå¦çããã¾ããããããèªåçã«èªèããã®ã¯é£ããããå½±é¿ãçããã¨ãã«åæ§ç¯ããå¿
è¦ãåºã¦ãã¾ãã(ããªãå®å
¨æ§ãéè¦ãã¦ãã¾ã)
ãªãã·ã§ã³
æä½éã®ãã®ã ã説æãã¾ãã
type
memory
ã¨filesystem
ãåå¨ããã©ã¡ããé¸æãããã¨ãã§ãã¾ãã
buildDependencies
cache.buildDependencies
ã«ã¯ããã«ãã«ãããã³ã¼ãä¾åé¢ä¿ã追å ãã¾ãã
defaultWebpack
webpackã®ãã¹ã¦ã®ä¾åé¢ä¿ãåå¾ããããã«ãããã©ã«ãã§webpack/lib
ã¨ãªãã¾ãã
ãã®è¨å®ã¯åºæ¬çã«è¨å®ããå¿
è¦ã¯ãªãã§ãã
module.exports = { cache: { type: 'filesystem', buildDependencies: { defaultWebpack: ['webpack/lib'], } } };
config
å
¬å¼ã§ã¯ãææ°ã®è¨å®(webpack.config.js, etc)ã¨ãã¹ã¦ã®ä¾åãåå¾ããããã«__ filename
ãè¨å®ãããã¨ãæ¨å¥¨ããã¾ãã
ãã®ããã«æ¸ããã¨ã«ãããè¨å®ã¨ãã¹ã¦ã®ä¾åé¢ä¿ãåå¾ããããã«ãªãã¾ãã
module.exports = { cache: { type: 'filesystem', buildDependencies: { config: [__filename] } } };
ãã£ã¬ã¯ããªã®å ´åãæãè¿ãpackage.jsonã®ä¾åé¢ä¿ãåæããããã¡ã¤ã«ã®å ´åã¯ãNode.jsã®ã¢ã¸ã¥ã¼ã«ã®ãã£ãã·ã¥ãè¦ã¦ãä¾åé¢ä¿ãwebpackã¯ææ¡ãã¾ãã
注æç¹ã¨ãã¦ããã£ã¬ã¯ããªã®å ´åã¯ãå¿ ãã¹ã©ãã·ã¥ã§çµããå¿ è¦ãããã¾ã(ããããªãã¨ãã¡ã¤ã«ã¨èå¥ããã¦ãã¾ã)
version
ãã¨ãåãå
容ã§ãããã®å¤ãå¤æ´ãããã¨ã«ããæ°¸ç¶çãã£ãã·ã¥ãç¡å¹åãããã¨ãã§ãã¾ãã
ãã«ãã®ä¸é¨ã®ä¾åã«ããã¦ã表ç¾ã§ããªãå ´åãåå¨ãã¾ãã(e.g. DBããèªã¿è¾¼ã¾ããå¤ãç°å¢å¤æ°ãã³ãã³ãã©ã¤ã³ã§æ¸¡ãããå¤)
ãããã£ãã·ã¥ãããããå ´åãã®ãªãã·ã§ã³ã確èªã»æ¤è¨ãã¦ãã ããã
ããã³ã¼ããdefinePluginçµç±ã§ç°å¢å¤æ°ãå ¥ãã¦ãã¦ããããã³ãã«ã«åãè¾¼ãå ´åãããã¯ãã®ç°å¢å¤æ°(e.g. gitã®revision)ã¸ã®ä¾åãããã®ã§ããã¼ã¸ã§ã³åããã®å¤ã«ãããã£ãã·ã¥ãç¡å¹åããå¿ è¦ãããã¾ãã
module.exports = { cache: { type: 'filesystem', buildDependencies: { config: [__filename] } }, plugins: [ new webpack.DefinePlugin({ 'process.env.foo': JSON.stringify('foo') }) ] };
ä¸è¨ã®å ´åã¯config
ã§ãã®webpack.config.jsãç£è¦ä¸ã«ç½®ãã¦ãããããã§foo
ãå®ç¾©ãã¦ããããããã®æååãå¤æ´ãããwebpackã¯æ¤ç¥ã§ããããåé¡ãªãã§ããåé¡ã¯ãç£è¦ä¸ã§webpackãå¤æ´ããããèªç¥ã§ããªã(ãã£ã¨æ«ç«¯ã¾ã§å¤æ°ç)ã§ãã
ãããã.env
ãå©ç¨ããå ´åã¯ä»¥ä¸ã®ããã«version
ãæå®ããªãã¨ãã£ãã·ã¥ã¯æ´æ°ããã¾ããã
# .env VERSION=1.0
const webpack = require('webpack'); const { config } = require('dotenv'); config(); module.exports = { cache: { type: 'filesystem', buildDependencies: { config: [__filename] }, version: process.env.VERSION }, plugins: [ new webpack.DefinePlugin({ 'process.env.version': process.env.VERSION }) ] };
ãã¦ããã§åé¡ã«ãªãã®ããããããããã£ããè¨å®ããªãã¨éå»ã®ã¹ãããã·ã§ãããåç
§ããããçæçµæãéå»ã®ç¶æ
ã«ãªããã¨ã§ãããããwebpackãæãã¦ããåé¡ã§ãããã®å ´åã ã¨ãversion
ãæå®ããªãå ´åãåºåã¯å¸¸ã«æåã®ãã«ãæã®ãã®ã¨ãªã.envã§æ¸ãæããã¦ãåæ ããã¾ããã
ã¤ã¾ããcache.name
ã§ãããã£ãã·ã¥ãç¡å¹åã§ããããã³ã¼ãã«ä¾åãã¾ããå°å
¥ã®æ¤è¨ããå¯è½æ§ãããã¾ãã
ããã©ã¼ãã³ã¹ã®æé©å
node_modulesã®ã³ã¼ãã«å¯¾ãã¦ãtimestamp + hashã§ç®¡çããã¨ã³ã¹ããããããã«ãé度ãä½ä¸ããããwebpackã§ã¯ãpackage.jsonå ã®ãã¼ã¸ã§ã³ã¨ååãå©ç¨ãè©ä¾¡ãã¦ãã¾ãã ãªã®ã§ã絶対ã«node_moduleså ã®ã³ã¼ããç·¨éãããã¨ã¯é¿ãã¦ãã ããã
ãã®æé©åã¯ãsnapshot.managedPaths
ã®ãã¹ã«é©å¿ãããããã©ã«ãã§ã¯webpackãã¤ã³ã¹ãã¼ã«ããã¦ããnode_modulesã¨ãªãã¾ããyarn.pnpã®å ´åããã¡ã¤ã«ãã¹ã§ããã·ã¥ãå©ç¨ããããä¸è¨ã®æé©åã¯yarnãã«ãã¼ããããè¡ããã¾ããã
d3ã使ã£ãå ´åã®ããã©ã¼ãã³ã¹æ¸¬å®
// index.js import * as d3 from 'd3'; import 'foo.js';
// foo.js console.log('foo');
// webpack.config.js module.exports = { cache: { type: 'filesystem', buildDependencies: { config: [__filename] } } };
æåã¯ãã£ãã·ã¥ããªããããwebpack@4åæ§ã«ä»¥ä¸ã®é度ãããã¨ãªãã¾ãã
asset main.js 36.3 KiB [emitted] [minimized] (name: main) orphan modules 584 KiB [orphan] 554 modules cacheable modules 117 KiB ./src/index.js + 103 modules 117 KiB [built] [code generated] ./src/foo.js 21 bytes [built] [code generated] webpack 5.0.0-rc.2 compiled successfully in 1836 ms
ããä¸åº¦ãä½ãå¤æ´ããã«ãã«ããè¡ã£ã¦ã¿ã¾ãã
asset main.js 36.3 KiB [compared for emit] [minimized] (name: main) cached modules 700 KiB [cached] 556 modules webpack 5.0.0-rc.2 compiled successfully in 429 ms
ååã¨åæ§ãªã®ã§ãã¹ã¦ãã£ãã·ã¥ã使ããã¦ãããã¨ãããããç´4.5åç¨åº¦æ©ããªã£ããã¨ã確èªã§ãã¾ãã
ããã§ã¯ãfoo.jsã®ä¸èº«ãå¤æ´ãã¾ãã
asset main.js 36.3 KiB [emitted] [minimized] (name: main) cached modules 700 KiB [cached] 555 modules ./src/foo.js 18 bytes [built] [code generated] webpack 5.0.0-rc.2 compiled successfully in 1228 ms
foo.jsã®ã³ã¼ãã®ã¿ãå度çæãããfoo.jsã®ã¹ãããã·ã§ãããæ´æ°ããã¾ãããindex.jsãå«ãæ´æ°ãããããã§ã¯ãªãããé度ã¯ãã«ãã«ãã®æãããéããªãã¾ãã
å度ãä½ãå¤æ´ããã«ãã«ããè¡ãã¾ãã
asset main.js 36.3 KiB [compared for emit] [minimized] (name: main) cached modules 700 KiB [cached] 556 modules webpack 5.0.0-rc.2 compiled successfully in 416 ms
ãã¹ã¦ã®ãã£ãã·ã¥ãå©ç¨ããå¤æ´ããªãããã400mså°ã§è½ã¡çãã¾ããã
ãããã«
webpack5ã¸ã®æ©è½è¿½å ã¨ãã¦ãä¸çªæ票çãå¤ãã£ãã®ããã®æ°¸ç¶çãã£ãã·ã¥ã¨ããæ©è½ã§ããã
ãã®æ©è½ã¯ãéçºæã«å¤§ãã«å½¹ã«ç«ã¤ã¨æãã¾ãããããwebpackã®ãã«ãæéã«ä¸æºããã人ã¯ãã®æ©è½ã試ãã¦ã¿ãã¨è¯ãããªã¨æãã¾ãã