diff --git a/packages/compat/src/compat-app-builder.ts b/packages/compat/src/compat-app-builder.ts index de6285b29..32de08e87 100644 --- a/packages/compat/src/compat-app-builder.ts +++ b/packages/compat/src/compat-app-builder.ts @@ -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'; @@ -279,6 +280,7 @@ export class CompatAppBuilder { activePackageRules: this.activeRules(), options, autoRun: this.compatApp.autoRun, + staticAppPaths: this.options.staticAppPaths, }; return config; @@ -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) { if (asset.kind === 'ember') { let prior = this.assets.get(asset.relativePath); diff --git a/packages/compat/tests/audit.test.ts b/packages/compat/tests/audit.test.ts index d153d7e52..71fa88239 100644 --- a/packages/compat/tests/audit.test.ts +++ b/packages/compat/tests/audit.test.ts @@ -52,6 +52,7 @@ describe('audit', function () { ], resolvableExtensions, autoRun: true, + staticAppPaths: [], }; let babel: TransformOptions = { diff --git a/packages/core/package.json b/packages/core/package.json index b7d23b375..3cef5649c 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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", @@ -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", @@ -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" }, @@ -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", diff --git a/packages/core/src/app-files.ts b/packages/core/src/app-files.ts index d3ec4fa6b..26322af6d 100644 --- a/packages/core/src/app-files.ts +++ b/packages/core/src/app-files.ts @@ -23,6 +23,7 @@ export class AppFiles { appFiles: Set, fastbootFiles: Set, resolvableExtensions: RegExp, + staticAppPathsPattern: RegExp | undefined, podModulePrefix?: string ) { let tests: string[] = []; @@ -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; diff --git a/packages/core/src/module-resolver.ts b/packages/core/src/module-resolver.ts index e34415144..ab34268c2 100644 --- a/packages/core/src/module-resolver.ts +++ b/packages/core/src/module-resolver.ts @@ -96,6 +96,7 @@ export interface Options { podModulePrefix?: string; amdCompatibility: Required; autoRun: boolean; + staticAppPaths: string[]; } // TODO: once we can remove the stage2 entrypoint this type can get streamlined diff --git a/packages/core/src/virtual-entrypoint.ts b/packages/core/src/virtual-entrypoint.ts index 9b1d8f202..7f1b0bed4 100644 --- a/packages/core/src/virtual-entrypoint.ts +++ b/packages/core/src/virtual-entrypoint.ts @@ -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 = /(?.*)[\\/]-embroider-entrypoint.js/; @@ -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 } @@ -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 ); diff --git a/packages/core/src/virtual-route-entrypoint.ts b/packages/core/src/virtual-route-entrypoint.ts index c099d8f5d..82f589fe6 100644 --- a/packages/core/src/virtual-route-entrypoint.ts +++ b/packages/core/src/virtual-route-entrypoint.ts @@ -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 = /(?.*)[\\/]-embroider-route-entrypoint.js:route=(?.*)/; @@ -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 ); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1c947a4e2..63ec42f22 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -408,6 +408,9 @@ importers: debug: specifier: ^4.3.2 version: 4.3.4(supports-color@9.4.0) + escape-string-regexp: + specifier: ^4.0.0 + version: 4.0.0 fast-sourcemap-concat: specifier: ^1.4.0 version: 1.4.0 @@ -8339,9 +8342,6 @@ packages: /ajv-formats@2.1.1: resolution: {integrity: sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==} - peerDependenciesMeta: - ajv: - optional: true dependencies: ajv: 8.13.0 diff --git a/tests/scenarios/compat-resolver-test.ts b/tests/scenarios/compat-resolver-test.ts index f4babf45b..724a33193 100644 --- a/tests/scenarios/compat-resolver-test.ts +++ b/tests/scenarios/compat-resolver-test.ts @@ -101,6 +101,7 @@ Scenarios.fromProject(() => new Project()) }, ], autoRun: true, + staticAppPaths: [], }; givenFiles({ diff --git a/tests/scenarios/compat-stage2-test.ts b/tests/scenarios/compat-stage2-test.ts index 30fc37b8b..8100bbeec 100644 --- a/tests/scenarios/compat-stage2-test.ts +++ b/tests/scenarios/compat-stage2-test.ts @@ -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); }); }); @@ -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'); }); }); diff --git a/tests/scenarios/core-resolver-test.ts b/tests/scenarios/core-resolver-test.ts index e6a8664be..a4c4e8073 100644 --- a/tests/scenarios/core-resolver-test.ts +++ b/tests/scenarios/core-resolver-test.ts @@ -133,6 +133,7 @@ Scenarios.fromProject(() => new Project()) }, ], autoRun: true, + staticAppPaths: [], }; givenFiles({