Skip to content

Commit

Permalink
Merge pull request #796 from charlespierce/babel_loader_options
Browse files Browse the repository at this point in the history
Allow customization of Webpack's babel loader options
  • Loading branch information
ef4 authored May 4, 2021
2 parents 76d9546 + 4552d90 commit ea6f2dd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
39 changes: 28 additions & 11 deletions packages/webpack/src/ember-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import makeDebug from 'debug';
import { format } from 'util';
import { tmpdir } from 'os';
import { warmup as threadLoaderWarmup } from 'thread-loader';
import Options from './options';
import { Options, BabelLoaderOptions } from './options';
import { HTMLEntrypoint } from './html-entrypoint';
import { StatSummary } from './stat-summary';
import crypto from 'crypto';
Expand Down Expand Up @@ -67,6 +67,7 @@ const Webpack: Packager<Options> = class Webpack implements PackagerInstance {
private passthroughCache: Map<string, Stats> = new Map();
private publicAssetURL: string | undefined;
private extraThreadLoaderOptions: object | false | undefined;
private extraBabelLoaderOptions: BabelLoaderOptions | undefined;

constructor(
pathToVanillaApp: string,
Expand All @@ -83,6 +84,7 @@ const Webpack: Packager<Options> = class Webpack implements PackagerInstance {
this.extraConfig = options?.webpackConfig;
this.publicAssetURL = options?.publicAssetURL;
this.extraThreadLoaderOptions = options?.threadLoaderOptions;
this.extraBabelLoaderOptions = options?.babelLoaderOptions;
warmUp(this.extraThreadLoaderOptions);
}

Expand Down Expand Up @@ -162,7 +164,12 @@ const Webpack: Packager<Options> = class Webpack implements PackagerInstance {
test: require(join(this.pathToVanillaApp, babel.fileFilter)),
use: nonNullArray([
maybeThreadLoader(babel.isParallelSafe, this.extraThreadLoaderOptions),
babelLoaderOptions(babel.majorVersion, variant, join(this.pathToVanillaApp, babel.filename)),
babelLoaderOptions(
babel.majorVersion,
variant,
join(this.pathToVanillaApp, babel.filename),
this.extraBabelLoaderOptions
),
]),
},
{
Expand Down Expand Up @@ -545,15 +552,25 @@ function nonNullArray<T>(array: T[]): NonNullable<T>[] {
return array.filter(Boolean) as NonNullable<T>[];
}

function babelLoaderOptions(majorVersion: 6 | 7, variant: Variant, appBabelConfigPath: string) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
let options = Object.assign({}, applyVariantToBabelConfig(variant, require(appBabelConfigPath)), {
// all stage3 packagers should keep persistent caches under
// `join(tmpdir(), 'embroider')`. An important reason is that
// they should have exactly the same lifetime as some of
// embroider's own caches.
cacheDirectory: join(tmpdir(), 'embroider', 'webpack-babel-loader'),
});
function babelLoaderOptions(
majorVersion: 6 | 7,
variant: Variant,
appBabelConfigPath: string,
extraOptions: BabelLoaderOptions | undefined
) {
let options = Object.assign(
{},
// eslint-disable-next-line @typescript-eslint/no-require-imports
applyVariantToBabelConfig(variant, require(appBabelConfigPath)),
{
// Unless explicitly overridden, all stage3 packagers should keep
// persistent caches under `join(tmpdir(), 'embroider')`. An
// important reason is that they should have exactly the same
// lifetime as some of embroider's own caches.
cacheDirectory: join(tmpdir(), 'embroider', 'webpack-babel-loader'),
},
extraOptions
);
if (majorVersion === 7) {
if (options.plugins) {
options.plugins = options.plugins.slice();
Expand Down
14 changes: 13 additions & 1 deletion packages/webpack/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { Configuration } from 'webpack';

export default interface Options {
// [babel-loader](https://webpack.js.org/loaders/babel-loader/#options) specific options.
// This does not include the babel configuration, which is pulled from the app, only the
// additional options that `babel-loader` supports.
export interface BabelLoaderOptions {
cacheDirectory?: boolean | string;
cacheIdentifier?: string;
cacheCompression?: boolean;
customize?: string;
}

export interface Options {
webpackConfig: Configuration;

// the base public URL for your assets in production. Use this when you want
Expand All @@ -18,4 +28,6 @@ export default interface Options {
// Note that setting `JOBS=1` in the environment will also disable
// `thread-loader`.
threadLoaderOptions?: object | false;

babelLoaderOptions?: BabelLoaderOptions;
}

0 comments on commit ea6f2dd

Please sign in to comment.