Skip to content

Commit

Permalink
Merge branch 'main' into release/1-0-0-alpha-1
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jul 4, 2024
2 parents 9599a02 + add78bb commit 58ca484
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 379 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/reusable-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,20 @@ jobs:
# if: ${{ inputs.target == 'x86_64-unknown-linux-gnu' && !inputs.skipable }}
# run: node ./scripts/test/diff.cjs

### x86_64-apple-darwin
### *-apple-darwin

- name: Test x86_64-apple-darwin
- name: Test apple-darwin
timeout-minutes: 15 # Tests should finish within 15 mins, please fix your tests instead of changing this to a higher timeout.
if: ${{ inputs.target == 'x86_64-apple-darwin' && !inputs.skipable }}
run: pnpm run test:ci
if: ${{ contains(inputs.target, 'apple-darwin') && !inputs.skipable }}
run: |
# arch is ARM and target is ARM
if [[ '${{ runner.arch }}' == ARM* && '${{ inputs.target }}' == 'aarch64-apple-darwin' ]]; then
pnpm run test:ci
fi
# arch is x64 and target is x64
if [[ '${{ runner.arch }}' != ARM* && '${{ inputs.target }}' != 'aarch64-apple-darwin' ]]; then
pnpm run test:ci
fi
### x86_64-pc-windows-msvc

Expand Down
17 changes: 0 additions & 17 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,6 @@ impl ContextNameSpaceObject {
}
}

pub fn context_reg_exp(expr: &str, flags: &str) -> Option<RspackRegex> {
if expr.is_empty() {
return None;
}
let regexp = RspackRegex::with_flags(expr, flags).expect("reg failed");
clean_regexp_in_context_module(regexp)
}

