From ce39dc45ba411a142b351301efefb62b4af57f5f Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Fri, 29 Sep 2023 11:56:25 -0700 Subject: [PATCH] feat: avoid loading babel config by default --- .changeset/seven-trains-mate.md | 5 +++ README.md | 2 +- src/index.ts | 78 +++++++++++++++++++-------------- 3 files changed, 52 insertions(+), 33 deletions(-) create mode 100644 .changeset/seven-trains-mate.md diff --git a/.changeset/seven-trains-mate.md b/.changeset/seven-trains-mate.md new file mode 100644 index 0000000..85dbc8c --- /dev/null +++ b/.changeset/seven-trains-mate.md @@ -0,0 +1,5 @@ +--- +"@marko/vite": major +--- + +No longer load babel config by default unless configured. diff --git a/README.md b/README.md index d9931ee..cc7f166 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ app.listen(3000); ### options.babelConfig -You can manually override Marko's Babel configuration by passing a `babelConfig` object to the `@marko/vite` plugin. By default Babel's regular [config file resolution](https://babeljs.io/docs/en/config-files) will be used. +You can manually override Marko's Babel configuration by passing a `babelConfig` object to the `@marko/vite` plugin. If no babel configuration is specified, babel related config files will not be considered. ```javascript marko({ diff --git a/src/index.ts b/src/index.ts index c306c98..db34d7e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -97,6 +97,13 @@ const resolveOpts = { skipSelf: true }; const cache = new Map(); const thisFile = typeof __filename === "string" ? __filename : fileURLToPath(import.meta.url); +const babelCaller = { + name: "@marko/vite", + supportsStaticESM: true, + supportsDynamicImport: true, + supportsTopLevelAwait: true, + supportsExportNamespaceFrom: true, +}; export default function markoPlugin(opts: Options = {}): vite.Plugin[] { let compiler: typeof Compiler; @@ -108,26 +115,24 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] { let domConfig: Compiler.Config; let hydrateConfig: Compiler.Config; - const resolveViteVirtualDep: Compiler.Config["resolveVirtualDependency"] = ( - from, - dep - ) => { - const query = `${virtualFileQuery}&id=${encodeURIComponent( - normalizePath(dep.virtualPath) - )}`; - const normalizedFrom = normalizePath(from); - const id = normalizePath(normalizedFrom) + query; - - if (devServer) { - const prev = virtualFiles.get(id); - if (isDeferredPromise(prev)) { - prev.resolve(dep); + const resolveVirtualDependency: Compiler.Config["resolveVirtualDependency"] = + (from, dep) => { + const query = `${virtualFileQuery}&id=${encodeURIComponent( + normalizePath(dep.virtualPath) + )}`; + const normalizedFrom = normalizePath(from); + const id = normalizePath(normalizedFrom) + query; + + if (devServer) { + const prev = virtualFiles.get(id); + if (isDeferredPromise(prev)) { + prev.resolve(dep); + } } - } - virtualFiles.set(id, dep); - return `./${path.posix.basename(normalizedFrom) + query}`; - }; + virtualFiles.set(id, dep); + return `./${path.posix.basename(normalizedFrom) + query}`; + }; let root: string; let devEntryFile: string; @@ -163,34 +168,43 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] { runtimeId, sourceMaps: true, writeVersionComment: false, - babelConfig: { - ...opts.babelConfig, - caller: { - name: "@marko/vite", - supportsStaticESM: true, - supportsDynamicImport: true, - supportsTopLevelAwait: true, - supportsExportNamespaceFrom: true, - ...opts.babelConfig?.caller, - }, - }, + babelConfig: opts.babelConfig + ? { + ...opts.babelConfig, + caller: opts.babelConfig.caller + ? { + name: "@marko/vite", + supportsStaticESM: true, + supportsDynamicImport: true, + supportsTopLevelAwait: true, + supportsExportNamespaceFrom: true, + ...opts.babelConfig.caller, + } + : babelCaller, + } + : { + babelrc: false, + configFile: false, + browserslistConfigFile: false, + caller: babelCaller, + }, }; ssrConfig = { ...baseConfig, - resolveVirtualDependency: resolveViteVirtualDep, + resolveVirtualDependency, output: "html", }; domConfig = { ...baseConfig, - resolveVirtualDependency: resolveViteVirtualDep, + resolveVirtualDependency, output: "dom", }; hydrateConfig = { ...baseConfig, - resolveVirtualDependency: resolveViteVirtualDep, + resolveVirtualDependency, output: "hydrate", };