Unverified Commit d65c28ec authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Share Turbopack constant export analysis

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent fe9cbbd1
...@@ -212,7 +212,7 @@ pub async fn get_inline_export( ...@@ -212,7 +212,7 @@ pub async fn get_inline_export(
let Some(module) = resolved.first_module().await? else { let Some(module) = resolved.first_module().await? else {
return Ok(None); return Ok(None);
}; };
let constants = get_inline_constants(*module, compile_time_info).await?; let constants = get_constants(*module, compile_time_info).await?;
let Some(constants) = constants.as_ref() else { let Some(constants) = constants.as_ref() else {
return Ok(None); return Ok(None);
}; };
...@@ -234,22 +234,6 @@ pub async fn get_inline_export( ...@@ -234,22 +234,6 @@ pub async fn get_inline_export(
pub async fn get_constants( pub async fn get_constants(
module: ResolvedVc<Box<dyn Module>>, module: ResolvedVc<Box<dyn Module>>,
compile_time_info: Vc<CompileTimeInfo>, compile_time_info: Vc<CompileTimeInfo>,
) -> Result<Vc<OptionConstantsModule>> {
get_constants_internal(module, compile_time_info, false).await
}
#[turbo_tasks::function]
async fn get_inline_constants(
module: ResolvedVc<Box<dyn Module>>,
compile_time_info: Vc<CompileTimeInfo>,
) -> Result<Vc<OptionConstantsModule>> {
get_constants_internal(module, compile_time_info, true).await
}
async fn get_constants_internal(
module: ResolvedVc<Box<dyn Module>>,
compile_time_info: Vc<CompileTimeInfo>,
include_default_expression: bool,
) -> Result<Vc<OptionConstantsModule>> { ) -> Result<Vc<OptionConstantsModule>> {
let Some(parseable) = ResolvedVc::try_sidecast::<Box<dyn EcmascriptParsable>>(module) else { let Some(parseable) = ResolvedVc::try_sidecast::<Box<dyn EcmascriptParsable>>(module) else {
// should never actually happen, there should be a "imported module is not chunkable" error // should never actually happen, there should be a "imported module is not chunkable" error
...@@ -273,19 +257,18 @@ async fn get_constants_internal( ...@@ -273,19 +257,18 @@ async fn get_constants_internal(
let arena = ThreadLocal::new(); let arena = ThreadLocal::new();
let default_export_value = let default_export_value = if let Program::Module(module) = program {
if include_default_expression && let Program::Module(module) = program { module.body.iter().find_map(|item| match item {
module.body.iter().find_map(|item| match item { ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(export)) => {
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(export)) => { Some(GLOBALS.set(globals, || {
Some(GLOBALS.set(globals, || { eval_context.eval(arena.get_or_default(), &export.expr)
eval_context.eval(arena.get_or_default(), &export.expr) }))
})) }
} _ => None,
_ => None, })
}) } else {
} else { None
None };
};
let var_graph = { let var_graph = {
let supports_block_scoping = *compile_time_info let supports_block_scoping = *compile_time_info
......
import value from 'analyzed-default' with { turbopackConstants: 'true' }
if (value !== 'dev') require('./marker')
function readValue() {
return value
}
it('shares default export constants with analyzer-aware inlining', () => {
expect(readValue()).toBe('dev')
expect(readValue.toString()).toContain('TURBOPACK compile-time value')
const modules = Array.from(__turbopack_modules__.keys())
expect(modules).not.toContainEqual(expect.stringMatching(/input\/marker\.js/))
expect(modules).not.toContainEqual(
expect.stringMatching(/node_modules\/analyzed-default\/index\.js/)
)
})
{
"scopeHoisting": false,
"mangleExportNames": false,
"crossModuleConstants": true,
"inlineConstantExports": false
}
...@@ -103,6 +103,8 @@ struct SnapshotOptions { ...@@ -103,6 +103,8 @@ struct SnapshotOptions {
#[serde(default)] #[serde(default)]
cross_module_constants: bool, cross_module_constants: bool,
#[serde(default)] #[serde(default)]
inline_constant_exports: bool,
#[serde(default)]
scope_hoisting: bool, scope_hoisting: bool,
#[serde(default)] #[serde(default)]
shared_runtime: bool, shared_runtime: bool,
...@@ -151,6 +153,7 @@ impl Default for SnapshotOptions { ...@@ -151,6 +153,7 @@ impl Default for SnapshotOptions {
mangle_export_names: false, mangle_export_names: false,
cjs_scope_hoisting: false, cjs_scope_hoisting: false,
cross_module_constants: false, cross_module_constants: false,
inline_constant_exports: false,
scope_hoisting: false, scope_hoisting: false,
shared_runtime: false, shared_runtime: false,
production_chunking: false, production_chunking: false,
...@@ -429,6 +432,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> { ...@@ -429,6 +432,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
mangle_export_names: options.mangle_export_names, mangle_export_names: options.mangle_export_names,
cjs_scope_hoisting: options.cjs_scope_hoisting, cjs_scope_hoisting: options.cjs_scope_hoisting,
cross_module_constants: options.cross_module_constants, cross_module_constants: options.cross_module_constants,
inline_constant_exports: options.inline_constant_exports,
enable_exports_info_inlining: true, enable_exports_info_inlining: true,
enable_rust_react_compiler: options.enable_rust_react_compiler.then_some( enable_rust_react_compiler: options.enable_rust_react_compiler.then_some(
ReactCompilerTransformOptions { ReactCompilerTransformOptions {
......
import { UPPER, lower } from './other'
import value from './other' with { turbopackConstants: 'true' }
if (UPPER !== 'x') require('./dead-uppercase')
if (value !== 'dev') require('./dead-default')
console.log(lower)
'use turbopack: constants'
export default 'dev'
export const UPPER = 'x'
export const lower = 'y'
export const NON_CONSTANT = globalThis.value
error - [analysis] /turbopack/crates/turbopack-tests/tests/snapshot/comptime/cross-module-shared-analysis/input/other.js:6:0 Export NON_CONSTANT is not a constant
2 |
3 | export default 'dev'
4 | export const UPPER = 'x'
5 | export const lower = 'y'
+ v------------------------------------------v
6 + export const NON_CONSTANT = globalThis.value
+ ^------------------------------------------^
7 |
It was analyzed to be FreeVar(globalThis)["value"]
It has to be a constant because the module contains use turbopack: constants or was imported with with {turbopackConstants: 'true'}
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/snapshot/comptime/cross-module-shared-analysis/input/other.js
./turbopack/crates/turbopack-tests/tests/snapshot/comptime/cross-module-shared-analysis/input/index.js
\ No newline at end of file
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1ece_tests_snapshot_comptime_cross-module-shared-analysis_input_index_191u528yd4usk.js",
{"otherChunks":["output/1jsg_tests_snapshot_comptime_cross-module-shared-analysis_input_1n_-7ozvq6i9y._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/cross-module-shared-analysis/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime
\ No newline at end of file
(()=>{"use strict";(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1jsg_tests_snapshot_comptime_cross-module-shared-analysis_input_1n_-7ozvq6i9y._.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/cross-module-shared-analysis/input/index.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
__turbopack_context__.s([]);
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$comptime$2f$cross$2d$module$2d$shared$2d$analysis$2f$input$2f$other$2e$js__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/cross-module-shared-analysis/input/other.js [test] (ecmascript)");
;
;
if ("TURBOPACK compile-time falsy", 0) //TURBOPACK unreachable
;
if ("TURBOPACK compile-time falsy", 0) //TURBOPACK unreachable
;
console.log(("TURBOPACK compile-time value", "y"));
}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/cross-module-shared-analysis/input/other.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
__turbopack_context__.s([
"NON_CONSTANT",
()=>NON_CONSTANT,
"UPPER",
()=>UPPER,
"default",
()=>__TURBOPACK__default__export__,
"lower",
()=>lower
]);
'use turbopack: constants';
const __TURBOPACK__default__export__ = 'dev';
const UPPER = 'x';
const lower = 'y';
const NON_CONSTANT = globalThis.value;
}),
]);})()
//# sourceMappingURL=1jsg_tests_snapshot_comptime_cross-module-shared-analysis_input_1n_-7ozvq6i9y._.js.map
\ No newline at end of file
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/cross-module-shared-analysis/input/index.js"],"sourcesContent":["import { UPPER, lower } from './other'\nimport value from './other' with { turbopackConstants: 'true' }\n\nif (UPPER !== 'x') require('./dead-uppercase')\nif (value !== 'dev') require('./dead-default')\nconsole.log(lower)\n"],"names":["console","log"],"mappings":";AAAA;;;AAGA;;AACA;;AACAA,QAAQC,GAAG"}},
{"offset": {"line": 17, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/cross-module-shared-analysis/input/other.js"],"sourcesContent":["'use turbopack: constants'\n\nexport default 'dev'\nexport const UPPER = 'x'\nexport const lower = 'y'\nexport const NON_CONSTANT = globalThis.value\n"],"names":["UPPER","lower","NON_CONSTANT","globalThis","value"],"mappings":";;;;;;;;;;AAAA;uCAEe;AACR,MAAMA,QAAQ;AACd,MAAMC,QAAQ;AACd,MAAMC,eAAeC,WAAWC,KAAK"}}]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment