From 13df5d4b8d4632ad5ba750b4d67a83b276345283 Mon Sep 17 00:00:00 2001 From: Kris Selden Date: Wed, 28 Jul 2021 13:44:35 -0700 Subject: [PATCH] use require for adjust import json --- packages/compat/src/compat-app.ts | 4 ++-- packages/compat/src/resolver.ts | 9 +++++---- packages/core/src/app.ts | 9 +++++++-- packages/core/src/babel-plugin-adjust-imports.ts | 16 +++++++--------- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/compat/src/compat-app.ts b/packages/compat/src/compat-app.ts index 183fee90a..4fd0f8e49 100644 --- a/packages/compat/src/compat-app.ts +++ b/packages/compat/src/compat-app.ts @@ -338,12 +338,12 @@ class CompatAppAdapter implements AppAdapter { 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; diff --git a/packages/compat/src/resolver.ts b/packages/compat/src/resolver.ts index 465805090..de4dbf910 100644 --- a/packages/compat/src/resolver.ts +++ b/packages/compat/src/resolver.ts @@ -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 { @@ -119,7 +119,7 @@ interface RehydrationParamsBase { } interface RehydrationParamsWithFile extends RehydrationParamsBase { - adjustImportsOptionsFile: string; + adjustImportsOptionsPath: string; } interface RehydrationParamsWithOptions extends RehydrationParamsBase { @@ -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; } diff --git a/packages/core/src/app.ts b/packages/core/src/app.ts index 4101403d7..de4f75779 100644 --- a/packages/core/src/app.ts +++ b/packages/core/src/app.ts @@ -111,7 +111,7 @@ export interface AppAdapter { // compatibility adjustImportsOptions(): AdjustImportsOptions; - adjustImportsOptionsFile(): string; + adjustImportsOptionsPath(): string; // The template preprocessor plugins that are configured in the app. htmlbarsPlugins(): TemplateCompilerPlugins; @@ -417,9 +417,14 @@ export class AppBuilder { 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, + }, ]; } diff --git a/packages/core/src/babel-plugin-adjust-imports.ts b/packages/core/src/babel-plugin-adjust-imports.ts index de4ae06bf..da377c7b4 100644 --- a/packages/core/src/babel-plugin-adjust-imports.ts +++ b/packages/core/src/babel-plugin-adjust-imports.ts @@ -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'; @@ -14,8 +14,8 @@ interface State { } export interface DeflatedOptions { - adjustImportsOptionsFile: string; - relocatedFiles: { [relativePath: string]: string }; + adjustImportsOptionsPath: string; + relocatedFilesPath: string; } type BabelTypes = typeof t; @@ -348,7 +348,7 @@ export default function main(babel: unknown) { Program: { enter(path: NodePath, 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, state: State) { @@ -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; }