Skip to content

Commit

Permalink
entrypoints.json reorganisation
Browse files Browse the repository at this point in the history
  • Loading branch information
lhapaipai committed Oct 31, 2023
1 parent ca167ed commit 8a8e56b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 183 deletions.
99 changes: 40 additions & 59 deletions src/entryPointsHelper.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import type { ResolvedConfig } from "vite";
import { getLegacyName, prepareRollupInputs } from "./utils";
import { EntryPoints, EntryPoint, StringMapping, GeneratedFiles, FileInfos, FileWithHash } from "./types";
import { EntryPoints, EntryPoint, StringMapping, GeneratedFiles, FileInfos, FilesMetadatas } from "./types";

export const getDevEntryPoints = (config: ResolvedConfig, viteDevServerUrl: string): EntryPoints => {
const entryPoints: EntryPoints = {};

for (const [entryName, { inputRelPath, inputType }] of Object.entries(prepareRollupInputs(config))) {
entryPoints[entryName] = {
[inputType]: [
{
path: `${viteDevServerUrl}${config.base}${inputRelPath}`,
hash: null,
},
],
[inputType]: [`${viteDevServerUrl}${config.base}${inputRelPath}`],
};
}
return entryPoints;
};

export const getFilesMetadatas = (base: string, generatedFiles: GeneratedFiles): FilesMetadatas => {
return Object.fromEntries(
Object.values(generatedFiles)
.filter((fileInfos: FileInfos) => fileInfos.hash)
.map((fileInfos: FileInfos) => [
`${base}${fileInfos.outputRelPath}`,
{
hash: fileInfos.hash,
},
]),
);
};

