íë¬ê·¸ì¸ì webpack ìíê³ì íµì¬ ììì´ë©°
커뮤ëí°ì webpackì ì»´íì¼ íë¡ì¸ì¤ë¥¼ íì©í ì ìë ê°ë ¥í ë°©ë²ì ì ê³µí©ëë¤.
íë¬ê·¸ì¸ì ê° ì»´íì¼ ê³¼ì ìì ë°ìíë 주ì ì´ë²¤í¸ì íí¹ í ì ììµëë¤.
모ë ë¨ê³ìì íë¬ê·¸ì¸ì ì»´íì¼ë¬,
ê²½ì°ì ë°ë¼ìë íì¬ ì§í ì¤ì¸ ì»´íì¼ì ìì í ì ê·¼ ê¶íì ê°ì§ëë¤.
먼ì webpack íë¬ê·¸ì¸ ì¸í°íì´ì¤ì ê·¼ê°ì ì ê³µíë
tapable ì í¸ë¦¬í°ì ëí´ ì´í´ë³´ê² ìµëë¤.
ì´ ìì ë¼ì´ë¸ë¬ë¦¬ë webpackì íµì¬ ì í¸ë¦¬í°ì´ì§ë§
ì ì¬í íë¬ê·¸ì¸ ì¸í°íì´ì¤ë¥¼ ì ê³µí기 ìí´ ë¤ë¥¸ ê³³ììë ì¬ì©í ì ììµëë¤.
webpackì ë§ì ê°ì²´ê° Tapable í´ëì¤ë¥¼ íì¥í©ëë¤.
Tapable í´ëì¤ê° ì ê³µíë tap, tapAsnyc, tapPromise ë©ìë를 íë¬ê·¸ì¸ìì ì¬ì©íì¬
ì»´íì¼ ê³¼ì ìì ì¤íë 커ì¤í
ë¹ë ë¨ê³ë¥¼ ì½ì
í ì ììµëë¤.
ìì¸í ë´ì©ì 문ì를 ì°¸ê³ íì¸ì.
ì¸ ê°ì§ tap ë©ìëì
ì´ë¥¼ ì ê³µíë í
ì ì´í´íë ê²ì ì¤ìí©ëë¤.
Tapableì íì¥í ê°ì²´(ì: ì»´íì¼ë¬),
í
, ê° í
ì íì
(ì: SyncHook)ì ëí´ ìê² ë ê²ì
ëë¤.
ì¬ì©ë í
ê³¼ ì ì©ë tap ë©ìëì ë°ë¼
íë¬ê·¸ì¸ì ë¤ìí ë°©ìì¼ë¡ ìëí ì ììµëë¤.
ìë ë°©ìì Tapableì´ ì ê³µíë í
ê³¼ ë°ì í ê´ë ¨ì´ ììµëë¤.
ì»´íì¼ë¬ í
ì ìí©ì ë°ë¼ Tapable í
ì íµí´
ì´ë¤ tap ë©ìëê° ì¬ì© ê°ë¥íì§ ì ì ììµëë¤.
ë°ë¼ì ì´ë¤ ì´ë²¤í¸ì tap íëëì ë°ë¼ íë¬ê·¸ì¸ì ë¤ë¥´ê² ëìí ì ììµëë¤.
ì를 ë¤ì´, ì»´íì¼ ë¨ê³ì íí¹ íë ê²½ì°
ë기ì tap ë©ìëë§ ì¬ì©í ì ììµëë¤.
compiler.hooks.compile.tap('MyPlugin', (params) => {
console.log('Synchronously tapping the compile hook.');
});ê·¸ë¬ë AsyncHookì íì©íë runì ê²½ì°
tapë¿ë§ ìëë¼ tapAsync ëë tapPromiseë ì¬ì©í ì ììµëë¤.
compiler.hooks.run.tapAsync(
'MyPlugin',
(source, target, routesList, callback) => {
console.log('Asynchronously tapping the run hook.');
callback();
}
);
compiler.hooks.run.tapPromise('MyPlugin', (source, target, routesList) => {
return new Promise((resolve) => setTimeout(resolve, 1000)).then(() => {
console.log('Asynchronously tapping the run hook with a delay.');
});
});
compiler.hooks.run.tapPromise(
'MyPlugin',
async (source, target, routesList) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log('Asynchronously tapping the run hook with a delay.');
}
);ì¦, ì»´íì¼ë¬ì íí¹ íë ë¤ìí ë°©ë²ì´ ìì¼ë©°,
ê° ë°©ë²ì íë¬ê·¸ì¸ì ì í©íë¤ë©´ ì¤íë ì ììµëë¤.
ë¤ë¥¸ íë¬ê·¸ì¸ì´ tap í ì ìëë¡ ì»´íì¼ì ì¬ì©ì ì ì í
ì ì ê³µíë ¤ë©´,
ë¤ìì ìíí´ì¼ í©ëë¤.
ì»´íì¼ í
ì ìí 모ë ë²ì WeakMapì ë§ëëë¤.
const compilationHooks = new WeakMap<Compilation, MyHooks>();
interface MyHooks {
custom: SyncHook<[number, string]>;
}íë¬ê·¸ì¸ì ì ì ë©ìë를 ë§ëëë¤.
static getCompilationHooks(compilation: Compilation) : MyHooks {
let hooks = compilationHooks.get(compilation);
if(hooks === undefined) {
compilationHooks.set(compilation, hooks = {
custom: new SyncHook()
});
}
return hooks;
}íë¬ê·¸ì¸ìì ìëì ê°ì´ í ì í¸ì¶í©ëë¤.
const hooks = MyPlugin.getCompilationHooks(compilation);
hooks.custom.call(1, 'hello');ë¤ë¥¸ íë¬ê·¸ì¸ë ì¬ì©ì ì ì í ì ì ê·¼í ì ììµëë¤.
import MyPlugin from 'my-plugin';
const hooks = MyPlugin.getCompilationHooks(compilation);
hooks.custom.tap('OtherPlugin', (n, s) => {
// magic
});ë¤ìí í
í´ëì¤ì ëì ë°©ìì ëí´ ìì¸í ìê³ ì¶ë¤ë©´
tapable 문ì를 ì°¸ê³ íì¸ì.
íë¬ê·¸ì¸ì 기본ì ì¼ë¡ ì§í ìí©ì stderrì ì¶ë ¥íë ProgressPluginì íµí´ ì§í ìí©ì íì¸í ì ììµëë¤. ì§íë¥ ì íì¸íë ¤ë©´ webpack CLI를 ì¤íí ë --progress ì¸ì를 ì ë¬íì¸ì.
ProgressPluginì reportProgress í¨ìì ë¤ë¥¸ ì¸ì를 ì ë¬íì¬ ë©ìì§ ì¶ë ¥ì 커ì¤í
í ì ììµëë¤.
ì§í ìí©ì íì¸í기 ìí´ìë context: true ìµì
ì ì¬ì©íì¬ í
ì tap í´ì¼ í©ëë¤.
compiler.hooks.emit.tapAsync(
{
name: 'MyPlugin',
context: true,
},
(context, compiler, callback) => {
const reportProgress = context && context.reportProgress;
if (reportProgress) reportProgress(0.95, 'Starting work');
setTimeout(() => {
if (reportProgress) reportProgress(0.95, 'Done work');
callback();
}, 1000);
}
);reportProgress í¨ìë ë¤ìê³¼ ê°ì ì¸ì를 ì¬ì©íì¬ í¸ì¶í ì ììµëë¤.
reportProgress(percentage, ...args);percentage: ì´ ì¸ìë ì¬ì©ëì§ ììµëë¤. ëì ProgressPluginì´ íì¬ í
ì 기ë°ì¼ë¡ ë°±ë¶ì¨ì ê³ì°í©ëë¤....args: ProgressPlugin í¸ë¤ë¬ë¡ ì ë¬ëë ììì ìì 문ìì´ì
ëë¤.ì»´íì¼ë¬ ë° ì»´íì¼ í
ì íì ì§í©ë§ì´ reportProgress를 ì§ìí©ëë¤. ì ì²´ 목ë¡ì ProgressPlugin를 ì°¸ê³ íì¸ì.
ë¡ê¹
APIë webpack 4.37 릴리ì¤ë¶í° ì¬ì©í ì ììµëë¤. stats ì¤ì ìì loggingì´ íì±íë ê²½ì° ëë infrastructure loggingì´ íì±íë ê²½ì° íë¬ê·¸ì¸ì ê° ë¡ê±° íì(stats, infrastructure)ì¼ë¡ ë©ìì§ë¥¼ ë¡ê¹
í ì ììµëë¤.
compilation.getLogger('PluginName')를 ì¬ì©íë ê²ì´ ì¢ìµëë¤. ë¡ê·¸ë íìì ë°ë¼ í¬ë§¤í
ëì´ Statsì ì ì¥ë©ëë¤. ë¡ê·¸ë ì¬ì©ìê° íí°ë§íê³ ë´ë³´ë¼ ì ììµëë¤.compiler.getInfrastructureLogger('PluginName')를 ì¬ì©í ì ììµëë¤. infrastructure ë¡ê¹
ì Statsì ì ì¥ëì§ ì기 ë문ì íìì´ ì§ì ëì§ ììµëë¤. ì¼ë°ì ì¼ë¡ ì½ì / ëìë³´ë / GUIì ì§ì 기ë¡ë©ëë¤. ì¬ì©ìê° íí°ë§ í ì ììµëë¤.compilation.getLogger ? compilation.getLogger('PluginName') : consoleì ì¬ì©íì¬ ì»´íì¼ ê°ì²´ìì getLogger ë©ìë를 ì§ìíì§ ìë ì´ì webpack ë²ì ì´ ì¬ì©ëë ê²½ì°ì ëí í´ë°±ì ì ê³µ í ì ììµëë¤.ì¬ì© ê°ë¥í 모ë ì»´íì¼ë¬ í
ë° íë¼ë¯¸í°ì ëí ìì¸í 목ë¡ì
ì»´íì¼ë¬ í
ì¹ì
ì ì°¸ê³ íì¸ì.