Skip to content

Commit

Permalink
fix: vite migrate bugs (#912)
Browse files Browse the repository at this point in the history
* fix: vite migrate bugs

* chore: update tests

* fix: default node buildin externals

---------

Co-authored-by: brightwwu <[email protected]>
  • Loading branch information
wre232114 and brightwwu authored Jan 25, 2024
1 parent 069249a commit 116ffa9
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-ducks-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farmfe/core': patch
---

Fix bugs && Support object result of transformIndexHtml Hook
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Farm has implemented all basic features for a web building tool.
- [x] Tree Shaking
- [x] CSS Modules
- [x] Official Plugins like Sass
- [ ] Persistent Cache
- [x] Persistent Cache
- [x] Polyfill

See milestones: https://github.com/farm-fe/farm/milestones
Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Farm 目前已经实现了 Web 构建工具的所有基本功能。
- [x] Tree Shaking
- [x] CSS Modules
- [x] Official Plugins like Sass
- [ ] Persistent Cache
- [x] Persistent Cache
- [x] Polyfill

请参阅里程碑: https://github.com/farm-fe/farm/milestones
Expand Down
1 change: 1 addition & 0 deletions crates/compiler/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl Compiler {

// try load source map after load module content.
// TODO load source map in load hook and add a context.load_source_map method
// TODO load css source map
if context.config.sourcemap.enabled(module.immutable)
&& load_result.content.contains("//# sourceMappingURL")
{
Expand Down
14 changes: 8 additions & 6 deletions crates/plugin_static_assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ use farmfe_toolkit::{
fs::{read_file_raw, read_file_utf8, transform_output_filename},
lazy_static::lazy_static,
};
use farmfe_utils::stringify_query;

// Default supported static assets: png, jpg, jpeg, gif, svg, webp, mp4, webm, wav, mp3, wma, m4a, aac, ico, ttf, woff, woff2
lazy_static! {
static ref DEFAULT_STATIC_ASSETS: Vec<&'static str> = vec![
"png", "jpg", "jpeg", "gif", "svg", "webp", "mp4", "webm", "wav", "mp3", "wma", "m4a", "aac",
"ico", "ttf", "woff", "woff2", "txt"
"ico", "ttf", "woff", "woff2", "txt", "eot"
];
}

Expand Down Expand Up @@ -178,10 +179,6 @@ impl Plugin for FarmPluginStaticAssets {
ignore_previous_source_map: false,
}));
} else {
let filename = Path::new(param.resolved_path)
.file_prefix()
.and_then(|s| s.to_str())
.unwrap();
let bytes = if param.content.is_empty() {
read_file_raw(param.resolved_path)?
} else {
Expand All @@ -192,11 +189,16 @@ impl Plugin for FarmPluginStaticAssets {
)
)?
};

let ext = Path::new(param.resolved_path)
.extension()
.and_then(|s| s.to_str())
.unwrap();

