Skip to content

Commit 9bf8846

Browse files
committed
fix: 多客户端场景下,cjs文件语法错误
1 parent 288803c commit 9bf8846

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/lib/rebuild-dist.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const rebuildDist = async (
2323
);
2424
backupCJS = backupCJS.replace(
2525
/(__export\(.+?_exports, {)/,
26-
`$1\n${classNames.map((className) => `${className}: () => ${className},`)}`,
26+
`$1\n${classNames.map((className) => `${className}: () => ${className},`).join('\n')}`,
2727
);
2828
await writeFile(path.join(distDir, 'index.js'), backupCJS);
2929
}

test/lib/rebuild-dist.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,73 @@ test('内容写入文件', async () => {
9191
export { foo };"
9292
`);
9393
});
94+
95+
test('内容写入文件', async () => {
96+
await rebuildDist(root, jsContent, dtsContent, ['foo', 'bar', 'baz']);
97+
await expect(readFile(path.join(distDir, 'index.js'), 'utf8')).resolves
98+
.toMatchInlineSnapshot(`
99+
""use strict";
100+
var __defProp = Object.defineProperty;
101+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
102+
var __getOwnPropNames = Object.getOwnPropertyNames;
103+
var __hasOwnProp = Object.prototype.hasOwnProperty;
104+
var __export = (target, all) => {
105+
for (var name in all)
106+
__defProp(target, name, { get: all[name], enumerable: true });
107+
};
108+
var __copyProps = (to, from, except, desc) => {
109+
if (from && typeof from === "object" || typeof from === "function") {
110+
for (let key of __getOwnPropNames(from))
111+
if (!__hasOwnProp.call(to, key) && key !== except)
112+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
113+
}
114+
return to;
115+
};
116+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
117+
118+
// test/fixtures/index.ts
119+
var fixtures_exports = {};
120+
__export(fixtures_exports, {
121+
foo: () => foo,
122+
bar: () => bar,
123+
baz: () => baz,
124+
aaaaa: () => aaaaa
125+
});
126+
module.exports = __toCommonJS(fixtures_exports);
127+
var aaaaa = { bbbbb: "ccccc" };
128+
// Annotate the CommonJS export names for ESM import in node:
129+
var foo = { bar: "baz" }
130+
0 && (module.exports = {
131+
foo,bar,baz,
132+
aaaaa
133+
});
134+
//# sourceMappingURL=index.js.map
135+
"
136+
`);
137+
138+
await expect(readFile(path.join(distDir, 'index.d.ts'), 'utf8')).resolves
139+
.toMatchInlineSnapshot(`
140+
"declare const aaaaa: {
141+
bbbbb: string;
142+
};
143+
144+
export { aaaaa };
145+
146+
147+
declare const foo = { bar: "baz" };
148+
export { foo, bar, baz };"
149+
`);
150+
151+
await expect(readFile(path.join(distDir, 'esm', 'index.js'), 'utf8')).resolves
152+
.toMatchInlineSnapshot(`
153+
"// test/fixtures/index.ts
154+
var aaaaa = { bbbbb: "ccccc" };
155+
export {
156+
aaaaa
157+
};
158+
//# sourceMappingURL=index.js.map
159+
160+
var foo = { bar: "baz" }
161+
export { foo, bar, baz };"
162+
`);
163+
});

0 commit comments

Comments
 (0)