Skip to content

Commit 93b39c4

Browse files
authored
Merge pull request #1935 from embroider-build/align-package-json-mcros-config
Feat(remove rewritten-app) / output macros-config.json in the .embroider folder
2 parents d47891f + 187f072 commit 93b39c4

File tree

9 files changed

+31
-22
lines changed

9 files changed

+31
-22
lines changed

packages/compat/src/audit.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync, readJSONSync } from 'fs-extra';
1+
import { existsSync, readFileSync, readJSONSync } from 'fs-extra';
22
import { join, resolve as resolvePath, dirname } from 'path';
33
import type { AppMeta, ResolverOptions } from '@embroider/core';
44
import { explicitRelative, hbsToJS, locateEmbroiderWorkingDir, Resolver, RewrittenPackageCache } from '@embroider/core';
@@ -153,8 +153,14 @@ export class Audit {
153153

154154
@Memoize()
155155
private get babelConfig() {
156-
// eslint-disable-next-line @typescript-eslint/no-require-imports
157-
let config = require(join(this.movedAppRoot, this.meta.babel.filename));
156+
// Depending on how the app builds, the babel config is not at the same location
157+
let embroiderLocation = join(locateEmbroiderWorkingDir(this.originAppRoot), '_babel_config_.js');
158+
let config = existsSync(embroiderLocation)
159+
? // eslint-disable-next-line @typescript-eslint/no-require-imports
160+
require(embroiderLocation)
161+
: // eslint-disable-next-line @typescript-eslint/no-require-imports
162+
require(join(this.movedAppRoot, this.meta.babel.filename));
163+
158164
config = Object.assign({}, config);
159165
config.plugins = config.plugins.filter((p: any) => !isMacrosPlugin(p));
160166

packages/compat/src/compat-app-builder.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,7 @@ export class CompatAppBuilder {
488488
this.addAppBoot(this.compatApp.appBoot.readAppBoot());
489489
let babelConfig = await this.babelConfig(resolverConfig);
490490
this.addBabelConfig(babelConfig);
491-
writeFileSync(
492-
join(this.root, 'macros-config.json'),
493-
JSON.stringify(this.compatApp.macrosConfig.babelPluginConfig()[0], null, 2)
494-
);
491+
this.addMacrosConfig(this.compatApp.macrosConfig.babelPluginConfig()[0]);
495492
}
496493

497494
private combinePackageJSON(meta: AppMeta): object {
@@ -568,7 +565,7 @@ export class CompatAppBuilder {
568565
warn('Your build is slower because some babel plugins are non-serializable');
569566
}
570567
writeFileSync(
571-
join(this.root, '_babel_config_.js'),
568+
join(locateEmbroiderWorkingDir(this.compatApp.root), '_babel_config_.js'),
572569
`module.exports = ${JSON.stringify(pconfig.config, null, 2)}`,
573570
'utf8'
574571
);
@@ -595,6 +592,12 @@ export class CompatAppBuilder {
595592
});
596593
}
597594

595+
private addMacrosConfig(macrosConfig: any) {
596+
outputJSONSync(join(locateEmbroiderWorkingDir(this.compatApp.root), 'macros-config.json'), macrosConfig, {
597+
spaces: 2,
598+
});
599+
}
600+
598601
private addAppBoot(appBoot?: string) {
599602
writeFileSync(join(locateEmbroiderWorkingDir(this.compatApp.root), 'ember-app-boot.js'), appBoot ?? '');
600603
}

packages/core/src/virtual-entrypoint.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ function shouldSplitRoute(routeName: string, splitAtRoutes: (RegExp | string)[]
349349

350350
export function getAppFiles(appRoot: string): Set<string> {
351351
const files: string[] = walkSync(appRoot, {
352-
ignore: ['_babel_config_.js', '_babel_filter_.js', 'app.js', 'assets', 'testem.js', 'node_modules'],
352+
ignore: ['_babel_filter_.js', 'app.js', 'assets', 'testem.js', 'node_modules'],
353353
});
354354
return new Set(files);
355355
}

packages/vite/src/esbuild-resolver.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ export function esBuildResolver(root = process.cwd()): EsBuildPlugin {
7575
}
7676
let { src } = virtualContent(path, resolverLoader.resolver);
7777
if (!macrosConfig) {
78-
macrosConfig = readJSONSync(
79-
resolve(locateEmbroiderWorkingDir(root), 'rewritten-app', 'macros-config.json')
80-
) as PluginItem;
78+
macrosConfig = readJSONSync(resolve(locateEmbroiderWorkingDir(root), 'macros-config.json')) as PluginItem;
8179
}
8280
return { contents: runMacros(src, path, macrosConfig) };
8381
});
@@ -120,9 +118,7 @@ export function esBuildResolver(root = process.cwd()): EsBuildPlugin {
120118
src = readFileSync(path, 'utf8');
121119
}
122120
if (!macrosConfig) {
123-
macrosConfig = readJSONSync(
124-
resolve(locateEmbroiderWorkingDir(root), 'rewritten-app', 'macros-config.json')
125-
) as PluginItem;
121+
macrosConfig = readJSONSync(resolve(locateEmbroiderWorkingDir(root), 'macros-config.json')) as PluginItem;
126122
}
127123
return { contents: runMacros(src, path, macrosConfig) };
128124
});

