UNPKG

7.91 kBJavaScriptView Raw
1"use strict";
2/* -----------------------------------------------------------------------------
3| Copyright (c) Jupyter Development Team.
4| Distributed under the terms of the Modified BSD License.
5|----------------------------------------------------------------------------*/
6var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7 if (k2 === undefined) k2 = k;
8 var desc = Object.getOwnPropertyDescriptor(m, k);
9 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10 desc = { enumerable: true, get: function() { return m[k]; } };
11 }
12 Object.defineProperty(o, k2, desc);
13}) : (function(o, m, k, k2) {
14 if (k2 === undefined) k2 = k;
15 o[k2] = m[k];
16}));
17var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18 Object.defineProperty(o, "default", { enumerable: true, value: v });
19}) : function(o, v) {
20 o["default"] = v;
21});
22var __importStar = (this && this.__importStar) || function (mod) {
23 if (mod && mod.__esModule) return mod;
24 var result = {};
25 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
26 __setModuleDefault(result, mod);
27 return result;
28};
29var __importDefault = (this && this.__importDefault) || function (mod) {
30 return (mod && mod.__esModule) ? mod : { "default": mod };
31};
32Object.defineProperty(exports, "__esModule", { value: true });
33exports.WPPlugin = void 0;
34const duplicate_package_checker_webpack_plugin_1 = __importDefault(require("duplicate-package-checker-webpack-plugin"));
35const fs = __importStar(require("fs-extra"));
36const license_webpack_plugin_1 = require("license-webpack-plugin");
37// From
38// https://github.com/webpack/webpack/blob/95120bdf98a01649740b104bebc426b0123651ce/lib/WatchIgnorePlugin.js
39const IGNORE_TIME_ENTRY = 'ignore';
40var WPPlugin;
41(function (WPPlugin) {
42 /**
43 * A WebPack Plugin that copies the assets to the static directory
44 */
45 class FrontEndPlugin {
46 constructor(buildDir, staticDir) {
47 this.buildDir = buildDir;
48 this.staticDir = staticDir;
49 this._first = true;
50 }
51 apply(compiler) {
52 compiler.hooks.afterEmit.tap('FrontEndPlugin', () => {
53 // bail if no staticDir
54 if (!this.staticDir) {
55 return;
56 }
57 // ensure a clean static directory on the first emit
58 if (this._first && fs.existsSync(this.staticDir)) {
59 fs.removeSync(this.staticDir);
60 }
61 this._first = false;
62 fs.copySync(this.buildDir, this.staticDir);
63 });
64 }
65 }
66 WPPlugin.FrontEndPlugin = FrontEndPlugin;
67 /**
68 * A helper class for the WatchIgnoreFilterPlugin. This is a close copy of
69 * (the non-exported) webpack.IgnoringWatchFileSystem
70 */
71 class FilterIgnoringWatchFileSystem {
72 constructor(wfs, ignored) {
73 this.wfs = wfs;
74 // ignored should be a callback function that filters the build files
75 this.ignored = ignored;
76 }
77 watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) {
78 files = Array.from(files);
79 dirs = Array.from(dirs);
80 const notIgnored = (path) => !this.ignored(path);
81 const ignoredFiles = files.filter(this.ignored);
82 const ignoredDirs = dirs.filter(this.ignored);
83 const watcher = this.wfs.watch(files.filter(notIgnored), dirs.filter(notIgnored), missing, startTime, options, (err, fileTimestamps, dirTimestamps, changedFiles, removedFiles) => {
84 if (err)
85 return callback(err);
86 for (const path of ignoredFiles) {
87 fileTimestamps.set(path, IGNORE_TIME_ENTRY);
88 }
89 for (const path of ignoredDirs) {
90 dirTimestamps.set(path, IGNORE_TIME_ENTRY);
91 }
92 callback(err, fileTimestamps, dirTimestamps, changedFiles, removedFiles);
93 }, callbackUndelayed);
94 return {
95 close: () => watcher.close(),
96 pause: () => watcher.pause(),
97 getContextTimeInfoEntries: () => {
98 const dirTimestamps = watcher.getContextTimeInfoEntries();
99 for (const path of ignoredDirs) {
100 dirTimestamps.set(path, IGNORE_TIME_ENTRY);
101 }
102 return dirTimestamps;
103 },
104 getFileTimeInfoEntries: () => {
105 const fileTimestamps = watcher.getFileTimeInfoEntries();
106 for (const path of ignoredFiles) {
107 fileTimestamps.set(path, IGNORE_TIME_ENTRY);
108 }
109 return fileTimestamps;
110 }
111 };
112 }
113 }
114 /**
115 * A WebPack Plugin that ignores files files that are filtered
116 * by a callback during a `--watch` build
117 */
118 class FilterWatchIgnorePlugin {
119 constructor(ignored) {
120 this.ignored = ignored;
121 }
122 apply(compiler) {
123 compiler.hooks.afterEnvironment.tap('FilterWatchIgnorePlugin', () => {
124 compiler.watchFileSystem = new FilterIgnoringWatchFileSystem(compiler.watchFileSystem, this.ignored);
125 });
126 }
127 }
128 WPPlugin.FilterWatchIgnorePlugin = FilterWatchIgnorePlugin;
129 class NowatchDuplicatePackageCheckerPlugin extends duplicate_package_checker_webpack_plugin_1.default {
130 apply(compiler) {
131 const options = this.options;
132 compiler.hooks.run.tap('NowatchDuplicatePackageCheckerPlugin', compiler => {
133 const p = new duplicate_package_checker_webpack_plugin_1.default(options);
134 p.apply(compiler);
135 });
136 }
137 }
138 WPPlugin.NowatchDuplicatePackageCheckerPlugin = NowatchDuplicatePackageCheckerPlugin;
139 /**
140 * A well-known filename for third-party license information.
141 *
142 * ### Note
143 * If an alternate JupyterLab-based ecosystem wanted to implement a different
144 * name, they may _still_ need to handle the presence of this file if reusing
145 * any core files or extensions.
146 *
147 * If multiple files are found by `jupyterlab_server, their `packages` will
148 * be concatenated.
149 */
150 WPPlugin.DEFAULT_LICENSE_REPORT_FILENAME = 'third-party-licenses.json';
151 /**
152 * a plugin that creates a predictable, machine-readable report of licenses for
153 * all modules included in this build
154 */
155 class JSONLicenseWebpackPlugin extends license_webpack_plugin_1.LicenseWebpackPlugin {
156 constructor(pluginOptions = {}) {
157 super({
158 outputFilename: WPPlugin.DEFAULT_LICENSE_REPORT_FILENAME,
159 ...pluginOptions,
160 renderLicenses: modules => this.renderLicensesJSON(modules),
161 perChunkOutput: false
162 });
163 }
164 /** render an SPDX-like record */
165 renderLicensesJSON(modules) {
166 const report = { packages: [] };
167 modules.sort((left, right) => (left.name < right.name ? -1 : 1));
168 for (const mod of modules) {
169 report.packages.push({
170 name: mod.name || '',
171 versionInfo: mod.packageJson.version || '',
172 licenseId: mod.licenseId || '',
173 extractedText: mod.licenseText || ''
174 });
175 }
176 return JSON.stringify(report, null, 2);
177 }
178 }
179 WPPlugin.JSONLicenseWebpackPlugin = JSONLicenseWebpackPlugin;
180})(WPPlugin = exports.WPPlugin || (exports.WPPlugin = {}));
181//# sourceMappingURL=webpack-plugins.js.map
\No newline at end of file