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: bundle asset source #1567

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion crates/toolkit/src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ pub fn codegen_html_document(document: &Document, minify: bool) -> String {
html_writer,
CodegenConfig {
minify,
quotes: Some(true),
..Default::default()
},
);

html_gen.emit(document).unwrap();

html_code
}

Expand Down
11 changes: 9 additions & 2 deletions examples/vite-adapter-react/farm.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { defineConfig } from '@farmfe/core';
import Pages from 'vite-plugin-pages';
import react from '@farmfe/plugin-react'
import { viteSingleFile } from "vite-plugin-singlefile"

export default defineConfig({
plugins: [
react({ runtime: 'automatic', refresh: true})
],
compilation:{
persistentCache: false
persistentCache: false,
minify: false,
output: {
publicPath: '/a',
targetEnv: 'browser',
}
},
vitePlugins: [
Pages({
resolver: 'react'
})
}),
viteSingleFile()
]
});
3 changes: 2 additions & 1 deletion examples/vite-adapter-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"@types/react-dom": "18",
"farm-plugin-remove-console": "^0.1.4",
"react-refresh": "^0.14.0",
"vite-plugin-pages": "^0.32.0"
"vite-plugin-pages": "^0.32.0",
"vite-plugin-singlefile": "^2.0.2"
},
"scripts": {
"dev": "farm start",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/config/normalize-config/normalize-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ function tryGetDefaultPublicPath(
if (!targetEnv) {
return publicPath;
}
console.log('🤖 == publicPath:', publicPath);

if (publicPath) {
if (urlRegex.test(publicPath)) {
Expand Down Expand Up @@ -250,6 +251,7 @@ export function normalizePublicPath(
logger.warn(
` (!) Irregular 'publicPath' options: '${publicPath}', it should only be an absolute path like '/publicPath/', './', an url or an empty string.`
);
console.log('🤖 == defaultPublicPath:', defaultPublicPath);

return defaultPublicPath;
}
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/plugin/js/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,21 @@ export function transformResourceInfo2RollupResource(
type: 'chunk',
code: source,
name: resource.name,
fileName: resource.name,
map: undefined,
sourcemapFileName: null,
preliminaryFileName: resource.origin.value
} satisfies OutputChunk;
} else {
let source: string | Uint8Array = Uint8Array.from(resource.bytes);
if (resource.resourceType === 'html' || resource.resourceType === 'css') {
source = Buffer.from(resource.bytes).toString('utf-8');
}
return {
fileName: resource.name,
name: resource.name,
needsCodeReference: false,
source: Uint8Array.from(resource.bytes),
source,
type: 'asset'
} satisfies OutputAsset;
}
Expand All @@ -331,6 +336,10 @@ export function transformRollupResource2FarmResource(
name: chunk.name
};
} else {
if (chunk.fileName.endsWith('.html') || chunk.fileName.endsWith('.css')) {
const encoder = new TextEncoder();
chunk.source = encoder.encode(chunk.source as string);
}
return {
bytes: Array.from(chunk.source as Uint8Array) as number[],
emitted: originResource.emitted,
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/plugin/js/vite-plugin-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,6 @@ export class VitePluginAdapter implements JsPlugin {
},
{} as OutputBundle
);

await hook?.(
transformFarmConfigToRollupNormalizedOutputOptions(param.config),
bundles
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export function staticMiddleware(devServerContext: Server): Middleware {

return async (ctx: Context, next: Next) => {
const requestPath = ctx.request?.path;
console.log('🤖 == return == requestPath:', requestPath);

if (requestPath && requestPath.startsWith(config.output.publicPath)) {
const modifiedPath = requestPath.substring(
config.output.publicPath.length
);
console.log('🤖 == return == modifiedPath:', modifiedPath);

ctx.request.path = `/${modifiedPath}`;

Expand Down
41 changes: 41 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading