Skip to content

Commit

Permalink
fix: handle optional alloy component files
Browse files Browse the repository at this point in the history
  • Loading branch information
janvennemann committed Nov 16, 2023
1 parent 319c65c commit 45d1c3d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@rollup/plugin-node-resolve": "^15.0.1",
"acorn": "^8.2.4",
"alloy-compiler": "0.2.7",
"alloy-utils": "^0.2.5",
"alloy-utils": "^0.2.7",
"chalk": "^4.1.2",
"debug": "^4.3.1",
"esbuild": "^0.11.12",
Expand Down
64 changes: 49 additions & 15 deletions src/node/plugins/alloy/component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import path from 'path';
import qs from 'querystring';
import fs from 'fs-extra';
import { createFilter } from '@rollup/pluginutils';
import { ResolvedId } from 'rollup';
import { ModuleNode, Plugin, ResolvedConfig, ViteDevServer } from 'vite';

import { stripBase } from '../../utils/vite.js';
import { cleanUrl, stripBase } from '../../utils/vite.js';
import { AlloyContext } from './context';

const controllerRE =
/(?:[/\\]widgets[/\\]([^/\\]+))?[/\\](?:controllers)[/\\](.*)/;
const EMPTY_EXPORT = 'export default {}';
const VIEW_ONLY_PREFIX = '\0alloyview:';

interface AlloyQuery {
alloy?: boolean;
Expand Down Expand Up @@ -70,6 +72,20 @@ export function componentPlugin(ctx: AlloyContext): Plugin {
importer,
{ skipSelf: true }
);
if (!result) {
// No controller found, but maybe there is a view only
const view = await this.resolve(
path.join(appDir, 'views', `${componentId}.xml`),
importer,
{ skipSelf: true }
);
if (view) {
return (
VIEW_ONLY_PREFIX +
view.id.replace('/app/views/', '/app/controllers/')
);
}
}
}
if (result) {
return result.id;
Expand All @@ -78,6 +94,11 @@ export function componentPlugin(ctx: AlloyContext): Plugin {
},

async load(id) {
if (id.startsWith(VIEW_ONLY_PREFIX)) {
console.log('Load view only controller');
return '';
}

const { filename, query } = parseAlloyRequest(id);
// select corresponding block for sub-part virtual modules
if (query.alloy) {
Expand All @@ -93,34 +114,47 @@ export function componentPlugin(ctx: AlloyContext): Plugin {
},

async transform(code, id) {
if (id.startsWith(VIEW_ONLY_PREFIX)) {
// Map virtual view only id back to controller id
id = id
.replace(VIEW_ONLY_PREFIX, '')
.replace('/app/views/', '/app/controllers/')
.replace(/\.xml/, '.js');
}

const { filename, query } = parseAlloyRequest(id);
if (!query.alloy && !filter(filename)) {
return;
}

const cleanId = cleanUrl(id);

if (!query.alloy) {
ctx.compiler.purgeStyleCache(id);
ctx.compiler.purgeStyleCache(cleanId);
const {
code: controllerCode,
map,
dependencies
} = ctx.compiler.compileComponent({
controllerContent: code,
file: id
file: cleanId
});

const deps = dependencies.map((dep) => {
// Make sure changes to view and style files trigger a controller rebuild
this.addWatchFile(dep);

if (dep.endsWith('.tss')) {
return dep + '?alloy&type=style';
} else if (dep.endsWith('.xml')) {
return dep + '?alloy&type=template';
} else {
throw new Error(`Unknown Alloy component dependency: ${dep}`);
}
});
const deps = dependencies
// Only consider deps that actually exist
.filter((d) => fs.pathExistsSync(d))
.map((dep) => {
// Make sure changes to view and style files trigger a controller rebuild
this.addWatchFile(dep);

if (dep.endsWith('.tss')) {
return dep + '?alloy&type=style';
} else if (dep.endsWith('.xml')) {
return dep + '?alloy&type=template';
} else {
throw new Error(`Unknown Alloy component dependency: ${dep}`);
}
});

if (server) {
// server only handling for view and style dependency hmr
Expand Down
2 changes: 1 addition & 1 deletion src/node/plugins/alloy/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function widgetPlugin(appDir: string): Plugin {
* @todo: Support Widgets from `node_modules`.
*/
async resolveId(id, importer) {
if (id.startsWith('/')) {
if (id.startsWith('/') && !id.startsWith(appDir)) {
// check WPATH generated url `/<widget>/<id>`
const secondSlashIndex = id.indexOf('/', 1);
if (secondSlashIndex !== -1) {
Expand Down
2 changes: 1 addition & 1 deletion src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function resolvePlugins({
clientInjectionsPlugin(),
nativeModulesPlugin(nativeModules),
nodeBuiltinsPlugin(),
resolvePlugin(type, platform),
resolvePlugin(projectDir, type, platform),
i18nPlugin(projectDir, type)
];
if (nativeModules.includes('hyperloop')) {
Expand Down
6 changes: 6 additions & 0 deletions src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { FS_PREFIX } from '../constants.js';
* - Support bare module and absolute ids as relative to source root
*/
export function resolvePlugin(
projectDir: string,
projectType: ProjectType,
platform: Platform
): Plugin {
Expand Down Expand Up @@ -39,6 +40,11 @@ export function resolvePlugin(
return;
}

// prevent nested full path resolving
if (id.startsWith(projectDir)) {
return;
}

const platformResolve = async (id: string, base: string) => {
const result = await this.resolve(path.join(base, id), importer, {
skipSelf: true
Expand Down
16 changes: 0 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1741,22 +1741,6 @@ [email protected]:
walk-sync "^2.0.2"
xmldom "^0.6.0"

alloy-utils@^0.2.5:
version "0.2.5"
resolved "https://registry.npmjs.org/alloy-utils/-/alloy-utils-0.2.5.tgz"
integrity sha512-fzktykZU5Niny/mVNNx07bJcrAfG9paaPA2oYF2vGhuuipgUrnWDvpNnXc7ADWiKUKcBIgQJ2I1paHIrRoI+mA==
dependencies:
"@babel/code-frame" "^7.8.3"
chmodr "^1.2.0"
colors "^1.4.0"
fs-extra "^9.0.0"
global-paths "^1.0.0"
jsonlint "^1.6.3"
lodash "^4.17.15"
moment "^2.24.0"
resolve "^1.12.0"
xmldom "^0.3.0"

alloy-utils@^0.2.7:
version "0.2.7"
resolved "https://registry.npmjs.org/alloy-utils/-/alloy-utils-0.2.7.tgz"
Expand Down

0 comments on commit 45d1c3d

Please sign in to comment.