Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use require for retrieving the adjust imports info #912

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/compat/src/compat-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ class CompatAppAdapter implements AppAdapter<TreeNames> {
podModulePrefix: this.podModulePrefix(),
options: this.options,
activePackageRules: this.activeRules(),
adjustImportsOptionsFile: this.adjustImportsOptionsFile(),
adjustImportsOptionsPath: this.adjustImportsOptionsPath(),
});
}

@Memoize()
adjustImportsOptionsFile(): string {
adjustImportsOptionsPath(): string {
let file = join(this.root, '_adjust_imports.json');
writeFileSync(file, JSON.stringify(this.adjustImportsOptions()));
return file;
Expand Down
9 changes: 5 additions & 4 deletions packages/compat/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Options from './options';
import { ResolvedDep } from '@embroider/core/src/resolver';
import { dasherize } from './dasherize-component-name';
import { makeResolverTransform } from './resolver-transform';
import { pathExistsSync, readFileSync } from 'fs-extra';
import { pathExistsSync } from 'fs-extra';
import resolve from 'resolve';

export interface ComponentResolution {
Expand Down Expand Up @@ -119,7 +119,7 @@ interface RehydrationParamsBase {
}

interface RehydrationParamsWithFile extends RehydrationParamsBase {
adjustImportsOptionsFile: string;
adjustImportsOptionsPath: string;
}

interface RehydrationParamsWithOptions extends RehydrationParamsBase {
Expand Down Expand Up @@ -201,8 +201,9 @@ export default class CompatResolver implements Resolver {
@Memoize()
get adjustImportsOptions(): AdjustImportsOptions {
const { params } = this;
return 'adjustImportsOptionsFile' in params
? JSON.parse(readFileSync(params.adjustImportsOptionsFile, 'utf8'))
return 'adjustImportsOptionsPath' in params
? // eslint-disable-next-line @typescript-eslint/no-require-imports
require(params.adjustImportsOptionsPath)
: params.adjustImportsOptions;
}

Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export interface AppAdapter<TreeNames> {
// compatibility
adjustImportsOptions(): AdjustImportsOptions;

adjustImportsOptionsFile(): string;
adjustImportsOptionsPath(): string;

// The template preprocessor plugins that are configured in the app.
htmlbarsPlugins(): TemplateCompilerPlugins;
Expand Down Expand Up @@ -417,9 +417,14 @@ export class AppBuilder<TreeNames> {
relocatedFiles[join(destPath, relativePath).split(sep).join('/')] = originalPath;
}
}
let relocatedFilesPath = join(this.root, '_relocated_files.json');
writeFileSync(relocatedFilesPath, JSON.stringify({ relocatedFiles }));
return [
require.resolve('./babel-plugin-adjust-imports'),
{ adjustImportsOptionsFile: this.adapter.adjustImportsOptionsFile(), relocatedFiles },
{
adjustImportsOptionsPath: this.adapter.adjustImportsOptionsPath(),
relocatedFilesPath,
},
];
}

Expand Down
16 changes: 7 additions & 9 deletions packages/core/src/babel-plugin-adjust-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join, dirname, resolve } from 'path';
import type { NodePath } from '@babel/traverse';
import type * as t from '@babel/types';
import { PackageCache, Package, V2Package, explicitRelative } from '@embroider/shared-internals';
import { outputFileSync, readFileSync } from 'fs-extra';
import { outputFileSync } from 'fs-extra';
import { Memoize } from 'typescript-memoize';
import { compile } from './js-handlebars';
import { handleImportDeclaration } from './mini-modules-polyfill';
Expand All @@ -14,8 +14,8 @@ interface State {
}

export interface DeflatedOptions {
adjustImportsOptionsFile: string;
relocatedFiles: { [relativePath: string]: string };
adjustImportsOptionsPath: string;
relocatedFilesPath: string;
}

type BabelTypes = typeof t;
Expand Down Expand Up @@ -348,7 +348,7 @@ export default function main(babel: unknown) {
Program: {
enter(path: NodePath<t.Program>, state: State) {
let opts = ensureOpts(state);
state.adjustFile = new AdjustFile(path.hub.file.opts.filename, state.opts.relocatedFiles);
state.adjustFile = new AdjustFile(path.hub.file.opts.filename, opts.relocatedFiles);
addExtraImports(t, path, opts.extraImports);
},
exit(path: NodePath<t.Program>, state: State) {
Expand Down Expand Up @@ -495,11 +495,9 @@ class AdjustFile {

function ensureOpts(state: State): Options {
let { opts } = state;
if ('adjustImportsOptionsFile' in opts) {
let inflated = JSON.parse(readFileSync(opts.adjustImportsOptionsFile, 'utf8'));
inflated.relocatedFiles = opts.relocatedFiles;
state.opts = inflated;
return inflated;
if ('adjustImportsOptionsPath' in opts) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return (state.opts = { ...require(opts.adjustImportsOptionsPath), ...require(opts.relocatedFilesPath) });
}
return opts;
}
Expand Down