Skip to content

Commit

Permalink
feat(babel): introduces useBabelConfigs feature flag (#1355)
Browse files Browse the repository at this point in the history
* feat(babel): introduces `babelrc` feature flag

* fix(babel): rename babelrc flag to useBabelConfigs
  • Loading branch information
Anber committed Sep 26, 2023
1 parent 70000ec commit 25ba134
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/short-cats-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@linaria/babel-preset': patch
'@linaria/testkit': patch
'@linaria/utils': patch
---

`useBabelConfigs` feature flag.
7 changes: 7 additions & 0 deletions docs/FEATURE_FLAGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ The `happyDOM` is enabled by default. This feature enables usage of https://gith
# `softErrors` Feature

The `softErrors` is disabled by default. It is designed to provide a more lenient evaluation of styles and values that are interpolated in styles. This flag is useful for debugging and prevents the build from failing even if some files cannot be processed with Linaria.


# 'useBabelConfigs' Feature

The `useBabelConfigs` feature is enabled by default. If it is enabled, Linaria will try to resolve the `.babelrc` file for each processed file. Otherwise, it will use the default Babel configuration from `babelOptions` in the configuration.

Please note that the default value of `useBabelConfigs` will be changed to `false` in the next major release.
13 changes: 12 additions & 1 deletion packages/babel/src/transform/Entrypoint.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
buildOptions,
getFileIdx,
getPluginKey,
isFeatureEnabled,
loadBabelOptions,
} from '@linaria/utils';

Expand Down Expand Up @@ -100,8 +101,18 @@ function buildConfigs(
commonOptions
);

const useBabelConfigs = isFeatureEnabled(
pluginOptions.features,
'useBabelConfigs',
name
);

if (!useBabelConfigs) {
rawConfig.configFile = false;
}

const parseConfig = loadBabelOptions(babel, name, {
babelrc: true,
babelrc: useBabelConfigs,
...rawConfig,
});

Expand Down
1 change: 1 addition & 0 deletions packages/babel/src/transform/helpers/loadLinariaOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default function loadLinariaOptions(
globalCache: true,
happyDOM: true,
softErrors: false,
useBabelConfigs: true,
};

const options: StrictOptions = {
Expand Down
27 changes: 24 additions & 3 deletions packages/testkit/src/babel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
import { linariaLogger } from '@linaria/logger';
import type {
Evaluator,
StrictOptions,
OnEvent,
OnActionStartArgs,
OnActionFinishArgs,
FeatureFlags,
} from '@linaria/utils';
import { EventEmitter } from '@linaria/utils';
import type { EntrypointEvent } from '@linaria/utils/types/EventEmitter';
Expand All @@ -31,9 +31,13 @@ expect.addSnapshotSerializer(serializer);

const dirName = __dirname;

type PartialOptions = Partial<Omit<PluginOptions, 'features'>> & {
features?: Partial<FeatureFlags>;
};

type Options = [
evaluator: Evaluator,
linariaConfig?: Partial<StrictOptions>,
linariaConfig?: PartialOptions,
extension?: 'js' | 'ts' | 'jsx' | 'tsx',
filename?: string,
babelConfig?: babel.TransformOptions,
Expand Down Expand Up @@ -63,7 +67,7 @@ const getPresets = (extension: 'js' | 'ts' | 'jsx' | 'tsx') => {

const getLinariaConfig = (
evaluator: Evaluator,
linariaConfig: Partial<StrictOptions>,
linariaConfig: PartialOptions,
presets: PluginItem[],
stage?: Stage
): PluginOptions =>
Expand Down Expand Up @@ -2825,6 +2829,23 @@ describe('strategy shaker', () => {
);
});

it('should fail because babelrc options is disabled', async () => {
const fn = () =>
transformFile(
resolve(__dirname, './__fixtures__/with-babelrc/index.js'),
[
evaluator,
{
features: {
useBabelConfigs: false,
},
},
]
);

expect(fn).rejects.toThrowError(`Cannot find module '_/re-exports'`);
});

it('respects module-resolver plugin and show waring', async () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {});

Expand Down
1 change: 1 addition & 0 deletions packages/testkit/src/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const options: StrictOptions = {
globalCache: true,
happyDOM: true,
softErrors: false,
useBabelConfigs: true,
},
highPriorityPlugins: [],
overrideContext: (context) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/options/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type AllFeatureFlags = {
globalCache: FeatureFlag;
happyDOM: FeatureFlag;
softErrors: FeatureFlag;
useBabelConfigs: FeatureFlag;
};

export type FeatureFlags<
Expand Down

0 comments on commit 25ba134

Please sign in to comment.