@docusaurus/core 0.0.0-6080 → 0.0.0-6082

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,227 +0,0 @@
1
- "use strict";
2
- /**
3
- * Copyright (c) Facebook, Inc. and its affiliates.
4
- *
5
- * This source code is licensed under the MIT license found in the
6
- * LICENSE file in the root directory of this source tree.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.formatStatsErrorMessage = formatStatsErrorMessage;
10
- exports.printStatsWarnings = printStatsWarnings;
11
- exports.createStyleLoadersFactory = createStyleLoadersFactory;
12
- exports.getCustomBabelConfigFilePath = getCustomBabelConfigFilePath;
13
- exports.getBabelOptions = getBabelOptions;
14
- exports.createJsLoaderFactory = createJsLoaderFactory;
15
- exports.compile = compile;
16
- exports.getHttpsConfig = getHttpsConfig;
17
- const tslib_1 = require("tslib");
18
- const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
19
- const path_1 = tslib_1.__importDefault(require("path"));
20
- const crypto_1 = tslib_1.__importDefault(require("crypto"));
21
- const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
22
- const utils_1 = require("@docusaurus/utils");
23
- const webpack_1 = tslib_1.__importDefault(require("webpack"));
24
- const formatWebpackMessages_1 = tslib_1.__importDefault(require("react-dev-utils/formatWebpackMessages"));
25
- const faster_1 = require("../faster");
26
- const currentBundler_1 = require("./currentBundler");
27
- function formatStatsErrorMessage(statsJson) {
28
- if (statsJson?.errors?.length) {
29
- // TODO formatWebpackMessages does not print stack-traces
30
- // Also the error causal chain is lost here
31
- // We log the stacktrace inside serverEntry.tsx for now (not ideal)
32
- const { errors } = (0, formatWebpackMessages_1.default)(statsJson);
33
- return errors
34
- .map((str) => logger_1.default.red(str))
35
- .join(`\n\n${logger_1.default.yellow('--------------------------')}\n\n`);
36
- }
37
- return undefined;
38
- }
39
- function printStatsWarnings(statsJson) {
40
- if (statsJson?.warnings?.length) {
41
- statsJson.warnings?.forEach((warning) => {
42
- logger_1.default.warn(warning);
43
- });
44
- }
45
- }
46
- async function createStyleLoadersFactory({ currentBundler, }) {
47
- const CssExtractPlugin = await (0, currentBundler_1.getCSSExtractPlugin)({ currentBundler });
48
- return function getStyleLoaders(isServer, cssOptionsArg = {}) {
49
- const cssOptions = {
50
- // TODO turn esModule on later, see https://github.com/facebook/docusaurus/pull/6424
51
- esModule: false,
52
- ...cssOptionsArg,
53
- };
54
- // On the server we don't really need to extract/emit CSS
55
- // We only need to transform CSS module imports to a styles object
56
- if (isServer) {
57
- return cssOptions.modules
58
- ? [
59
- {
60
- loader: require.resolve('css-loader'),
61
- options: cssOptions,
62
- },
63
- ]
64
- : // Ignore regular CSS files
65
- [{ loader: require.resolve('null-loader') }];
66
- }
67
- return [
68
- {
69
- loader: CssExtractPlugin.loader,
70
- options: {
71
- esModule: true,
72
- },
73
- },
74
- {
75
- loader: require.resolve('css-loader'),
76
- options: cssOptions,
77
- },
78
- // TODO apart for configurePostCss(), do we really need this loader?
79
- // Note: using postcss here looks inefficient/duplicate
80
- // But in practice, it's not a big deal because css-loader also uses postcss
81
- // and is able to reuse the parsed AST from postcss-loader
82
- // See https://github.com/webpack-contrib/css-loader/blob/master/src/index.js#L159
83
- {
84
- // Options for PostCSS as we reference these options twice
85
- // Adds vendor prefixing based on your specified browser support in
86
- // package.json
87
- loader: require.resolve('postcss-loader'),
88
- options: {
89
- postcssOptions: {
90
- // Necessary for external CSS imports to work
91
- // https://github.com/facebook/create-react-app/issues/2677
92
- ident: 'postcss',
93
- plugins: [
94
- // eslint-disable-next-line global-require
95
- require('autoprefixer'),
96
- ],
97
- },
98
- },
99
- },
100
- ];
101
- };
102
- }
103
- async function getCustomBabelConfigFilePath(siteDir) {
104
- const customBabelConfigurationPath = path_1.default.join(siteDir, utils_1.BABEL_CONFIG_FILE_NAME);
105
- return (await fs_extra_1.default.pathExists(customBabelConfigurationPath))
106
- ? customBabelConfigurationPath
107
- : undefined;
108
- }
109
- function getBabelOptions({ isServer, babelOptions, } = {}) {
110
- if (typeof babelOptions === 'string') {
111
- return {
112
- babelrc: false,
113
- configFile: babelOptions,
114
- caller: { name: isServer ? 'server' : 'client' },
115
- };
116
- }
117
- return {
118
- ...(babelOptions ?? { presets: [require.resolve('../babel/preset')] }),
119
- babelrc: false,
120
- configFile: false,
121
- caller: { name: isServer ? 'server' : 'client' },
122
- };
123
- }
124
- const BabelJsLoaderFactory = ({ isServer, babelOptions, }) => {
125
- return {
126
- loader: require.resolve('babel-loader'),
127
- options: getBabelOptions({ isServer, babelOptions }),
128
- };
129
- };
130
- // Confusing: function that creates a function that creates actual js loaders
131
- // This is done on purpose because the js loader factory is a public API
132
- // It is injected in configureWebpack plugin lifecycle for plugin authors
133
- async function createJsLoaderFactory({ siteConfig, }) {
134
- const jsLoader = siteConfig.webpack?.jsLoader ?? 'babel';
135
- if (jsLoader instanceof Function &&
136
- siteConfig.future?.experimental_faster.swcJsLoader) {
137
- throw new Error("You can't use a custom webpack.jsLoader and experimental_faster.swcJsLoader at the same time");
138
- }
139
- if (jsLoader instanceof Function) {
140
- return ({ isServer }) => jsLoader(isServer);
141
- }
142
- if (siteConfig.future?.experimental_faster.swcJsLoader) {
143
- return (0, faster_1.importSwcJsLoaderFactory)();
144
- }
145
- if (jsLoader === 'babel') {
146
- return BabelJsLoaderFactory;
147
- }
148
- throw new Error(`Docusaurus bug: unexpected jsLoader value${jsLoader}`);
149
- }
150
- function compile(config) {
151
- return new Promise((resolve, reject) => {
152
- const compiler = (0, webpack_1.default)(config);
153
- compiler.run((err, stats) => {
154
- if (err) {
155
- logger_1.default.error(err.stack ?? err);
156
- if (err.details) {
157
- logger_1.default.error(err.details);
158
- }
159
- reject(err);
160
- }
161
- // Let plugins consume all the stats
162
- const errorsWarnings = stats?.toJson('errors-warnings');
163
- if (stats?.hasErrors()) {
164
- const statsErrorMessage = formatStatsErrorMessage(errorsWarnings);
165
- reject(new Error(`Failed to compile due to Webpack errors.\n${statsErrorMessage}`));
166
- }
167
- printStatsWarnings(errorsWarnings);
168
- // Webpack 5 requires calling close() so that persistent caching works
169
- // See https://github.com/webpack/webpack.js.org/pull/4775
170
- compiler.close((errClose) => {
171
- if (errClose) {
172
- logger_1.default.error(`Error while closing Webpack compiler: ${errClose}`);
173
- reject(errClose);
174
- }
175
- else {
176
- resolve(stats);
177
- }
178
- });
179
- });
180
- });
181
- }
182
- // Ensure the certificate and key provided are valid and if not
183
- // throw an easy to debug error
184
- function validateKeyAndCerts({ cert, key, keyFile, crtFile, }) {
185
- let encrypted;
186
- try {
187
- // publicEncrypt will throw an error with an invalid cert
188
- encrypted = crypto_1.default.publicEncrypt(cert, Buffer.from('test'));
189
- }
190
- catch (err) {
191
- logger_1.default.error `The certificate path=${crtFile} is invalid.`;
192
- throw err;
193
- }
194
- try {
195
- // privateDecrypt will throw an error with an invalid key
196
- crypto_1.default.privateDecrypt(key, encrypted);
197
- }
198
- catch (err) {
199
- logger_1.default.error `The certificate key path=${keyFile} is invalid.`;
200
- throw err;
201
- }
202
- }
203
- // Read file and throw an error if it doesn't exist
204
- async function readEnvFile(file, type) {
205
- if (!(await fs_extra_1.default.pathExists(file))) {
206
- throw new Error(`You specified ${type} in your env, but the file "${file}" can't be found.`);
207
- }
208
- return fs_extra_1.default.readFile(file);
209
- }
210
- // Get the https config
211
- // Return cert files if provided in env, otherwise just true or false
212
- async function getHttpsConfig() {
213
- const appDirectory = await fs_extra_1.default.realpath(process.cwd());
214
- const { SSL_CRT_FILE, SSL_KEY_FILE, HTTPS } = process.env;
215
- const isHttps = HTTPS === 'true';
216
- if (isHttps && SSL_CRT_FILE && SSL_KEY_FILE) {
217
- const crtFile = path_1.default.resolve(appDirectory, SSL_CRT_FILE);
218
- const keyFile = path_1.default.resolve(appDirectory, SSL_KEY_FILE);
219
- const config = {
220
- cert: await readEnvFile(crtFile, 'SSL_CRT_FILE'),
221
- key: await readEnvFile(keyFile, 'SSL_KEY_FILE'),
222
- };
223
- validateKeyAndCerts({ ...config, keyFile, crtFile });
224
- return config;
225
- }
226
- return isHttps;
227
- }