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

feat: implicit qhooks #125

Merged
merged 6 commits into from
Jan 5, 2022
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
2 changes: 1 addition & 1 deletion src/optimizer/core/benches/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn transform_benchmark(b: &mut Criterion) {
b.bench_function("transform", |b| {
b.iter(|| {
let code = r#"
import {qHook} from '@builderio/qwik';
import {qHook} from '@builder.io/qwik';


const Header2 = qComponent({
Expand Down
26 changes: 20 additions & 6 deletions src/optimizer/core/src/code_move.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use crate::collector::{new_ident_from_id, GlobalCollect, Id, ImportKind};
use crate::parse::{emit_source_code, HookAnalysis, PathData, TransformModule, TransformOutput};
use crate::transform::{create_internal_call, create_synthetic_wildcard_import};
use crate::words::*;

use std::collections::HashMap;
use std::path::Path;

use anyhow::{format_err, Context, Error};
use swc_atoms::JsWord;
use swc_common::comments::{Comments, SingleThreadedComments};
use swc_common::comments::SingleThreadedComments;
use swc_common::{sync::Lrc, SourceMap, DUMMY_SP};
use swc_ecmascript::ast;

pub fn new_module(
expr: ast::CallExpr,
expr: Box<ast::Expr>,
path: &PathData,
name: &str,
origin: &str,
Expand All @@ -26,6 +28,12 @@ pub fn new_module(
shebang: None,
};

// Inject qwik internal import
module.body.push(create_synthetic_wildcard_import(
&QWIK_INTERNAL,
&BUILDER_IO_QWIK,
));

for id in local_idents {
if let Some(import) = global.imports.get(id) {
let specifier = match import.kind {
Expand Down Expand Up @@ -142,11 +150,17 @@ pub fn fix_path<S: AsRef<Path>, D: AsRef<Path>>(
}

fn create_named_export(
expr: ast::CallExpr,
expr: Box<ast::Expr>,
name: &str,
comments: &SingleThreadedComments,
_comments: &SingleThreadedComments,
) -> ast::ModuleItem {
comments.add_pure_comment(expr.span.lo);
// comments.add_pure_comment(expr.span().lo);

let call_expr = Box::new(ast::Expr::Call(create_internal_call(
&QHOOK_HANDLER,
vec![*expr],
None,
)));

ast::ModuleItem::ModuleDecl(ast::ModuleDecl::ExportDecl(ast::ExportDecl {
span: DUMMY_SP,
Expand All @@ -161,7 +175,7 @@ fn create_named_export(
JsWord::from(name),
DUMMY_SP,
))),
init: Some(Box::new(ast::Expr::Call(expr))),
init: Some(call_expr),
}],
}),
}))
Expand Down
35 changes: 29 additions & 6 deletions src/optimizer/core/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,12 @@ pub struct HookCollect {
}

impl HookCollect {
pub fn new(node: &ast::CallExpr) -> Self {
let mut collect = Self {
pub fn new() -> Self {
Self {
local_decl: HashSet::new(),
local_idents: HashSet::new(),
expr_ctxt: vec![],
};
node.visit_with(&mut collect);
collect
}
}

pub fn get_words(self) -> (Vec<JsWord>, Vec<Id>) {
Expand Down Expand Up @@ -434,9 +432,34 @@ impl Visit for HookCollect {
self.expr_ctxt.pop();
}

fn visit_jsx_opening_element(&mut self, node: &ast::JSXOpeningElement) {
let mut stacked = false;
if let ast::JSXElementName::Ident(ref ident) = node.name {
let ident_name = ident.sym.as_ref().chars().next();
if let Some('A'..='Z') = ident_name {
self.expr_ctxt.push(ExprOrSkip::Expr);
} else {
self.expr_ctxt.push(ExprOrSkip::Skip);
}
stacked = true;
}

node.visit_children_with(self);
if stacked {
self.expr_ctxt.pop();
}
}
fn visit_jsx_attr(&mut self, node: &ast::JSXAttr) {
self.expr_ctxt.push(ExprOrSkip::Skip);
node.visit_children_with(self);
self.expr_ctxt.pop();
}

fn visit_ident(&mut self, node: &ast::Ident) {
if let Some(ExprOrSkip::Expr) = self.expr_ctxt.last() {
self.local_idents.insert(id!(node));
if node.span.ctxt() != SyntaxContext::empty() {
self.local_idents.insert(id!(node));
}
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/optimizer/core/src/entry_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::parse::PathData;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use swc_atoms::JsWord;
use swc_ecmascript::ast::CallExpr;
use swc_ecmascript::ast::Expr;

use lazy_static::lazy_static;

Expand All @@ -30,7 +30,7 @@ pub trait EntryPolicy: Send + Sync {
location: &PathData,
context: &[String],
analytics: &HookCollect,
expr: &CallExpr,
expr: &Expr,
) -> Option<JsWord>;
}

Expand All @@ -44,7 +44,7 @@ impl EntryPolicy for SingleStrategy {
_path: &PathData,
_context: &[String],
_analytics: &HookCollect,
_expr: &CallExpr,
_expr: &Expr,
) -> Option<JsWord> {
Some(ENTRY_HOOKS.clone())
}
Expand All @@ -60,7 +60,7 @@ impl EntryPolicy for PerHookStrategy {
_path: &PathData,
_context: &[String],
_analytics: &HookCollect,
_expr: &CallExpr,
_expr: &Expr,
) -> Option<JsWord> {
None
}
Expand All @@ -76,7 +76,7 @@ impl EntryPolicy for PerComponentStrategy {
_path: &PathData,
context: &[String],
_analytics: &HookCollect,
_expr: &CallExpr,
_expr: &Expr,
) -> Option<JsWord> {
context.first().map_or_else(
|| Some(ENTRY_HOOKS.clone()),
Expand All @@ -95,7 +95,7 @@ impl EntryPolicy for SmartStrategy {
_path: &PathData,
context: &[String],
_analytics: &HookCollect,
_expr: &CallExpr,
_expr: &Expr,
) -> Option<JsWord> {
if context.iter().any(|h| h == "onMount") {
return Some(ENTRY_SERVER.clone());
Expand Down Expand Up @@ -136,7 +136,7 @@ impl EntryPolicy for ManualStrategy {
_path: &PathData,
_context: &[String],
_analytics: &HookCollect,
_expr: &CallExpr,
_expr: &Expr,
) -> Option<JsWord> {
let entry = self.map.get(symbol);
Some(match entry {
Expand Down
1 change: 1 addition & 0 deletions src/optimizer/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod entry_strategy;
mod parse;
mod transform;
mod utils;
mod words;

#[cfg(feature = "parallel")]
use rayon::prelude::*;
Expand Down
33 changes: 14 additions & 19 deletions src/optimizer/core/src/snapshots/qwik_core__test__example_1.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/test.rs
source: src/optimizer/core/src/test.rs
expression: output

---
Expand All @@ -17,28 +17,32 @@ const Header = qComponent({

============================= test.tsx ==

import * as __qwik__ from "@builder.io/qwik";
/*#__PURE__*/ qComponent({
"onMount": qHook(()=>import("./h_test_header_onmount")
"onMount": __qwik__.qHook(()=>import("./h_test_header_onmount")
, "Header_onMount"),
onRender: qHook(()=>import("./h_test_header_onrender")
onRender: __qwik__.qHook(()=>import("./h_test_header_onrender")
, "Header_onRender")
});

============================= h_test_header_onmount.tsx (ENTRY POINT)==

export const Header_onMount = /*#__PURE__*/ qHook(()=>{
import * as __qwik__ from "@builder.io/qwik";
export const Header_onMount = __qwik__.qHook(()=>{
console.log("mount");
});

============================= h_test_header_onrender_div_onclick.tsx (ENTRY POINT)==

export const Header_onRender_div_onClick = /*#__PURE__*/ qHook((ctx)=>console.log(ctx)
import * as __qwik__ from "@builder.io/qwik";
export const Header_onRender_div_onClick = __qwik__.qHook((ctx)=>console.log(ctx)
);

============================= h_test_header_onrender.tsx (ENTRY POINT)==

export const Header_onRender = /*#__PURE__*/ qHook(()=>{
return (<div onClick={qHook(()=>import("./h_test_header_onrender_div_onclick")
import * as __qwik__ from "@builder.io/qwik";
export const Header_onRender = __qwik__.qHook(()=>{
return (<div onClick={__qwik__.qHook(()=>import("./h_test_header_onrender_div_onclick")
, "Header_onRender_div_onClick")}/>);
});

Expand All @@ -52,9 +56,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{
"canonicalFilename": "h_test_header_onmount",
"localDecl": [],
"localIdents": [
"console",
"log",
"qHook"
"console"
]
},
{
Expand All @@ -67,9 +69,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{
],
"localIdents": [
"console",
"ctx",
"log",
"qHook"
"ctx"
]
},
{
Expand All @@ -78,12 +78,7 @@ export const Header_onRender = /*#__PURE__*/ qHook(()=>{
"entry": null,
"canonicalFilename": "h_test_header_onrender",
"localDecl": [],
"localIdents": [
"div",
"import",
"onClick",
"qHook"
]
"localIdents": []
}
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ const Header = qHook((decl1, {decl2}, [decl3]) => {

============================= project/test.tsx ==

qHook(()=>import("../h_test_header")
import * as __qwik__ from "@builder.io/qwik";
__qwik__.qHook(()=>import("../h_test_header")
, "Header");

============================= h_test_header.tsx (ENTRY POINT)==

export const Header = /*#__PURE__*/ qHook((decl1, { decl2 }, [decl3])=>{
import * as __qwik__ from "@builder.io/qwik";
export const Header = __qwik__.qHook((decl1, { decl2 }, [decl3])=>{
const hola = ident1.no;
ident2;
const a = ident1 + ident3;
Expand Down Expand Up @@ -73,10 +75,6 @@ export const Header = /*#__PURE__*/ qHook((decl1, { decl2 }, [decl3])=>{
"ident11"
],
"localIdents": [
"decl1",
"decl2",
"decl3",
"div",
"ident1",
"ident10",
"ident11",
Expand All @@ -88,11 +86,7 @@ export const Header = /*#__PURE__*/ qHook((decl1, { decl2 }, [decl3])=>{
"ident6",
"ident7",
"ident8",
"ident9",
"no",
"onClick",
"qHook",
"required"
"ident9"
]
}
]
Expand Down
Loading