Skip to content

Commit

Permalink
Add typeModule experimental flag (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Jan 15, 2024
1 parent 1a2ee56 commit d8e9cea
Show file tree
Hide file tree
Showing 23 changed files with 1,512 additions and 386 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-windows-kiss.md
@@ -0,0 +1,5 @@
---
"@preconstruct/cli": patch
---

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

Add `distInRoot` experimental flag
4 changes: 2 additions & 2 deletions package.json
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
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
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
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}}}
`);
});

0 comments on commit d8e9cea

Please sign in to comment.