Skip to content

Commit

Permalink
feat(babel): softErrors feature flag and docs for globalCache and hap…
Browse files Browse the repository at this point in the history
…pyDOM
  • Loading branch information
Anber committed Sep 1, 2023
1 parent 7df5a65 commit df96d44
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 20 deletions.
16 changes: 16 additions & 0 deletions docs/FEATURE_FLAGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Feature flags are used to enable or disable specific features provided. The `fea
- `["glob1", "glob2"]`: Enables the feature for files matching any of the specified glob patterns.
- `["glob1", "!glob2"]`: Enables the feature for files matching `glob1` but excludes files that match `glob2`.


# `dangerousCodeRemover` Feature

The `dangerousCodeRemover` is a flag that is enabled by default. It is designed to enhance the static evaluation of values that are interpolated in styles and to optimize the processing of styled-wrapped components during the build stage. This optimization is crucial for maintaining a stable and fast build process. It is important to note that the `dangerousCodeRemover` does not impact the runtime code; it solely focuses on the code used during the build.
Expand Down Expand Up @@ -45,3 +46,18 @@ Suppose you have a file named `specialComponent.js` that contains code that shou
```

You are instructing Linaria to exclude the `specialComponent.js` file from the removal process. As a result, any code within this file that would have been removed by the `dangerousCodeRemover` will be retained in the build output.


# `globalCache` Feature

The `globalCache` is enabled by default. Linaria uses two levels of caching to improve the performance of the build process. The first level is used to cache transformation and evaluation results for each `transform` call, usually a single call of Webpack's loader or Vite's transform hook. The second level is used to cache the results of the entire build process. The `globalCache` feature controls the second level of caching. Turning off this feature will result in a slower build process but decreased memory usage.


# `happyDOM` Feature

The `happyDOM` is enabled by default. This feature enables usage of https://github.com/capricorn86/happy-dom to emulate a browser environment during the build process. Typically, the `dangerousCodeRemover` feature should remove all browser-related code. However, some libraries may still contain browser-specific code that cannot be statically evaluated. In such cases, the `happyDOM` feature can be used to emulate a browser environment during the build process. This allows Linaria to evaluate the code without encountering errors caused by missing browser APIs.


# `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.
40 changes: 22 additions & 18 deletions packages/babel/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,19 @@ export function transformSync(

return result;
} catch (err) {
if (process.env.NODE_ENV === 'test') {
throw err;
if (
isFeatureEnabled(pluginOptions.features, 'softErrors', options.filename)
) {
// eslint-disable-next-line no-console
console.error(`Error during transform of ${entrypoint.name}:`, err);

return {
code: originalCode,
sourceMap: options.inputSourceMap,
};
}

// eslint-disable-next-line no-console
console.error(`Error during transform of ${entrypoint.name}:`, err);

return {
code: originalCode,
sourceMap: options.inputSourceMap,
};
throw err;
}
}

Expand Down Expand Up @@ -155,16 +157,18 @@ export default async function transform(

return result;
} catch (err) {
if (process.env.NODE_ENV === 'test') {
throw err;
if (
isFeatureEnabled(pluginOptions.features, 'softErrors', options.filename)
) {
// eslint-disable-next-line no-console
console.error(`Error during transform of ${entrypoint.name}:`, err);

return {
code: originalCode,
sourceMap: options.inputSourceMap,
};
}

// eslint-disable-next-line no-console
console.error(`Error during transform of ${entrypoint.name}:`, err);

return {
code: originalCode,
sourceMap: options.inputSourceMap,
};
throw err;
}
}
9 changes: 7 additions & 2 deletions packages/babel/src/transform/helpers/loadLinariaOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ const searchPlaces = [

const explorerSync = cosmiconfigSync('linaria', { searchPlaces });

const cache = new WeakMap<Partial<PluginOptions>, StrictOptions>();
type PartialOptions = Partial<Omit<PluginOptions, 'features'>> & {
features?: Partial<FeatureFlags>;
};

const cache = new WeakMap<Partial<PartialOptions>, StrictOptions>();
const defaultOverrides = {};
const nodeModulesRegExp = /[\\/]node_modules[\\/]/;

export default function loadLinariaOptions(
overrides: Partial<PluginOptions> = defaultOverrides
overrides: PartialOptions = defaultOverrides
): StrictOptions {
if (cache.has(overrides)) {
return cache.get(overrides)!;
Expand All @@ -48,6 +52,7 @@ export default function loadLinariaOptions(
dangerousCodeRemover: true,
globalCache: true,
happyDOM: true,
softErrors: false,
};

const options: StrictOptions = {
Expand Down
3 changes: 3 additions & 0 deletions packages/testkit/src/babel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ const getLinariaConfig = (
action: 'ignore',
},
],
features: {
softErrors: false,
},
stage,
...linariaConfig,
});
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 @@ -45,6 +45,7 @@ const options: StrictOptions = {
dangerousCodeRemover: true,
globalCache: true,
happyDOM: true,
softErrors: false,
},
highPriorityPlugins: [],
};
Expand Down
6 changes: 6 additions & 0 deletions packages/utils/src/debug/perfMetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ export const createPerfMeter = (
)
);
}

timings.clear();
processedDependencies.clear();
queueActions.clear();

console.log(process.memoryUsage());
},
};
};
1 change: 1 addition & 0 deletions packages/utils/src/options/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type AllFeatureFlags = {
dangerousCodeRemover: FeatureFlag;
globalCache: FeatureFlag;
happyDOM: FeatureFlag;
softErrors: FeatureFlag;
};

export type FeatureFlags<
Expand Down

0 comments on commit df96d44

Please sign in to comment.