Skip to content

Commit 93f5d7e

Browse files
committed
fix: update import extensions in compiled source
1 parent cf6caa4 commit 93f5d7e

7 files changed

Lines changed: 81 additions & 14 deletions

File tree

.npm/compile.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Compile source for NPM
3+
*/
4+
5+
import swc from '@swc/core'
6+
import { mkdirSync, rmSync, writeFileSync } from 'node:fs'
7+
import { glob } from 'node:fs/promises'
8+
import path, { dirname } from 'node:path'
9+
import { fileURLToPath } from 'node:url'
10+
import tsconfig from './tsconfig.npm.json' with { type: 'json' }
11+
import { updateImports } from './updateImports.ts'
12+
13+
const __dirname = dirname(fileURLToPath(import.meta.url))
14+
15+
const outDir = path.join(__dirname, '..', 'dist')
16+
17+
rmSync(outDir, { recursive: true, force: true })
18+
19+
for await (const file of glob(
20+
`./.npm/{${tsconfig.include.join(',')}}/**/*.ts`,
21+
)) {
22+
if (file.endsWith('.spec.ts')) continue
23+
let compiled = (
24+
await swc.transformFile(file, {
25+
jsc: {
26+
parser: {
27+
syntax: 'typescript',
28+
},
29+
target: 'es2024',
30+
},
31+
module: {
32+
type: 'es6',
33+
},
34+
})
35+
).code
36+
37+
compiled = updateImports(compiled)
38+
39+
const targetFile = path.join(outDir, file.replace(/\.ts$/, '.js'))
40+
41+
mkdirSync(dirname(targetFile), { recursive: true })
42+
43+
writeFileSync(targetFile, compiled, 'utf8')
44+
45+
console.log(file)
46+
}

.npm/tsconfig.npm.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"emitDeclarationOnly": true,
5+
"declaration": true,
6+
"noEmit": false
7+
},
8+
"include": [
9+
"../generator",
10+
"../lwm2m",
11+
"../markdown",
12+
"../models",
13+
"../senml",
14+
"../api"
15+
]
16+
}

.npm/updateImports.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import assert from 'node:assert'
2+
import { describe, it } from 'node:test'
3+
import { updateImports } from './updateImports.ts'
4+
5+
void describe('updateImports', () => {
6+
void it('replaces .ts with .js in relative imports', () => {
7+
const input = `import { foo } from './bar.ts';`
8+
const expected = `import { foo } from './bar.js';`
9+
assert.equal(updateImports(input), expected)
10+
})
11+
})

.npm/updateImports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const updateImports = (source: string): string =>
2+
source.replace(/from ['"](.+?)\.ts['"]/g, "from '$1.js'")

compile.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
},
3232
"scripts": {
3333
"prepare": "husky && case \"$npm_command\" in install|ci) check-node-version --package ;; esac",
34-
"test": "npx globstar -- node --experimental-transform-types --no-warnings --test --test-reporter spec \"!(node_modules)/**/*.spec.ts\"",
35-
"prepublishOnly": "./compile.sh"
34+
"test": "node --no-warnings --experimental-transform-types --test \"!(node_modules)/**/*.spec.ts\"",
35+
"test:watch": "node --no-warnings --experimental-transform-types --test --watch \"!(node_modules)/**/*.spec.ts\"",
36+
"prepublishOnly": "node --experimental-strip-types ./.npm/compile.ts"
3637
},
3738
"devDependencies": {
3839
"@bifravst/eslint-config-typescript": "7.0.34",

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"verbatimModuleSyntax": true,
1919
"skipLibCheck": true,
2020
"allowImportingTsExtensions": true,
21+
"rootDir": ".",
2122
"paths": {
2223
"@hello.nrfcloud.com/proto-map/generator": ["./generator/index.d.ts"],
2324
"@hello.nrfcloud.com/proto-map/lwm2m": ["./lwm2m/index.d.ts"],
@@ -26,5 +27,6 @@
2627
"@hello.nrfcloud.com/proto-map/models": ["./models/index.d.ts"],
2728
"@hello.nrfcloud.com/proto-map/senml": ["./senml/index.d.ts"]
2829
}
29-
}
30+
},
31+
"include": ["./**/*.ts", "./.npm/*.ts"]
3032
}

0 commit comments

Comments
 (0)