Skip to content

Commit

Permalink
Merge pull request #1435 from embroider-build/done-rewrite-node-modules2
Browse files Browse the repository at this point in the history
Working to eliminate node_modules rewriting
  • Loading branch information
ef4 authored Jun 28, 2023
2 parents f7c5518 + 55b592c commit 3030376
Show file tree
Hide file tree
Showing 74 changed files with 3,333 additions and 3,367 deletions.
4 changes: 4 additions & 0 deletions packages/compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"@babel/code-frame": "^7.14.5",
"@babel/core": "^7.14.5",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.14.5",
"@babel/preset-env": "^7.14.5",
"@babel/runtime": "^7.18.6",
"@babel/traverse": "^7.14.5",
"@embroider/macros": "workspace:*",
"@types/babel__code-frame": "^7.0.2",
Expand All @@ -47,6 +49,8 @@
"broccoli-source": "^3.0.1",
"chalk": "^4.1.1",
"debug": "^4.3.2",
"escape-string-regexp": "^4.0.0",
"fast-sourcemap-concat": "^1.4.0",
"fs-extra": "^9.1.0",
"fs-tree-diff": "^2.0.1",
"jsdom": "^16.6.0",
Expand Down
69 changes: 31 additions & 38 deletions packages/compat/src/audit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { readFileSync, readJSONSync } from 'fs-extra';
import { dirname, join, resolve as resolvePath } from 'path';
import { AppMeta, explicitRelative, hbsToJS, Resolver, ResolverOptions } from '@embroider/core';
import {
AppMeta,
explicitRelative,
hbsToJS,
locateEmbroiderWorkingDir,
Resolver,
ResolverOptions,
RewrittenPackageCache,
} from '@embroider/core';
import { Memoize } from 'typescript-memoize';
import chalk from 'chalk';
import jsdom from 'jsdom';
Expand Down Expand Up @@ -213,18 +221,11 @@ export class Audit {
private frames = new CodeFrameStorage();

static async run(options: AuditBuildOptions): Promise<AuditResults> {
let dir: string;

if (options.outputDir) {
dir = options.outputDir;
} else {
if (!options['reuse-build']) {
await buildApp(options);
}
dir = await this.findStage2Output(options);
if (!options['reuse-build']) {
await buildApp(options);
}

let audit = new this(dir, options);
let audit = new this(options.app, options);
if (options['reuse-build']) {
if (!audit.meta.babel.isParallelSafe) {
throw new BuildError(
Expand All @@ -237,29 +238,17 @@ export class Audit {
return audit.run();
}

private static async findStage2Output(options: AuditBuildOptions): Promise<string> {
try {
if (!options.app) {
throw new Error(`AuditBuildOptions needs "app" directory`);
}
return readFileSync(join(options.app, 'dist/.stage2-output'), 'utf8');
} catch (err) {
if (err.code === 'ENOENT') {
throw new BuildError(
`${chalk.yellow(
'Your build'
)} did not produce expected Embroider stage2 output.\nMake sure you actually have Embroider configured.`
);
}
throw err;
}
}

constructor(private appDir: string, private options: AuditOptions = {}) {}
constructor(private originAppRoot: string, private options: AuditOptions = {}) {}

@Memoize()
private get pkg() {
return readJSONSync(join(this.appDir, 'package.json'));
return readJSONSync(join(this.movedAppRoot, 'package.json'));
}

@Memoize()
private get movedAppRoot() {
let cache = RewrittenPackageCache.shared('embroider', this.originAppRoot);
return cache.maybeMoved(cache.get(this.originAppRoot)).root;
}

private get meta() {
Expand All @@ -269,7 +258,7 @@ export class Audit {
@Memoize()
private get babelConfig() {
// eslint-disable-next-line @typescript-eslint/no-require-imports
let config = require(join(this.appDir, this.meta.babel.filename));
let config = require(join(this.movedAppRoot, this.meta.babel.filename));
config = Object.assign({}, config);
config.plugins = config.plugins.filter((p: any) => !isMacrosPlugin(p));

Expand All @@ -278,7 +267,7 @@ export class Audit {
}

private get resolverParams(): ResolverOptions {
return readJSONSync(join(this.appDir, '.embroider', 'resolver.json'));
return readJSONSync(join(locateEmbroiderWorkingDir(this.originAppRoot), 'resolver.json'));
}

private resolver = new Resolver(this.resolverParams);
Expand Down Expand Up @@ -344,13 +333,14 @@ export class Audit {
this.debug(`meta`, this.meta);
for (let asset of this.meta.assets) {
if (asset.endsWith('.html')) {
this.scheduleVisit(resolvePath(this.appDir, asset), { isRoot: true });
this.scheduleVisit(resolvePath(this.movedAppRoot, asset), { isRoot: true });
}
}
await this.drainQueue();
this.linkModules();
this.inspectModules();
return AuditResults.create(this.appDir, this.findings, this.modules);

return AuditResults.create(this.originAppRoot, this.findings, this.modules);
} finally {
delete (globalThis as any).embroider_audit;
}
Expand Down Expand Up @@ -468,7 +458,10 @@ export class Audit {
}
if (src.startsWith(this.meta['root-url'])) {
// root-relative URLs are actually relative to the appDir
src = explicitRelative(dirname(filename), resolvePath(this.appDir, src.replace(this.meta['root-url'], '')));
src = explicitRelative(
dirname(filename),
resolvePath(this.movedAppRoot, src.replace(this.meta['root-url'], ''))
);
}
dependencies.push(src);
}
Expand Down Expand Up @@ -513,7 +506,7 @@ export class Audit {
{
filename,
message: `failed to parse`,
detail: err.toString().replace(filename, explicitRelative(this.appDir, filename)),
detail: err.toString().replace(filename, explicitRelative(this.originAppRoot, filename)),
},
];
} else {
Expand Down Expand Up @@ -544,7 +537,7 @@ export class Audit {
{
filename,
message: `failed to parse JSON`,
detail: err.toString().replace(filename, explicitRelative(this.appDir, filename)),
detail: err.toString().replace(filename, explicitRelative(this.originAppRoot, filename)),
},
];
}
Expand Down
3 changes: 1 addition & 2 deletions packages/compat/src/audit/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ export interface AuditOptions {

export interface AuditBuildOptions extends AuditOptions {
'reuse-build'?: boolean;
app?: string;
outputDir?: string;
app: string;
}
6 changes: 4 additions & 2 deletions packages/compat/src/babel-plugin-adjust-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { types as t } from '@babel/core';
import { ImportUtil } from 'babel-import-util';
import { readJSONSync } from 'fs-extra';
import { CompatResolverOptions } from './resolver-transform';
import { Package, packageName, Resolver, unrelativize } from '@embroider/core';
import { locateEmbroiderWorkingDir, Package, packageName, Resolver, unrelativize } from '@embroider/core';
import { snippetToDasherizedName } from './dasherize-component-name';
import { ActivePackageRules, appTreeRulesDir, ComponentRules, ModuleRules, TemplateRules } from './dependency-rules';

Expand Down Expand Up @@ -42,7 +42,9 @@ export default function main(babel: typeof Babel) {
if (cached) {
return cached;
}
let resolverOptions: CompatResolverOptions = readJSONSync(join(appRoot, '.embroider', 'resolver.json'));
let resolverOptions: CompatResolverOptions = readJSONSync(
join(locateEmbroiderWorkingDir(appRoot), 'resolver.json')
);
let resolver = new Resolver(resolverOptions);
cached = {
resolverOptions,
Expand Down
14 changes: 14 additions & 0 deletions packages/compat/src/compat-adapters/@ember-data/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AddonMeta } from '@embroider/core';
import V1Addon from '../../v1-addon';

export default class EmberDataDebug extends V1Addon {
get packageMeta(): Partial<AddonMeta> {
let meta = super.packageMeta;

// See also the compat-adapter for @ember-data/store where we make this an
// implicit-module.
meta.externals = [...(meta.externals ?? []), '@ember-data/store'];

return meta;
}
}
18 changes: 17 additions & 1 deletion packages/compat/src/compat-adapters/@ember-data/store.ts
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export { EmberDataBase as default } from '../ember-data';
import { AddonMeta } from '@embroider/core';
import { EmberDataBase } from '../ember-data';

export default class EmberDataStore extends EmberDataBase {
get packageMeta(): Partial<AddonMeta> {
let meta = super.packageMeta;

// this is here because the compat-adapter for @ember-data/debug adds this
// to externals because it has an undeclared peerDep on us, and thus might
// resolve totally incorrect copies. By making it external we leave it up to
// runtime, where we will find this implicit-module for the actual copy of
// @ember-data/store that is active in app.
meta['implicit-modules'] = [...(meta['implicit-modules'] ?? []), './index.js'];

return meta;
}
}
Loading

0 comments on commit 3030376

Please sign in to comment.