Skip to content

Commit

Permalink
fix: lazy compilation cache && postcss tailwind does not work on wind…
Browse files Browse the repository at this point in the history
…ows (#1808)
  • Loading branch information
wre232114 authored Oct 8, 2024
1 parent 978e2dd commit 9740859
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 98 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-gorillas-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/js-plugin-visualizer": patch
---

Optimize plugin performance panel
5 changes: 5 additions & 0 deletions .changeset/hot-humans-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/core": patch
---

Fix persistent cache panic when working with lazy compilation
5 changes: 5 additions & 0 deletions .changeset/red-fans-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/js-plugin-postcss": patch
---

Fix postcss/tailwind postcss does not work on windows. fix #1777
33 changes: 20 additions & 13 deletions crates/compiler/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,25 @@ impl Compiler {
module
}

pub(crate) fn create_module_from_resolve_result(
resolve_module_id_result: &ResolveModuleIdResult,
context: &Arc<CompilationContext>,
) -> Module {
let module_id_str = resolve_module_id_result.module_id.to_string();
Compiler::create_module(
resolve_module_id_result.module_id.clone(),
resolve_module_id_result.resolve_result.external,
// treat all lazy virtual modules as mutable
!module_id_str.ends_with(DYNAMIC_VIRTUAL_SUFFIX)
&& context
.config
.partial_bundling
.immutable_modules
.iter()
.any(|im| im.is_match(&module_id_str)),
)
}

pub(crate) fn insert_dummy_module(module_id: &ModuleId, module_graph: &mut ModuleGraph) {
// insert a dummy module to the graph to prevent the module from being handled twice
module_graph.add_module(Compiler::create_module(module_id.clone(), false, false));
Expand Down Expand Up @@ -648,20 +667,8 @@ fn resolve_module(
};

Compiler::insert_dummy_module(&resolve_module_id_result.module_id, &mut module_graph);
let module_id_str = resolve_module_id_result.module_id.to_string();
ResolveModuleResult::Success(Box::new(ResolvedModuleInfo {
module: Compiler::create_module(
resolve_module_id_result.module_id.clone(),
resolve_module_id_result.resolve_result.external,
// treat all lazy virtual modules as mutable
!module_id_str.ends_with(DYNAMIC_VIRTUAL_SUFFIX)
&& context
.config
.partial_bundling
.immutable_modules
.iter()
.any(|im| im.is_match(&module_id_str)),
),
module: Compiler::create_module_from_resolve_result(&resolve_module_id_result, context),
resolve_module_id_result,
}))
};
Expand Down
12 changes: 2 additions & 10 deletions crates/compiler/src/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ impl Compiler {
let module = module_graph.module(id).unwrap();
!module.module_type.is_script()
});

let (immutable_resources, mutable_resources) = if should_reload_page {
("window.location.reload()".to_string(), "{}".to_string())
} else if generate_update_resource {
Expand Down Expand Up @@ -756,16 +757,7 @@ fn resolve_module(
);

Ok(ResolveModuleResult::Success(Box::new(ResolvedModuleInfo {
module: Compiler::create_module(
resolve_module_id_result.module_id.clone(),
resolve_module_id_result.resolve_result.external,
context
.config
.partial_bundling
.immutable_modules
.iter()
.any(|im| im.is_match(&module_id.to_string())),
),
module: Compiler::create_module_from_resolve_result(&resolve_module_id_result, context),
resolve_module_id_result,
})))
}
Expand Down
66 changes: 4 additions & 62 deletions crates/compiler/src/update/regenerate_resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use farmfe_core::{
cache::cache_store::CacheStoreKey,
context::CompilationContext,
enhanced_magic_string::types::SourceMapOptions,
error::CompilationError,
module::{module_graph::ModuleGraph, module_group::ModuleGroupId, Module, ModuleId},
module::{module_group::ModuleGroupId, Module, ModuleId},
resource::resource_pot::{ResourcePot, ResourcePotMetaData, ResourcePotType},
};

