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 building parcel with a debug build of itself #9829

Merged
merged 6 commits into from
Jul 2, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Write some documentation for some transformations
pyamada-atlassian committed Jul 1, 2024
commit 357ba604ef5e5e70996ebfd6b6ce03b946bb15ca
23 changes: 23 additions & 0 deletions packages/transformers/js/core/src/constant_module.rs
Original file line number Diff line number Diff line change
@@ -19,6 +19,29 @@ fn is_safe_literal(lit: &Lit) -> bool {
)
}

/// Run analysis over a module to return whether it is a 'constant module'. A constant module is one
/// which only consists of constant variable declaration export statements and is safe to inline
/// at its usage site. Declarations are safe if they refer to value type literals (string, bool,
/// null, big-int, numbers, certain template strings).
///
/// For example, this is a constant module:
/// ```skip
/// export const ANGLE = 30;
/// export const COLOR = 'red';
/// ```
///
/// For example, this is not a constant module:
/// ```skip
/// bail-out due to non-decl statement:
/// import {writeFileSync, readFileSync} from 'fs';
///
/// // bail-out due to non-decl statement:
/// writeFileSync('test', 'file');
///
/// // bail-out to non constant declarator RHS (only value type literals are supported):
/// export const COLOR = readFileSync('test');
///
/// ```
pub struct ConstantModule {
pub is_constant_module: bool,
constants: HashSet<JsWord>,
4 changes: 0 additions & 4 deletions packages/transformers/js/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -351,10 +351,6 @@ pub fn transform(

let module = {
let mut passes = chain!(
// Optional::new(
// TypeofReplacer { unresolved_mark },
// config.source_type != SourceType::Script
// ),
// Inline process.env and process.browser
Optional::new(
EnvReplacer {
2 changes: 2 additions & 0 deletions packages/transformers/js/core/src/node_replacer.rs
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@ use crate::utils::is_unresolved;
use crate::utils::SourceLocation;
use crate::utils::SourceType;

/// Replaces __filename and __dirname with globals that reference to string literals for the
/// file-path of this file.
pub struct NodeReplacer<'a> {
pub source_map: &'a SourceMap,
pub items: &'a mut Vec<DependencyDescriptor>,
102 changes: 2 additions & 100 deletions packages/transformers/js/core/src/typeof_replacer.rs
Original file line number Diff line number Diff line change
@@ -4,9 +4,7 @@ use swc_core::ecma::ast::Lit;
use swc_core::ecma::ast::Str;
use swc_core::ecma::ast::UnaryOp;
use swc_core::ecma::atoms::js_word;
use swc_core::ecma::utils::stack_size::maybe_grow_default;
use swc_core::ecma::visit::FoldWith;
use swc_core::ecma::visit::{Fold, VisitMut};
use swc_core::ecma::visit::VisitMut;

use crate::utils::is_unresolved;

@@ -73,16 +71,6 @@ impl VisitMut for TypeofReplacer {
}
}

impl Fold for TypeofReplacer {
fn fold_expr(&mut self, node: Expr) -> Expr {
if let Some(replacement) = self.get_replacement(&node) {
return replacement;
}

maybe_grow_default(|| node.fold_children_with(self))
}
}

#[cfg(test)]
mod test {
use swc_core::common::input::StringInput;
@@ -92,7 +80,7 @@ mod test {
use swc_core::ecma::parser::lexer::Lexer;
use swc_core::ecma::parser::Parser;
use swc_core::ecma::transforms::base::resolver;
use swc_core::ecma::visit::VisitMutWith;
use swc_core::ecma::visit::{FoldWith, VisitMutWith};

use super::*;

@@ -142,98 +130,12 @@ function wrapper({ require, module, exports }) {
assert_eq!(output_code, expected_code);
}

#[test]
fn test_typeof_replacer_without_shadowing() {
let code = r#"
const x = typeof require;
const m = typeof module;
const e = typeof exports;
"#;

let output_code = run_fold(code, |context| TypeofReplacer {
unresolved_mark: context.unresolved_mark,
});

let expected_code = r#"
const x = "function";
const m = "object";
const e = "object";
"#
.trim_start();
assert_eq!(output_code, expected_code);
}

#[test]
fn test_typeof_replacer_with_shadowing() {
let code = r#"
function wrapper({ require, module, exports }) {
const x = typeof require;
const m = typeof module;
const e = typeof exports;
}
"#;

let output_code = run_fold(code, |context| TypeofReplacer {
unresolved_mark: context.unresolved_mark,
});

let expected_code = r#"
function wrapper({ require, module, exports }) {
const x = typeof require;
const m = typeof module;
const e = typeof exports;
}
"#
.trim_start();
assert_eq!(output_code, expected_code);
}

struct RunTestContext {
#[allow(unused)]
global_mark: Mark,
unresolved_mark: Mark,
}

fn run_fold<F: Fold>(code: &str, make_fold: impl FnOnce(RunTestContext) -> F) -> String {
let source_map = Lrc::new(SourceMap::default());
let source_file = source_map.new_source_file(FileName::Anon, code.into());

let lexer = Lexer::new(
Default::default(),
Default::default(),
StringInput::from(&*source_file),
None,
);

let mut parser = Parser::new_from(lexer);
let module = parser.parse_module().unwrap();

let output_code = GLOBALS.set(&Globals::new(), || {
let global_mark = Mark::new();
let unresolved_mark = Mark::new();
let module = module.fold_with(&mut resolver(unresolved_mark, global_mark, false));

let mut fold = make_fold(RunTestContext {
global_mark,
unresolved_mark,
});
let module = module.fold_with(&mut fold);

let mut output_buffer = vec![];
let writer = JsWriter::new(source_map.clone(), "\n", &mut output_buffer, None);
let mut emitter = swc_core::ecma::codegen::Emitter {
cfg: Default::default(),
cm: source_map,
comments: None,
wr: writer,
};
emitter.emit_module(&module).unwrap();
let output_code = String::from_utf8(output_buffer).unwrap();
output_code
});
output_code
}

fn run_visit<V: VisitMut>(code: &str, make_visit: impl FnOnce(RunTestContext) -> V) -> String {
let source_map = Lrc::new(SourceMap::default());
let source_file = source_map.new_source_file(FileName::Anon, code.into());