export const getBuildEntryPoints = (
generatedFiles: GeneratedFiles,
viteConfig: ResolvedConfig,
Expand Down Expand Up @@ -71,11 +79,10 @@ export const resolveEntrypoint = (
legacyEntryName: boolean | string,
resolvedImportOutputRelPaths: string[] = [],
): EntryPoint => {
const assets: FileWithHash[] = [];
const css: FileWithHash[] = [];
const js: FileWithHash[] = [];
const preload: FileWithHash[] = [];
const dynamic: FileWithHash[] = [];
const css: string[] = [];
const js: string[] = [];
const preload: string[] = [];
const dynamic: string[] = [];

resolvedImportOutputRelPaths.push(fileInfos.outputRelPath);

Expand All @@ -92,86 +99,60 @@ export const resolveEntrypoint = (
}

const {
assets: importAssets,
css: importCss,
dynamic: importDynamic,
js: importJs,
preload: importPreload,
} = resolveEntrypoint(importFileInfos, generatedFiles, config, false, resolvedImportOutputRelPaths);

for (const dependencyWithHash of importCss) {
if (css.findIndex((file) => file.path === dependencyWithHash.path) === -1) {
css.push(dependencyWithHash);
for (const dependency of importCss) {
if (css.indexOf(dependency) === -1) {
css.push(dependency);
}
}

// imports are preloaded not js files
for (const dependencyWithHash of importJs) {
if (preload.findIndex((file) => file.path === dependencyWithHash.path) === -1) {
preload.push(dependencyWithHash);
}
}
for (const dependencyWithHash of importPreload) {
if (preload.findIndex((file) => file.path === dependencyWithHash.path) === -1) {
preload.push(dependencyWithHash);
for (const dependency of importJs) {
if (preload.indexOf(dependency) === -1) {
preload.push(dependency);
}
}
for (const dependencyWithHash of importDynamic) {
if (dynamic.findIndex((file) => file.path === dependencyWithHash.path) === -1) {
dynamic.push(dependencyWithHash);
for (const dependency of importPreload) {
if (preload.indexOf(dependency) === -1) {
preload.push(dependency);
}
}
for (const dependencyWithHash of importAssets) {
if (assets.findIndex((file) => file.path === dependencyWithHash.path) === -1) {
assets.push(dependencyWithHash);
for (const dependency of importDynamic) {
if (dynamic.indexOf(dependency) === -1) {
dynamic.push(dependency);
}
}
}

fileInfos.assets.forEach((dependency) => {
if (assets.findIndex((file) => file.path === dependency) === -1) {
assets.push({
path: `${config.base}${dependency}`,
hash: generatedFiles[dependency].hash,
});
}
});
fileInfos.js.forEach((dependency) => {
if (js.findIndex((file) => file.path === dependency) === -1) {
js.push({
path: `${config.base}${dependency}`,
hash: generatedFiles[dependency].hash,
});
if (js.indexOf(dependency) === -1) {
js.push(`${config.base}${dependency}`);
}
});
fileInfos.preload.forEach((dependency) => {
if (preload.findIndex((file) => file.path === dependency) === -1) {
preload.push({
path: `${config.base}${dependency}`,
hash: generatedFiles[dependency].hash,
});
if (preload.indexOf(dependency) === -1) {
preload.push(`${config.base}${dependency}`);
}
});
fileInfos.dynamic.forEach((dependency) => {
if (dynamic.findIndex((file) => file.path === dependency) === -1) {
dynamic.push({
path: `${config.base}${dependency}`,
hash: generatedFiles[dependency].hash,
});
if (dynamic.indexOf(dependency) === -1) {
dynamic.push(`${config.base}${dependency}`);
}
});
}

if (fileInfos.type === "js" || fileInfos.type === "css") {
fileInfos.css.forEach((dependency) => {
if (css.findIndex((file) => file.path === dependency) === -1) {
css.push({
path: `${config.base}${dependency}`,
hash: generatedFiles[dependency].hash,
});
if (css.indexOf(dependency) === -1) {
css.push(`${config.base}${dependency}`);
}
});
}

return { assets, css, dynamic, js, legacy: legacyEntryName, preload };
return { css, dynamic, js, legacy: legacyEntryName, preload };
};
15 changes: 7 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import colors from "picocolors";

import type { RenderedChunk, OutputAsset, NormalizedOutputOptions, OutputChunk } from "rollup";

import { getDevEntryPoints, getBuildEntryPoints } from "./entryPointsHelper";
import { getDevEntryPoints, getBuildEntryPoints, getFilesMetadatas } from "./entryPointsHelper";
import {
normalizePath,
writeJson,
Expand Down Expand Up @@ -143,13 +143,11 @@ export default function symfony(userOptions: Partial<VitePluginSymfonyOptions> =

const entryPointsPath = resolve(viteConfig.root, viteConfig.build.outDir, entryPointsBasename);
writeJson(entryPointsPath, {
isBuild: false,
viteServer: {
origin: viteDevServerUrl,
base: viteConfig.base,
},
base: viteConfig.base,
entryPoints,
legacy: false,
metadatas: {},
viteServer: viteDevServerUrl,
});
}

Expand Down Expand Up @@ -250,10 +248,11 @@ export default function symfony(userOptions: Partial<VitePluginSymfonyOptions> =
fileName: entryPointsBasename,
source: JSON.stringify(
{
base: viteConfig.base,
entryPoints,
isBuild: true,
legacy: typeof entryPoints["polyfills-legacy"] !== "undefined",
viteServer: false,
metadatas: getFilesMetadatas(viteConfig.base, generatedFiles),
viteServer: null,
},
null,
pluginOptions.debug ? 2 : null,
Expand Down
10 changes: 2 additions & 8 deletions src/tests/entryPointsHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@ describe("getDevEntryPoints", () => {
{
"app": {
"js": [
{
"hash": null,
"path": "http://localhost:5173/build/path/to/filename.ts",
},
"http://localhost:5173/build/path/to/filename.ts",
],
},
"theme": {
"css": [
{
"hash": null,
"path": "http://localhost:5173/build/other/place/to/theme.scss",
},
"http://localhost:5173/build/other/place/to/theme.scss",
],
},
}
Expand Down
Loading

0 comments on commit 8a8e56b

Please sign in to comment.