let filename = Path::new(param.resolved_path)
.file_prefix()
.and_then(|s| s.to_str())
.unwrap();
let resource_name = transform_output_filename(
context.config.output.assets_filename.clone(),
filename,
Expand Down Expand Up @@ -225,7 +227,7 @@ impl Plugin for FarmPluginStaticAssets {
// TODO refactor this to support cache
context.emit_file(EmitFileParams {
resolved_path: param.module_id.clone(),
name: resource_name,
name: resource_name + stringify_query(&param.query).as_str(),
content: bytes,
resource_type: ResourceType::Asset(ext.to_string()),
});
Expand Down
6 changes: 3 additions & 3 deletions crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ pub use pathdiff::diff_paths;

pub mod hash;

pub const PARSE_QUERY_TRUE: &str = "true";
pub const PARSE_QUERY_TRUE: &str = "";

/// parse `?a=b` to `HashMap { a: b }`, `?a` to `HashMap { a: "true" }`
/// parse `?a=b` to `HashMap { a: b }`, `?a` to `HashMap { a: "" }`
pub fn parse_query(path: &str) -> Vec<(String, String)> {
if !path.contains('?') {
return vec![];
Expand Down Expand Up @@ -138,7 +138,7 @@ mod tests {
assert_eq!(
parsed_query,
vec![
("inline".to_string(), "true".to_string()),
("inline".to_string(), "".to_string()),
("b".to_string(), "c".to_string())
]
);
Expand Down
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@
"apng",
"jfif",
"pjpeg",
"flac"
"flac",
"Yuxi"
],
"ignorePaths": [
"pnpm-lock.yaml",
Expand Down
1 change: 1 addition & 0 deletions packages/core/binding/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export interface Config {
* Configure the imports that are external, and the imports that are external will not appear in the compiled product.
*/
external?: string[];
externalNodeBuiltins?: boolean | string[];
mode?: 'development' | 'production';
root?: string;
runtime?: RuntimeConfig;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ export class Compiler {
: path.join(this.config.config.root, configOutputPath);

for (const [name, resource] of Object.entries(resources)) {
if (process.env.NODE_ENV === 'test') {
console.log('Writing', name, 'to disk');
}
// remove query params and hash of name
const nameWithoutQuery = name.split('?')[0];
const nameWithoutHash = nameWithoutQuery.split('#')[0];

const filePath = path.join(outputPath, base, name);
const filePath = path.join(outputPath, base, nameWithoutHash);

if (!existsSync(path.dirname(filePath))) {
mkdirSync(path.dirname(filePath), { recursive: true });
Expand Down
8 changes: 1 addition & 7 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export async function resolveConfig(
);

let vitePluginAdapters: JsPlugin[] = [];
const vitePlugins = userConfig?.vitePlugins ?? [];
const vitePlugins = (userConfig?.vitePlugins ?? []).filter(Boolean);
// run config and configResolved hook
if (vitePlugins.length) {
vitePluginAdapters = await handleVitePlugins(
Expand Down Expand Up @@ -222,12 +222,6 @@ export async function normalizeUserCompilationConfig(

config.coreLibPath = bindingPath;

config.external = [
...module.builtinModules.map((m) => `^${m}$`),
...module.builtinModules.map((m) => `^node:${m}$`),
...(Array.isArray(config.external) ? config.external : [])
];

normalizeOutput(config, isProduction);
normalizeExternal(config);

Expand Down
37 changes: 32 additions & 5 deletions packages/core/src/config/normalize-config/normalize-external.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import module from 'node:module';

import { Config } from '../../../binding/index.js';
import { existsSync, readFileSync } from 'node:fs';
import path from 'node:path';

export function normalizeExternal(config: Config['config']) {
const defaultExternals = [...module.builtinModules, ...module.builtinModules]
.filter((m) => !config.resolve?.alias?.[m])
.map((m) => `^${m}$`);
const defaultExternals: string[] = [];
const externalNodeBuiltins = config.externalNodeBuiltins ?? true;

if (externalNodeBuiltins) {
if (Array.isArray(externalNodeBuiltins)) {
defaultExternals.push(...externalNodeBuiltins);
} else if (externalNodeBuiltins === true) {
let packageJson: any = {};
const pkgPath = path.join(config.root || process.cwd(), 'package.json');
// the project installed polyfill
if (existsSync(pkgPath)) {
try {
packageJson = JSON.parse(readFileSync(pkgPath, 'utf8'));
} catch {
/**/
}
}

defaultExternals.push(
...[...module.builtinModules].filter(
(m) =>
!config.resolve?.alias?.[m] &&
!packageJson?.devDependencies?.[m] &&
!packageJson?.dependencies?.[m]
)
);
}
}

config.external = [
...(config.external ?? []),
...defaultExternals.map((m) => `^${m}($|/)`),
...defaultExternals.map((m) => `^node:${m}($|/)`)
...defaultExternals.map((m) => `^${m}($|/promises$)`),
...defaultExternals.map((m) => `^node:${m}($|/promises$)`)
];
}
3 changes: 3 additions & 0 deletions packages/core/src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const compilationConfigSchema = z
.optional(),
define: z.record(z.any()).optional(),
external: z.array(z.string()).optional(),
externalNodeBuiltins: z
.union([z.boolean(), z.array(z.string())])
.optional(),
mode: z.string().optional(),
watch: z
.union([
Expand Down
Loading

0 comments on commit 116ffa9

Please sign in to comment.