Skip to content

Commit

Permalink
Merge pull request #1736 from patricklx/vite-copy-assets
Browse files Browse the repository at this point in the history
vite find assets & build copy assets
  • Loading branch information
ef4 authored Mar 28, 2024
2 parents 11f2c70 + 45e96e5 commit e1142b1
Show file tree
Hide file tree
Showing 7 changed files with 101 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 @@ -4,4 +4,5 @@ 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';
export * from './src/build.js';
1 change: 1 addition & 0 deletions packages/vite/index.mjs
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/optimize-deps.js';
export * from './src/build.js';
export * from './src/assets.js';
5 changes: 4 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
"fs-extra": "^10.0.0",
"jsdom": "^16.6.0",
"source-map-url": "^0.4.1",
"terser": "^5.7.0"
"terser": "^5.7.0",
"fast-glob": "^3.3.2",
"send": "^0.18.0"
},
"devDependencies": {
"@embroider/core": "workspace:^",
"@types/babel__core": "^7.20.1",
"@types/send": "^0.17.4",
"@types/debug": "^4.1.5",
"@types/jsdom": "^16.2.11",
"@types/fs-extra": "^9.0.12",
Expand Down
78 changes: 78 additions & 0 deletions packages/vite/src/assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type { Resolver } from '@embroider/core';
import { ResolverLoader } from '@embroider/core';
import type { Plugin } from 'vite';
import * as process from 'process';
import { join, posix } from 'path';
import { existsSync, readFileSync } from 'fs-extra';
import send from 'send';
import type { Readable } from 'stream';

function findPublicAsset(relativePath: string, resolver: Resolver) {
const packageCache = resolver.packageCache;
let pkg = packageCache.ownerOfFile(relativePath);

for (const engine of resolver.options.engines) {
for (const addon of engine.activeAddons) {
pkg = packageCache.ownerOfFile(addon.root);
if (pkg && pkg.meta && pkg.isV2Addon() && pkg.meta['public-assets']) {
const asset = Object.entries(pkg.meta['public-assets']).find(
([_key, a]) => posix.resolve('/', a) === relativePath
)?.[0];
let local = asset ? join(addon.root, asset) : null;
if (local && existsSync(local)) {
return local;
}
}
}
}
}

export function assets(): Plugin {
const cwd = process.cwd();
const resolverLoader = new ResolverLoader(cwd);
let mode: 'build' | 'serve' = 'build';
let publicDir = 'public';
return {
name: 'assets',
enforce: 'post',
configResolved(options) {
mode = options.command;
publicDir = options.publicDir;
},
configureServer(server) {
return () => {
server.middlewares.use((req, res, next) => {
if (req.originalUrl && req.originalUrl.length > 1) {
const assetUrl = findPublicAsset(req.originalUrl.split('?')[0], resolverLoader.resolver);
if (assetUrl) {
return send(req as Readable, assetUrl).pipe(res);
}
}
return next();
});
};
},
async buildStart() {
if (mode !== 'build') return;
const engines = resolverLoader.resolver.options.engines;
for (const engine of engines) {
const packages = engine.activeAddons.map(a => resolverLoader.resolver.packageCache.ownerOfFile(a.root));
packages.forEach(pkg => {
if (!pkg || !pkg.isV2Addon()) return;
const assets = pkg.meta['public-assets'] || {};
Object.entries(assets).forEach(([path, dest]) => {
// do not override app public assets
if (existsSync(join(publicDir, dest))) {
return;
}
this.emitFile({
type: 'asset',
source: readFileSync(join(pkg.root, path)),
fileName: posix.resolve('/', dest).slice(1),
});
});
});
}
},
};
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/scenarios/vite-app-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ viteAppScenarios
const distFiles = readdirSync(join(app.dir, 'dist'));
assert.ok(distFiles.length > 1, 'should have created dist folder');
assert.ok(distFiles.includes('assets'), 'should have created assets folder');
assert.ok(distFiles.includes('ember-welcome-page'), 'should have copied addon asset files');
assert.ok(distFiles.includes('robots.txt'), 'should have copied app assets');

const assetFiles = readdirSync(join(app.dir, 'dist', 'assets'));
assert.ok(assetFiles.length > 1, 'should have created asset files');
Expand Down
3 changes: 3 additions & 0 deletions tests/vite-app/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
templateTag,
optimizeDeps,
compatPrebuild,
assets,
} from "@embroider/vite";
import { resolve } from "path";
import { babel } from "@rollup/plugin-babel";
Expand All @@ -23,6 +24,7 @@ export default defineConfig({
scripts(),
resolver(),
compatPrebuild(),
assets(),

babel({
babelHelpers: "runtime",
Expand All @@ -41,6 +43,7 @@ export default defineConfig({
ignored: ["!**/node_modules/.embroider/rewritten-app/**"],
},
},
publicDir: resolve(process.cwd(), "public"),
build: {
outDir: resolve(process.cwd(), "dist"),
rollupOptions: {
Expand Down

0 comments on commit e1142b1

Please sign in to comment.