UNPKG

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