Expand All @@ -15,13 +14,13 @@ use farmfe_plugin_runtime::render_resource_pot::{
};
use farmfe_plugin_runtime::ASYNC_MODULES;
use farmfe_toolkit::hash::base64_encode;
use farmfe_utils::{hash::sha256, relative};
use farmfe_utils::relative;

use crate::{
generate::render_resource_pots::{
render_resource_pot_generate_resources, render_resource_pots_and_generate_resources,
},
write_cache_async,
write_cache,
};

use super::diff_and_patch_module_graph::DiffResult;
Expand All @@ -30,20 +29,6 @@ mod generate_and_diff_resource_pots;

use generate_and_diff_resource_pots::generate_and_diff_resource_pots;

fn gen_cache_key_for_update_resource_pot(
update_resource_pot: &ResourcePot,
module_graph: &ModuleGraph,
) -> String {
let mut str_to_hash = String::new();

for m in update_resource_pot.modules() {
str_to_hash.push_str(&m.to_string());
str_to_hash.push_str(&module_graph.module(m).unwrap().content_hash);
}

sha256(str_to_hash.as_bytes(), 32)
}

pub fn render_and_generate_update_resource(
updated_module_ids: &Vec<ModuleId>,
diff_result: &DiffResult,
Expand Down Expand Up @@ -91,30 +76,8 @@ pub fn render_and_generate_update_resource(
}
}

let is_lazy = updated_module_ids.iter().any(|id| {
id.to_string()
.ends_with(farmfe_plugin_lazy_compilation::DYNAMIC_VIRTUAL_SUFFIX)
});
let cached_enabled = context.config.persistent_cache.enabled() && is_lazy;

