Skip to content

Commit 452bcf3

Browse files
committed
splitting builds into separate processes to avoid memory issues
1 parent 9a70683 commit 452bcf3

File tree

3 files changed

+68
-43
lines changed

3 files changed

+68
-43
lines changed

build.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* This file is used to compile the TypeScript files in the assets/src directory
3+
* of each package.
4+
*
5+
* It allows each package to spawn its own rollup process, which is necessary
6+
* to keep memory usage down.
7+
*/
8+
const { spawnSync } = require('child_process');
9+
const glob = require('glob');
10+
11+
const files = [
12+
// custom handling for StimulusBundle
13+
'src/StimulusBundle/assets/src/loader.ts',
14+
'src/StimulusBundle/assets/src/controllers.ts',
15+
...glob.sync('src/*/assets/src/*controller.ts'),
16+
];
17+
18+
files.forEach((file) => {
19+
const result = spawnSync('node', [
20+
'node_modules/.bin/rollup',
21+
'-c',
22+
'--environment',
23+
`INPUT_FILE:${file}`,
24+
], {
25+
stdio: 'inherit',
26+
shell: true
27+
});
28+
29+
if (result.error) {
30+
console.error(`Error compiling ${file}:`, result.error);
31+
}
32+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"src/*/assets"
55
],
66
"scripts": {
7-
"build": "yarn rollup -c",
7+
"build": "node build.js",
88
"test": "yarn workspaces run jest",
99
"lint": "yarn workspaces run eslint src test",
1010
"format": "prettier src/*/assets/src/*.ts src/*/assets/test/*.js {,src/*/}*.{json,md} --write",

rollup.config.js

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -61,47 +61,40 @@ const moveTypescriptDeclarationsPlugin = (packagePath) => ({
6161
}
6262
});
6363

64-
const files = [
65-
// custom handling for StimulusBundle
66-
'src/StimulusBundle/assets/src/loader.ts',
67-
'src/StimulusBundle/assets/src/controllers.ts',
68-
...glob.sync('src/*/assets/src/*controller.ts'),
69-
]
70-
module.exports = files.map((file) => {
71-
const packageRoot = path.join(file, '..', '..');
72-
const packagePath = path.join(packageRoot, 'package.json');
73-
const packageData = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
74-
const peerDependencies = [
75-
'@hotwired/stimulus',
76-
...(packageData.peerDependencies ? Object.keys(packageData.peerDependencies) : [])
77-
];
64+
const file = process.env.INPUT_FILE;
65+
const packageRoot = path.join(file, '..', '..');
66+
const packagePath = path.join(packageRoot, 'package.json');
67+
const packageData = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
68+
const peerDependencies = [
69+
'@hotwired/stimulus',
70+
...(packageData.peerDependencies ? Object.keys(packageData.peerDependencies) : [])
71+
];
7872

79-
// custom handling for StimulusBundle
80-
if (file.includes('StimulusBundle/assets/src/loader.ts')) {
81-
peerDependencies.push('./controllers.js');
82-
}
73+
// custom handling for StimulusBundle
74+
if (file.includes('StimulusBundle/assets/src/loader.ts')) {
75+
peerDependencies.push('./controllers.js');
76+
}
8377

84-
return {
85-
input: file,
86-
output: {
87-
file: path.join(packageRoot, 'dist', path.basename(file, '.ts') + '.js'),
88-
format: 'esm',
89-
},
90-
external: peerDependencies,
91-
plugins: [
92-
resolve(),
93-
typescript({
94-
filterRoot: packageRoot,
95-
include: ['src/**/*.ts'],
96-
compilerOptions: {
97-
outDir: 'dist',
98-
declaration: true,
99-
emitDeclarationOnly: true,
100-
}
101-
}),
102-
commonjs(),
103-
wildcardExternalsPlugin(peerDependencies),
104-
moveTypescriptDeclarationsPlugin(packageRoot),
105-
],
106-
};
107-
});
78+
module.exports = {
79+
input: file,
80+
output: {
81+
file: path.join(packageRoot, 'dist', path.basename(file, '.ts') + '.js'),
82+
format: 'esm',
83+
},
84+
external: peerDependencies,
85+
plugins: [
86+
resolve(),
87+
typescript({
88+
filterRoot: packageRoot,
89+
include: ['src/**/*.ts'],
90+
compilerOptions: {
91+
outDir: 'dist',
92+
declaration: true,
93+
emitDeclarationOnly: true,
94+
}
95+
}),
96+
commonjs(),
97+
wildcardExternalsPlugin(peerDependencies),
98+
moveTypescriptDeclarationsPlugin(packageRoot),
99+
],
100+
};

0 commit comments

Comments
 (0)