test-packages/support/transpiler.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { readJSONSync } from 'fs-extra';
1+
import { readJSONSync, existsSync } from 'fs-extra';
22
import { join } from 'path';
33
import type { TransformOptions } from '@babel/core';
44
import { transform } from '@babel/core';
55
import type { BoundExpectFile } from './file-assertions';
66
import type { AppMeta } from '../../packages/core/src/index';
7-
import { hbsToJS, RewrittenPackageCache } from '../../packages/core/src/index';
7+
import { hbsToJS, locateEmbroiderWorkingDir, RewrittenPackageCache } from '../../packages/core/src/index';
88
import { Memoize } from 'typescript-memoize';
99
import { getRewrittenLocation } from './rewritten-path';
1010

@@ -53,6 +53,10 @@ export class Transpiler {
5353
throw new Error(`@embroider/test-support only suports babel 7`);
5454
}
5555

56-
return require(join(this.appOutputPath, this.emberMeta['babel'].filename)) as TransformOptions;
56+
// Depending on how the app builds, the babel config is not at the same location
57+
let embroiderLocation = join(locateEmbroiderWorkingDir(this.appDir), '_babel_config_.js');
58+
return existsSync(embroiderLocation)
59+
? (require(embroiderLocation) as TransformOptions)
60+
: (require(join(this.appDir, this.emberMeta['babel'].filename)) as TransformOptions);
5761
}
5862
}

tests/addon-template/babel.config.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if (
1313
) {
1414
config = {};
1515
} else {
16-
config = require("./node_modules/.embroider/rewritten-app/_babel_config_");
16+
config = require("./node_modules/.embroider/_babel_config_");
1717
}
1818

1919
module.exports = config;

tests/app-template/babel.config.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if (
1313
) {
1414
config = {};
1515
} else {
16-
config = require("./node_modules/.embroider/rewritten-app/_babel_config_");
16+
config = require("./node_modules/.embroider/_babel_config_");
1717
}
1818

1919
module.exports = config;

tests/ts-app-template-classic/babel.config.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let config;
1010
if (process.env.EMBROIDER_PREBUILD || process.env.EMBROIDER_TEST_SETUP_FORCE === 'classic') {
1111
config = {};
1212
} else {
13-
config = require('./node_modules/.embroider/rewritten-app/_babel_config_');
13+
config = require('./node_modules/.embroider/_babel_config_');
1414
}
1515

1616
module.exports = config;

tests/ts-app-template/babel.config.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let config;
1010
if (process.env.EMBROIDER_PREBUILD || process.env.EMBROIDER_TEST_SETUP_FORCE === 'classic') {
1111
config = {};
1212
} else {
13-
config = require('./node_modules/.embroider/rewritten-app/_babel_config_');
13+
config = require('./node_modules/.embroider/_babel_config_');
1414
}
1515

1616
module.exports = config;

0 commit comments

Comments
 (0)