Skip to content

Commit

Permalink
Feat: Add config option to enable treeshake compensation (#24)
Browse files Browse the repository at this point in the history
* Feat: Add config option to enable treeshake compensation

* Prettier
  • Loading branch information
nmn authored Dec 1, 2023
1 parent 08f90b8 commit 32d1d4e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ function transform(source, opts = options) {
flow: 'all',
},
babelrc: false,
plugins: [jsx, [stylexPlugin, opts]],
plugins: [
jsx,
[
stylexPlugin,
{
treeshakeCompensation: true,
...opts,
},
],
],
});
}

Expand Down
5 changes: 4 additions & 1 deletion packages/babel-plugin/src/utils/evaluate-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,10 @@ function _evaluate(path: NodePath<>, state: State): any {
? evaluateThemeRef(value, importedName, state)
: evaluateImportedFile(value, importedName, state);
if (state.confident) {
if (!state.addedImports.has(importPath.node.source.value)) {
if (
!state.addedImports.has(importPath.node.source.value) &&
state.traversalState.treeshakeCompensation
) {
importPath.insertBefore(
t.importDeclaration([], importPath.node.source),
);
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-plugin/src/utils/state-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ModuleResolution =
type StyleXOptions = {
...RuntimeOptions,
importSources: Array<string>,
treeshakeCompensation?: boolean,
genConditionalClasses: boolean,
unstable_moduleResolution: void | ModuleResolution,
...
Expand Down Expand Up @@ -90,6 +91,7 @@ export default class StateManager {
(options: $FlowFixMe).styleResolution ?? 'application-order',
unstable_moduleResolution:
(options: $FlowFixMe).unstable_moduleResolution ?? undefined,
treeshakeCompensation: !!(options: $FlowFixMe).treeshakeCompensation,
};
this._state.opts = (opts: $FlowFixMe);
return this._state.opts;
Expand Down Expand Up @@ -135,6 +137,10 @@ export default class StateManager {
return this.options.definedStylexCSSVariables;
}

get treeshakeCompensation(): boolean {
return !!this.options.treeshakeCompensation;
}

get fileNameForHashing(): null | string {
const filename = this.filename;
const themeFileExtension =
Expand Down
1 change: 1 addition & 0 deletions packages/open-props/__tests__/open-props-compile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function transform(source, opts = {}) {
stylexPlugin,
{
...opts,
treeshakeCompensation: true,
unstable_moduleResolution: {
type: 'commonJS',
rootDir: path.join(__dirname, '../../'),
Expand Down

0 comments on commit 32d1d4e

Please sign in to comment.