pub fn clean_regexp_in_context_module(regexp: RspackRegex) -> Option<RspackRegex> {
if regexp.sticky() || regexp.global() {
// TODO: warning
None
} else {
Some(regexp)
}
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, PartialOrd, Ord, Eq)]
pub enum ContextTypePrefix {
Import,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use itertools::Itertools;
use rspack_core::{
context_reg_exp, ConstDependency, ContextMode, DependencyCategory, ErrorSpan, SpanExt,
};
use rspack_core::{ConstDependency, ContextMode, DependencyCategory, ErrorSpan, SpanExt};
use rspack_core::{ContextNameSpaceObject, ContextOptions};
use rspack_error::Severity;
use swc_core::common::{Span, Spanned};
Expand All @@ -13,7 +11,8 @@ use crate::dependency::{CommonJsFullRequireDependency, CommonJsRequireContextDep
use crate::dependency::{CommonJsRequireDependency, RequireResolveDependency};
use crate::utils::eval::{self, BasicEvaluatedExpression};
use crate::visitors::{
create_context_dependency, create_traceable_error, expr_matcher, expr_name, JavascriptParser,
context_reg_exp, create_context_dependency, create_traceable_error, expr_matcher, expr_name,
JavascriptParser,
};
use crate::visitors::{extract_require_call_info, is_require_call_start};

Expand All @@ -29,7 +28,7 @@ fn create_commonjs_require_context_dependency(
let options = ContextOptions {
mode: ContextMode::Sync,
recursive: true,
reg_exp: context_reg_exp(&result.reg, ""),
reg_exp: context_reg_exp(&result.reg, "", None, parser),
include: None,
exclude: None,
category: DependencyCategory::CommonJS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rspack_core::{
clean_regexp_in_context_module, context_reg_exp, ContextMode, ContextNameSpaceObject,
ContextOptions, DependencyCategory, SpanExt,
ContextMode, ContextNameSpaceObject, ContextOptions, DependencyCategory, SpanExt,
};
use rspack_regex::RspackRegex;
use swc_core::common::Spanned;
Expand All @@ -10,11 +9,13 @@ use super::JavascriptParserPlugin;
use crate::dependency::ImportMetaContextDependency;
use crate::utils::eval::{self, BasicEvaluatedExpression};
use crate::utils::{get_bool_by_obj_prop, get_literal_str_by_obj_prop, get_regex_by_obj_prop};
use crate::visitors::{expr_name, JavascriptParser};
use crate::visitors::{
clean_regexp_in_context_module, context_reg_exp, expr_name, JavascriptParser,
};

fn create_import_meta_context_dependency(
node: &CallExpr,
optional: bool,
parser: &mut JavascriptParser,
) -> Option<ImportMetaContextDependency> {
assert!(node.callee.is_expr());
let dyn_imported = node.args.first()?;
Expand Down Expand Up @@ -43,7 +44,9 @@ fn create_import_meta_context_dependency(
})?;
let reg = r"^\.\/.*$";
let context_options = if let Some(obj) = node.args.get(1).and_then(|arg| arg.expr.as_object()) {
let regexp = get_regex_by_obj_prop(obj, "regExp")
let regexp = get_regex_by_obj_prop(obj, "regExp");
let regexp_span = regexp.map(|r| r.span().into());
let regexp = regexp
.map(|regexp| RspackRegex::try_from(regexp).expect("reg failed"))
.unwrap_or(RspackRegex::new(reg).expect("reg failed"));
// let include = get_regex_by_obj_prop(obj, "include")
Expand All @@ -57,7 +60,7 @@ fn create_import_meta_context_dependency(
.map(|bool| bool.value)
.unwrap_or(true);
ContextOptions {
reg_exp: clean_regexp_in_context_module(regexp),
reg_exp: clean_regexp_in_context_module(regexp, regexp_span, parser),
include: None,
exclude: None,
recursive,
Expand All @@ -77,7 +80,7 @@ fn create_import_meta_context_dependency(
mode: ContextMode::Sync,
include: None,
exclude: None,
reg_exp: context_reg_exp(reg, ""),
reg_exp: context_reg_exp(reg, "", None, parser),
category: DependencyCategory::Esm,
request: context.clone(),
context,
Expand All @@ -93,7 +96,7 @@ fn create_import_meta_context_dependency(
node.span.real_hi(),
context_options,
Some(node.span.into()),
optional,
parser.in_try,
))
}

Expand Down Expand Up @@ -131,7 +134,7 @@ impl JavascriptParserPlugin for ImportMetaContextDependencyParserPlugin {
|| expr.args.len() > 2
{
None
} else if let Some(dep) = create_import_meta_context_dependency(expr, parser.in_try) {
} else if let Some(dep) = create_import_meta_context_dependency(expr, parser) {
parser.dependencies.push(Box::new(dep));
Some(true)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rspack_core::{
context_reg_exp, AsyncDependenciesBlock, DependencyLocation, DynamicImportMode, ErrorSpan,
GroupOptions,
AsyncDependenciesBlock, DependencyLocation, DynamicImportMode, ErrorSpan, GroupOptions,
};
use rspack_core::{ChunkGroupOptions, DynamicImportFetchPriority};
use rspack_core::{ContextNameSpaceObject, ContextOptions, DependencyCategory, SpanExt};
Expand All @@ -10,7 +9,8 @@ use swc_core::ecma::ast::{CallExpr, Callee};
use super::JavascriptParserPlugin;
use crate::dependency::{ImportContextDependency, ImportDependency, ImportEagerDependency};
use crate::visitors::{
create_context_dependency, parse_order_string, ContextModuleScanResult, JavascriptParser,
context_reg_exp, create_context_dependency, parse_order_string, ContextModuleScanResult,
JavascriptParser,
};
use crate::webpack_comment::try_extract_webpack_magic_comment;

Expand Down Expand Up @@ -120,6 +120,7 @@ impl JavascriptParserPlugin for ImportParserPlugin {
fragment,
replaces,
} = create_context_dependency(&param, parser);
let reg_exp = context_reg_exp(&reg, "", Some(dyn_imported.span().into()), parser);
parser
.dependencies
.push(Box::new(ImportContextDependency::new(
Expand All @@ -129,7 +130,7 @@ impl JavascriptParserPlugin for ImportParserPlugin {
ContextOptions {
mode: mode.into(),
recursive: true,
reg_exp: context_reg_exp(&reg, ""),
reg_exp,
include: None,
exclude: None,
category: DependencyCategory::Esm,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rspack_core::{clean_regexp_in_context_module, try_convert_str_to_context_mode};
use rspack_core::try_convert_str_to_context_mode;
use rspack_core::{ContextMode, ContextOptions, DependencyCategory, SpanExt};
use rspack_regex::RspackRegex;
use swc_core::common::Spanned;
Expand All @@ -7,7 +7,7 @@ use swc_core::ecma::ast::CallExpr;
use super::JavascriptParserPlugin;
use crate::dependency::RequireContextDependency;
use crate::visitors::expr_matcher::is_require_context;
use crate::visitors::JavascriptParser;
use crate::visitors::{clean_regexp_in_context_module, JavascriptParser};

pub struct RequireContextDependencyParserPlugin;

Expand Down Expand Up @@ -37,17 +37,21 @@ impl JavascriptParserPlugin for RequireContextDependencyParserPlugin {
ContextMode::Sync
};

let reg_exp = if expr.args.len() >= 3 {
let (reg_exp, reg_exp_span) = if expr.args.len() >= 3 {
let reg_exp_expr = parser.evaluate_expression(&expr.args[2].expr);
if !reg_exp_expr.is_regexp() {
let reg_exp = if !reg_exp_expr.is_regexp() {
// FIXME: return `None` in webpack
RspackRegex::new(DEFAULT_REGEXP_STR).expect("reg should success")
} else {
let (expr, flags) = reg_exp_expr.regexp();
RspackRegex::with_flags(expr.as_str(), flags.as_str()).expect("reg should success")
}
};
(reg_exp, Some(expr.args[2].expr.span().into()))
} else {
RspackRegex::new(DEFAULT_REGEXP_STR).expect("reg should success")
(
RspackRegex::new(DEFAULT_REGEXP_STR).expect("reg should success"),
None,
)
};

let recursive = if expr.args.len() >= 2 {
Expand All @@ -62,12 +66,13 @@ impl JavascriptParserPlugin for RequireContextDependencyParserPlugin {
true
};

if !expr.args.is_empty() {
let request_expr = parser.evaluate_expression(&expr.args[0].expr);
if let Some(arg) = expr.args.first() {
let request_expr = parser.evaluate_expression(&arg.expr);
if !request_expr.is_string() {
return None;
}

let reg_exp = clean_regexp_in_context_module(reg_exp, reg_exp_span, parser);
parser
.dependencies
.push(Box::new(RequireContextDependency::new(
Expand All @@ -76,7 +81,7 @@ impl JavascriptParserPlugin for RequireContextDependencyParserPlugin {
ContextOptions {
mode,
recursive,
reg_exp: clean_regexp_in_context_module(reg_exp),
reg_exp,
include: None,
exclude: None,
category: DependencyCategory::CommonJS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::borrow::Cow;

use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
use rspack_core::parse_resource;
use rspack_error::Severity;
use rspack_util::json_stringify;

use super::context_helper::{quote_meta, split_context_from_prefix};
use super::{create_traceable_error, ContextModuleScanResult};
use super::create_traceable_error;
use crate::utils::eval::{BasicEvaluatedExpression, TemplateStringKind};

// FIXME: delete this after `parserOptions.wrappedContextRegExp.source`
Expand Down Expand Up @@ -190,3 +191,27 @@ pub fn create_context_dependency(
}
}
}

pub struct ContextModuleScanResult {
pub context: String,
pub reg: String,
pub query: String,
pub fragment: String,
pub replaces: Vec<(String, u32, u32)>,
}

pub(super) fn split_context_from_prefix(prefix: String) -> (String, String) {
if let Some(idx) = prefix.rfind('/') {
(prefix[..idx].to_string(), format!(".{}", &prefix[idx..]))
} else {
(".".to_string(), prefix)
}
}

static META_REG: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"[-\[\]\\/{}()*+?.^$|]").expect("Failed to initialize `MATCH_RESOURCE_REGEX`")
});

pub fn quote_meta(str: &str) -> Cow<str> {
META_REG.replace_all(str, "\\$0")
}
Loading

0 comments on commit 58ca484

Please sign in to comment.