Skip to content

Commit

Permalink
bundle up vite plugins in a nicer way
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Dec 3, 2024
1 parent 35f48d4 commit 6c41d72
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 65 deletions.
2 changes: 2 additions & 0 deletions packages/vite/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
9 changes: 9 additions & 0 deletions packages/vite/src/classic-ember-support.ts
Original file line number Diff line number Diff line change
@@ -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()];
}
50 changes: 50 additions & 0 deletions packages/vite/src/ember.ts
Original file line number Diff line number Diff line change
@@ -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;
}
75 changes: 10 additions & 65 deletions tests/app-template/vite.config.mjs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 6c41d72

Please sign in to comment.