Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,23 @@ function createConfig(options, entry, format, writeMeta) {
const absMain = resolve(options.cwd, getMain({ options, entry, format }));
const outputDir = dirname(absMain);
const outputEntryFileName = basename(absMain);
let ts;
let tsconfigOptions = {};
if (useTypescript) {
const tsconfigPath = resolve(
options.tsconfig || resolve(options.cwd, 'tsconfig.json'),
);
ts = require(resolveFrom.silent(options.cwd, 'typescript') || 'typescript');
if (fs.existsSync(tsconfigPath)) {
const tsconfigJSON = ts.readConfigFile(tsconfigPath, ts.sys.readFile)
.config;
tsconfigOptions = ts.parseJsonConfigFileContent(
tsconfigJSON,
ts.sys,
'./',
).options;
}
}

let config = {
/** @type {import('rollup').InputOptions} */
Expand Down Expand Up @@ -480,10 +497,7 @@ function createConfig(options, entry, format, writeMeta) {
},
(useTypescript || emitDeclaration) &&
typescript({
typescript: require(resolveFrom.silent(
options.cwd,
'typescript',
) || 'typescript'),
typescript: ts,
cacheRoot: `./node_modules/.cache/.rts2_cache_${format}`,
useTsconfigDeclarationDir: true,
tsconfigDefaults: {
Expand Down Expand Up @@ -624,7 +638,7 @@ function createConfig(options, entry, format, writeMeta) {
globals,
strict: options.strict === true,
freeze: false,
esModule: false,
esModule: useTypescript ? !!tsconfigOptions.esModuleInterop : false,
sourcemap: options.sourcemap,
get banner() {
return shebang[options.name];
Expand Down
56 changes: 56 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2782,3 +2782,59 @@ exports[`fixtures build ts-module with microbundle 7`] = `
//# sourceMappingURL=ts-module.umd.js.map
"
`;

exports[`fixtures build ts-module-interop with microbundle 1`] = `
"Used script: microbundle

Directory tree:

ts-module-interop
dist
index.d.ts
ts-module.esm.js
ts-module.esm.js.map
ts-module.js
ts-module.js.map
ts-module.umd.js
ts-module.umd.js.map
node_modules
package.json
src
index.ts
tsconfig.json


Build \\"tsModule\\" to dist:
122 B: ts-module.js.gz
91 B: ts-module.js.br
87 B: ts-module.esm.js.gz
73 B: ts-module.esm.js.br
207 B: ts-module.umd.js.gz
156 B: ts-module.umd.js.br"
`;

exports[`fixtures build ts-module-interop with microbundle 2`] = `7`;

exports[`fixtures build ts-module-interop with microbundle 3`] = `
"export default function foo(): string;
export declare function hello(): string;
"
`;

exports[`fixtures build ts-module-interop with microbundle 4`] = `
"function r(){return\\"bar\\"}function t(){return\\"hello\\"}export default r;export{t as hello};
//# sourceMappingURL=ts-module.esm.js.map
"
`;

exports[`fixtures build ts-module-interop with microbundle 5`] = `
"Object.defineProperty(exports,\\"__esModule\\",{value:!0}),exports.default=function(){return\\"bar\\"},exports.hello=function(){return\\"hello\\"};
//# sourceMappingURL=ts-module.js.map
"
`;

exports[`fixtures build ts-module-interop with microbundle 6`] = `
"!function(e,o){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?o(exports):\\"function\\"==typeof define&&define.amd?define([\\"exports\\"],o):o((e||self).tsModule={})}(this,function(e){e.default=function(){return\\"bar\\"},e.hello=function(){return\\"hello\\"},Object.defineProperty(e,\\"__esModule\\",{value:!0})});
//# sourceMappingURL=ts-module.umd.js.map
"
`;
3 changes: 3 additions & 0 deletions test/fixtures/ts-module-interop/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ts-module"
}
5 changes: 5 additions & 0 deletions test/fixtures/ts-module-interop/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function foo() {
return 'bar';
}

export function hello() { return 'hello' }
6 changes: 6 additions & 0 deletions test/fixtures/ts-module-interop/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"module": "CommonJS",
"esModuleInterop": true
}
}