-
Notifications
You must be signed in to change notification settings - Fork 42
/
webpack.config.js
142 lines (133 loc) · 4.91 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
// Plugins and extra supporting libs for webpack you might want to extend in your labratory! 😍
const webpack = require('webpack');
// const memoryFs = require('memory-fs');
// const webpackSources = require('webpack-sources');
// const enhancedResolve = require('enhanced-resolve');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
/**
* @description
* Typedef's so that you can access type
* intellisense for each class and instance
* inside of your hooks and parameters
*
* Use @typedef `import("webpack/lib/WhateverClass")` if you want to pull in a class and its properties from webpack!!!
*
*/
const welcome = 'webpack-developer-kit';
console.log(welcome);
/** @typedef {import("webpack/lib/Compiler")} Compiler */
/** @typedef {import("webpack/lib/Compilation")} Compilation */
/** @typedef {import("webpack/lib/NormalModule")} NormalModule */
/** @typedef {import("webpack/lib/ContextModule")} ContextModule */
/** @typedef {import("webpack/lib/NormalModuleFactory")} NormalModuleFactory */
/** @typedef {import("webpack/lib/ContextModuleFactory")} ContextModuleFactory */
/** @typedef {import("webpack/lib/Module")} Module */
/** @typedef {import("webpack/lib/Chunk")} Chunk */
/** @typedef {import("webpack/lib/Parser")} Parser */
/** @typedef {import("webpack/lib/ChunkGroup")} ChunkGroup */
/** @typedef {import("webpack/lib/Dependency")} Dependency */
/** @typedef {import("@types/acorn").Node} Node */
/** @typedef {{normalModuleFactory: NormalModuleFactory, contextModuleFactory: ContextModuleFactory, compilationDependencies: Set<Dependency>}} CompilationParams */
module.exports = {
entry: {
car: ['./app/car.js'],
},
output: {
path: path.join(__dirname, '/dist'),
filename: '[name].bundle.js',
},
module: {
rules: [
// We are chaining the custom loader to babel loader.
// Purely optional but know that the `first` loader in the chain (babel in this case)
// must always return JavaScript (as it is then processed into the compilation)
{
test: /\.js$/,
oneOf: [
{
loaders: ['babel-loader', 'custom-loader'],
},
],
},
],
},
// This allows us to add resolving functionality for our custom loader
// It's used just like the resolve property and we are referencing the
// custom loader file.
resolveLoader: {
alias: {
'custom-loader': require.resolve('./custom-loader'),
},
},
plugins: [
// This function is the `apply` function if you were to create an external plugin
// Having it inline provides some nice conviences for debugging and development
function apply() {
/** @type {Compiler} */
var compiler = this;
compiler.hooks.compilation.tap('MyCustomInlinePlugin', compilationTapFunction);
},
new HtmlWebpackPlugin({
template: './app/index.html',
}),
],
devtool: 'source-map',
mode: 'none',
};
/**
* @param {Compilation} compilation
* @param {CompilationParams} params
*/
function compilationTapFunction(compilation, { normalModuleFactory, contextModuleFactory, _compilationDependencies }) {
compilation.hooks.afterOptimizeModules.tap('MyCustomInlinePlugin', modulesTapFunction);
compilation.hooks.afterOptimizeChunks.tap('MyCustomInlinePlugin', chunksTapFunction);
// Inline Tap Functions for ModuleFactories
normalModuleFactory.hooks.beforeResolve.tapAsync('MyCustomInlinePlugin', (_data, cb) => {
// debugger;
cb();
});
contextModuleFactory.hooks.afterResolve.tapAsync('MyCustomInlinePlugin', (_data, cb) => {
// debugger;
cb();
});
normalModuleFactory.hooks.parser.for('javascript/esm').tap('MyCustomInlinePlugin', parserTapFunction);
}
/**
* `afterOptimizeModules` tap function
* @param {(Module|ContextModule|NormalModule)[]} _modules
*/
function modulesTapFunction(_modules) {
// debugger;
}
/**
* `afterOptimizeModules` tap function
* @param {Chunk[]} _chunks
* @param {ChunkGroup[]} _chunkGroups
*/
function chunksTapFunction(_chunks, _chunkGroups) {
// debugger;
// uncomment the statement above and run the debug script to explore the data in this function/hook/tap
}
/**
*
* @param {Parser} parser
* @param {*} _parserOptions
*/
function parserTapFunction(parser, _parserOptions) {
parser.hooks.expression.for('this').tap('MyCustomInlinePlugin', parserExpressionTopLevelThisTapFunction);
// debugger;
// uncomment the statement above and run the debug script to explore the data in this function/hook/tap
}
/**
* When you tap into a parser hook, you are getting an event for when
* the parser comes across a sepcific expression/syntax. This function is broken out so you can see type
* support for the acorn.Node instance.
* @param {Node} _node
*/
function parserExpressionTopLevelThisTapFunction(_node) {
// debugger;
// console.log(_node.type);
// uncomment these statements above and run the debug script to explore the data in this function/hook/tap
}