Skip to content

Commit

Permalink
use
Browse files Browse the repository at this point in the history
* this.emitFile
* send
* use vite's stock public folder support, use enfore:post
  • Loading branch information
patricklx committed Mar 12, 2024
1 parent c7179e7 commit c751471
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
4 changes: 3 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
"jsdom": "^16.6.0",
"source-map-url": "^0.4.1",
"terser": "^5.7.0",
"fast-glob": "^3.3.2"
"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
57 changes: 25 additions & 32 deletions packages/vite/src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
import type { Resolver } from '@embroider/core';
import { ResolverLoader, locateEmbroiderWorkingDir } from '@embroider/core';
import { ResolverLoader } from '@embroider/core';
import type { Plugin } from 'vite';
import * as process from 'process';
import { dirname, join } from 'path';
import { copyFileSync, mkdirpSync, existsSync } from 'fs-extra';
import { join } from 'path';
import { existsSync, readFileSync } from 'fs-extra';
import glob from 'fast-glob';
import send from 'send';

function findPublicAsset(relativePath: string, resolver: Resolver, embroiderWorkingDir: string) {
function findPublicAsset(relativePath: string, resolver: Resolver) {
const packageCache = resolver.packageCache;
const cwd = process.cwd();
const publicDir = join(cwd, 'public');
// check public path
let pkg = packageCache.ownerOfFile(relativePath);
let p = join(publicDir, relativePath);
if (pkg && pkg.isV2App() && existsSync(p)) {
return '/' + p;
}

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]) => a === relativePath)?.[0];
let local = asset ? join(addon.root, asset) : null;
if (!local?.includes(embroiderWorkingDir) && asset) {
// remap to local path without symlinks so vite can find it
const localNodeModulePath = local?.split('/node_modules/').slice(-1)[0]!;
local = join('node_modules', localNodeModulePath);
}
if (local && existsSync(local)) {
return '/' + local;
return local;
}
}
}
Expand All @@ -39,46 +28,50 @@ function findPublicAsset(relativePath: string, resolver: Resolver, embroiderWork
export function assets(): Plugin {
const cwd = process.cwd();
const resolverLoader = new ResolverLoader(cwd);
const embroiderWorkingDir = locateEmbroiderWorkingDir(cwd);
let mode: 'build' | 'serve' = 'build';
return {
name: 'assets',
enforce: 'pre',
outputOptions(options) {
options.dir = join(process.cwd(), 'dist');
},
enforce: 'post',
configureServer(server) {
server.middlewares.use((req, _res, next) => {
mode = server.config.command;
server.middlewares.use((req, res, next) => {
if (req.originalUrl?.includes('?')) {
return next();
}
if (req.originalUrl && req.originalUrl.length > 1) {
const newUrl = findPublicAsset(req.originalUrl, resolverLoader.resolver, embroiderWorkingDir);
if (newUrl) {
req.originalUrl = newUrl;
(req as any).url = newUrl;
const assetUrl = findPublicAsset(req.originalUrl, resolverLoader.resolver);
if (assetUrl) {
return send(req, assetUrl).pipe(res);
}
}
return next();
});
},
async writeBundle(options) {
async buildStart() {
if (mode !== 'build') return;
const engines = resolverLoader.resolver.options.engines;
const pubDir = join(process.cwd(), 'public');
const publicAppFiles = glob.sync('**/*', {
cwd: pubDir,
});
for (const publicAppFile of publicAppFiles) {
mkdirpSync(dirname(join(options.dir!, publicAppFile)));
copyFileSync(join(pubDir, publicAppFile), join(options.dir!, publicAppFile));
this.emitFile({
type: 'asset',
source: readFileSync(join(pubDir, publicAppFile)),
fileName: publicAppFile,
});
}
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));
this.emitFile({
type: 'asset',
source: readFileSync(join(pkg.root, path)),
fileName: dest.slice(1),
});
});
});
}
Expand Down
1 change: 0 additions & 1 deletion packages/vite/src/template-tag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createFilter } from '@rollup/pluginutils';
import type { Plugin } from 'vite';
import { Preprocessor } from 'content-tag';
import { RollupModuleRequest } from './request';

const gjsFilter = createFilter('**/*.{gjs,gts}?(\\?)*');

Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit c751471

Please sign in to comment.