Skip to content
Draft
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
35 changes: 27 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,21 @@ function ncc (
}));
}

const tsCompilerOptions = {
module: 'esnext',
target: 'esnext',
...fullTsconfig.compilerOptions,
allowSyntheticDefaultImports: true,
noEmit: false,
outDir: '//'
};

if (esm && emitsNonEsmModule(tsCompilerOptions.module)) {
// ESM builds need TypeScript to preserve imports so webpack can resolve
// dependencies with "import" conditions instead of "require" conditions.
tsCompilerOptions.module = 'esnext';
}

const compiler = webpack({
entry,
cache: cache === false ? undefined : {
Expand Down Expand Up @@ -365,14 +380,7 @@ function ncc (
options: {
transpileOnly,
compiler: eval('__dirname + "/typescript.js"'),
compilerOptions: {
module: 'esnext',
target: 'esnext',
...fullTsconfig.compilerOptions,
allowSyntheticDefaultImports: true,
noEmit: false,
outDir: '//'
}
compilerOptions: tsCompilerOptions
}
}]
},
Expand Down Expand Up @@ -635,6 +643,17 @@ function ncc (
}
}

function emitsNonEsmModule(moduleKind) {
if (typeof moduleKind === 'string') {
return ['commonjs', 'amd', 'umd', 'system'].includes(moduleKind.toLowerCase());
}

// Values match TypeScript's ModuleKind enum:
// CommonJS=1, AMD=2, UMD=3, System=4.
// https://github.com/microsoft/TypeScript/blob/v5.2.2/src/compiler/types.ts#L7255-L7260
return moduleKind >= 1 && moduleKind <= 4;
}

// this could be rewritten with actual FS apis / globs, but this is simpler
function getFlatFiles(mfsData, output, getAssetMeta, tsconfig, curBase = "") {
for (const path of Object.keys(mfsData)) {
Expand Down
3 changes: 3 additions & 0 deletions test/unit/ts-esm-import-condition/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { value } from 'esm-only-condition-package';

console.log(value);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/unit/ts-esm-import-condition/opt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esm": true
}
13 changes: 13 additions & 0 deletions test/unit/ts-esm-import-condition/output-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = new URL('.', import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/) ? 1 : 0, -1) + "/";
/******/
/************************************************************************/
var __webpack_exports__ = {};

;// CONCATENATED MODULE: ./test/unit/ts-esm-import-condition/node_modules/esm-only-condition-package/index.js
const value = 'from-esm-only-package';

;// CONCATENATED MODULE: ./test/unit/ts-esm-import-condition/input.ts

console.log(value);
13 changes: 13 additions & 0 deletions test/unit/ts-esm-import-condition/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = new URL('.', import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/) ? 1 : 0, -1) + "/";
/******/
/************************************************************************/
var __webpack_exports__ = {};

;// CONCATENATED MODULE: ./test/unit/ts-esm-import-condition/node_modules/esm-only-condition-package/index.js
const value = 'from-esm-only-package';

;// CONCATENATED MODULE: ./test/unit/ts-esm-import-condition/input.ts

console.log(value);
7 changes: 7 additions & 0 deletions test/unit/ts-esm-import-condition/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super strange behavior since you have module=commonjs in tsconfig.json here but type=module in package.json

Does this code even compile and run using tsc + node?

I dont understand why ncc should support this 🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No you’re so right, the fixture makes the case look a bit weird.

I was trying to test ncc’s ESM output path instaed of saying that this setup should be run directly with tsc + Node. Lemme clean it up by moving the import-only package into node_modules and making esm: true explicit in the fixture options so the test is clearer.

"moduleResolution": "node",
"target": "es2020"
}
}