UNPKG

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