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

Split asset symbols #9913

Draft
wants to merge 8 commits into
base: v2
Choose a base branch
from
Draft
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: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/core/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,10 @@ export type TransformerResult = {|
*/
+sideEffects?: boolean,
/** The symbols that the asset exports. */
+symbols?: $ReadOnlyMap<Symbol, {|local: Symbol, loc: ?SourceLocation|}>,
+symbols?: ?$ReadOnlyMap<
Symbol,
{|local: Symbol, loc: ?SourceLocation, meta?: ?Meta|},
>,
/**
* When a transformer returns multiple assets, it can give them unique keys to identify them.
* This can be used to find assets during packaging, or to create dependencies between multiple
Expand Down
4 changes: 3 additions & 1 deletion packages/packagers/js/src/ScopeHoistingPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const NON_ID_CONTINUE_RE = /[^$_\u200C\u200D\p{ID_Continue}]/gu;
// General regex used to replace imports with the resolved code, references with resolutions,
// and count the number of newlines in the file for source maps.
const REPLACEMENT_RE =
/\n|import\s+"([0-9a-f]{16}:.+?)";|(?:\$[0-9a-f]{16}\$exports)|(?:\$[0-9a-f]{16}\$(?:import|importAsync|require)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;
/\n|import\s+"([0-9a-f]{16,}:.+?)";|(?:\$[0-9a-f]{16,}\$exports)|(?:\$[0-9a-f]{16,}\$(?:import|importAsync|require)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;

const BUILTINS = Object.keys(globals.builtin);
const GLOBALS_BY_CONTEXT = {
Expand Down Expand Up @@ -443,6 +443,7 @@ export class ScopeHoistingPackager {
}

let [depMap, replacements] = this.buildReplacements(asset, deps);
// console.log('ASSET', asset.id, asset.meta.id, depMap, '-', code, '-');
let [prepend, prependLines, append] = this.buildAssetPrelude(asset, deps);
if (prependLines > 0) {
sourceMap?.offsetLines(1, prependLines);
Expand Down Expand Up @@ -474,6 +475,7 @@ export class ScopeHoistingPackager {
// If we matched an import, replace with the source code for the dependency.
if (d != null) {
let deps = depMap.get(d);
// console.log('match', d, deps);
if (!deps) {
return m;
}
Expand Down
1 change: 1 addition & 0 deletions packages/transformers/js/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ dunce = "1.0.1"
pathdiff = "0.2.0"
path-slash = "0.1.4"
indexmap = "1.9.2"
petgraph = "0.6.3"
13 changes: 13 additions & 0 deletions packages/transformers/js/core/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ macro_rules! collect_visit_fn {
};
}

pub fn collect(
module: &Module,
source_map: Lrc<swc_common::SourceMap>,
decls: HashSet<Id>,
ignore_mark: Mark,
global_mark: Mark,
trace_bailouts: bool,
) -> Collect {
let mut collect = Collect::new(source_map, decls, ignore_mark, global_mark, trace_bailouts);
module.visit_with(&mut collect);
collect
}

#[derive(Debug, Deserialize, PartialEq, Eq, Clone, Copy, Serialize)]
pub enum ImportKind {
Require,
Expand Down
14 changes: 13 additions & 1 deletion packages/transformers/js/core/src/decl_collector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use swc_ecmascript::ast::{self, Id};
use swc_ecmascript::ast::{self, ClassExpr, FnExpr, Id};
use swc_ecmascript::visit::{Visit, VisitWith};

/// This pass collects all declarations in a module into a single HashSet of tuples
Expand Down Expand Up @@ -41,6 +41,18 @@ impl Visit for DeclCollector {
node.visit_children_with(self);
}

fn visit_default_decl(&mut self, node: &ast::DefaultDecl) {
match node {
ast::DefaultDecl::Class(ClassExpr { ident, .. })
| ast::DefaultDecl::Fn(FnExpr { ident, .. }) => {
if let Some(ident) = ident {
self.decls.insert((ident.sym.clone(), ident.span.ctxt()));
}
}
_ => {}
}
}

fn visit_var_declarator(&mut self, node: &ast::VarDeclarator) {
self.in_var = true;
node.name.visit_with(self);
Expand Down
Loading