Skip to content

Commit

Permalink
fix vite build
Browse files Browse the repository at this point in the history
copy assets to correct folder
  • Loading branch information
patricklx committed Dec 22, 2023
1 parent 241f631 commit 03e3f3e
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/vite/index.d.ts
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';
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';
3 changes: 2 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"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"
},
"devDependencies": {
"@embroider/core": "workspace:^",
Expand Down
40 changes: 40 additions & 0 deletions packages/vite/src/assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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';
import glob from 'fast-glob';

export function assets(): Plugin {
const cwd = process.cwd();
const resolverLoader = new ResolverLoader(cwd);
const engines = resolverLoader.resolver.options.engines;
return {
name: 'assets',
enforce: 'pre',
outputOptions(options) {
options.dir = join(process.cwd(), 'dist');
},
async writeBundle(options) {
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));
}
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));
});
});
}
},
};
}
8 changes: 7 additions & 1 deletion packages/vite/src/hbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import { hbsToJS } from '@embroider/core';
import assertNever from 'assert-never';
import { parse as pathParse } from 'path';
import makeDebug from 'debug';
import { RollupModuleRequest } from './request';

const debug = makeDebug('embroider:hbs-plugin');

export function hbs(): Plugin {
return {
name: 'rollup-hbs-plugin',
enforce: 'pre',
async resolveId(source: string, importer: string | undefined) {
async resolveId(source: string, importer: string | undefined, options) {
let request = RollupModuleRequest.from(source, importer, options.custom);
if (!request) {
// fallthrough to other rollup plugins
return null;
}
let resolution = await this.resolve(source, importer, {
skipSelf: true,
});
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/template-tag.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createFilter } from '@rollup/pluginutils';
import type { Plugin } from 'vite';
import { Preprocessor } from 'content-tag';
import { RollupModuleRequest } from './request';

const gjsFilter = createFilter('**/*.gjs?(\\?)*');

Expand All @@ -15,7 +16,12 @@ export function templateTag(): Plugin {
name: 'embroider-template-tag',
enforce: 'pre',

async resolveId(id: string, importer: string | undefined) {
async resolveId(id: string, importer: string | undefined, options) {
let request = RollupModuleRequest.from(id, importer, options.custom);
if (!request) {
// fallthrough to other rollup plugins
return null;
}
let resolution = await this.resolve(id, importer, {
skipSelf: true,
});
Expand Down
9 changes: 9 additions & 0 deletions tests/scenarios/vite-app-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,14 @@ viteAppScenarios
);
assert.ok(depCache.length > 0, 'should have created cached deps');
});

test(`pnpm build`, async function (assert) {
let result = await app.execute('pnpm build');
assert.equal(result.exitCode, 0, result.output);
const distFiles = readdirSync(join(app.dir, 'dist'));
assert.ok(distFiles.length > 0, 'should have created assets in dist');
assert.ok(distFiles.includes('ember-welcome-page'), 'should have copied addons assets');
assert.ok(distFiles.includes('robots.txt'), 'should have copied app assets');
});
});
});
2 changes: 2 additions & 0 deletions tests/vite-app/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
scripts,
templateTag,
optimizeDeps,
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 03e3f3e

Please sign in to comment.