Skip to content

Commit

Permalink
copy assets to correct folder
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Dec 21, 2023
1 parent 2a6775b commit 1afbab4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/vite/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
1 change: 1 addition & 0 deletions packages/vite/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
30 changes: 30 additions & 0 deletions packages/vite/src/assets.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
}
},
};
}
4 changes: 3 additions & 1 deletion tests/vite-app/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -18,6 +19,7 @@ export default defineConfig({
templateTag(),
scripts(),
resolver(),
assets(),

babel({
babelHelpers: "runtime",
Expand Down

0 comments on commit 1afbab4

Please sign in to comment.