UNPKG

4.99 kBJavaScriptView Raw
1"use strict";
2/*-----------------------------------------------------------------------------
3| Copyright (c) Jupyter Development Team.
4| Distributed under the terms of the Modified BSD License.
5|----------------------------------------------------------------------------*/
6var __importStar = (this && this.__importStar) || function (mod) {
7 if (mod && mod.__esModule) return mod;
8 var result = {};
9 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
10 result["default"] = mod;
11 return result;
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14const webpack = __importStar(require("webpack"));
15const fs = __importStar(require("fs-extra"));
16var WPPlugin;
17(function (WPPlugin) {
18 /**
19 * A WebPack Plugin that copies the assets to the static directory
20 */
21 class FrontEndPlugin {
22 constructor(buildDir, staticDir) {
23 this.buildDir = buildDir;
24 this.staticDir = staticDir;
25 this._first = true;
26 }
27 apply(compiler) {
28 compiler.hooks.afterEmit.tap('FrontEndPlugin', () => {
29 // bail if no staticDir
30 if (!this.staticDir) {
31 return;
32 }
33 // ensure a clean static directory on the first emit
34 if (this._first && fs.existsSync(this.staticDir)) {
35 fs.removeSync(this.staticDir);
36 }
37 this._first = false;
38 fs.copySync(this.buildDir, this.staticDir);
39 });
40 }
41 }
42 WPPlugin.FrontEndPlugin = FrontEndPlugin;
43 /**
44 * A WebPack Plugin that ignores files that are filtered by a callback
45 */
46 class FilterIgnorePlugin extends webpack.IgnorePlugin {
47 constructor(ignored) {
48 super({});
49 // ignored should be a callback function that filters the build files
50 this.ignored = ignored;
51 }
52 checkIgnore(result) {
53 if (!result) {
54 return result;
55 }
56 return this.ignored(result.resource) ? result : null;
57 }
58 }
59 WPPlugin.FilterIgnorePlugin = FilterIgnorePlugin;
60 /**
61 * A helper class for the WatchIgnoreFilterPlugin. This is a close copy of
62 * (the non-exported) webpack.IgnoringWatchFileSystem
63 */
64 class FilterIgnoringWatchFileSystem {
65 constructor(wfs, ignored) {
66 this.wfs = wfs;
67 // ignored should be a callback function that filters the build files
68 this.ignored = ignored;
69 }
70 watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) {
71 const notIgnored = (path) => !this.ignored(path);
72 const ignoredFiles = files.filter(this.ignored);
73 const ignoredDirs = dirs.filter(this.ignored);
74 const watcher = this.wfs.watch(files.filter(notIgnored), dirs.filter(notIgnored), missing, startTime, options, (err, filesModified, dirsModified, missingModified, fileTimestamps, dirTimestamps, removedFiles) => {
75 if (err) {
76 return callback(err);
77 }
78 for (const path of ignoredFiles) {
79 fileTimestamps.set(path, 1);
80 }
81 for (const path of ignoredDirs) {
82 dirTimestamps.set(path, 1);
83 }
84 callback(err, filesModified, dirsModified, missingModified, fileTimestamps, dirTimestamps, removedFiles);
85 }, callbackUndelayed);
86 return {
87 close: () => watcher.close(),
88 pause: () => watcher.pause(),
89 getContextTimestamps: () => {
90 const dirTimestamps = watcher.getContextTimestamps();
91 for (const path of ignoredDirs) {
92 dirTimestamps.set(path, 1);
93 }
94 return dirTimestamps;
95 },
96 getFileTimestamps: () => {
97 const fileTimestamps = watcher.getFileTimestamps();
98 for (const path of ignoredFiles) {
99 fileTimestamps.set(path, 1);
100 }
101 return fileTimestamps;
102 }
103 };
104 }
105 }
106 /**
107 * A WebPack Plugin that ignores files files that are filtered
108 * by a callback during a `--watch` build
109 */
110 class FilterWatchIgnorePlugin {
111 constructor(ignored) {
112 this.ignored = ignored;
113 }
114 apply(compiler) {
115 compiler.hooks.afterEnvironment.tap('FilterWatchIgnorePlugin', () => {
116 compiler.watchFileSystem = new FilterIgnoringWatchFileSystem(compiler.watchFileSystem, this.ignored);
117 });
118 }
119 }
120 WPPlugin.FilterWatchIgnorePlugin = FilterWatchIgnorePlugin;
121})(WPPlugin = exports.WPPlugin || (exports.WPPlugin = {}));
122//# sourceMappingURL=webpack-plugins.js.map
\No newline at end of file