Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typeModule experimental flag #586

Merged
merged 18 commits into from
Jan 15, 2024
5 changes: 5 additions & 0 deletions .changeset/cuddly-windows-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preconstruct/cli": patch
---

Add `typeModule` experimental flag
5 changes: 5 additions & 0 deletions .changeset/fast-jobs-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preconstruct/cli": patch
---

Add `distInRoot` experimental flag
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"@babel/preset-env": "^7.7.7",
"@babel/preset-flow": "^7.10.4",
"@babel/preset-typescript": "^7.9.0",
"@changesets/changelog-github": "^0.4.1",
"@changesets/cli": "^2.17.0",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@manypkg/cli": "^0.17.0",
"@types/babel__code-frame": "^7.0.1",
"@types/fs-extra": "^9.0.6",
Expand Down
155 changes: 154 additions & 1 deletion packages/cli/src/__tests__/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ test("exports field with worker condition", async () => {
},
},
}),
"src/index.js": "console.log(1)",
"src/index.js": 'console.log("1")',
})
);
await dev(tmpPath);
Expand Down Expand Up @@ -687,3 +687,156 @@ test("dev command entrypoint", async () => {
expect(stdout.toString().split("\n")).toEqual(["message from something", ""]);
expect(code).toBe(0);
});

test("multiple entrypoints", async () => {
let dir = await testdir({
"package.json": JSON.stringify({
name: "multiple-entrypoints",
main: "dist/multiple-entrypoints.cjs.js",
module: "dist/multiple-entrypoints.esm.js",
exports: {
".": {
types: {
import: "./dist/multiple-entrypoints.cjs.mjs",
default: "./dist/multiple-entrypoints.cjs.js",
},
module: "./dist/multiple-entrypoints.esm.js",
import: "./dist/multiple-entrypoints.cjs.mjs",
default: "./dist/multiple-entrypoints.cjs.js",
},
"./multiply": {
types: {
import: "./dist/multiple-entrypoints-multiply.cjs.mjs",
default: "./dist/multiple-entrypoints-multiply.cjs.js",
},
module: "./dist/multiple-entrypoints-multiply.esm.js",
import: "./dist/multiple-entrypoints-multiply.cjs.mjs",
default: "./dist/multiple-entrypoints-multiply.cjs.js",
},
"./package.json": "./package.json",
},
preconstruct: {
exports: {
importConditionDefaultExport: "default",
},
entrypoints: ["index.ts", "multiply.ts"],
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
importsConditions: true,
distInRoot: true,
},
},
}),
"multiply/package.json": JSON.stringify({
main: "../dist/multiple-entrypoints-multiply.cjs.js",
module: "../dist/multiple-entrypoints-multiply.esm.js",
}),
"src/index.ts": js`
export let sum = (a, b) => a + b;
export default "a";
`,
"src/multiply.ts": js`
export let multiply = (a, b) => a * b;
`,
"something.js": js`
const { multiply } = require("multiple-entrypoints/multiply");
console.log(multiply(2, 2) + "");
`,
});

await dev(dir);

let { code, stdout, stderr } = await spawn("node", [
path.join(dir, "something"),
]);
expect(stderr.toString()).toBe("");
expect(stdout.toString().split("\n")).toEqual(["4", ""]);
expect(code).toBe(0);
});

test("type: module", async () => {
let dir = await testdir({
"package.json": JSON.stringify({
name: "multiple-entrypoints",
type: "module",
exports: {
".": "./dist/multiple-entrypoints.js",
"./multiply": "./dist/multiple-entrypoints-multiply.js",
"./package.json": "./package.json",
},
preconstruct: {
exports: true,
entrypoints: ["index.ts", "multiply.ts"],
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
importsConditions: true,
distInRoot: true,
typeModule: true,
},
},
}),
"src/index.ts": js`
export let a = "a";
`,
"src/multiply.ts": js`
export let b = "b";
`,
});

await dev(dir);

expect(await getFiles(dir, ["**/dist/**/*"])).toMatchInlineSnapshot(`
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/multiple-entrypoints-multiply.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export * from "../src/multiply";
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibXVsdGlwbGUtZW50cnlwb2ludHMtbXVsdGlwbHkuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9tdWx0aXBseS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/multiple-entrypoints-multiply.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
⎯ symlink to src/multiply.ts
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/multiple-entrypoints.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export * from "../src/index";
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibXVsdGlwbGUtZW50cnlwb2ludHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/multiple-entrypoints.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
⎯ symlink to src/index.ts
`);
});

