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

fix(turbopack): Analyze for-of stmts to allow using sharp #7424

Merged
merged 19 commits into from
Feb 23, 2024
Merged

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Feb 20, 2024

Description

Analyze code like

const paths = [
  `../src/build/Release/sharp-${runtimePlatform}.node`,
  '../src/build/Release/sharp-wasm32.node',
  `@img/sharp-${runtimePlatform}/sharp.node`,
  '@img/sharp-wasm32/sharp.node'
];

let sharp;
const errors = [];
for (const path of paths) {
  try {
    sharp = require(path);
    break;
  } catch (err) {
    /* istanbul ignore next */
    errors.push(err);
  }
}

Testing Instructions

Look at test changes.

@kdy1 kdy1 self-assigned this Feb 20, 2024
@kdy1 kdy1 changed the title fix(turbopack): Analyze for-of stmts to allow using shart fix(turbopack): Analyze for-of stmts to allow using sharp Feb 20, 2024
Copy link

vercel bot commented Feb 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
examples-gatsby-web 🔄 Building (Inspect) Visit Preview 💬 Add feedback Feb 23, 2024 9:20am
examples-native-web 🔄 Building (Inspect) Visit Preview 💬 Add feedback Feb 23, 2024 9:20am
examples-svelte-web 🔄 Building (Inspect) Visit Preview 💬 Add feedback Feb 23, 2024 9:20am
rust-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 23, 2024 9:20am
7 Ignored Deployments
Name Status Preview Comments Updated (UTC)
examples-basic-web ⬜️ Ignored (Inspect) Visit Preview Feb 23, 2024 9:20am
examples-designsystem-docs ⬜️ Ignored (Inspect) Visit Preview Feb 23, 2024 9:20am
examples-kitchensink-blog ⬜️ Ignored (Inspect) Visit Preview Feb 23, 2024 9:20am
examples-nonmonorepo ⬜️ Ignored (Inspect) Visit Preview Feb 23, 2024 9:20am
examples-tailwind-web ⬜️ Ignored (Inspect) Visit Preview Feb 23, 2024 9:20am
examples-vite-web ⬜️ Ignored (Inspect) Visit Preview Feb 23, 2024 9:20am
turbo-site ⬜️ Ignored (Inspect) Visit Preview Feb 23, 2024 9:20am

Copy link
Contributor

github-actions bot commented Feb 20, 2024

🟢 Turbopack Benchmark CI successful 🟢

Thanks

Copy link
Contributor

github-actions bot commented Feb 20, 2024

⚠️ CI failed ⚠️

The following steps have failed in CI:

  • Turbopack Rust tests (mac/win, non-blocking)
  • Turbopack Rust benchmark tests (mac/win, non-blocking)

See workflow summary for details

Copy link
Contributor

✅ This change can build next-swc

Comment on lines 1419 to 1423
if let JsValue::Variable(id) = &array {
if let Some(value) = self.data.values.get(id) {
array = value.clone();
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't read from values here. This should only happen during linking. Imagine this example:

let y;

x();

for(const z of y) {

}


function x() {
  y = [1,2,3];
}

In this case values is not set yet while visiting the for of loop.


Instead you need to keep the Variable and have some operation that converts an array to all iterated values of it: JsValue::Iterated.
Now you can define a built-in transform for JsValue::Iterated(JsValue::Array(...)) to JsValue::Alternatives.

Comment on lines 1425 to 1441
if let JsValue::Array { items, .. } = array {
for item in items {
let mut ast_path =
ast_path.with_guard(AstParentNodeRef::ForOfStmt(n, ForOfStmtField::Left));

self.current_value = Some(item);

self.visit_for_head(&n.left, &mut ast_path);
}

let mut ast_path =
ast_path.with_guard(AstParentNodeRef::ForOfStmt(n, ForOfStmtField::Body));

self.visit_stmt(&n.body, &mut ast_path);
} else {
n.visit_children_with_path(self, ast_path);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to visit the AST nodes multiple times. Just once.

Comment on lines 7 to 12
path = (
| `../src/build/Release/sharp-${runtimePlatform}.node`
| "../src/build/Release/sharp-wasm32.node"
| `@img/sharp-${runtimePlatform}/sharp.node`
| "@img/sharp-wasm32/sharp.node"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unexpected that it already has this value here. It should have some kind of operation of paths. Maybe path = Iterated(paths)

@kdy1 kdy1 marked this pull request as draft February 23, 2024 01:33
@kdy1 kdy1 marked this pull request as ready for review February 23, 2024 02:33
@kdy1 kdy1 requested a review from sokra February 23, 2024 02:36
crates/turbopack-ecmascript/src/analyzer/builtin.rs Outdated Show resolved Hide resolved
crates/turbopack-ecmascript/src/analyzer/mod.rs Outdated Show resolved Hide resolved
crates/turbopack-ecmascript/src/analyzer/mod.rs Outdated Show resolved Hide resolved
kdy1 and others added 4 commits February 23, 2024 17:22
Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
@kdy1 kdy1 merged commit 0719e5d into main Feb 23, 2024
47 of 50 checks passed
@kdy1 kdy1 deleted the kdy1/pack-2521 branch February 23, 2024 09:20
timneutkens pushed a commit to vercel/next.js that referenced this pull request Feb 26, 2024
* vercel/turbo#7437 <!-- Tobias Koppers - report
relative start/end and fix start time for events -->
* vercel/turbo#7446 <!-- Tobias Koppers - allow
to aggregate all spans -->
* vercel/turbo#6651 <!-- max -
fix(postcss_configs): support for resolve `postcss.config.json` file -->
* vercel/turbo#7448 <!-- Leah - chore: remove
"rust" npm package -->
* vercel/turbo#7436 <!-- Leah -
refactor(turbo-tasks-fs): move file watching into separate file -->
* vercel/turbo#7396 <!-- Donny/강동윤 -
feat(turbopack): Apply critical ES lint rules -->
* vercel/turbo#7421 <!-- Donny/강동윤 -
fix(turbopack): Use different title for non-parsing issues -->
* vercel/turbo#7501 <!-- Tobias Koppers -
Tracing: Report progress on initial read -->
* vercel/turbo#7508 <!-- Donny/강동윤 -
fix(turbopack): Fix CSS Modules of turbopack in swc mode -->
* vercel/turbo#7424 <!-- Donny/강동윤 -
fix(turbopack): Analyze for-of stmts to allow using `sharp` -->
* vercel/turbo#7510 <!-- Tobias Koppers - dedupe
primary_modules to avoid deduping in chunking, reduce memory usage -->
* vercel/turbo#7509 <!-- Tobias Koppers - split
graph_node_to_referenced_nodes into cacheable and non-cacheable parts
-->
* vercel/turbo#7516 <!-- Tobias Koppers - fix
ignore in package.json -->
* vercel/turbo#7500 <!-- Tobias Koppers - add
span count to trace-server -->
* vercel/turbo#7507 <!-- Donny/강동윤 -
fix(turbopack): Fix CSS module purity validator -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Turbopack is incompatible with sharp when using plaiceholder
2 participants