Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-bars-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/react": patch
---

Auto import `@lynx-js/react/experimental/lazy/import` when using `<component is={url} />`
2 changes: 1 addition & 1 deletion packages/react/runtime/lazy/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
sExportsReactInternal,
sExportsReactLepus,
target,
} from './target';
} from './target.js';

Object.defineProperty(target, sExportsReact, {
value: ReactAPIs,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/runtime/lepus/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { jsx as createVNode } from './jsx-runtime';
import { jsx as createVNode } from './jsx-runtime/index.js';

const slice = /* @__PURE__ */ [].slice;

Expand Down
2 changes: 1 addition & 1 deletion packages/react/runtime/lepus/jsx-dev-runtime/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
export * from '../jsx-runtime';
export * from '../jsx-runtime/index.js';
43 changes: 43 additions & 0 deletions packages/react/transform/src/swc_plugin_compat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ where
old_runtime_import_ids: Vec<Id>,
runtime_id: Lazy<Ident>,
comments: Option<C>,
has_component_is: bool,
}

impl<C> Default for CompatVisitor<C>
Expand All @@ -163,6 +164,7 @@ where
add_component_element_state: vec![],
runtime_id: Lazy::new(|| private_ident!("ReactLynx")),
comments,
has_component_is: false,
}
}

Expand Down Expand Up @@ -404,6 +406,7 @@ where
};

if is_component_is {
self.has_component_is = true;
match &n.opening.name {
JSXElementName::Ident(_) => {
n.opening.name = JSXElementName::JSXMemberExpr(JSXMemberExpr {
Expand Down Expand Up @@ -998,6 +1001,24 @@ where
}
None => {}
}

if self.has_component_is {
prepend_stmt(
&mut n.body,
ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
span: DUMMY_SP,
phase: ImportPhase::Evaluation,
specifiers: vec![],
src: Box::new(Str {
span: DUMMY_SP,
raw: None,
value: format!("{}/experimental/lazy/import", self.opts.new_runtime_pkg).into(),
}),
type_only: Default::default(),
with: Default::default(),
})),
);
}
}
}

Expand Down Expand Up @@ -1560,4 +1581,26 @@ mod tests {
a1, a2, a3, a4, b;
"#
);

test!(
module,
Syntax::Es(EsSyntax {
jsx: true,
..Default::default()
}),
|_| (
resolver(Mark::new(), Mark::new(), true),
visit_mut_pass(CompatVisitor::<&SingleThreadedComments>::new(
CompatVisitorConfig {
..Default::default()
},
None
)),
hygiene_with_config(Default::default()),
),
should_add_component_is_import,
r#"
let c = <component is="xxxx" />;
"#
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "@lynx-js/react/experimental/lazy/import";
import * as ReactLynx from "@lynx-js/react/internal";
let c = <ReactLynx.__ComponentIsPolyfill is="xxxx"/>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// <reference types="vitest/globals" />

const sExportsReact = Symbol.for('__REACT_LYNX_EXPORTS__(@lynx-js/react)');
const sExportsReactLepus = Symbol.for(
'__REACT_LYNX_EXPORTS__(@lynx-js/react/lepus)',
);
const sExportsReactInternal = Symbol.for(
'__REACT_LYNX_EXPORTS__(@lynx-js/react/internal)',
);
const sExportsJSXRuntime = Symbol.for(
'__REACT_LYNX_EXPORTS__(@lynx-js/react/jsx-runtime)',
);
const sExportsJSXDevRuntime = Symbol.for(
'__REACT_LYNX_EXPORTS__(@lynx-js/react/jsx-dev-runtime)',
);
const sExportsLegacyReactRuntime = Symbol.for(
'__REACT_LYNX_EXPORTS__(@lynx-js/react/legacy-react-runtime)',
);

export {};

it('should not have experimental/lazy/import imported', () => {
if (__BACKGROUND__) {
expect(lynx[sExportsReact]).toBeUndefined();
expect(lynx[sExportsReactLepus]).toBeUndefined();
expect(lynx[sExportsReactInternal]).toBeUndefined();
expect(lynx[sExportsJSXRuntime]).toBeUndefined();
expect(lynx[sExportsJSXDevRuntime]).toBeUndefined();
expect(lynx[sExportsLegacyReactRuntime]).toBeUndefined();
} else {
expect(globalThis[sExportsReact]).toBeUndefined();
expect(globalThis[sExportsReactLepus]).toBeUndefined();
expect(globalThis[sExportsReactInternal]).toBeUndefined();
expect(globalThis[sExportsJSXRuntime]).toBeUndefined();
expect(globalThis[sExportsJSXDevRuntime]).toBeUndefined();
expect(globalThis[sExportsLegacyReactRuntime]).toBeUndefined();
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createConfig } from '../../../create-react-config.js';

/** @type {import('@rspack/core').Configuration} */
export default {
context: __dirname,
...createConfig(),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import("@lynx-js/test-tools").TConfigCaseConfig} */
module.exports = {
bundlePath: [
'main:main-thread.js',
'main:background.js',
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// <reference types="vitest/globals" />

const sExportsReact = Symbol.for('__REACT_LYNX_EXPORTS__(@lynx-js/react)');
const sExportsReactLepus = Symbol.for(
'__REACT_LYNX_EXPORTS__(@lynx-js/react/lepus)',
);
const sExportsReactInternal = Symbol.for(
'__REACT_LYNX_EXPORTS__(@lynx-js/react/internal)',
);
const sExportsJSXRuntime = Symbol.for(
'__REACT_LYNX_EXPORTS__(@lynx-js/react/jsx-runtime)',
);
const sExportsJSXDevRuntime = Symbol.for(
'__REACT_LYNX_EXPORTS__(@lynx-js/react/jsx-dev-runtime)',
);
const sExportsLegacyReactRuntime = Symbol.for(
'__REACT_LYNX_EXPORTS__(@lynx-js/react/legacy-react-runtime)',
);

export {};

it('should have experimental/lazy/import imported', () => {
const jsx = <component is='foo' />;
expect(jsx).toBeDefined();
if (__BACKGROUND__) {
expect(lynx[sExportsReact]).not.toBeUndefined();
expect(lynx[sExportsReactLepus]).not.toBeUndefined();
expect(lynx[sExportsReactInternal]).not.toBeUndefined();
expect(lynx[sExportsJSXRuntime]).not.toBeUndefined();
expect(lynx[sExportsJSXDevRuntime]).not.toBeUndefined();
expect(lynx[sExportsLegacyReactRuntime]).not.toBeUndefined();
} else {
expect(globalThis[sExportsReact]).not.toBeUndefined();
expect(globalThis[sExportsReactLepus]).not.toBeUndefined();
expect(globalThis[sExportsReactInternal]).not.toBeUndefined();
expect(globalThis[sExportsJSXRuntime]).not.toBeUndefined();
expect(globalThis[sExportsJSXDevRuntime]).not.toBeUndefined();
expect(globalThis[sExportsLegacyReactRuntime]).not.toBeUndefined();
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createConfig } from '../../../create-react-config.js';

/** @type {import('@rspack/core').Configuration} */
export default {
context: __dirname,
...createConfig({
compat: {
disableDeprecatedWarning: true,
},
}),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import("@lynx-js/test-tools").TConfigCaseConfig} */
module.exports = {
bundlePath: [
'main:main-thread.js',
'main:background.js',
],
};
Loading