|
| 1 | +#!/usr/bin/env node |
| 2 | +/* |
| 3 | + * Copyright 2023 Adobe. All rights reserved. |
| 4 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 6 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 9 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 10 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 11 | + * governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +// this script is used instead of individual webpack-config.js configurations per action, |
| 15 | +// as these would require additional dependencies (e.g. loaders) to be installed as dev |
| 16 | +// dependency on the application level. |
| 17 | + |
| 18 | +import * as url from 'node:url'; |
| 19 | +import * as path from 'node:path'; |
| 20 | +import { fork } from 'node:child_process'; |
| 21 | +import { existsSync } from 'node:fs'; |
| 22 | +import { readdir } from 'node:fs/promises'; |
| 23 | + |
| 24 | +// read all directories, for those that have a webpack executable, run it with --mode production |
| 25 | + |
| 26 | +const webpackArgs = ['--mode', 'production']; |
| 27 | +const dirname = url.fileURLToPath(new URL('.', import.meta.url)); |
| 28 | +const children = await readdir(dirname, { withFileTypes: true }); |
| 29 | + |
| 30 | +await Promise.all(children.map(async (child) => { |
| 31 | + if (child.isDirectory) { |
| 32 | + const childDir = path.resolve(dirname, child.name); |
| 33 | + const webpackScript = path.resolve(childDir, 'node_modules/.bin/webpack'); |
| 34 | + if (existsSync(webpackScript)) { |
| 35 | + return new Promise((resolve, reject) => { |
| 36 | + // run the webpack script in the childDir |
| 37 | + // eslint-disable-next-line no-console |
| 38 | + console.log(`Bundling ${child.name} ... `); |
| 39 | + const webpackProcess = fork(webpackScript, webpackArgs, { cwd: childDir, silent: true }); |
| 40 | + webpackProcess.on('error', (err) => reject(err)); |
| 41 | + webpackProcess.on('exit', (code) => { |
| 42 | + if (code > 0) { |
| 43 | + reject(code); |
| 44 | + } else { |
| 45 | + resolve(); |
| 46 | + } |
| 47 | + }); |
| 48 | + }) |
| 49 | + .catch((err) => { |
| 50 | + // eslint-disable-next-line no-console |
| 51 | + console.log(`Failed bundling ${child.name}.`); |
| 52 | + throw err; |
| 53 | + }); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return Promise.resolve(); |
| 58 | +})); |
0 commit comments