Skip to content

Commit c3ae67e

Browse files
committed
fix: Prevent npm from symlinking local packages
1 parent 75f5ad1 commit c3ae67e

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/__tests__/__snapshots__/index.test.js.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`@ryaninvents/plugin-bundle-dependencies should correctly resolve local dependencies 1`] = `
4+
Array [
5+
"test-library",
6+
"test-library/index.js",
7+
"test-library/package.json",
8+
"test-library/src",
9+
"test-library/src/index.js",
10+
]
11+
`;
12+
313
exports[`@ryaninvents/plugin-bundle-dependencies should create a zip file with node_modules 1`] = `
414
Array [
515
"is-sorted",

src/__tests__/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ describe('@ryaninvents/plugin-bundle-dependencies', () => {
107107
.not.toThrow();
108108
const contents = String(await fs.readFile(`${mainProj.workingDir}/node_modules/test-library/index.js`));
109109
expect(contents).toBe('// Hello world');
110+
111+
await execa('npm', ['run', 'build'], {
112+
env: { NODE_ENV: 'production' },
113+
cwd: mainProj.workingDir
114+
});
115+
const archiveEntries = await new Promise((resolve, reject) => {
116+
archive.list(`${mainProj.workingDir}/pkg/dist-dependencies.zip`, (err, results) => {
117+
if (err) return reject(err);
118+
return resolve(results.map(file => file.getPath()).sort());
119+
});
120+
});
121+
expect(archiveEntries).toMatchSnapshot();
110122
}, 60e3);
111123

112124
it('should create a zip file with node_modules', async () => {

src/util.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import fs from 'fs-extra';
55

66
const LOCAL_PACKAGE_PREFIX = 'file:';
77

8+
export async function npmPack (pkgPath, workingDir) {
9+
const { stdout } = await execa('npm', ['pack', '--json', pkgPath], {
10+
cwd: workingDir
11+
});
12+
const [output] = JSON.parse(stdout);
13+
return resolve(workingDir, output.filename);
14+
}
15+
816
export function getPackageManagerCommand ({ cwd, options }) {
917
if (options.packageManager) {
1018
const { packageManager } = options;
@@ -34,12 +42,16 @@ export function getPackageManagerCommand ({ cwd, options }) {
3442

3543
export async function createManifest (tempdir, { cwd, manifest }) {
3644
const updatedManifest = { ...manifest };
45+
const originalDeps = manifest.dependencies || {};
46+
updatedManifest.dependencies = { ...originalDeps };
3747

38-
Object.keys(manifest.dependencies).forEach((pkgName) => {
39-
const pkgVersion = manifest.dependencies[pkgName];
48+
await Promise.all(Object.keys(originalDeps).map(async (pkgName) => {
49+
const pkgVersion = originalDeps[pkgName];
4050
if (!pkgVersion.startsWith(LOCAL_PACKAGE_PREFIX)) return;
41-
updatedManifest.dependencies[pkgName] = `${LOCAL_PACKAGE_PREFIX}${resolve(cwd, pkgVersion.slice(LOCAL_PACKAGE_PREFIX.length))}`;
42-
});
51+
const absolutePath = resolve(cwd, pkgVersion.slice(LOCAL_PACKAGE_PREFIX.length));
52+
const bundledAbsolutePath = await npmPack(absolutePath, cwd);
53+
updatedManifest.dependencies[pkgName] = `${LOCAL_PACKAGE_PREFIX}${bundledAbsolutePath}`;
54+
}));
4355

4456
await fs.writeFile(resolve(tempdir, 'package.json'), JSON.stringify(updatedManifest, null, 2));
4557
}

0 commit comments

Comments
 (0)