Skip to content

Commit 16c168e

Browse files
committed
Try to avoid node resolution nonsense
1 parent a672eb8 commit 16c168e

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

config/fix-imports.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Adapted from,
2+
// typescript-esm-example
3+
// By Andrey Sakharov
4+
// Original: https://github.com/muturgan/typescript-esm-example/blob/main/buildtools/fix-imports.js
5+
// MIT License: https://github.com/muturgan/typescript-esm-example/blob/main/LICENSE
6+
7+
import { extname, join } from 'path';
8+
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from 'fs';
9+
10+
const START_PATH = join(process.cwd(), 'dist/esm');
11+
const IMPORT_REGEXP = /^((import|export) [^';]* from "(\.\/|(\.\.\/)+)[^";]*)"/g;
12+
const JUST_ADD_AN_EXTENSION = `$1.js"`;
13+
const ADD_INDEX_FILE = `$1/index.js"`;
14+
const JS_EXT = '.js';
15+
16+
/**
17+
* @param {string} rootPath
18+
*/
19+
function fixImportsAtFolder(rootPath) {
20+
const entries = readdirSync(rootPath);
21+
22+
entries.forEach((entry) => {
23+
const entryPath = join(rootPath, entry);
24+
if (entry.endsWith(JS_EXT)) {
25+
fixImportsAtFile(entryPath);
26+
}
27+
else {
28+
const extName = extname(entry);
29+
if (!extName) {
30+
const stat = statSync(entryPath);
31+
if (stat.isDirectory()) {
32+
fixImportsAtFolder(entryPath);
33+
}
34+
}
35+
}
36+
});
37+
}
38+
39+
/**
40+
*
41+
* @param {string} filePath
42+
*/
43+
function fixImportsAtFile(filePath) {
44+
const content = readFileSync(filePath).toString('utf8');
45+
const lines = content.split('\n');
46+
const fixedLines = lines.map((l) => {
47+
if (!l.match(IMPORT_REGEXP)) {
48+
return l;
49+
}
50+
51+
const [_, importPath] = l.split(`"`);
52+
const fullPath = join(filePath, '..', importPath);
53+
const exists = existsSync(fullPath);
54+
if (exists === false) {
55+
return l.replace(IMPORT_REGEXP, JUST_ADD_AN_EXTENSION);
56+
}
57+
58+
const stat = statSync(fullPath);
59+
const isDirectory = stat.isDirectory();
60+
if (isDirectory === true) {
61+
return l.replace(IMPORT_REGEXP, ADD_INDEX_FILE);
62+
}
63+
64+
return l;
65+
});
66+
const withFixedImports = fixedLines.join('\n');
67+
writeFileSync(filePath, withFixedImports);
68+
}
69+
70+
fixImportsAtFolder(START_PATH);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"build:main": "tsc --removeComments --module commonjs --outDir dist/commonjs",
2525
"build:declaration": "tsc --module commonjs --outDir dist/types --declaration --emitDeclarationOnly",
2626
"build:esm": "tsc --removeComments --outDir dist/esm",
27-
"postbuild": "node --experimental-json-modules config/copy",
27+
"postbuild": "node config/copy && node config/fix-imports",
2828
"lint": "eslint -c .eslintrc.cjs --ext .ts src",
2929
"doc": "npx rimraf doc && typedoc ./src --exclude \"**/_*/*.ts\" --exclude \"**/bindArrayEnumerable.ts\" --gitRevision master --out ./doc --excludePrivate --readme README.md --name \"LINQ To TypeScript\" --includeVersion && node config/editdocs",
3030
"release:npm": "npm publish ./dist",

0 commit comments

Comments
 (0)