Skip to content

Commit

Permalink
Merge pull request #912 from krisselden/require-adjust-imports-json
Browse files Browse the repository at this point in the history
Use `require` for retrieving the adjust imports info
  • Loading branch information
ef4 authored Jul 29, 2021
2 parents 578d385 + 13df5d4 commit bf888b1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
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

0 comments on commit bf888b1

Please sign in to comment.