From 7f7eb5653bed67f30524caae8e5ec0ec18912621 Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Tue, 8 Mar 2022 08:37:34 -0700 Subject: [PATCH] fix: populate optimizeDeps options in build mode --- src/index.ts | 79 ++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/src/index.ts b/src/index.ts index b9ee6e7..c705d14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -152,56 +152,55 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] { }); } - if (!isBuild) { - const lookup = compiler.taglib.buildLookup(root) as any; - const domRuntimeDeps = compiler.getRuntimeEntryFiles( - "dom", - opts.translator - ); - const htmlRuntimeDeps = compiler.getRuntimeEntryFiles( - "html", - opts.translator - ); - const taglibDeps: string[] = []; - - for (const name in lookup.taglibsById) { - const taglib = lookup.taglibsById[name]; - if (/[\\/]node_modules[\\/](?!@marko[\\/])/.test(taglib.dirname)) { - for (const tagName in taglib.tags) { - const tag = taglib.tags[tagName]; - const entry = tag.template || tag.renderer; - - if (entry) { - taglibDeps.push( - entry.replace(/^.*?[\\/]node_modules[\\/]/, "") - ); - } + const lookup = compiler.taglib.buildLookup(root) as any; + const taglibDeps: string[] = []; + + for (const name in lookup.taglibsById) { + const taglib = lookup.taglibsById[name]; + if (/[\\/]node_modules[\\/](?!@marko[\\/])/.test(taglib.dirname)) { + for (const tagName in taglib.tags) { + const tag = taglib.tags[tagName]; + const entry = tag.template || tag.renderer; + + if (entry) { + taglibDeps.push( + entry.replace(/^.*?[\\/]node_modules[\\/]/, "") + ); } } } + } - const domDeps = Array.from( - new Set(domRuntimeDeps.concat(taglibDeps)) - ); - const serverDeps = Array.from( - new Set(htmlRuntimeDeps.concat(taglibDeps)) - ); + const domDeps = Array.from( + new Set( + compiler + .getRuntimeEntryFiles("dom", opts.translator) + .concat(taglibDeps) + ) + ); - const optimizeDeps = (config.optimizeDeps ??= {}); - optimizeDeps.include ??= []; - optimizeDeps.include = optimizeDeps.include.concat( - domDeps.filter((dep) => path.extname(dep) !== markoExt) - ); + const optimizeDeps = (config.optimizeDeps ??= {}); + optimizeDeps.include ??= []; + optimizeDeps.include = optimizeDeps.include.concat( + domDeps.filter((dep) => path.extname(dep) !== markoExt) + ); - optimizeDeps.exclude ??= []; - optimizeDeps.exclude = optimizeDeps.exclude.concat( - domDeps.filter((dep) => path.extname(dep) === markoExt) - ); + optimizeDeps.exclude ??= []; + optimizeDeps.exclude = optimizeDeps.exclude.concat( + domDeps.filter((dep) => path.extname(dep) === markoExt) + ); + if (!isBuild) { + const serverDeps = Array.from( + new Set( + compiler + .getRuntimeEntryFiles("html", opts.translator) + .concat(taglibDeps) + ) + ); const ssr = ((config as any).ssr ??= {}); ssr.external ??= []; ssr.external = ssr.external.concat(serverDeps); - // Vite cannot handle commonjs modules, which many Marko component libraries // use in conjunction with the `.marko` files. To support this // we tell Vite to ignore all `.marko` files in node_modules for the server.