UNPKG

7.98 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.Build = void 0;
34const mini_css_extract_plugin_1 = __importDefault(require("mini-css-extract-plugin"));
35const mini_svg_data_uri_1 = __importDefault(require("mini-svg-data-uri"));
36const fs = __importStar(require("fs-extra"));
37const glob = __importStar(require("glob"));
38const path = __importStar(require("path"));
39/**
40 * A namespace for JupyterLab build utilities.
41 */
42var Build;
43(function (Build) {
44 /**
45 * Ensures that the assets of plugin packages are populated for a build.
46 *
47 * @ Returns An array of lab extension config data.
48 */
49 function ensureAssets(options) {
50 var _a;
51 const { output, schemaOutput = output, themeOutput = output, packageNames } = options;
52 const themeConfig = [];
53 const packagePaths = ((_a = options.packagePaths) === null || _a === void 0 ? void 0 : _a.slice()) || [];
54 let cssImports = [];
55 packageNames.forEach(name => {
56 packagePaths.push(path.dirname(require.resolve(path.join(name, 'package.json'))));
57 });
58 packagePaths.forEach(packagePath => {
59 const packageDataPath = require.resolve(path.join(packagePath, 'package.json'));
60 const packageDir = path.dirname(packageDataPath);
61 const data = fs.readJSONSync(packageDataPath);
62 const name = data.name;
63 const extension = normalizeExtension(data);
64 const { schemaDir, themePath } = extension;
65 // We prefer the styleModule key if it exists, falling back to
66 // the normal style key.
67 if (typeof data.styleModule === 'string') {
68 cssImports.push(`${name}/${data.styleModule}`);
69 }
70 else if (typeof data.style === 'string') {
71 cssImports.push(`${name}/${data.style}`);
72 }
73 // Handle schemas.
74 if (schemaDir) {
75 const schemas = glob.sync(path.join(path.join(packageDir, schemaDir), '*'));
76 const destination = path.join(schemaOutput, 'schemas', name);
77 // Remove the existing directory if necessary.
78 if (fs.existsSync(destination)) {
79 try {
80 const oldPackagePath = path.join(destination, 'package.json.orig');
81 const oldPackageData = fs.readJSONSync(oldPackagePath);
82 if (oldPackageData.version === data.version) {
83 fs.removeSync(destination);
84 }
85 }
86 catch (e) {
87 fs.removeSync(destination);
88 }
89 }
90 // Make sure the schema directory exists.
91 fs.mkdirpSync(destination);
92 // Copy schemas.
93 schemas.forEach(schema => {
94 const file = path.basename(schema);
95 fs.copySync(schema, path.join(destination, file));
96 });
97 // Write the package.json file for future comparison.
98 fs.copySync(path.join(packageDir, 'package.json'), path.join(destination, 'package.json.orig'));
99 }
100 if (!themePath) {
101 return;
102 }
103 themeConfig.push({
104 mode: 'production',
105 entry: {
106 index: path.join(packageDir, themePath)
107 },
108 output: {
109 path: path.resolve(path.join(themeOutput, 'themes', name)),
110 // we won't use these JS files, only the extracted CSS
111 filename: '[name].js',
112 hashFunction: 'sha256'
113 },
114 module: {
115 rules: [
116 {
117 test: /\.css$/,
118 use: [mini_css_extract_plugin_1.default.loader, 'css-loader']
119 },
120 {
121 test: /\.svg/,
122 type: 'asset/inline',
123 generator: {
124 dataUrl: (content) => (0, mini_svg_data_uri_1.default)(content.toString())
125 }
126 },
127 {
128 test: /\.(cur|png|jpg|gif|ttf|woff|woff2|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
129 type: 'asset'
130 }
131 ]
132 },
133 plugins: [
134 new mini_css_extract_plugin_1.default({
135 // Options similar to the same options in webpackOptions.output
136 // both options are optional
137 filename: '[name].css',
138 chunkFilename: '[id].css'
139 })
140 ]
141 });
142 });
143 cssImports.sort((a, b) => a.localeCompare(b));
144 const styleContents = `/* This is a generated file of CSS imports */
145/* It was generated by @jupyterlab/builder in Build.ensureAssets() */
146
147${cssImports.map(x => `import '${x}';`).join('\n')}
148`;
149 const stylePath = path.join(output, 'style.js');
150 // Make sure the output dir exists before writing to it.
151 if (!fs.existsSync(output)) {
152 fs.mkdirSync(output);
153 }
154 fs.writeFileSync(stylePath, styleContents, {
155 encoding: 'utf8'
156 });
157 return themeConfig;
158 }
159 Build.ensureAssets = ensureAssets;
160 /**
161 * Returns JupyterLab extension metadata from a module.
162 */
163 function normalizeExtension(module) {
164 let { jupyterlab, main, name } = module;
165 main = main || 'index.js';
166 if (!jupyterlab) {
167 throw new Error(`Module ${name} does not contain JupyterLab metadata.`);
168 }
169 let { extension, mimeExtension, schemaDir, themePath } = jupyterlab;
170 extension = extension === true ? main : extension;
171 mimeExtension = mimeExtension === true ? main : mimeExtension;
172 if (extension && mimeExtension && extension === mimeExtension) {
173 const message = 'extension and mimeExtension cannot be the same export.';
174 throw new Error(message);
175 }
176 return { extension, mimeExtension, schemaDir, themePath };
177 }
178 Build.normalizeExtension = normalizeExtension;
179})(Build = exports.Build || (exports.Build = {}));
180//# sourceMappingURL=build.js.map
\No newline at end of file