From 6c41d7285c06c245d936ef15f159102a562bb246 Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Tue, 26 Nov 2024 18:18:09 +0000 Subject: [PATCH] bundle up vite plugins in a nicer way --- packages/vite/index.mjs | 2 + packages/vite/src/classic-ember-support.ts | 9 +++ packages/vite/src/ember.ts | 50 +++++++++++++++ tests/app-template/vite.config.mjs | 75 +++------------------- 4 files changed, 71 insertions(+), 65 deletions(-) create mode 100644 packages/vite/src/classic-ember-support.ts create mode 100644 packages/vite/src/ember.ts diff --git a/packages/vite/index.mjs b/packages/vite/index.mjs index c4d1d6ac4..7025b6548 100644 --- a/packages/vite/index.mjs +++ b/packages/vite/index.mjs @@ -7,3 +7,5 @@ export * from './src/optimize-deps.js'; export * from './src/build.js'; export * from './src/assets.js'; export * from './src/content-for.js'; +export * from './src/classic-ember-support.js'; +export * from './src/ember.js'; diff --git a/packages/vite/src/classic-ember-support.ts b/packages/vite/src/classic-ember-support.ts new file mode 100644 index 000000000..93c1e4539 --- /dev/null +++ b/packages/vite/src/classic-ember-support.ts @@ -0,0 +1,9 @@ +import { hbs } from './hbs.js'; +import { scripts } from './scripts.js'; +import { compatPrebuild } from './build.js'; +import { assets } from './assets.js'; +import { contentFor } from './content-for.js'; + +export function classicEmberSupport() { + return [hbs(), scripts(), compatPrebuild(), assets(), contentFor()]; +} diff --git a/packages/vite/src/ember.ts b/packages/vite/src/ember.ts new file mode 100644 index 000000000..9cbce4134 --- /dev/null +++ b/packages/vite/src/ember.ts @@ -0,0 +1,50 @@ +import { templateTag } from './template-tag.js'; +import { resolver } from './resolver.js'; +import { mergeConfig, type UserConfig, type ConfigEnv } from 'vite'; +import { esBuildResolver } from './esbuild-resolver.js'; + +export let extensions = ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.hbs.js', '.json']; + +export function ember() { + return [ + templateTag(), + resolver(), + { + name: 'vite-plugin-ember-config', + async config(config: UserConfig, env: ConfigEnv) { + return mergeConfig( + { + resolve: { + extensions, + }, + + optimizeDeps: { + exclude: ['@embroider/macros'], + extensions: ['.hbs', '.gjs', '.gts'], + esbuildOptions: { + plugins: [esBuildResolver()], + }, + }, + + build: { + rollupOptions: { + input: { + main: 'index.html', + ...(shouldBuildTests(env.mode) ? { tests: 'tests/index.html' } : undefined), + }, + }, + }, + server: { + port: 4200, + }, + }, + config + ); + }, + }, + ]; +} + +function shouldBuildTests(mode: string) { + return mode !== 'production' || process.env.FORCE_BUILD_TESTS; +} diff --git a/tests/app-template/vite.config.mjs b/tests/app-template/vite.config.mjs index e52d013ba..dbf452687 100644 --- a/tests/app-template/vite.config.mjs +++ b/tests/app-template/vite.config.mjs @@ -1,70 +1,15 @@ import { defineConfig } from "vite"; -import { - resolver, - hbs, - scripts, - templateTag, - optimizeDeps, - compatPrebuild, - assets, - contentFor, -} from "@embroider/vite"; -import { resolve } from "path"; +import { extensions, classicEmberSupport, ember } from "@embroider/vite"; import { babel } from "@rollup/plugin-babel"; -export default defineConfig(({ mode }) => { - return { - cacheDir: resolve("node_modules", ".vite"), - resolve: { - extensions: [ - ".mjs", - ".gjs", - ".js", - ".mts", - ".gts", - ".ts", - ".hbs", - ".hbs.js", - ".json", - ], - }, - plugins: [ - hbs(), - templateTag(), - scripts(), - resolver(), - compatPrebuild(), - assets(), - contentFor(), +export default defineConfig({ + plugins: [ + classicEmberSupport(), + ember(), - babel({ - babelHelpers: "runtime", - - // this needs .hbs because our hbs() plugin above converts them to - // javascript but the javascript still also needs babel, but we don't want - // to rename them because vite isn't great about knowing how to hot-reload - // them if we resolve them to made-up names. - extensions: [".gjs", ".js", ".hbs", ".ts", ".gts"], - }), - ], - optimizeDeps: optimizeDeps(), - server: { - port: 4200, - }, - build: { - outDir: "dist", - rollupOptions: { - input: { - main: "index.html", - ...(shouldBuildTests(mode) - ? { tests: "tests/index.html" } - : undefined), - }, - }, - }, - }; + babel({ + babelHelpers: "runtime", + extensions, + }), + ], }); - -function shouldBuildTests(mode) { - return mode !== "production" || process.env.FORCE_BUILD_TESTS; -}