Skip to content

Commit ca2d209

Browse files
committed
buildScripts/buildThemes: mode all => combine dist/esm & dist/prod #6727
1 parent be208d6 commit ca2d209

1 file changed

Lines changed: 69 additions & 61 deletions

File tree

buildScripts/buildThemes.mjs

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ if (programOpts.info) {
8989
}
9090
}
9191

92-
inquirer.prompt(questions).then(answers => {
92+
inquirer.prompt(questions).then(async answers => {
9393
const env = answers.env || programOpts.env || 'all',
9494
themes = answers.themes || programOpts.themes || 'all',
9595
insideNeo = programOpts.framework || false,
@@ -124,14 +124,14 @@ if (programOpts.info) {
124124
* @param {String} p
125125
* @param {String} mode development or production
126126
*/
127-
function buildEnv(p, mode) {
128-
parseScssFiles(getAllScssFiles(path.join(p, 'src')), mode, 'src');
127+
async function buildEnv(p, mode) {
128+
await parseScssFiles(getAllScssFiles(path.join(p, 'src')), mode, 'src');
129129

130-
themeFolders.forEach(themeFolder => {
130+
for (const themeFolder of themeFolders) {
131131
if (themes === 'all' || themes === themeFolder) {
132-
parseScssFiles(getAllScssFiles(path.join(p, themeFolder)), mode, themeFolder);
132+
await parseScssFiles(getAllScssFiles(path.join(p, themeFolder)), mode, themeFolder);
133133
}
134-
});
134+
}
135135
}
136136

137137
/**
@@ -247,17 +247,17 @@ if (programOpts.info) {
247247
* @param {String} mode development or production
248248
* @param {String} target src or a theme
249249
*/
250-
function parseScssFiles(files, mode, target) {
251-
let devMode = mode === 'development',
252-
map;
250+
async function parseScssFiles(files, mode, target) {
251+
let devMode = mode === 'development';
253252

254253
totalFiles[mode] += files.length;
255254

256-
files.forEach(file => {
255+
for (const file of files) {
257256
addItemToThemeMap(file, target);
258257

259258
let folderPath = path.resolve(cwd, `dist/${mode}/css/${target}/${file.relativePath}`),
260-
destPath = path.resolve(folderPath, `${file.name}.css`);
259+
destPath = path.resolve(folderPath, `${file.name}.css`),
260+
map;
261261

262262
let result = sass.compile(file.path, {
263263
outFile : destPath,
@@ -268,91 +268,99 @@ if (programOpts.info) {
268268
const plugins = [autoprefixer];
269269

270270
if (mode === 'esm' || mode === 'production') {
271-
plugins.push(cssnano)
271+
plugins.push(cssnano) // CSSNano works async only
272272
}
273273

274274
map = result.sourceMap;
275275

276-
postcss(plugins).process(result.css, {
276+
const postcssResult = await postcss(plugins).process(result.css, {
277277
from: file.path,
278278
to : destPath,
279279
map : !devMode ? null : {
280280
inline: false,
281281
prev : map && JSON.stringify(map)
282282
}
283-
}).then(postcssResult => {
284-
fs.mkdirpSync(folderPath);
285-
fileCount[mode]++;
283+
});
286284

287-
let currentFileNumber = fileCount.development + fileCount.esm + fileCount.production,
288-
map = postcssResult.map,
289-
processTime = (Math.round((new Date - startDate) * 100) / 100000).toFixed(2);
285+
fs.mkdirpSync(folderPath);
286+
fileCount[mode]++;
290287

291-
console.log('Writing file:', currentFileNumber, chalk.blue(`${processTime}s`), destPath);
288+
let currentFileNumber = fileCount.development + fileCount.esm + fileCount.production,
289+
processTime = (Math.round((new Date - startDate) * 100) / 100000).toFixed(2);
292290

293-
fs.writeFileSync(
294-
destPath,
295-
map ?
296-
`${postcssResult.css}\n\n/*# sourceMappingURL=${path.relative(path.dirname(destPath), postcssResult.opts.to + '.map')} */` :
297-
postcssResult.css,
298-
() => true
299-
);
291+
map = postcssResult.map;
300292

301-
if (map) {
302-
let mapString = map.toString(),
303-
jsonMap = JSON.parse(mapString),
304-
sources = jsonMap.sources;
305-
306-
// Somehow files contain both: a relative & an absolute file url
307-
// We only want to keep the relative ones.
308-
[...sources].forEach((item, index) => {
309-
if (!item.startsWith('../')) {
310-
sources.splice(index, 1);
311-
}
312-
});
293+
console.log('Writing file:', currentFileNumber, chalk.blue(`${processTime}s`), destPath);
313294

314-
map = JSON.stringify(jsonMap);
295+
fs.writeFileSync(
296+
destPath,
297+
map ?
298+
`${postcssResult.css}\n\n/*# sourceMappingURL=${path.relative(path.dirname(destPath), postcssResult.opts.to + '.map')} */` :
299+
postcssResult.css,
300+
() => true
301+
);
315302

316-
fs.writeFileSync(postcssResult.opts.to + '.map', map);
317-
}
303+
if (map) {
304+
let mapString = map.toString(),
305+
jsonMap = JSON.parse(mapString),
306+
sources = jsonMap.sources;
318307

319-
if (fileCount[mode] === totalFiles[mode]) {
320-
fs.writeFileSync(
321-
path.resolve(cwd, themeMapFile),
322-
JSON.stringify(themeMap, null, 0)
323-
);
308+
// Somehow files contain both: a relative & an absolute file url
309+
// We only want to keep the relative ones.
310+
[...sources].forEach((item, index) => {
311+
if (!item.startsWith('../')) {
312+
sources.splice(index, 1);
313+
}
314+
});
324315

325-
fs.mkdirpSync(path.join(cwd, '/dist/', mode, '/resources'), {
326-
recursive: true
327-
});
316+
map = JSON.stringify(jsonMap);
328317

329-
fs.writeFileSync(
330-
path.join(cwd, '/dist/', mode, themeMapFile),
331-
JSON.stringify(themeMap, null, 0)
332-
);
333-
}
334-
});
335-
});
318+
fs.writeFileSync(postcssResult.opts.to + '.map', map);
319+
}
320+
321+
if (fileCount[mode] === totalFiles[mode]) {
322+
fs.writeFileSync(
323+
path.resolve(cwd, themeMapFile),
324+
JSON.stringify(themeMap, null, 0)
325+
);
326+
327+
fs.mkdirpSync(path.join(cwd, '/dist/', mode, '/resources'), {recursive: true});
328+
329+
fs.writeFileSync(
330+
path.join(cwd, '/dist/', mode, themeMapFile),
331+
JSON.stringify(themeMap, null, 0)
332+
);
333+
}
334+
}
336335
}
337336

338337
themeMap = getThemeMap(themeMapFile);
339338

340339
// dist/development
341340
if (env === 'all' || env === 'dev') {
342341
console.log(chalk.blue(`${programName} starting dist/development`));
343-
buildEnv(scssPath, 'development');
342+
await buildEnv(scssPath, 'development');
344343
}
345344

346345
// dist/esm
347346
if (env === 'all' || env === 'esm') {
348347
console.log(chalk.blue(`${programName} starting dist/esm`));
349-
buildEnv(scssPath, 'esm');
348+
await buildEnv(scssPath, 'esm');
350349
}
351350

352351
// dist/production
353-
if (env === 'all' || env === 'prod') {
352+
if (env === 'prod') {
354353
console.log(chalk.blue(`${programName} starting dist/production`));
355-
buildEnv(scssPath, 'production');
354+
await buildEnv(scssPath, 'production');
355+
} else if (env === 'all') {
356+
// dist/esm & dist/production contain the same output, so we can just copy it over.
357+
const cssPath = path.join(cwd, '/dist/production/css');
358+
359+
fs.mkdirpSync(cssPath, {recursive: true});
360+
fs.mkdirpSync(path.join(cwd, '/dist/production/resources'), {recursive: true});
361+
362+
fs.copySync(path.join(cwd, '/dist/esm/css'), cssPath);
363+
fs.copySync(path.join(cwd, '/dist/esm/resources/theme-map.json'), path.join(cwd, '/dist/production/resources/theme-map.json'));
356364
}
357365
});
358366
}

0 commit comments

Comments
 (0)