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: support case sensitive #1714

Merged
merged 24 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
os: [ ubuntu-latest, macos-latest ]
runs-on: ${{ matrix.os }}
if: ${{ !startsWith(github.event.head_commit.message, 'release:') && !startsWith(github.event.head_commit.message, 'ci:') && !startsWith(github.event.head_commit.message, 'docs:') }}
steps:
Expand Down Expand Up @@ -90,7 +90,7 @@ jobs:
fail-fast: false
matrix:
script: [ "test:e2e", "test:hmr", "test:umi" ]
os: [ ubuntu-latest ]
os: [ ubuntu-latest, macos-latest ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub struct BuildParams {
ignoredPaths?: string[];
_nodeModulesRegexes?: string[];
};
caseSensitiveCheck?: boolean;
}"#)]
pub config: serde_json::Value,
pub plugins: Vec<JsHooks>,
Expand Down
4 changes: 4 additions & 0 deletions crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ impl Compiler {
},
)));
}
#[cfg(target_os = "macos")]
if config.case_sensitive_check {
plugins.push(Arc::new(plugins::case_sensitive::CaseSensitivePlugin::new()));
}

if let Some(duplicate_package_checker) = &config.check_duplicate_package {
plugins.push(Arc::new(
Expand Down
7 changes: 7 additions & 0 deletions crates/mako/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ pub struct Config {
default
)]
pub check_duplicate_package: Option<DuplicatePackageCheckerConfig>,
// 是否开启 case sensitive 检查,只有mac平台才需要开启
#[serde(rename = "caseSensitiveCheck", default = "default_case_sensitive")]
pub case_sensitive_check: bool,
}

fn default_case_sensitive() -> bool {
std::env::consts::OS == "macos"
}

const CONFIG_FILE: &str = "mako.config.json";
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod async_runtime;
pub mod bundless_compiler;
pub mod case_sensitive;
pub mod central_ensure;
pub mod context_module;
pub mod copy;
Expand Down
95 changes: 95 additions & 0 deletions crates/mako/src/plugins/case_sensitive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::sync::{Arc, Mutex};

use anyhow::{anyhow, Result};

use crate::ast::file::Content;
use crate::compiler::Context;
use crate::plugin::{Plugin, PluginLoadParam};

pub struct CaseSensitivePlugin {
cache_map: Arc<Mutex<HashMap<String, Vec<String>>>>,
}

impl CaseSensitivePlugin {
pub fn new() -> Self {

Check warning on line 17 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L17

Added line #L17 was not covered by tests
CaseSensitivePlugin {
cache_map: Default::default(),

Check warning on line 19 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L19

Added line #L19 was not covered by tests
}
}

Check warning on line 21 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L21

Added line #L21 was not covered by tests

pub fn is_checkable(&self, load_param: &PluginLoadParam, root: &String) -> bool {
let file_path = &load_param.file.path;
if !file_path.starts_with(root) {
return false;

Check warning on line 26 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L23-L26

Added lines #L23 - L26 were not covered by tests
}
for component in file_path.iter() {
if component.eq_ignore_ascii_case("node_modules") {
return false;

Check warning on line 30 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L28-L30

Added lines #L28 - L30 were not covered by tests
}
}
true
}

Check warning on line 34 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L33-L34

Added lines #L33 - L34 were not covered by tests

pub fn check_case_sensitive(&self, file: &Path, root: &str) -> String {

Check warning on line 36 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L36

Added line #L36 was not covered by tests
// 可变变量,在循环内会被修改
let mut file_path = file.to_path_buf();
let mut case_name = String::new();

Check warning on line 39 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L38-L39

Added lines #L38 - L39 were not covered by tests
// 缓存map,file path做为key存在对应路径下的文件名和文件夹名
let mut cache_map = self.cache_map.lock().unwrap_or_else(|e| e.into_inner());
while file_path.to_string_lossy().len() >= root.len() {
if let Some(current) = file_path.file_name() {
let current_str = current.to_string_lossy().to_string();
file_path.pop(); // parent directory
let mut entries: Vec<String> = Vec::new();
if let Some(dir) = file_path.to_str() {
if let Some(i) = cache_map.get(dir as &str) {
entries = i.to_vec();
} else if let Ok(files) = fs::read_dir(dir) {
files.for_each(|entry| {
entries.push(entry.unwrap().file_name().to_string_lossy().to_string());
});
cache_map.insert(dir.to_string(), entries.to_vec());
}

Check warning on line 55 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L41-L55

Added lines #L41 - L55 were not covered by tests
}
if !entries.contains(&current_str) {
if let Some(correct_name) = entries

Check warning on line 58 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L57-L58

Added lines #L57 - L58 were not covered by tests
.iter()
.find(|&x| x.to_lowercase() == current_str.to_lowercase())

Check warning on line 60 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L60

Added line #L60 was not covered by tests
{
case_name = correct_name.to_string();

Check warning on line 62 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L62

Added line #L62 was not covered by tests
break;
}
}
}

Check warning on line 66 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L66

Added line #L66 was not covered by tests
}
case_name
}

Check warning on line 69 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L68-L69

Added lines #L68 - L69 were not covered by tests
}

impl Plugin for CaseSensitivePlugin {
fn name(&self) -> &str {

Check warning on line 73 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L73

Added line #L73 was not covered by tests
"case_sensitive_plugin"
}

Check warning on line 75 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L75

Added line #L75 was not covered by tests

fn load(

Check warning on line 77 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L77

Added line #L77 was not covered by tests
&self,
load_param: &PluginLoadParam,
context: &Arc<Context>,
) -> Result<Option<Content>> {
let root = &context.root.to_string_lossy().to_string();
if self.is_checkable(load_param, root) {
let dist_path = self.check_case_sensitive(load_param.file.path.as_path(), root);
if !dist_path.is_empty() {
return Err(anyhow!(

Check warning on line 86 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L82-L86

Added lines #L82 - L86 were not covered by tests
"{} does not match the corresponding path on disk [{}]",
load_param.file.path.to_string_lossy().to_string(),

Check warning on line 88 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L88

Added line #L88 was not covered by tests
dist_path
));
}
}
Ok(None)
}

Check warning on line 94 in crates/mako/src/plugins/case_sensitive.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/case_sensitive.rs#L92-L94

Added lines #L92 - L94 were not covered by tests
}
16 changes: 16 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ Whether to automatically enable CSS Modules.

If not enabled, only files with `.module.css` or `.module.less` will be treated as CSS Modules; if enabled, named imports like `import styles from './a.css'` will also be treated as CSS Modules.

### caseSensitiveCheck

- Type: `boolean`
- Default: `true`

Whether to enable case-sensitive check.

e.g.

```ts
{
caseSensitiveCheck: false,
}
```


### clean

- Type: `boolean`
Expand Down
17 changes: 17 additions & 0 deletions docs/config.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@

如果未启用,只有 `.module.css` 或 `.module.less` 的文件会被视为 CSS Modules;如果启用,像 `import styles from './a.css'` 这样的命名导入也会被视为 CSS Modules。

### caseSensitiveCheck

- 类型:`boolean`
- 默认值:`true`

是否启用大小写敏感检查。

e.g.

```ts
{
caseSensitiveCheck: false,
}
```


### clean

- 类型:`boolean`
Expand Down Expand Up @@ -850,3 +866,4 @@ babel-plugin-import 的简化版本,仅支持三个配置项:libraryName,l
- 默认值:`true`

是否在开发模式下将构建结果写入磁盘。

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions e2e/fixtures/error.case-sensitive/expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const assert = require("assert");
const os = require("os");
const { files } = parseBuildResult(__dirname);

module.exports = (err) => {
if (os.platform() === 'darwin') {
assert(
err.stderr.includes(`/Assets/umi-logo.png does not match the corresponding path on disk [assets]`),
"should throw error"
);
} else {
assert("index.js" in files, "should have file: index.js");
}
};
4 changes: 4 additions & 0 deletions e2e/fixtures/error.case-sensitive/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import UmiLogo from "./Assets/umi-logo.png";
console.log(UmiLogo);

export default UmiLogo;
4 changes: 4 additions & 0 deletions e2e/fixtures/error.case-sensitive/mako.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mode": "production",
"caseSensitiveCheck": true
}
8 changes: 8 additions & 0 deletions e2e/fixtures/error.case-sensitive/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test",
"version": "1.0.0",
"dependencies": {
"a": "~1.0.0",
"b": "~1.0.0"
}
}
Loading