vite-plugin-symfony
Advanced tools
Comparing version
@@ -5,3 +5,2 @@ import type { ResolvedConfig } from "vite"; | ||
declare const addBuildEntryPoints: (options: NormalizedOutputOptions, config: ResolvedConfig, bundle: OutputBundle, entryPoints: EntryPoints) => EntryPoints; | ||
export declare const getEntryFilesMapping: (config: ResolvedConfig) => EntryFilesMapping; | ||
export { getDevEntryPoints, addBuildEntryPoints }; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.addBuildEntryPoints = exports.getDevEntryPoints = exports.getEntryFilesMapping = void 0; | ||
exports.addBuildEntryPoints = exports.getDevEntryPoints = void 0; | ||
const process_1 = require("process"); | ||
@@ -134,17 +134,2 @@ const path_1 = require("path"); | ||
}; | ||
const getEntryFilesMapping = (config) => { | ||
const inputParsed = {}; | ||
// let isLegacy = (options.format === 'system' && bundle[chunkName].name.includes('-legacy')); | ||
for (const [entryName, entryPath] of Object.entries(config.build.rollupOptions.input)) { | ||
const entryAbsolutePath = (0, utils_1.normalizePath)((0, path_1.resolve)((0, process_1.cwd)(), entryPath)); | ||
if (entryAbsolutePath.indexOf(config.root) !== 0) { | ||
console.error("Entry points must be inside Vite root directory"); | ||
process.exit(1); | ||
} | ||
const entryRelativePath = entryAbsolutePath.substring(config.root.length + 1); | ||
inputParsed[entryName] = entryRelativePath; | ||
} | ||
return inputParsed; | ||
}; | ||
exports.getEntryFilesMapping = getEntryFilesMapping; | ||
//# sourceMappingURL=configResolver.js.map |
@@ -7,11 +7,9 @@ "use strict"; | ||
exports.refreshPaths = void 0; | ||
const node_path_1 = require("node:path"); | ||
const node_fs_1 = require("node:fs"); | ||
const sirv_1 = __importDefault(require("sirv")); | ||
const path_1 = require("path"); | ||
const fs_1 = require("fs"); | ||
const configResolver_1 = require("./configResolver"); | ||
const assetsResolver_1 = require("./assetsResolver"); | ||
const fileHelper_1 = require("./fileHelper"); | ||
const picocolors_1 = __importDefault(require("picocolors")); | ||
const namesMapping_1 = require("./namesMapping"); | ||
const entryPointsHelper_1 = require("./entryPointsHelper"); | ||
const utils_1 = require("./utils"); | ||
const pluginOptions_1 = require("./pluginOptions"); | ||
/* not imported from vite because we don't want vite in package.json dependancy */ | ||
@@ -28,33 +26,2 @@ const FS_PREFIX = `/@fs/`; | ||
exports.refreshPaths = ["templates/**/*.twig"]; | ||
function resolveBase(config) { | ||
return "/" + config.buildDirectory + "/"; | ||
} | ||
function resolveOutDir(config) { | ||
return (0, path_1.join)(config.publicDirectory, config.buildDirectory); | ||
} | ||
function resolvePluginOptions(userConfig = {}) { | ||
if (typeof userConfig.publicDirectory === "string") { | ||
userConfig.publicDirectory = userConfig.publicDirectory.trim().replace(/^\/+/, ""); | ||
if (userConfig.publicDirectory === "") { | ||
throw new Error("vite-plugin-symfony: publicDirectory must be a subdirectory. E.g. 'public'."); | ||
} | ||
} | ||
if (typeof userConfig.buildDirectory === "string") { | ||
userConfig.buildDirectory = userConfig.buildDirectory.trim().replace(/^\/+/, "").replace(/\/+$/, ""); | ||
if (userConfig.buildDirectory === "") { | ||
throw new Error("vite-plugin-symfony: buildDirectory must be a subdirectory. E.g. 'build'."); | ||
} | ||
} | ||
if (userConfig.servePublic !== false) { | ||
userConfig.servePublic = true; | ||
} | ||
return { | ||
servePublic: userConfig.servePublic, | ||
publicDirectory: userConfig.publicDirectory ?? "public", | ||
buildDirectory: userConfig.buildDirectory ?? "build", | ||
refresh: userConfig.refresh ?? false, | ||
viteDevServerHostname: userConfig.viteDevServerHostname ?? null, | ||
verbose: userConfig.verbose === true ?? false, | ||
}; | ||
} | ||
function resolveDevServerUrl(address, config, pluginOptions) { | ||
@@ -70,3 +37,3 @@ if (config.server?.origin) { | ||
const configHost = typeof config.server.host === "string" ? config.server.host : null; | ||
const serverAddress = isIpv6(address) ? `[${address.address}]` : address.address; | ||
const serverAddress = (0, utils_1.isIpv6)(address) ? `[${address.address}]` : address.address; | ||
const host = configHmrHost ?? pluginOptions.viteDevServerHostname ?? configHost ?? serverAddress; | ||
@@ -77,43 +44,4 @@ const configHmrClientPort = typeof config.server.hmr === "object" ? config.server.hmr.clientPort : null; | ||
} | ||
function isIpv6(address) { | ||
return (address.family === "IPv6" || | ||
// In node >=18.0 <18.4 this was an integer value. This was changed in a minor version. | ||
// See: https://github.com/laravel/vite-plugin/issues/103 | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore-next-line | ||
address.family === 6); | ||
} | ||
function logConfig(config, server, depth) { | ||
Object.entries(config).map(([key, value]) => { | ||
const prefix = " ".repeat(depth); | ||
const keySpaces = prefix + picocolors_1.default.dim(key) + " ".repeat(30 - key.length - prefix.length); | ||
if (typeof value === "undefined" || | ||
typeof value === "boolean" || | ||
typeof value === "number" || | ||
typeof value === "bigint") { | ||
server.config.logger.info(`${keySpaces}: ${value ? picocolors_1.default.green(value.toString()) : value}`); | ||
} | ||
else if (typeof value === "string") { | ||
server.config.logger.info(`${keySpaces}: ${value ? picocolors_1.default.green('"' + value.toString() + '"') : value}`); | ||
} | ||
else if (typeof value === "symbol") { | ||
server.config.logger.info(`${keySpaces}: symbol`); | ||
} | ||
else if (typeof value === "function") { | ||
server.config.logger.info(`${keySpaces}: function`); | ||
} | ||
else if (value === null) { | ||
server.config.logger.info(`${keySpaces}: null`); | ||
} | ||
else if (typeof value === "object") { | ||
server.config.logger.info(`${key}:`); | ||
logConfig(value, server, depth + 2); | ||
} | ||
else { | ||
server.config.logger.info(`${keySpaces}: unknown`); | ||
} | ||
}); | ||
} | ||
function symfony(userOptions = {}) { | ||
const pluginOptions = resolvePluginOptions(userOptions); | ||
const pluginOptions = (0, pluginOptions_1.resolvePluginOptions)(userOptions); | ||
let viteConfig; | ||
@@ -123,3 +51,2 @@ let viteDevServerUrl; | ||
const entryPoints = {}; | ||
const assets = {}; | ||
let outputCount = 0; | ||
@@ -135,14 +62,8 @@ return { | ||
const extraConfig = { | ||
base: userConfig.base ?? resolveBase(pluginOptions), | ||
base: userConfig.base ?? (0, pluginOptions_1.resolveBase)(pluginOptions), | ||
publicDir: false, | ||
build: { | ||
manifest: true, | ||
outDir: userConfig.build?.outDir ?? resolveOutDir(pluginOptions), | ||
outDir: userConfig.build?.outDir ?? (0, pluginOptions_1.resolveOutDir)(pluginOptions), | ||
}, | ||
server: { | ||
watch: { | ||
// needed if you want to reload dev server with twig | ||
disableGlobbing: false, | ||
}, | ||
}, | ||
optimizeDeps: { | ||
@@ -159,10 +80,12 @@ //Set to true to force dependency pre-bundling. | ||
configureServer(devServer) { | ||
// vite server is running | ||
const { watcher, ws } = devServer; | ||
// empty the buildDir and create an entrypoints.json file inside. | ||
devServer.httpServer?.once("listening", () => { | ||
if (viteConfig.env.DEV) { | ||
const buildDir = (0, path_1.resolve)(viteConfig.root, viteConfig.build.outDir); | ||
if (!(0, fs_1.existsSync)(buildDir)) { | ||
(0, fs_1.mkdirSync)(buildDir, { recursive: true }); | ||
const buildDir = (0, node_path_1.resolve)(viteConfig.root, viteConfig.build.outDir); | ||
if (!(0, node_fs_1.existsSync)(buildDir)) { | ||
(0, node_fs_1.mkdirSync)(buildDir, { recursive: true }); | ||
} | ||
(0, fs_1.existsSync)(buildDir) && (0, fileHelper_1.emptyDir)(buildDir); | ||
(0, node_fs_1.existsSync)(buildDir) && (0, utils_1.emptyDir)(buildDir); | ||
const address = devServer.httpServer?.address(); | ||
@@ -175,5 +98,5 @@ const isAddressInfo = (x) => typeof x === "object"; | ||
viteDevServerUrl = resolveDevServerUrl(address, devServer.config, pluginOptions); | ||
const entryPoints = (0, configResolver_1.getDevEntryPoints)(viteConfig, viteDevServerUrl); | ||
const entryPointsPath = (0, path_1.resolve)(viteConfig.root, viteConfig.build.outDir, entryPointsFilename); | ||
(0, fileHelper_1.writeJson)(entryPointsPath, { | ||
const entryPoints = (0, entryPointsHelper_1.getDevEntryPoints)(viteConfig, viteDevServerUrl); | ||
const entryPointsPath = (0, node_path_1.resolve)(viteConfig.root, viteConfig.build.outDir, entryPointsFilename); | ||
(0, utils_1.writeJson)(entryPointsPath, { | ||
isProd: false, | ||
@@ -185,3 +108,2 @@ viteServer: { | ||
entryPoints, | ||
assets: null, | ||
legacy: false, | ||
@@ -193,3 +115,3 @@ }); | ||
devServer.config.logger.info(`\n${picocolors_1.default.green("➜")} Vite Config`); | ||
logConfig(viteConfig, devServer, 0); | ||
(0, utils_1.logConfig)(viteConfig, devServer, 0); | ||
devServer.config.logger.info(`\n${picocolors_1.default.green("➜")} End of config \n`); | ||
@@ -199,3 +121,4 @@ }, 100); | ||
}); | ||
if (pluginOptions.refresh) { | ||
// full reload vite dev server if twig files are modified. | ||
if (pluginOptions.refresh !== false) { | ||
const paths = pluginOptions.refresh === true ? exports.refreshPaths : pluginOptions.refresh; | ||
@@ -233,3 +156,3 @@ for (const path of paths) { | ||
res.statusCode = 404; | ||
res.end((0, fs_1.readFileSync)((0, path_1.join)(__dirname, "dev-server-404.html"))); | ||
res.end((0, node_fs_1.readFileSync)((0, node_path_1.join)(__dirname, "dev-server-404.html"))); | ||
return; | ||
@@ -246,21 +169,31 @@ } | ||
async renderChunk(code, chunk, opts) { | ||
// if entryPoint is not a js file but a css/scss file, only this hook give us the | ||
// complete path | ||
if (!chunk.isEntry) { | ||
return; | ||
} | ||
// facadeModuleId give us the complete path of the entryPoint | ||
// -> /path-to-your-project/assets/welcome.js | ||
// -> /path-to-your-project/assets/theme.scss | ||
const fileExt = chunk.facadeModuleId.split(".").pop(); | ||
if (["scss", "css"].indexOf(fileExt) === -1) { | ||
if (["css", "scss", "sass", "less", "styl", "stylus", "postcss"].indexOf(fileExt) === -1) { | ||
return; | ||
} | ||
// Here we have only css entryPoints | ||
const cssAssetName = chunk.facadeModuleId | ||
? (0, utils_1.normalizePath)((0, path_1.relative)(viteConfig.root, chunk.facadeModuleId)) | ||
? (0, utils_1.normalizePath)((0, node_path_1.relative)(viteConfig.root, chunk.facadeModuleId)) | ||
: chunk.name; | ||
// chunk.viteMetadata.importedCss contains a Set of relative file paths of css files | ||
// generated from cssAssetName | ||
chunk.viteMetadata.importedCss.forEach((cssBuildFilename) => { | ||
namesMapping_1.name2exportName[cssAssetName] = cssBuildFilename; | ||
// eg: entryPath2exportPath['assets/theme.scss'] = 'assets/theme-44b5be96.css'; | ||
entryPointsHelper_1.entryPath2exportPath[cssAssetName] = cssBuildFilename; | ||
}); | ||
}, | ||
generateBundle(options, bundle) { | ||
(0, configResolver_1.addBuildEntryPoints)(options, viteConfig, bundle, entryPoints); | ||
(0, assetsResolver_1.addBuildAssets)(viteConfig, bundle, assets); | ||
(0, entryPointsHelper_1.addBuildEntryPoints)(options, viteConfig, bundle, entryPoints); | ||
outputCount++; | ||
const output = viteConfig.build.rollupOptions?.output; | ||
// if we have multiple build passes output is an array of each passe. | ||
// else we have an object of this unique pass | ||
const outputLength = Array.isArray(output) ? output.length : 1; | ||
@@ -275,3 +208,2 @@ if (outputCount >= outputLength) { | ||
entryPoints, | ||
assets, | ||
legacy: typeof entryPoints["polyfills-legacy"] !== "undefined", | ||
@@ -278,0 +210,0 @@ }, null, 2), |
@@ -1,5 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.name2exportName = void 0; | ||
exports.name2exportName = {}; | ||
//# sourceMappingURL=namesMapping.js.map |
@@ -0,1 +1,4 @@ | ||
/// <reference types="node" /> | ||
import type { ViteDevServer } from "vite"; | ||
import type { AddressInfo } from "net"; | ||
export declare const isWindows: boolean; | ||
@@ -5,1 +8,5 @@ export declare function slash(p: string): string; | ||
export declare function getLegacyName(name: string): string; | ||
export declare function isIpv6(address: AddressInfo): boolean; | ||
export declare function logConfig(config: any, server: ViteDevServer, depth: number): void; | ||
export declare const writeJson: (filePath: string, jsonData: any) => void; | ||
export declare const emptyDir: (dir: string) => void; |
@@ -6,5 +6,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getLegacyName = exports.normalizePath = exports.slash = exports.isWindows = void 0; | ||
exports.emptyDir = exports.writeJson = exports.logConfig = exports.isIpv6 = exports.getLegacyName = exports.normalizePath = exports.slash = exports.isWindows = void 0; | ||
const node_os_1 = __importDefault(require("node:os")); | ||
const node_path_1 = __importDefault(require("node:path")); | ||
const picocolors_1 = __importDefault(require("picocolors")); | ||
const fs_1 = require("fs"); | ||
const path_1 = require("path"); | ||
exports.isWindows = node_os_1.default.platform() === "win32"; | ||
@@ -26,2 +29,59 @@ function slash(p) { | ||
exports.getLegacyName = getLegacyName; | ||
function isIpv6(address) { | ||
return (address.family === "IPv6" || | ||
// In node >=18.0 <18.4 this was an integer value. This was changed in a minor version. | ||
// See: https://github.com/laravel/vite-plugin/issues/103 | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore-next-line | ||
address.family === 6); | ||
} | ||
exports.isIpv6 = isIpv6; | ||
function logConfig(config, server, depth) { | ||
Object.entries(config).map(([key, value]) => { | ||
const prefix = " ".repeat(depth); | ||
const keySpaces = prefix + picocolors_1.default.dim(key) + " ".repeat(30 - key.length - prefix.length); | ||
if (typeof value === "undefined" || | ||
typeof value === "boolean" || | ||
typeof value === "number" || | ||
typeof value === "bigint") { | ||
server.config.logger.info(`${keySpaces}: ${value ? picocolors_1.default.green(value.toString()) : value}`); | ||
} | ||
else if (typeof value === "string") { | ||
server.config.logger.info(`${keySpaces}: ${value ? picocolors_1.default.green('"' + value.toString() + '"') : value}`); | ||
} | ||
else if (typeof value === "symbol") { | ||
server.config.logger.info(`${keySpaces}: symbol`); | ||
} | ||
else if (typeof value === "function") { | ||
server.config.logger.info(`${keySpaces}: function`); | ||
} | ||
else if (value === null) { | ||
server.config.logger.info(`${keySpaces}: null`); | ||
} | ||
else if (typeof value === "object") { | ||
server.config.logger.info(`${key}:`); | ||
logConfig(value, server, depth + 2); | ||
} | ||
else { | ||
server.config.logger.info(`${keySpaces}: unknown`); | ||
} | ||
}); | ||
} | ||
exports.logConfig = logConfig; | ||
const writeJson = (filePath, jsonData) => { | ||
try { | ||
(0, fs_1.writeFileSync)(filePath, JSON.stringify(jsonData, null, 2)); | ||
} | ||
catch (err) { | ||
throw new Error(`Error writing entrypoints.json ${err.message}`); | ||
} | ||
}; | ||
exports.writeJson = writeJson; | ||
const emptyDir = (dir) => { | ||
const files = (0, fs_1.readdirSync)(dir); | ||
for (const file of files) { | ||
(0, fs_1.rmSync)((0, path_1.join)(dir, file), { recursive: true }); | ||
} | ||
}; | ||
exports.emptyDir = emptyDir; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "vite-plugin-symfony", | ||
"version": "0.7.4", | ||
"version": "0.7.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -48,4 +48,4 @@ # Vite plugin Symfony | ||
/* boolean or array of paths for your twig templates */ | ||
refresh: ["templates/**/*.twig"], | ||
/* boolean or array of paths for your twig templates : true, false, ["templates/**/*.twig"] */ | ||
refresh: false, | ||
@@ -52,0 +52,0 @@ /** If you set server.host: '0.0.0.0' in your vite.config.js |
133
src/index.ts
@@ -1,18 +0,15 @@ | ||
import { Plugin, UserConfig, ResolvedConfig, ViteDevServer } from "vite"; | ||
import { resolve, join, relative } from "node:path"; | ||
import { existsSync, mkdirSync, readFileSync } from "node:fs"; | ||
import type { AddressInfo } from "node:net"; | ||
import { Plugin, UserConfig, ResolvedConfig } from "vite"; | ||
import sirv from "sirv"; | ||
import colors from "picocolors"; | ||
import type { OutputChunk } from "rollup"; | ||
import { resolve, join, relative } from "path"; | ||
import { existsSync, mkdirSync, readFileSync } from "fs"; | ||
import { AddressInfo } from "net"; | ||
import { getDevEntryPoints, addBuildEntryPoints, entryPath2exportPath } from "./entryPointsHelper"; | ||
import { logConfig, normalizePath, isIpv6, writeJson, emptyDir } from "./utils"; | ||
import { resolvePluginOptions, resolveBase, resolveOutDir } from "./pluginOptions"; | ||
import { getDevEntryPoints, addBuildEntryPoints } from "./configResolver"; | ||
import { addBuildAssets } from "./assetsResolver"; | ||
import { writeJson, emptyDir } from "./fileHelper"; | ||
import colors from "picocolors"; | ||
import { name2exportName } from "./namesMapping"; | ||
import { normalizePath } from "./utils"; | ||
/* not imported from vite because we don't want vite in package.json dependancy */ | ||
@@ -32,41 +29,2 @@ const FS_PREFIX = `/@fs/`; | ||
function resolveBase(config: Required<PluginOptions>): string { | ||
return "/" + config.buildDirectory + "/"; | ||
} | ||
function resolveOutDir(config: Required<PluginOptions>): string { | ||
return join(config.publicDirectory, config.buildDirectory); | ||
} | ||
function resolvePluginOptions(userConfig: PluginOptions = {}): Required<PluginOptions> { | ||
if (typeof userConfig.publicDirectory === "string") { | ||
userConfig.publicDirectory = userConfig.publicDirectory.trim().replace(/^\/+/, ""); | ||
if (userConfig.publicDirectory === "") { | ||
throw new Error("vite-plugin-symfony: publicDirectory must be a subdirectory. E.g. 'public'."); | ||
} | ||
} | ||
if (typeof userConfig.buildDirectory === "string") { | ||
userConfig.buildDirectory = userConfig.buildDirectory.trim().replace(/^\/+/, "").replace(/\/+$/, ""); | ||
if (userConfig.buildDirectory === "") { | ||
throw new Error("vite-plugin-symfony: buildDirectory must be a subdirectory. E.g. 'build'."); | ||
} | ||
} | ||
if (userConfig.servePublic !== false) { | ||
userConfig.servePublic = true; | ||
} | ||
return { | ||
servePublic: userConfig.servePublic, | ||
publicDirectory: userConfig.publicDirectory ?? "public", | ||
buildDirectory: userConfig.buildDirectory ?? "build", | ||
refresh: userConfig.refresh ?? false, | ||
viteDevServerHostname: userConfig.viteDevServerHostname ?? null, | ||
verbose: userConfig.verbose === true ?? false, | ||
}; | ||
} | ||
function resolveDevServerUrl( | ||
@@ -97,41 +55,2 @@ address: AddressInfo, | ||
function isIpv6(address: AddressInfo): boolean { | ||
return ( | ||
address.family === "IPv6" || | ||
// In node >=18.0 <18.4 this was an integer value. This was changed in a minor version. | ||
// See: https://github.com/laravel/vite-plugin/issues/103 | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore-next-line | ||
address.family === 6 | ||
); | ||
} | ||
function logConfig(config: any, server: ViteDevServer, depth: number) { | ||
Object.entries(config).map(([key, value]) => { | ||
const prefix = " ".repeat(depth); | ||
const keySpaces = prefix + colors.dim(key) + " ".repeat(30 - key.length - prefix.length); | ||
if ( | ||
typeof value === "undefined" || | ||
typeof value === "boolean" || | ||
typeof value === "number" || | ||
typeof value === "bigint" | ||
) { | ||
server.config.logger.info(`${keySpaces}: ${value ? colors.green(value.toString()) : value}`); | ||
} else if (typeof value === "string") { | ||
server.config.logger.info(`${keySpaces}: ${value ? colors.green('"' + value.toString() + '"') : value}`); | ||
} else if (typeof value === "symbol") { | ||
server.config.logger.info(`${keySpaces}: symbol`); | ||
} else if (typeof value === "function") { | ||
server.config.logger.info(`${keySpaces}: function`); | ||
} else if (value === null) { | ||
server.config.logger.info(`${keySpaces}: null`); | ||
} else if (typeof value === "object") { | ||
server.config.logger.info(`${key}:`); | ||
logConfig(value, server, depth + 2); | ||
} else { | ||
server.config.logger.info(`${keySpaces}: unknown`); | ||
} | ||
}); | ||
} | ||
export default function symfony(userOptions: PluginOptions = {}): Plugin { | ||
@@ -145,3 +64,2 @@ const pluginOptions = resolvePluginOptions(userOptions); | ||
const entryPoints: EntryPoints = {}; | ||
const assets: StringMapping = {}; | ||
let outputCount = 0; | ||
@@ -165,9 +83,2 @@ | ||
}, | ||
server: { | ||
watch: { | ||
// needed if you want to reload dev server with twig | ||
disableGlobbing: false, | ||
}, | ||
}, | ||
optimizeDeps: { | ||
@@ -185,4 +96,7 @@ //Set to true to force dependency pre-bundling. | ||
configureServer(devServer) { | ||
// vite server is running | ||
const { watcher, ws } = devServer; | ||
// empty the buildDir and create an entrypoints.json file inside. | ||
devServer.httpServer?.once("listening", () => { | ||
@@ -218,3 +132,2 @@ if (viteConfig.env.DEV) { | ||
entryPoints, | ||
assets: null, | ||
legacy: false, | ||
@@ -233,3 +146,4 @@ }); | ||
if (pluginOptions.refresh) { | ||
// full reload vite dev server if twig files are modified. | ||
if (pluginOptions.refresh !== false) { | ||
const paths = pluginOptions.refresh === true ? refreshPaths : pluginOptions.refresh; | ||
@@ -282,10 +196,17 @@ for (const path of paths) { | ||
async renderChunk(code, chunk: OutputChunk & { viteMetadata: ChunkMetadata }, opts) { | ||
// if entryPoint is not a js file but a css/scss file, only this hook give us the | ||
// complete path | ||
if (!chunk.isEntry) { | ||
return; | ||
} | ||
// facadeModuleId give us the complete path of the entryPoint | ||
// -> /path-to-your-project/assets/welcome.js | ||
// -> /path-to-your-project/assets/theme.scss | ||
const fileExt = chunk.facadeModuleId.split(".").pop(); | ||
if (["scss", "css"].indexOf(fileExt) === -1) { | ||
if (["css", "scss", "sass", "less", "styl", "stylus", "postcss"].indexOf(fileExt) === -1) { | ||
return; | ||
} | ||
// Here we have only css entryPoints | ||
const cssAssetName = chunk.facadeModuleId | ||
@@ -295,4 +216,7 @@ ? normalizePath(relative(viteConfig.root, chunk.facadeModuleId)) | ||
// chunk.viteMetadata.importedCss contains a Set of relative file paths of css files | ||
// generated from cssAssetName | ||
chunk.viteMetadata.importedCss.forEach((cssBuildFilename) => { | ||
name2exportName[cssAssetName] = cssBuildFilename; | ||
// eg: entryPath2exportPath['assets/theme.scss'] = 'assets/theme-44b5be96.css'; | ||
entryPath2exportPath[cssAssetName] = cssBuildFilename; | ||
}); | ||
@@ -302,6 +226,8 @@ }, | ||
addBuildEntryPoints(options, viteConfig, bundle, entryPoints); | ||
addBuildAssets(viteConfig, bundle, assets); | ||
outputCount++; | ||
const output = viteConfig.build.rollupOptions?.output; | ||
// if we have multiple build passes output is an array of each passe. | ||
// else we have an object of this unique pass | ||
const outputLength = Array.isArray(output) ? output.length : 1; | ||
@@ -318,3 +244,2 @@ | ||
entryPoints, | ||
assets, | ||
legacy: typeof entryPoints["polyfills-legacy"] !== "undefined", | ||
@@ -321,0 +246,0 @@ }, |
@@ -8,4 +8,4 @@ // declare module 'rollup' { | ||
interface ChunkMetadata { | ||
importedAssets: Set<string> | ||
importedCss: Set<string> | ||
importedAssets: Set<string>; | ||
importedCss: Set<string>; | ||
} | ||
@@ -23,3 +23,3 @@ | ||
assets: StringMapping; | ||
legacy: Boolean | ||
legacy: boolean; | ||
}; | ||
@@ -31,3 +31,3 @@ | ||
preload?: string[]; | ||
legacy?: Boolean | String; | ||
legacy?: boolean | string; | ||
}; | ||
@@ -39,6 +39,6 @@ type EntryPoints = { | ||
type StringMapping = { | ||
[k: string]: string | ||
[k: string]: string; | ||
}; | ||
type ParsedInput = { | ||
type ParsedInputs = { | ||
[k: string]: ParsedEntry; | ||
@@ -54,3 +54,3 @@ }; | ||
[k: string]: string; | ||
} | ||
}; | ||
@@ -57,0 +57,0 @@ type ManifestEntry = { |
@@ -0,3 +1,8 @@ | ||
import type { ViteDevServer } from "vite"; | ||
import os from "node:os"; | ||
import path from "node:path"; | ||
import type { AddressInfo } from "net"; | ||
import colors from "picocolors"; | ||
import { writeFileSync, rmSync, readdirSync } from "fs"; | ||
import { join } from "path"; | ||
@@ -20,1 +25,55 @@ export const isWindows = os.platform() === "win32"; | ||
} | ||
export function isIpv6(address: AddressInfo): boolean { | ||
return ( | ||
address.family === "IPv6" || | ||
// In node >=18.0 <18.4 this was an integer value. This was changed in a minor version. | ||
// See: https://github.com/laravel/vite-plugin/issues/103 | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore-next-line | ||
address.family === 6 | ||
); | ||
} | ||
export function logConfig(config: any, server: ViteDevServer, depth: number) { | ||
Object.entries(config).map(([key, value]) => { | ||
const prefix = " ".repeat(depth); | ||
const keySpaces = prefix + colors.dim(key) + " ".repeat(30 - key.length - prefix.length); | ||
if ( | ||
typeof value === "undefined" || | ||
typeof value === "boolean" || | ||
typeof value === "number" || | ||
typeof value === "bigint" | ||
) { | ||
server.config.logger.info(`${keySpaces}: ${value ? colors.green(value.toString()) : value}`); | ||
} else if (typeof value === "string") { | ||
server.config.logger.info(`${keySpaces}: ${value ? colors.green('"' + value.toString() + '"') : value}`); | ||
} else if (typeof value === "symbol") { | ||
server.config.logger.info(`${keySpaces}: symbol`); | ||
} else if (typeof value === "function") { | ||
server.config.logger.info(`${keySpaces}: function`); | ||
} else if (value === null) { | ||
server.config.logger.info(`${keySpaces}: null`); | ||
} else if (typeof value === "object") { | ||
server.config.logger.info(`${key}:`); | ||
logConfig(value, server, depth + 2); | ||
} else { | ||
server.config.logger.info(`${keySpaces}: unknown`); | ||
} | ||
}); | ||
} | ||
export const writeJson = (filePath: string, jsonData: any) => { | ||
try { | ||
writeFileSync(filePath, JSON.stringify(jsonData, null, 2)); | ||
} catch (err) { | ||
throw new Error(`Error writing entrypoints.json ${err.message}`); | ||
} | ||
}; | ||
export const emptyDir = (dir: string) => { | ||
const files = readdirSync(dir); | ||
for (const file of files) { | ||
rmSync(join(dir, file), { recursive: true }); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
148976
10.65%34
13.33%1246
17.55%