-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { foo } from "foo"; | ||
import { bar } from "bar"; | ||
import "./shared"; | ||
|
||
foo(true); | ||
bar(true); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { bar } from "bar"; | ||
import "./shared"; | ||
|
||
bar(true); | ||
|
||
import("./import").then(({ foo }) => { | ||
foo(true); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"treeShakingMode": "module-fragments" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// shared package |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { foo } from "foo"; | ||
import { bar } from "bar"; | ||
import "./shared"; | ||
|
||
foo(true); | ||
bar(true); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { bar } from "bar"; | ||
import "./shared"; | ||
|
||
bar(true); | ||
|
||
import("./import").then(({ foo }) => { | ||
foo(true); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// shared package |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"minifyType": "NoMinify", | ||
"runtime": "Build", | ||
"treeShakingMode": "module-fragments" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { foo } from "foo"; | ||
|
||
foo(true); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"treeShakingMode": "module-fragments" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const inlined = 3; | ||
const message = getMessage(); | ||
|
||
console.log("Hello," + " world!", inlined, message); | ||
console.log(message); | ||
|
||
function getMessage() { | ||
return "Hello"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"runtime": "Build", | ||
"minifyType": "Minify", | ||
"treeShakingMode": "module-fragments" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
import { foo } from "foo"; | ||
|
||
foo(true); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"treeShakingMode": "module-fragments" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Adapted from webpack | ||
https://github.com/webpack/webpack/blob/6be4065ade1e252c1d8dcba4af0f43e32af1bdc1/examples/top-level-await/README.md |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { CreateUserAction } from "./Actions.js"; | ||
|
||
(async () => { | ||
await CreateUserAction("John"); | ||
console.log("created user John"); | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"treeShakingMode": "module-fragments" | ||
} |