Skip to content

Commit

Permalink
Make entrypoint paths platform independant (#135)
Browse files Browse the repository at this point in the history
* Make entrypoint paths platform independant
  • Loading branch information
ojkelly authored Oct 26, 2021
1 parent e47d119 commit 3993b67
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 32 deletions.
12 changes: 5 additions & 7 deletions e2e/lambda-project/lambda-project.integration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import * as fs from "fs-extra";
import * as os from "os";
import * as crypto from "crypto";
Expand Down Expand Up @@ -57,6 +58,7 @@ test("run lambda-project after bundling without compression", async () => {
// THEN
expect(fs.existsSync(path.join(bundleOutput, "package.json"))).toEqual(true);
expect(fs.existsSync(path.join(bundleOutput, ".pnp.cjs"))).toEqual(true);
expect(fs.existsSync(path.join(bundleOutput, "entrypoint.js"))).toEqual(true);
expect(fs.existsSync(path.join(bundleOutput, ".yarn"))).toEqual(true);
expect(fs.readdirSync(path.join(bundleOutput, ".yarn", "cache"))).toEqual([
".gitignore",
Expand All @@ -67,13 +69,9 @@ test("run lambda-project after bundling without compression", async () => {
// Now run the bundled code to see that it works!
// lambda-project's dependencies look like this: lambda -> lib -> uglify-js
// Calling the lambda's api handler tests the uglify-js transitive dependency
const execResult = execa.sync(
"node",
["--require", "./.pnp.cjs", "packages/lambda/dist/api.js"],
{
cwd: bundleOutput,
}
);
const execResult = execa.sync("node", ["entrypoint.js"], {
cwd: bundleOutput,
});

const responsePayload = JSON.parse(execResult.stdout);

Expand Down
1 change: 1 addition & 0 deletions e2e/lambda-project/packages/lambda/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "lambda",
"version": "1.0.0",
"main": "dist/index.js",
"scripts": {
"build": "tsc --project tsconfig.dev.json"
},
Expand Down
11 changes: 0 additions & 11 deletions e2e/lambda-project/packages/lambda/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,3 @@ export async function handler(): Promise<lambda.APIGatewayProxyResult> {
};
}
}

// When run, this file executes the handler and outputs the result.
if (require.main === module) {
handler()
.then((res) => {
console.log(JSON.stringify(res));
})
.catch((e) => {
console.error(e);
});
}
11 changes: 11 additions & 0 deletions e2e/lambda-project/packages/lambda/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { handler } from "./api";

// When run, this file executes the handler and outputs the result.
handler()
.then((res) => {
// eslint-disable-next-line no-console
console.log(JSON.stringify(res));
})
.catch((e) => {
console.error(e);
});
2 changes: 1 addition & 1 deletion packages/plugins/plugin-build/src/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class Build extends BaseCommand {

if (typeof this.buildTarget[0] === "string") {
targetDirectory =
`${configuration.projectCwd}${path.sep}${this.buildTarget[0]}` as PortablePath;
`${configuration.projectCwd}${path.posix.sep}${this.buildTarget[0]}` as PortablePath;
}

const { project, workspace: cwdWorkspace } = await Project.find(
Expand Down
14 changes: 9 additions & 5 deletions packages/plugins/plugin-build/src/commands/bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,16 @@ export default class Bundler extends BaseCommand {
// Add entrypoint
// TODO: make mainFile configurable
const mainFile =
workspace.relativeCwd + path.sep + workspace?.manifest?.raw?.main;
workspace.relativeCwd +
path.posix.sep +
workspace?.manifest?.raw?.main;

// TODO: check if it's .pnp.js or .pnp.cjs
// https://github.com/yarnpkg/berry/pull/2286
const pnp = `./.pnp.cjs`;
const pnp = `.pnp.cjs`;

xfs.writeFilePromise(
`${tmpDir}${path.sep}entrypoint.js` as PortablePath,
`${tmpDir}${path.posix.sep}entrypoint.js` as PortablePath,
generateEntrypointFile(mainFile, pnp)
);
}
Expand Down Expand Up @@ -452,9 +454,11 @@ export default class Bundler extends BaseCommand {
const generateEntrypointFile = (main: string, pnp: string): string => `
"use strict";
const pnp = require("${pnp}").setup();
const path = require("path");
const index = require("./${main}");
const pnp = require(path.normalize(path.resolve( __dirname, "${pnp}"))).setup();
const index = require(path.normalize(path.resolve( __dirname,"${main}")));
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-build/src/commands/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default class Test extends BaseCommand {

if (typeof this.runTarget[0] === "string") {
targetDirectory =
`${configuration.projectCwd}${path.sep}${this.runTarget[0]}` as PortablePath;
`${configuration.projectCwd}${path.posix.sep}${this.runTarget[0]}` as PortablePath;
}

const { project, workspace: cwdWorkspace } = await Project.find(
Expand Down
14 changes: 7 additions & 7 deletions packages/plugins/plugin-build/src/supervisor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,15 +529,15 @@ class RunSupervisor {
typeof workspace?.manifest.raw["yarn.build"].output === "string"
) {
ignore =
`${dir}${path.sep}${workspace?.manifest.raw["yarn.build"].output}` as PortablePath;
`${dir}${path.posix.sep}${workspace?.manifest.raw["yarn.build"].output}` as PortablePath;
} else if (this.pluginConfiguration.folders.output) {
ignore =
`${dir}${path.sep}${this.pluginConfiguration.folders.output}` as PortablePath;
`${dir}${path.posix.sep}${this.pluginConfiguration.folders.output}` as PortablePath;
} else if (workspace?.manifest.raw.main) {
ignore = `${dir}${path.sep}${
ignore = `${dir}${path.posix.sep}${
workspace?.manifest.raw.main.substring(
0,
workspace?.manifest.raw.main.lastIndexOf(path.sep)
workspace?.manifest.raw.main.lastIndexOf(path.posix.sep)
) as PortablePath
}` as PortablePath;
}
Expand All @@ -547,10 +547,10 @@ class RunSupervisor {
typeof workspace?.manifest.raw["yarn.build"].input === "string"
) {
srcDir =
`${dir}${path.sep}${workspace?.manifest.raw["yarn.build"].input}` as PortablePath;
`${dir}${path.posix.sep}${workspace?.manifest.raw["yarn.build"].input}` as PortablePath;
} else if (this.pluginConfiguration.folders.input) {
srcDir =
`${dir}${path.sep}${this.pluginConfiguration.folders.input}` as PortablePath;
`${dir}${path.posix.sep}${this.pluginConfiguration.folders.input}` as PortablePath;
}

// If the source directory is the package root, remove `/.` from the end of
Expand Down Expand Up @@ -1172,7 +1172,7 @@ const getLastModifiedForFolder = async (

await Promise.all(
files.map(async (file) => {
const filePath = `${folder}${path.sep}${file}` as PortablePath;
const filePath = `${folder}${path.posix.sep}${file}` as PortablePath;

if (ignore && filePath.startsWith(ignore)) {
return;
Expand Down

0 comments on commit 3993b67

Please sign in to comment.