test("type: module running", async () => {
let dir = await testdir({
"package.json": JSON.stringify({
name: "multiple-entrypoints",
type: "module",
exports: {
".": "./dist/multiple-entrypoints.js",
"./multiply": "./dist/multiple-entrypoints-multiply.js",
"./package.json": "./package.json",
},
preconstruct: {
exports: true,
entrypoints: ["index.js", "multiply.js"],
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
importsConditions: true,
distInRoot: true,
typeModule: true,
},
},
}),
"src/index.js": js`
export let a = "a";
`,
"src/multiply.js": js`
export let b = "b";
`,
"runtime-blah.mjs": js`
import { b } from "multiple-entrypoints/multiply";
console.log(b);
`,
});

await dev(dir);

let { code, stdout, stderr } = await spawn("node", [
path.join(dir, "runtime-blah.mjs"),
]);
expect(stderr.toString()).toBe("");
expect(stdout.toString().split("\n")).toEqual(["b", ""]);
expect(code).toBe(0);
});
45 changes: 45 additions & 0 deletions packages/cli/src/__tests__/fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,3 +1217,48 @@ test("nothing is written when another package's fixing throws an error", async (
}
`);
});

test("type: module removes package.json", async () => {
let dir = await testdir({
"package.json": JSON.stringify({
name: "multiple-entrypoints",
type: "module",
exports: {
".": "./dist/multiple-entrypoints.js",
"./multiply": "./dist/multiple-entrypoints-multiply.js",
"./package.json": "./package.json",
},
preconstruct: {
exports: true,
entrypoints: ["index.ts", "multiply.ts"],
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
importsConditions: true,
distInRoot: true,
typeModule: true,
},
},
}),
"multiply/package.json": JSON.stringify({
name: "multiple-entrypoints/multiply",
main: "dist/multiple-entrypoints-multiply.cjs.js",
module: "dist/multiple-entrypoints-multiply.esm.js",
}),
"src/index.ts": js`
export let a = "a";
`,
"src/multiply.ts": js`
export let b = "b";
`,
"runtime-blah.mjs": js`
import { b } from "multiple-entrypoints/multiply";
console.log(b);
`,
});

await fix(dir);

expect(await getFiles(dir, ["**/package.json"])).toMatchInlineSnapshot(`
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ package.json ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
{"name":"multiple-entrypoints","type":"module","exports":{".":"./dist/multiple-entrypoints.js","./multiply":"./dist/multiple-entrypoints-multiply.js","./package.json":"./package.json"},"preconstruct":{"exports":true,"entrypoints":["index.ts","multiply.ts"],"___experimentalFlags_WILL_CHANGE_IN_PATCH":{"importsConditions":true,"distInRoot":true,"typeModule":true}}}
`);
});
48 changes: 48 additions & 0 deletions packages/cli/src/__tests__/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
testdir,
js,
repoNodeModules,
getFiles,
} from "../../test-utils";
import { confirms as _confirms } from "../messages";
import { JSONValue } from "../utils";
Expand Down Expand Up @@ -1030,3 +1031,50 @@ test("experimental exports flag is removed", async () => {
`[Error: The behaviour from the experimental flag "exports" is the current behaviour now, the flag should be removed]`
);
});

test("type: module removes package.json", async () => {
let dir = await testdir({
"package.json": JSON.stringify({
name: "multiple-entrypoints",
type: "module",
exports: {
".": "./dist/multiple-entrypoints.js",
"./multiply": "./dist/multiple-entrypoints-multiply.js",
"./package.json": "./package.json",
},
preconstruct: {
exports: true,
entrypoints: ["index.ts", "multiply.ts"],
___experimentalFlags_WILL_CHANGE_IN_PATCH: {
importsConditions: true,
distInRoot: true,
typeModule: true,
},
},
}),
"multiply/package.json": JSON.stringify({
name: "multiple-entrypoints/multiply",
main: "dist/multiple-entrypoints-multiply.cjs.js",
module: "dist/multiple-entrypoints-multiply.esm.js",
}),
"src/index.ts": js`
export let a = "a";
`,
"src/multiply.ts": js`
export let b = "b";
`,
"runtime-blah.mjs": js`
import { b } from "multiple-entrypoints/multiply";
console.log(b);
`,
});

await confirms.deleteEntrypointPkgJson.mockResolvedValue(true);

await validate(dir);

expect(await getFiles(dir, ["**/package.json"])).toMatchInlineSnapshot(`
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ package.json ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
{"name":"multiple-entrypoints","type":"module","exports":{".":"./dist/multiple-entrypoints.js","./multiply":"./dist/multiple-entrypoints-multiply.js","./package.json":"./package.json"},"preconstruct":{"exports":true,"entrypoints":["index.ts","multiply.ts"],"___experimentalFlags_WILL_CHANGE_IN_PATCH":{"importsConditions":true,"distInRoot":true,"typeModule":true}}}
`);
});