let gen_resource_pot_code =
|resource_pot: &mut ResourcePot| -> farmfe_core::error::Result<String> {
let mut cache_key = None;

if cached_enabled {
cache_key = Some(gen_cache_key_for_update_resource_pot(
resource_pot,
&module_graph,
));
if let Some(result) = context
.cache_manager
.lazy_compile_store
.read_cache(cache_key.as_ref().unwrap())
{
return Ok(String::from_utf8(result).unwrap());
}
}

let async_modules = context.custom.get(ASYNC_MODULES).unwrap();
let async_modules = async_modules.downcast_ref::<HashSet<ModuleId>>().unwrap();
if !resource_pot.modules().is_empty() {
Expand Down Expand Up @@ -173,27 +136,6 @@ pub fn render_and_generate_update_resource(

let code = String::from_utf8(update_resources.resource.bytes).unwrap();

if cached_enabled {
let cache_key = cache_key.unwrap();
let store_key = CacheStoreKey {
name: cache_key.clone(),
key: cache_key,
};
if context
.cache_manager
.lazy_compile_store
.is_cache_changed(&store_key)
{
context
.cache_manager
.lazy_compile_store
.write_cache(std::collections::HashMap::from([(
store_key,
code.as_bytes().to_vec(),
)]));
}
}

return Ok(code);
}

Expand Down Expand Up @@ -282,7 +224,7 @@ pub fn regenerate_resources_for_affected_module_groups(
eprintln!("write plugin cache error: {:?}", err);
});

write_cache_async(context.clone());
write_cache(context.clone());
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/cache/cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use crate::config::Mode;

const FARM_CACHE_VERSION: &str = "0.4.7";
const FARM_CACHE_VERSION: &str = "0.4.8";
const FARM_CACHE_MANIFEST_FILE: &str = "farm-cache.json";

// TODO make CacheStore a trait and implement DiskCacheStore or RemoteCacheStore or more.
Expand Down
11 changes: 7 additions & 4 deletions examples/arcgis/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import visualizer from "@farmfe/js-plugin-visualizer";

export default defineConfig((env) => ({
compilation: {
persistentCache: false,
// persistentCache: false,
minify: env.mode === 'production' ? {
exclude: [
'/node_modules/@arcgis/core/',
]
} : false,
partialBundling: {
groups: [
enforceTargetConcurrentRequests: false,
enforceTargetMinSize: true,
targetMinSize: 1024 * 200,
enforceResources: [
{
name: '[resourceName]',
test: ['.*']
name: 'index',
test: ['^src/.+']
}
]
}
Expand Down
1 change: 0 additions & 1 deletion examples/vite-adapter-vue/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export default defineConfig({
path: "build",
publicPath: "/vue-public-path/",
},
persistentCache: false,
sourcemap: false,
},
plugins: [
Expand Down
10 changes: 7 additions & 3 deletions js-plugins/postcss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,15 @@ export default function farmPostcssPlugin(
} else if (message.type === 'dir-dependency') {
const { dir, glob: globPattern = '**' } = message;
// https://github.com/postcss/postcss/blob/main/docs/guidelines/runner.md#3-dependencies
const files = glob.sync(path.join(dir, globPattern), {
ignore: ['**/node_modules/**']
const files = glob.sync(globPattern, {
ignore: ['**/node_modules/**'],
cwd: dir
});
for (const file of files) {
context.addWatchFile(param.resolvedPath, file);
context.addWatchFile(
param.resolvedPath,
path.isAbsolute(file) ? file : path.join(dir, file)
);
}
} else if (message.type === 'warning') {
console.warn(`[${pluginName}] ${message.text}`);
Expand Down
2 changes: 1 addition & 1 deletion js-plugins/visualizer/src/client/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface QueryParams {
}

const axiosInstance = axios.create({
timeout: 5000
timeout: 60000
});

axiosInstance.interceptors.response.use(
Expand Down
33 changes: 30 additions & 3 deletions js-plugins/visualizer/src/client/src/pages/analysis/Plugin.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<template>
<Card title="Plugin Analysis">
<div>
<Select
style="width: 100%"
:options="allStats.map((item: any) => ({label:item.entries.join(','), value:item.entries.join(',')}))" v-model:value="selectedStat"></Select>
</div>
<Table :loading="loading" :columns="columns" :dataSource="tableData"></Table>
</Card>
</template>

<script lang="ts" setup>
import { computed, ref, onMounted } from 'vue';
import { Card, Table } from 'ant-design-vue';
import { computed, ref, onMounted, watchEffect } from 'vue';
import { Card, Select, Table } from 'ant-design-vue';
import { getPluginStats } from '../../api';
interface TableDataType {
Expand All @@ -23,13 +28,35 @@ interface HookType {
}
const plugin_stats = ref<any | null>(null);
const allStats = ref<any | null>([]);
const loading = ref(false);
const selectedStat = ref('');
watchEffect(() => {
if (selectedStat.value) {
const stat = allStats.value.find((item: any) => item.entries.join(',') === selectedStat.value);
if (stat) {
plugin_stats.value = stat.hookStatsMap;
} else {
plugin_stats.value = null;
}
}
});
onMounted(() => {
loading.value = true;
getPluginStats().then((data) => {
const parsedData = JSON.parse(data);
const result = [...parsedData.hmrCompilationFlowStats, parsedData.initialCompilationFlowStats];
allStats.value = result.map((item: any) => {
item.entries = item.entries.filter((item: any) => !item.endsWith('.farm-runtime'))
return item;
});
// TODO support HMR compilation compare
plugin_stats.value = JSON.parse(data).initialCompilationFlowStats.hookStatsMap;
plugin_stats.value = parsedData.initialCompilationFlowStats.hookStatsMap;
selectedStat.value = result[0]?.entries.join(',');
console.log('plugin_stats:', plugin_stats.value);
}).finally(() => {
loading.value = false;
});
Expand Down

0 comments on commit 9740859

Please sign in to comment.