forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack-plugins.ts
174 lines (150 loc) · 4.43 KB
/
webpack-plugins.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import * as webpack from 'webpack';
import * as fs from 'fs-extra';
export namespace WPPlugin {
/**
* A WebPack Plugin that copies the assets to the static directory
*/
export class FrontEndPlugin {
constructor(buildDir: string, staticDir: string) {
this.buildDir = buildDir;
this.staticDir = staticDir;
this._first = true;
}
apply(compiler: any) {
compiler.hooks.afterEmit.tap('FrontEndPlugin', () => {
// bail if no staticDir
if (!this.staticDir) {
return;
}
// ensure a clean static directory on the first emit
if (this._first && fs.existsSync(this.staticDir)) {
fs.removeSync(this.staticDir);
}
this._first = false;
fs.copySync(this.buildDir, this.staticDir);
});
}
buildDir: string;
staticDir: string;
private _first: boolean;
}
/**
* A WebPack Plugin that ignores files that are filtered by a callback
*/
export class FilterIgnorePlugin extends webpack.IgnorePlugin {
constructor(ignored: (path: string) => boolean) {
super({});
// ignored should be a callback function that filters the build files
this.ignored = ignored;
}
checkIgnore(result: any): any | null {
if (!result) {
return result;
}
return this.ignored(result.resource) ? result : null;
}
ignored: (path: string) => boolean;
}
/**
* A helper class for the WatchIgnoreFilterPlugin. This is a close copy of
* (the non-exported) webpack.IgnoringWatchFileSystem
*/
class FilterIgnoringWatchFileSystem {
constructor(wfs: any, ignored: (path: string) => boolean) {
this.wfs = wfs;
// ignored should be a callback function that filters the build files
this.ignored = ignored;
}
watch(
files: any,
dirs: any,
missing: any,
startTime: any,
options: any,
callback: any,
callbackUndelayed: any
) {
const notIgnored = (path: string) => !this.ignored(path);
const ignoredFiles = files.filter(this.ignored);
const ignoredDirs = dirs.filter(this.ignored);
const watcher = this.wfs.watch(
files.filter(notIgnored),
dirs.filter(notIgnored),
missing,
startTime,
options,
(
err: any,
filesModified: any,
dirsModified: any,
missingModified: any,
fileTimestamps: any,
dirTimestamps: any,
removedFiles: any
) => {
if (err) {
return callback(err);
}
for (const path of ignoredFiles) {
fileTimestamps.set(path, 1);
}
for (const path of ignoredDirs) {
dirTimestamps.set(path, 1);
}
callback(
err,
filesModified,
dirsModified,
missingModified,
fileTimestamps,
dirTimestamps,
removedFiles
);
},
callbackUndelayed
);
return {
close: () => watcher.close(),
pause: () => watcher.pause(),
getContextTimestamps: () => {
const dirTimestamps = watcher.getContextTimestamps();
for (const path of ignoredDirs) {
dirTimestamps.set(path, 1);
}
return dirTimestamps;
},
getFileTimestamps: () => {
const fileTimestamps = watcher.getFileTimestamps();
for (const path of ignoredFiles) {
fileTimestamps.set(path, 1);
}
return fileTimestamps;
}
};
}
ignored: (path: string) => boolean;
wfs: any;
}
/**
* A WebPack Plugin that ignores files files that are filtered
* by a callback during a `--watch` build
*/
export class FilterWatchIgnorePlugin {
constructor(ignored: (path: string) => boolean) {
this.ignored = ignored;
}
apply(compiler: any) {
compiler.hooks.afterEnvironment.tap('FilterWatchIgnorePlugin', () => {
compiler.watchFileSystem = new FilterIgnoringWatchFileSystem(
compiler.watchFileSystem,
this.ignored
);
});
}
ignored: (path: string) => boolean;
}
}