Skip to content

Commit

Permalink
fix(transform): undefined exports (regression #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Dec 7, 2023
1 parent 5c6ce3f commit 740e336
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 13 deletions.
8 changes: 8 additions & 0 deletions .changeset/nine-melons-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@wyw-in-js/transform': patch
'@wyw-in-js/vite': patch
'@wyw-in-js/cli': patch
'wyw-in-js': patch
---

Fix regression from #19 that kills some exports.
17 changes: 17 additions & 0 deletions packages/transform/src/__tests__/__snapshots__/shaker.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`shaker should carefully shake used exports 1`] = `
""use strict";
var _activeClass;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.activeClass = void 0;
_activeClass = "s1gxjcbn";
const _exp = /*#__PURE__*/() => _activeClass;
exports.__wywPreval = {
_exp: _exp
};
exports.activeClass = _activeClass;"
`;
55 changes: 55 additions & 0 deletions packages/transform/src/__tests__/shaker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { join } from 'path';

import * as babel from '@babel/core';
import dedent from 'dedent';

import { shaker } from '../shaker';

const run = (only: string[]) => (code: TemplateStringsArray) => {
const filename = join(__dirname, 'source.ts');
const formattedCode = dedent(code);
const parsed = babel.parseSync(formattedCode, {
filename,
});

return shaker(
{
filename,
plugins: [],
},
parsed!,
formattedCode,
{
features: {
dangerousCodeRemover: true,
globalCache: false,
happyDOM: false,
softErrors: false,
useBabelConfigs: false,
},
highPriorityPlugins: [],
onlyExports: only,
},
babel
)[1];
};

describe('shaker', () => {
it('should carefully shake used exports', () => {
const code = run(['__wywPreval'])`
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.activeClass = void 0;
exports.activeClass = "s1gxjcbn";
const _exp = /*#__PURE__*/() => exports.activeClass;
exports.__wywPreval = {
_exp: _exp,
};
`;

expect(code).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion packages/transform/src/shaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const safeResolve = (id: string, paths: (string | null)[]): string | null => {
}
};

const shaker: Evaluator = (
export const shaker: Evaluator = (
evalConfig,
ast,
code,
Expand Down
1 change: 1 addition & 0 deletions packages/transform/src/transform/generators/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export const prepareCode = (

log('[preeval] metadata %O', transformMetadata);
log('[evaluator:start] using %s', evaluator.name);
log.extend('source')('%s', preevalStageResult.code!);

const evaluatorConfig: EvaluatorConfig = {
onlyExports: only,
Expand Down
34 changes: 22 additions & 12 deletions packages/transform/src/utils/collectExportsAndImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,19 @@ function addExport(path: NodePath, exported: string, state: ILocalState): void {
}
}

const saveRef = (
state: ILocalState,
exportName: string,
memberExpression: NodePath<MemberExpression>
) => {
// Save all export.____ usages for later
if (!state.exportRefs.has(exportName)) {
state.exportRefs.set(exportName, []);
}

state.exportRefs.get(exportName)!.push(memberExpression);
};

function collectFromExports(
path: NodePath<Identifier>,
state: ILocalState
Expand All @@ -810,15 +823,6 @@ function collectFromExports(

const exportName = property.node.name;

const saveRef = () => {
// Save all export.____ usages for later
if (!state.exportRefs.has(exportName)) {
state.exportRefs.set(exportName, []);
}

state.exportRefs.get(exportName)!.push(memberExpression);
};

const assignmentExpression = memberExpression.parentPath;

if (
Expand All @@ -827,7 +831,7 @@ function collectFromExports(
})
) {
// If it's not `exports.prop = …`. Just save it.
saveRef();
saveRef(state, exportName, memberExpression);
return;
}

Expand All @@ -844,7 +848,7 @@ function collectFromExports(
return;
}

saveRef();
saveRef(state, exportName, memberExpression);
// eslint-disable-next-line no-param-reassign
state.exports[property.node.name] = right;

Expand Down Expand Up @@ -1179,7 +1183,9 @@ function collectFromAssignmentExpression(

let exported: IReexport['exported'] | undefined;

if (left.isMemberExpression() && isExports(left.get('object'))) {
const isExportRef =
left.isMemberExpression() && isExports(left.get('object'));
if (isExportRef) {
const property = left.get('property');
if (!left.node.computed && property.isIdentifier()) {
exported = property.node.name;
Expand Down Expand Up @@ -1213,6 +1219,10 @@ function collectFromAssignmentExpression(
state.exports[exported] = right;
}

if (isExportRef) {
saveRef(state, exported, left);
}

path.skip();
return;
}
Expand Down

0 comments on commit 740e336

Please sign in to comment.