Skip to content

Commit

Permalink
implement staticAppPaths for virtual entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed May 16, 2024
1 parent 9b29b55 commit a76f939
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 9 deletions.
12 changes: 12 additions & 0 deletions packages/compat/src/compat-app-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import type { Package, PackageInfo } from '@embroider/core';
import { ensureDirSync, copySync, readdirSync, pathExistsSync } from 'fs-extra';
import type { TransformOptions } from '@babel/core';
import { MacrosConfig } from '@embroider/macros/src/node';
import escapeRegExp from 'escape-string-regexp';

import type CompatApp from './compat-app';
import { SyncDir } from './sync-dir';
Expand Down Expand Up @@ -279,6 +280,7 @@ export class CompatAppBuilder {
activePackageRules: this.activeRules(),
options,
autoRun: this.compatApp.autoRun,
staticAppPaths: this.options.staticAppPaths,
};

return config;
Expand Down Expand Up @@ -546,11 +548,21 @@ export class CompatAppBuilder {
appSync.files,
fastbootSync?.files ?? new Set(),
this.resolvableExtensionsPattern,
this.staticAppPathsPattern,
this.podModulePrefix()
)
);
}

@Memoize()
private get staticAppPathsPattern(): RegExp | undefined {
if (this.options.staticAppPaths.length > 0) {
return new RegExp(
'^(?:' + this.options.staticAppPaths.map(staticAppPath => escapeRegExp(staticAppPath)).join('|') + ')(?:$|/)'
);
}
}

private prepareAsset(asset: Asset, appFiles: AppFiles[], prepared: Map<string, InternalAsset>) {
if (asset.kind === 'ember') {
let prior = this.assets.get(asset.relativePath);
Expand Down
1 change: 1 addition & 0 deletions packages/compat/tests/audit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('audit', function () {
],
resolvableExtensions,
autoRun: true,
staticAppPaths: [],
};

let babel: TransformOptions = {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@babel/parser": "^7.14.5",
"@babel/traverse": "^7.14.5",
"@embroider/macros": "workspace:*",
"@embroider/reverse-exports": "workspace:*",
"@embroider/shared-internals": "workspace:*",
"assert-never": "^1.2.1",
"babel-plugin-ember-template-compilation": "^2.1.1",
Expand All @@ -32,6 +33,7 @@
"broccoli-plugin": "^4.0.7",
"broccoli-source": "^3.0.1",
"debug": "^4.3.2",
"escape-string-regexp": "^4.0.0",
"fast-sourcemap-concat": "^1.4.0",
"filesize": "^10.0.7",
"fs-extra": "^9.1.0",
Expand All @@ -43,7 +45,6 @@
"resolve": "^1.20.0",
"resolve-package-path": "^4.0.1",
"resolve.exports": "^2.0.2",
"@embroider/reverse-exports": "workspace:*",
"typescript-memoize": "^1.0.1",
"walk-sync": "^3.0.0"
},
Expand All @@ -56,8 +57,8 @@
"@types/babel__traverse": "^7.18.5",
"@types/debug": "^4.1.5",
"@types/fs-extra": "^9.0.12",
"@types/jsdom": "^16.2.11",
"@types/js-string-escape": "^1.0.0",
"@types/jsdom": "^16.2.11",
"@types/lodash": "^4.14.170",
"@types/node": "^15.12.2",
"@types/resolve": "^1.20.0",
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/app-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class AppFiles {
appFiles: Set<string>,
fastbootFiles: Set<string>,
resolvableExtensions: RegExp,
staticAppPathsPattern: RegExp | undefined,
podModulePrefix?: string
) {
let tests: string[] = [];
Expand Down Expand Up @@ -114,7 +115,13 @@ export class AppFiles {
continue;
}

otherAppFiles.push(relativePath);
if (staticAppPathsPattern) {
if (!staticAppPathsPattern.test(relativePath)) {
otherAppFiles.push(relativePath);
}
} else {
otherAppFiles.push(relativePath);
}
}
this.tests = tests;
this.components = components;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export interface Options {
podModulePrefix?: string;
amdCompatibility: Required<UserOptions['amdCompatibility']>;
autoRun: boolean;
staticAppPaths: string[];
}

// TODO: once we can remove the stage2 entrypoint this type can get streamlined
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/virtual-entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import walkSync from 'walk-sync';
import type { V2AddonPackage } from '@embroider/shared-internals/src/package';
import { encodePublicRouteEntrypoint } from './virtual-route-entrypoint';
import { readFileSync } from 'fs-extra';
import escapeRegExp from 'escape-string-regexp';

const entrypointPattern = /(?<filename>.*)[\\/]-embroider-entrypoint.js/;

Expand All @@ -25,6 +26,12 @@ export function decodeEntrypoint(filename: string): { fromFile: string } | undef
}
}

export function staticAppPathsPattern(staticAppPaths: string[] | undefined): RegExp | undefined {
if (staticAppPaths && staticAppPaths.length > 0) {
return new RegExp('^(?:' + staticAppPaths.map(staticAppPath => escapeRegExp(staticAppPath)).join('|') + ')(?:$|/)');
}
}

export function renderEntrypoint(
resolver: Resolver,
{ fromFile }: { fromFile: string }
Expand Down Expand Up @@ -58,6 +65,7 @@ export function renderEntrypoint(
getAppFiles(owner.root),
hasFastboot ? getFastbootFiles(owner.root) : new Set(),
extensionsPattern(resolver.options.resolvableExtensions),
staticAppPathsPattern(resolver.options.staticAppPaths),
resolver.options.podModulePrefix
);

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/virtual-route-entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { resolve } from 'path';
import { compile } from './js-handlebars';
import { extensionsPattern } from '@embroider/shared-internals';
import { partition } from 'lodash';
import { getAppFiles, getFastbootFiles, importPaths, splitRoute } from './virtual-entrypoint';
import { getAppFiles, getFastbootFiles, importPaths, splitRoute, staticAppPathsPattern } from './virtual-entrypoint';

const entrypointPattern = /(?<filename>.*)[\\/]-embroider-route-entrypoint.js:route=(?<route>.*)/;

Expand Down Expand Up @@ -70,6 +70,7 @@ export function renderRouteEntrypoint(
getAppFiles(owner.root),
hasFastboot ? getFastbootFiles(owner.root) : new Set(),
extensionsPattern(resolver.options.resolvableExtensions),
staticAppPathsPattern(resolver.options.staticAppPaths),
resolver.options.podModulePrefix
);

Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions tests/scenarios/compat-resolver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Scenarios.fromProject(() => new Project())
},
],
autoRun: true,
staticAppPaths: [],
};

givenFiles({
Expand Down
4 changes: 2 additions & 2 deletions tests/scenarios/compat-stage2-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ stage2Scenarios
.resolves('/@embroider/core/entrypoint')
.toModule()
.withContents(content => {
return !/my-app\/static-dir\/my-library\.js"/.test(content);
return !/\.\/static-dir\/my-library\.js"/.test(content);
});
});

Expand All @@ -710,7 +710,7 @@ stage2Scenarios
.resolves('/@embroider/core/entrypoint')
.toModule()
.withContents(content => {
return !content.includes('my-app/top-level-static.js');
return !content.includes('./top-level-static.js');
});
});

Expand Down
1 change: 1 addition & 0 deletions tests/scenarios/core-resolver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Scenarios.fromProject(() => new Project())
},
],
autoRun: true,
staticAppPaths: [],
};

givenFiles({
Expand Down

0 comments on commit a76f939

Please sign in to comment.