extract linkLocalRspack logic into a reusable module and integrate it across test helpers and scripts.

This commit is contained in:
Nacho Codoñer
2026-03-05 18:16:10 +01:00
parent e96d9934a3
commit 651b87ece8
3 changed files with 85 additions and 45 deletions

View File

@@ -18,6 +18,7 @@
const path = require('path');
const fs = require('fs-extra');
const execa = require('execa');
const { linkLocalRspack } = require('./link-rspack');
const REPO_ROOT = path.resolve(__dirname, '../../..');
const METEOR_EXECUTABLE = path.join(REPO_ROOT, 'meteor');
@@ -363,12 +364,8 @@ async function setupFromApp(appName, destDir, { isMonorepo = false, force = fals
...execEnv,
});
log.step('Updating Meteor npm dependencies...');
await execa(METEOR_EXECUTABLE, ['update', '--npm'], {
cwd: meteorAppDir,
stdio: 'inherit',
...execEnv,
});
log.step('Linking local @meteorjs/rspack...');
await linkLocalRspack(meteorAppDir, { env: envVars });
if (isMonorepo) {
log.step('Running meteor npm install at root level...');
@@ -430,12 +427,8 @@ async function setupFromSkeleton(skeletonName, destDir, { force = false } = {})
...execEnv,
});
log.step('Updating Meteor npm dependencies...');
await execa(METEOR_EXECUTABLE, ['update', '--npm'], {
cwd: destDir,
stdio: 'inherit',
...execEnv,
});
log.step('Linking local @meteorjs/rspack...');
await linkLocalRspack(destDir, { env: envVars });
log.step('Running meteor npm install...');
await execa(METEOR_EXECUTABLE, ['npm', 'install'], { cwd: destDir, stdio: 'inherit', ...execEnv });

View File

@@ -0,0 +1,77 @@
#!/usr/bin/env node
/**
* Links the local npm-packages/meteor-rspack into a Meteor app so it runs
* against the latest dev version.
*
* Steps:
* 1. Run `meteor update --npm` in the app
* 2. Install the matching @rspack/core and @rspack/cli versions into the
* local meteor-rspack package (read from packages/rspack/lib/constants.js)
* 3. Install `ignore-loader` in the app
* 4. `npm link` the local meteor-rspack into the app
*
*/
const path = require('path');
const fs = require('fs');
const execa = require('execa');
const REPO_ROOT = path.resolve(__dirname, '..', '..', '..');
const METEOR_EXECUTABLE = path.join(REPO_ROOT, 'meteor');
const RSPACK_PACKAGE_DIR = path.join(REPO_ROOT, 'npm-packages', 'meteor-rspack');
const CONSTANTS_PATH = path.join(REPO_ROOT, 'packages', 'rspack', 'lib', 'constants.js');
async function linkLocalRspack(appDir, { env } = {}) {
const execOpts = env ? { env: { ...process.env, ...env } } : {};
console.log(`Running meteor update --npm in ${appDir}...`);
await execa(METEOR_EXECUTABLE, ['update', '--npm'], {
cwd: appDir,
stdio: 'inherit',
...execOpts,
});
const constantsContent = fs.readFileSync(CONSTANTS_PATH, 'utf8');
const rspackVersionMatch = constantsContent.match(
/DEFAULT_RSPACK_VERSION\s*=\s*['"]([^'"]+)['"]/
);
const rspackVersion = rspackVersionMatch?.[1];
if (rspackVersion) {
console.log(`Installing @rspack/core@${rspackVersion} and @rspack/cli@${rspackVersion}...`);
await execa(
'npm',
[
'install',
`@rspack/core@${rspackVersion}`,
`@rspack/cli@${rspackVersion}`,
'--no-save',
'--no-package-lock',
],
{ cwd: RSPACK_PACKAGE_DIR }
);
}
console.log('Installing ignore-loader in the app...');
await execa('npm', ['install', 'ignore-loader', '--save'], { cwd: appDir });
console.log(`Linking local meteor-rspack from ${RSPACK_PACKAGE_DIR}...`);
await execa('npm', ['link', RSPACK_PACKAGE_DIR], { cwd: appDir });
console.log('Local meteor-rspack linked successfully.');
}
module.exports = { linkLocalRspack, REPO_ROOT, METEOR_EXECUTABLE, RSPACK_PACKAGE_DIR };
// CLI mode
if (require.main === module) {
const appDir = process.argv[2];
if (!appDir) {
console.error('Usage: node link-rspack.js <appDir>');
process.exit(1);
}
linkLocalRspack(path.resolve(appDir)).catch(err => {
console.error(err.message);
process.exit(1);
});
}

View File

@@ -38,41 +38,11 @@ const npmLinkLocalRspack = process.env.NPM_LINK_RSPACK !== 'false';
const WAIT_ON = isCI ? 2000 : 500;
const { linkLocalRspack: _linkLocalRspack } = require('./scripts/link-rspack');
async function linkLocalRspack(appDir) {
if (!npmLinkLocalRspack) return;
const repoRoot = path.resolve(process.cwd(), '..', '..');
const meteorBin = path.join(repoRoot, "meteor");
console.log(`Running meteor update --npm in ${appDir}...`);
(await execa(meteorBin, ["update", "--npm"], {
cwd: appDir,
stdio: "inherit",
}));
const rspackPackageDir = path.join(repoRoot, 'npm-packages', 'meteor-rspack');
const constantsPath = path.join(repoRoot, 'packages', 'rspack', 'lib', 'constants.js');
const constantsContent = await fs.readFile(constantsPath, 'utf8');
const rspackVersionMatch = constantsContent.match(/DEFAULT_RSPACK_VERSION\s*=\s*['"]([^'"]+)['"]/);
const rspackVersion = rspackVersionMatch?.[1];
if (rspackVersion) {
console.log(`Installing @rspack/core@${rspackVersion} and @rspack/cli@${rspackVersion}...`);
await execa(
"npm",
[
"install",
`@rspack/core@${rspackVersion}`,
`@rspack/cli@${rspackVersion}`,
"--no-save",
"--no-package-lock",
],
{ cwd: rspackPackageDir }
);
}
console.log(`Installing ignore-loader in the app...`);
await execa('npm', ['install', 'ignore-loader', '--save'], { cwd: appDir });
console.log(`Linking local meteor-rspack from ${rspackPackageDir}...`);
await execa('npm', ['link', rspackPackageDir], { cwd: appDir });
console.log('Local meteor-rspack linked successfully.');
await _linkLocalRspack(appDir);
}
/**