@openedx/paragon 23.23.1 → 23.23.2
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.
- package/dist/theme-urls.json +6 -6
- package/lib/__tests__/build-scss.test.js +90 -2
- package/lib/build-scss.js +17 -10
- package/package.json +1 -1
package/dist/theme-urls.json
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"themeUrls": {
|
|
3
|
+
"core": {
|
|
4
|
+
"paths": {
|
|
5
|
+
"default": "./core.css",
|
|
6
|
+
"minified": "./core.min.css"
|
|
7
|
+
}
|
|
8
|
+
},
|
|
3
9
|
"defaults": {
|
|
4
10
|
"light": "light"
|
|
5
11
|
},
|
|
@@ -10,12 +16,6 @@
|
|
|
10
16
|
"minified": "./light.min.css"
|
|
11
17
|
}
|
|
12
18
|
}
|
|
13
|
-
},
|
|
14
|
-
"core": {
|
|
15
|
-
"paths": {
|
|
16
|
-
"default": "./core.css",
|
|
17
|
-
"minified": "./core.min.css"
|
|
18
|
-
}
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
}
|
|
@@ -3,6 +3,7 @@ const path = require('path');
|
|
|
3
3
|
const { spawn } = require('node:child_process');
|
|
4
4
|
const os = require('os');
|
|
5
5
|
const sass = require('sass');
|
|
6
|
+
const postCSS = require('postcss');
|
|
6
7
|
|
|
7
8
|
const buildScssCommand = require('../build-scss');
|
|
8
9
|
const { updateParagonThemeOutput } = require('../build-scss');
|
|
@@ -21,8 +22,25 @@ jest.mock('ora', () => jest.fn(() => ({
|
|
|
21
22
|
start: jest.fn().mockReturnThis(),
|
|
22
23
|
succeed: jest.fn().mockReturnThis(),
|
|
23
24
|
fail: jest.fn().mockReturnThis(),
|
|
25
|
+
stop: jest.fn().mockReturnThis(),
|
|
24
26
|
})));
|
|
25
27
|
|
|
28
|
+
const processedResult = {
|
|
29
|
+
css: '.processed { color: black; }',
|
|
30
|
+
map: {
|
|
31
|
+
toString: () => 'minified sourcemap content',
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// The minify pass is the only `process` call that asks for an external source map.
|
|
36
|
+
const isMinifyCall = (options) => options.map && options.map.inline === false;
|
|
37
|
+
|
|
38
|
+
const mockPostCss = (processImpl) => {
|
|
39
|
+
postCSS.mockImplementation(() => ({ process: jest.fn(processImpl) }));
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const writtenFiles = () => fs.writeFileSync.mock.calls.map(([filePath]) => filePath);
|
|
43
|
+
|
|
26
44
|
describe('updateParagonThemeOutput', () => {
|
|
27
45
|
it('should add core theme correctly', () => {
|
|
28
46
|
const input = {
|
|
@@ -104,6 +122,8 @@ describe('updateParagonThemeOutput', () => {
|
|
|
104
122
|
describe('buildScssCommand', () => {
|
|
105
123
|
beforeEach(() => {
|
|
106
124
|
jest.clearAllMocks();
|
|
125
|
+
mockPostCss(() => Promise.resolve(processedResult));
|
|
126
|
+
fs.writeFileSync.mockImplementation(() => {});
|
|
107
127
|
fs.existsSync.mockReturnValue(false);
|
|
108
128
|
fs.readdirSync.mockReturnValue([
|
|
109
129
|
{ name: 'light', isDirectory: () => true },
|
|
@@ -117,7 +137,7 @@ describe('buildScssCommand', () => {
|
|
|
117
137
|
});
|
|
118
138
|
});
|
|
119
139
|
|
|
120
|
-
it('should compile core and theme stylesheets', () => {
|
|
140
|
+
it('should compile core and theme stylesheets', async () => {
|
|
121
141
|
const args = [
|
|
122
142
|
'--corePath=styles/core.scss',
|
|
123
143
|
'--themesPath=styles/themes',
|
|
@@ -125,7 +145,7 @@ describe('buildScssCommand', () => {
|
|
|
125
145
|
'--defaultThemeVariants=light',
|
|
126
146
|
];
|
|
127
147
|
|
|
128
|
-
buildScssCommand(args);
|
|
148
|
+
await buildScssCommand(args);
|
|
129
149
|
|
|
130
150
|
expect(sass.compile).toHaveBeenCalledWith(
|
|
131
151
|
expect.stringContaining('core.scss'),
|
|
@@ -160,6 +180,74 @@ describe('buildScssCommand', () => {
|
|
|
160
180
|
expect.objectContaining({ withFileTypes: true }),
|
|
161
181
|
);
|
|
162
182
|
});
|
|
183
|
+
it('should resolve only after every stylesheet, minified file and source map is written', async () => {
|
|
184
|
+
mockPostCss((css, options) => {
|
|
185
|
+
if (isMinifyCall(options)) {
|
|
186
|
+
return new Promise((resolve) => { setTimeout(() => resolve(processedResult), 20); });
|
|
187
|
+
}
|
|
188
|
+
return Promise.resolve(processedResult);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
await buildScssCommand(['--outDir=dist']);
|
|
192
|
+
|
|
193
|
+
expect(writtenFiles()).toEqual(expect.arrayContaining(
|
|
194
|
+
['core', 'light', 'dark'].flatMap((name) => [
|
|
195
|
+
`dist/${name}.css`,
|
|
196
|
+
`dist/${name}.min.css`,
|
|
197
|
+
`dist/${name}.css.map`,
|
|
198
|
+
]),
|
|
199
|
+
));
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('should list core and every theme variant in theme-urls.json', async () => {
|
|
203
|
+
const files = {};
|
|
204
|
+
fs.writeFileSync.mockImplementation((filePath, content) => { files[filePath] = content; });
|
|
205
|
+
fs.existsSync.mockImplementation((filePath) => filePath in files);
|
|
206
|
+
fs.readFileSync.mockImplementation((filePath) => files[filePath]);
|
|
207
|
+
|
|
208
|
+
await buildScssCommand(['--outDir=dist', '--defaultThemeVariants=light']);
|
|
209
|
+
|
|
210
|
+
const { themeUrls } = JSON.parse(files['dist/theme-urls.json']);
|
|
211
|
+
expect(themeUrls.core.paths.minified).toBe('./core.min.css');
|
|
212
|
+
expect(Object.keys(themeUrls.variants)).toEqual(['light', 'dark']);
|
|
213
|
+
expect(themeUrls.defaults).toEqual({ light: 'light' });
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('should reject when Sass compilation fails', async () => {
|
|
217
|
+
sass.compile.mockImplementation(() => { throw new Error('Undefined variable.'); });
|
|
218
|
+
|
|
219
|
+
await expect(buildScssCommand(['--outDir=dist'])).rejects.toThrow('Undefined variable.');
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('should reject when PostCSS processing fails', async () => {
|
|
223
|
+
mockPostCss(() => Promise.reject(new Error('PostCSS failed')));
|
|
224
|
+
|
|
225
|
+
await expect(buildScssCommand(['--outDir=dist'])).rejects.toThrow('PostCSS failed');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('should reject when minification fails', async () => {
|
|
229
|
+
mockPostCss((css, options) => (
|
|
230
|
+
isMinifyCall(options) ? Promise.reject(new Error('Minify failed')) : Promise.resolve(processedResult)
|
|
231
|
+
));
|
|
232
|
+
|
|
233
|
+
await expect(buildScssCommand(['--outDir=dist'])).rejects.toThrow('Minify failed');
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('should reject when the output directory does not exist', async () => {
|
|
237
|
+
fs.writeFileSync.mockImplementation((filePath) => {
|
|
238
|
+
throw new Error(`ENOENT: no such file or directory, open '${filePath}'`);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
await expect(buildScssCommand(['--outDir=missing'])).rejects.toThrow('ENOENT');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('should not build theme variants after the core stylesheet fails', async () => {
|
|
245
|
+
mockPostCss(() => Promise.reject(new Error('PostCSS failed')));
|
|
246
|
+
|
|
247
|
+
await expect(buildScssCommand(['--outDir=dist'])).rejects.toThrow();
|
|
248
|
+
|
|
249
|
+
expect(sass.compile).toHaveBeenCalledTimes(1);
|
|
250
|
+
});
|
|
163
251
|
});
|
|
164
252
|
|
|
165
253
|
describe('SIGINT handling', () => {
|
package/lib/build-scss.js
CHANGED
|
@@ -75,6 +75,7 @@ const updateParagonThemeOutput = ({
|
|
|
75
75
|
* @param {string} outDir - indicates where to output compiled files
|
|
76
76
|
* @param {boolean} isThemeVariant - indicates whether the stylesheet is a theme variant
|
|
77
77
|
* @param {boolean} isDefaultThemeVariant - indicates whether the stylesheet is a default theme variant
|
|
78
|
+
* @returns {Promise<void>} Resolves once all files are written; rejects if any step fails.
|
|
78
79
|
*/
|
|
79
80
|
const compileAndWriteStyleSheets = ({
|
|
80
81
|
name,
|
|
@@ -109,17 +110,17 @@ const compileAndWriteStyleSheets = ({
|
|
|
109
110
|
];
|
|
110
111
|
|
|
111
112
|
const postCSSCompilation = ora(`Compilation for ${capitalize(name)} stylesheet...`).start();
|
|
112
|
-
postCSS(commonPostCssPlugins)
|
|
113
|
+
return postCSS(commonPostCssPlugins)
|
|
113
114
|
.process(compiledStyleSheet.css, { from: stylesPath, map: false })
|
|
114
115
|
.then((result) => {
|
|
115
116
|
postCSSCompilation.succeed(`Successfully compiled ${capitalize(name)} theme stylesheet`);
|
|
116
117
|
fs.writeFileSync(`${outDir}/${name}.css`, result.css);
|
|
117
|
-
postCSS([postCSSMinify()])
|
|
118
|
-
.process(result.css, { from: `${name}.css`, map: { inline: false } })
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
118
|
+
return postCSS([postCSSMinify()])
|
|
119
|
+
.process(result.css, { from: `${name}.css`, map: { inline: false } });
|
|
120
|
+
})
|
|
121
|
+
.then((minifiedResult) => {
|
|
122
|
+
fs.writeFileSync(`${outDir}/${name}.css.map`, minifiedResult.map.toString());
|
|
123
|
+
fs.writeFileSync(`${outDir}/${name}.min.css`, minifiedResult.css);
|
|
123
124
|
|
|
124
125
|
const hasExistingParagonThemeOutput = fs.existsSync(`${outDir}/${paragonThemeOutputFilename}`);
|
|
125
126
|
let paragonThemeOutput;
|
|
@@ -147,7 +148,11 @@ const compileAndWriteStyleSheets = ({
|
|
|
147
148
|
ora().succeed(chalk.underline.bold.green(`Successfully built stylesheet for ${capitalize(name)} theme!\n`));
|
|
148
149
|
})
|
|
149
150
|
.catch((error) => {
|
|
151
|
+
if (postCSSCompilation.isSpinning) {
|
|
152
|
+
postCSSCompilation.stop();
|
|
153
|
+
}
|
|
150
154
|
ora().fail(chalk.bold(`Failed to build stylesheets for ${capitalize(name)}: ${error.message}`));
|
|
155
|
+
throw error;
|
|
151
156
|
});
|
|
152
157
|
};
|
|
153
158
|
|
|
@@ -156,7 +161,7 @@ const compileAndWriteStyleSheets = ({
|
|
|
156
161
|
*
|
|
157
162
|
* @param {Array<string>} commandArgs - Command line arguments for building SCSS stylesheets.
|
|
158
163
|
*/
|
|
159
|
-
function buildScssCommand(commandArgs) {
|
|
164
|
+
async function buildScssCommand(commandArgs) {
|
|
160
165
|
const defaultArgs = {
|
|
161
166
|
corePath: path.resolve(process.cwd(), 'styles/scss/core/core.scss'),
|
|
162
167
|
excludeCore: false,
|
|
@@ -175,7 +180,7 @@ function buildScssCommand(commandArgs) {
|
|
|
175
180
|
|
|
176
181
|
// Core CSS
|
|
177
182
|
if (!excludeCore) {
|
|
178
|
-
compileAndWriteStyleSheets({
|
|
183
|
+
await compileAndWriteStyleSheets({
|
|
179
184
|
name: 'core',
|
|
180
185
|
stylesPath: corePath,
|
|
181
186
|
outDir,
|
|
@@ -186,8 +191,10 @@ function buildScssCommand(commandArgs) {
|
|
|
186
191
|
const themeDirs = fs.readdirSync(themesPath, { withFileTypes: true })
|
|
187
192
|
.filter((item) => item.isDirectory());
|
|
188
193
|
|
|
194
|
+
// Built one at a time so the first failure stops the build.
|
|
189
195
|
for (const themeDir of themeDirs) {
|
|
190
|
-
|
|
196
|
+
// eslint-disable-next-line no-await-in-loop
|
|
197
|
+
await compileAndWriteStyleSheets({
|
|
191
198
|
name: themeDir.name,
|
|
192
199
|
stylesPath: `${themesPath}/${themeDir.name}/index.css`,
|
|
193
200
|
outDir,
|