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

output macros-config.json and _babel_config_ in the .embroider folder #1935

Merged
merged 2 commits into from
May 22, 2024
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
12 changes: 9 additions & 3 deletions packages/compat/src/audit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, readJSONSync } from 'fs-extra';
import { existsSync, readFileSync, readJSONSync } from 'fs-extra';
import { join, resolve as resolvePath, dirname } from 'path';
import type { AppMeta, ResolverOptions } from '@embroider/core';
import { explicitRelative, hbsToJS, locateEmbroiderWorkingDir, Resolver, RewrittenPackageCache } from '@embroider/core';
Expand Down Expand Up @@ -153,8 +153,14 @@ export class Audit {

@Memoize()
private get babelConfig() {
// eslint-disable-next-line @typescript-eslint/no-require-imports
let config = require(join(this.movedAppRoot, this.meta.babel.filename));
// Depending on how the app builds, the babel config is not at the same location
let embroiderLocation = join(locateEmbroiderWorkingDir(this.originAppRoot), '_babel_config_.js');
let config = existsSync(embroiderLocation)
? // eslint-disable-next-line @typescript-eslint/no-require-imports
require(embroiderLocation)
: // eslint-disable-next-line @typescript-eslint/no-require-imports
require(join(this.movedAppRoot, this.meta.babel.filename));

config = Object.assign({}, config);
config.plugins = config.plugins.filter((p: any) => !isMacrosPlugin(p));

Expand Down
13 changes: 8 additions & 5 deletions packages/compat/src/compat-app-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,7 @@ export class CompatAppBuilder {
this.addAppBoot(this.compatApp.appBoot.readAppBoot());
let babelConfig = await this.babelConfig(resolverConfig);
this.addBabelConfig(babelConfig);
writeFileSync(
join(this.root, 'macros-config.json'),
JSON.stringify(this.compatApp.macrosConfig.babelPluginConfig()[0], null, 2)
);
this.addMacrosConfig(this.compatApp.macrosConfig.babelPluginConfig()[0]);
}

private combinePackageJSON(meta: AppMeta): object {
Expand Down Expand Up @@ -568,7 +565,7 @@ export class CompatAppBuilder {
warn('Your build is slower because some babel plugins are non-serializable');
}
writeFileSync(
join(this.root, '_babel_config_.js'),
join(locateEmbroiderWorkingDir(this.compatApp.root), '_babel_config_.js'),
`module.exports = ${JSON.stringify(pconfig.config, null, 2)}`,
'utf8'
);
Expand All @@ -595,6 +592,12 @@ export class CompatAppBuilder {
});
}

private addMacrosConfig(macrosConfig: any) {
outputJSONSync(join(locateEmbroiderWorkingDir(this.compatApp.root), 'macros-config.json'), macrosConfig, {
spaces: 2,
});
}

private addAppBoot(appBoot?: string) {
writeFileSync(join(locateEmbroiderWorkingDir(this.compatApp.root), 'ember-app-boot.js'), appBoot ?? '');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/virtual-entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ function shouldSplitRoute(routeName: string, splitAtRoutes: (RegExp | string)[]

export function getAppFiles(appRoot: string): Set<string> {
const files: string[] = walkSync(appRoot, {
ignore: ['_babel_config_.js', '_babel_filter_.js', 'app.js', 'assets', 'testem.js', 'node_modules'],
ignore: ['_babel_filter_.js', 'app.js', 'assets', 'testem.js', 'node_modules'],
});
return new Set(files);
}
Expand Down
8 changes: 2 additions & 6 deletions packages/vite/src/esbuild-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ export function esBuildResolver(root = process.cwd()): EsBuildPlugin {
}
let { src } = virtualContent(path, resolverLoader.resolver);
if (!macrosConfig) {
macrosConfig = readJSONSync(
resolve(locateEmbroiderWorkingDir(root), 'rewritten-app', 'macros-config.json')
) as PluginItem;
macrosConfig = readJSONSync(resolve(locateEmbroiderWorkingDir(root), 'macros-config.json')) as PluginItem;
}
return { contents: runMacros(src, path, macrosConfig) };
});
Expand Down Expand Up @@ -120,9 +118,7 @@ export function esBuildResolver(root = process.cwd()): EsBuildPlugin {
src = readFileSync(path, 'utf8');
}
if (!macrosConfig) {
macrosConfig = readJSONSync(
resolve(locateEmbroiderWorkingDir(root), 'rewritten-app', 'macros-config.json')
) as PluginItem;
macrosConfig = readJSONSync(resolve(locateEmbroiderWorkingDir(root), 'macros-config.json')) as PluginItem;
}
return { contents: runMacros(src, path, macrosConfig) };
});
Expand Down
10 changes: 7 additions & 3 deletions test-packages/support/transpiler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { readJSONSync } from 'fs-extra';
import { readJSONSync, existsSync } from 'fs-extra';
import { join } from 'path';
import type { TransformOptions } from '@babel/core';
import { transform } from '@babel/core';
import type { BoundExpectFile } from './file-assertions';
import type { AppMeta } from '../../packages/core/src/index';
import { hbsToJS, RewrittenPackageCache } from '../../packages/core/src/index';
import { hbsToJS, locateEmbroiderWorkingDir, RewrittenPackageCache } from '../../packages/core/src/index';
import { Memoize } from 'typescript-memoize';
import { getRewrittenLocation } from './rewritten-path';

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

return require(join(this.appOutputPath, this.emberMeta['babel'].filename)) as TransformOptions;
// Depending on how the app builds, the babel config is not at the same location
let embroiderLocation = join(locateEmbroiderWorkingDir(this.appDir), '_babel_config_.js');
return existsSync(embroiderLocation)
? (require(embroiderLocation) as TransformOptions)
: (require(join(this.appDir, this.emberMeta['babel'].filename)) as TransformOptions);
}
}
2 changes: 1 addition & 1 deletion tests/addon-template/babel.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if (
) {
config = {};
} else {
config = require("./node_modules/.embroider/rewritten-app/_babel_config_");
config = require("./node_modules/.embroider/_babel_config_");
}

module.exports = config;
2 changes: 1 addition & 1 deletion tests/app-template/babel.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if (
) {
config = {};
} else {
config = require("./node_modules/.embroider/rewritten-app/_babel_config_");
config = require("./node_modules/.embroider/_babel_config_");
}

module.exports = config;
2 changes: 1 addition & 1 deletion tests/ts-app-template-classic/babel.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let config;
if (process.env.EMBROIDER_PREBUILD || process.env.EMBROIDER_TEST_SETUP_FORCE === 'classic') {
config = {};
} else {
config = require('./node_modules/.embroider/rewritten-app/_babel_config_');
config = require('./node_modules/.embroider/_babel_config_');
}

module.exports = config;
2 changes: 1 addition & 1 deletion tests/ts-app-template/babel.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let config;
if (process.env.EMBROIDER_PREBUILD || process.env.EMBROIDER_TEST_SETUP_FORCE === 'classic') {
config = {};
} else {
config = require('./node_modules/.embroider/rewritten-app/_babel_config_');
config = require('./node_modules/.embroider/_babel_config_');
}

module.exports = config;
Loading