diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/import.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/import.js new file mode 100644 index 0000000000000..1c2b150c634ca --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/import.js @@ -0,0 +1,6 @@ +import { foo } from "foo"; +import { bar } from "bar"; +import "./shared"; + +foo(true); +bar(true); diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/index.js new file mode 100644 index 0000000000000..282fdbc134dd3 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/index.js @@ -0,0 +1,8 @@ +import { bar } from "bar"; +import "./shared"; + +bar(true); + +import("./import").then(({ foo }) => { + foo(true); +}); diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/bar/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/bar/index.js new file mode 100644 index 0000000000000..6e9a13a68ba32 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/bar/index.js @@ -0,0 +1,3 @@ +export function bar(value) { + console.assert(value); +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/bar/package.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/bar/package.json new file mode 100644 index 0000000000000..14ab704d8f639 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/bar/package.json @@ -0,0 +1,3 @@ +{ + "main": "index.js" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/foo/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/foo/index.js new file mode 100644 index 0000000000000..cb5877b67fcc3 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/foo/index.js @@ -0,0 +1,3 @@ +export function foo(value) { + console.assert(value); +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/foo/package.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/foo/package.json new file mode 100644 index 0000000000000..14ab704d8f639 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/node_modules/foo/package.json @@ -0,0 +1,3 @@ +{ + "main": "index.js" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/options.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/options.json new file mode 100644 index 0000000000000..000d78a6b3cbb --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/options.json @@ -0,0 +1,3 @@ +{ + "treeShakingMode": "module-fragments" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/shared.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/shared.js new file mode 100644 index 0000000000000..645a24a4851fa --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk/input/shared.js @@ -0,0 +1 @@ +// shared package diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/import.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/import.js new file mode 100644 index 0000000000000..1c2b150c634ca --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/import.js @@ -0,0 +1,6 @@ +import { foo } from "foo"; +import { bar } from "bar"; +import "./shared"; + +foo(true); +bar(true); diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/index.js new file mode 100644 index 0000000000000..282fdbc134dd3 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/index.js @@ -0,0 +1,8 @@ +import { bar } from "bar"; +import "./shared"; + +bar(true); + +import("./import").then(({ foo }) => { + foo(true); +}); diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/bar/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/bar/index.js new file mode 100644 index 0000000000000..6e9a13a68ba32 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/bar/index.js @@ -0,0 +1,3 @@ +export function bar(value) { + console.assert(value); +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/bar/package.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/bar/package.json new file mode 100644 index 0000000000000..14ab704d8f639 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/bar/package.json @@ -0,0 +1,3 @@ +{ + "main": "index.js" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/foo/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/foo/index.js new file mode 100644 index 0000000000000..cb5877b67fcc3 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/foo/index.js @@ -0,0 +1,3 @@ +export function foo(value) { + console.assert(value); +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/foo/package.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/foo/package.json new file mode 100644 index 0000000000000..14ab704d8f639 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/node_modules/foo/package.json @@ -0,0 +1,3 @@ +{ + "main": "index.js" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/shared.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/shared.js new file mode 100644 index 0000000000000..645a24a4851fa --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/input/shared.js @@ -0,0 +1 @@ +// shared package diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/options.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/options.json new file mode 100644 index 0000000000000..79465e397512a --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/async_chunk_build/options.json @@ -0,0 +1,5 @@ +{ + "minifyType": "NoMinify", + "runtime": "Build", + "treeShakingMode": "module-fragments" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/index.js new file mode 100644 index 0000000000000..9820516162f14 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/index.js @@ -0,0 +1,3 @@ +import { foo } from "foo"; + +foo(true); diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/node_modules/foo/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/node_modules/foo/index.js new file mode 100644 index 0000000000000..cb5877b67fcc3 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/node_modules/foo/index.js @@ -0,0 +1,3 @@ +export function foo(value) { + console.assert(value); +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/node_modules/foo/package.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/node_modules/foo/package.json new file mode 100644 index 0000000000000..14ab704d8f639 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/node_modules/foo/package.json @@ -0,0 +1,3 @@ +{ + "main": "index.js" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/options.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/options.json new file mode 100644 index 0000000000000..000d78a6b3cbb --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/chunked/input/options.json @@ -0,0 +1,3 @@ +{ + "treeShakingMode": "module-fragments" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/ecmascript_minify/input/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/ecmascript_minify/input/index.js new file mode 100644 index 0000000000000..f490af11ac233 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/ecmascript_minify/input/index.js @@ -0,0 +1,9 @@ +const inlined = 3; +const message = getMessage(); + +console.log("Hello," + " world!", inlined, message); +console.log(message); + +function getMessage() { + return "Hello"; +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/ecmascript_minify/options.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/ecmascript_minify/options.json new file mode 100644 index 0000000000000..a0441f3013104 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/ecmascript_minify/options.json @@ -0,0 +1,5 @@ +{ + "runtime": "Build", + "minifyType": "Minify", + "treeShakingMode": "module-fragments" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/index.js new file mode 100644 index 0000000000000..c5d9db6e1c7fc --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/index.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +import { foo } from "foo"; + +foo(true); diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/node_modules/foo/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/node_modules/foo/index.js new file mode 100644 index 0000000000000..db97d18906764 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/node_modules/foo/index.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +export function foo(value) { + console.assert(value); +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/node_modules/foo/package.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/node_modules/foo/package.json new file mode 100644 index 0000000000000..14ab704d8f639 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/node_modules/foo/package.json @@ -0,0 +1,3 @@ +{ + "main": "index.js" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/options.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/options.json new file mode 100644 index 0000000000000..000d78a6b3cbb --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/shebang/input/options.json @@ -0,0 +1,3 @@ +{ + "treeShakingMode": "module-fragments" +} diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/Actions.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/Actions.js new file mode 100644 index 0000000000000..0f2170c5a2d33 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/Actions.js @@ -0,0 +1,28 @@ +// import() doesn't care about whether a module is an async module or not +const UserApi = import("./UserAPI.js"); + +export const CreateUserAction = async (name) => { + console.log("Creating user", name); + // These are normal awaits, because they are in an async function + const { createUser } = await UserApi; + await createUser(name); +}; + +// You can place import() where you like +// Placing it at top-level will start loading and evaluating on +// module evaluation. +// see CreateUserAction above +// Here: Connecting to the DB starts when the application starts +// Placing it inside of an (async) function will start loading +// and evaluating when the function is called for the first time +// which basically makes it lazy-loaded. +// see AlternativeCreateUserAction below +// Here: Connecting to the DB starts when AlternativeCreateUserAction +// is called +export const AlternativeCreateUserAction = async (name) => { + const { createUser } = await import("./UserAPI.js"); + await createUser(name); +}; + +// Note: Using await import() at top-level doesn't make much sense +// except in rare cases. It will import modules sequentially. diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/README.md b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/README.md new file mode 100644 index 0000000000000..723f730dc5110 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/README.md @@ -0,0 +1,2 @@ +Adapted from webpack +https://github.com/webpack/webpack/blob/6be4065ade1e252c1d8dcba4af0f43e32af1bdc1/examples/top-level-await/README.md diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/UserAPI.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/UserAPI.js new file mode 100644 index 0000000000000..810fe24f9aa7f --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/UserAPI.js @@ -0,0 +1,7 @@ +import { dbCall } from "./db-connection.js"; + +export const createUser = async (name) => { + const command = `CREATE USER ${name}`; + // This is a normal await, because it's in an async function + await dbCall({ command }); +}; diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/db-connection.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/db-connection.js new file mode 100644 index 0000000000000..412eee71b72cf --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/db-connection.js @@ -0,0 +1,18 @@ +const connectToDB = async (url) => { + console.log("connecting to db", url); + await new Promise((r) => setTimeout(r, 1000)); +}; + +// This is a top-level-await +await connectToDB("my-sql://example.com"); + +export const dbCall = async (data) => { + console.log("dbCall", data); + // This is a normal await, because it's in an async function + await new Promise((r) => setTimeout(r, 100)); + return "fake data"; +}; + +export const close = () => { + console.log("closes the DB connection"); +}; diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/index.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/index.js new file mode 100644 index 0000000000000..db6ef5c78f9a9 --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/index.js @@ -0,0 +1,6 @@ +import { CreateUserAction } from "./Actions.js"; + +(async () => { + await CreateUserAction("John"); + console.log("created user John"); +})(); diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/options.json b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/options.json new file mode 100644 index 0000000000000..000d78a6b3cbb --- /dev/null +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/top-level-await/input/options.json @@ -0,0 +1,3 @@ +{ + "treeShakingMode": "module-fragments" +}