diff --git a/packages/vite/index.d.ts b/packages/vite/index.d.ts index a70a0ec5af..ef2bcbda8f 100644 --- a/packages/vite/index.d.ts +++ b/packages/vite/index.d.ts @@ -5,3 +5,4 @@ export * from './src/scripts.js'; export * from './src/template-tag.js'; export * from './src/addons.js'; export * from './src/optimize-deps.js'; +export * from './src/assets.js'; diff --git a/packages/vite/index.mjs b/packages/vite/index.mjs index a56a50fd97..0f4bf49eae 100644 --- a/packages/vite/index.mjs +++ b/packages/vite/index.mjs @@ -4,3 +4,4 @@ export * from './src/hbs.js'; export * from './src/scripts.js'; export * from './src/template-tag.js'; export * from './src/optimize-deps.js'; +export * from './src/assets.js'; diff --git a/packages/vite/src/assets.ts b/packages/vite/src/assets.ts new file mode 100644 index 0000000000..107be3feb0 --- /dev/null +++ b/packages/vite/src/assets.ts @@ -0,0 +1,30 @@ +import { ResolverLoader } from '@embroider/core'; +import type { Plugin } from 'vite'; +import * as process from 'process'; +import { dirname, join } from 'path'; +import { copyFileSync, mkdirpSync } from 'fs-extra'; + + + +export function assets(): Plugin { + const cwd = process.cwd(); + const resolverLoader = new ResolverLoader(cwd); + const engines = resolverLoader.resolver.options.engines; + return { + name: 'assets', + enforce: 'pre', + async writeBundle(options) { + for (const engine of engines) { + engine.activeAddons.forEach((addon) => { + const pkg = resolverLoader.resolver.packageCache.ownerOfFile(addon.root); + if (!pkg || !pkg.isV2Addon()) return; + const assets = pkg.meta['public-assets'] || {}; + Object.entries(assets).forEach(([path, dest]) => { + mkdirpSync(dirname(join(options.dir!, dest))) + copyFileSync(join(pkg.root, path), join(options.dir!, dest)); + }); + }); + } + }, + }; +} diff --git a/tests/vite-app/vite.config.mjs b/tests/vite-app/vite.config.mjs index 0ca8f84402..d8938a8a39 100644 --- a/tests/vite-app/vite.config.mjs +++ b/tests/vite-app/vite.config.mjs @@ -5,7 +5,8 @@ import { scripts, templateTag, optimizeDeps, -} from "@embroider/vite"; + assets, +} from '@embroider/vite'; import { resolve } from "path"; import { babel } from "@rollup/plugin-babel"; @@ -18,6 +19,7 @@ export default defineConfig({ templateTag(), scripts(), resolver(), + assets(), babel({ babelHelpers: "runtime",