Skip to content

Commit 4b0283d

Browse files
andipaetzoldwdavidw
andauthoredJul 10, 2022
feat: ts module Node16 and type declaration to exports field (#341)
* fix: add type declaration paths to exports field * fix: add types for csv-generate/stream * test(csv-ts-demo-node16): demo assiated with PR #341 Co-authored-by: David Worms <david@adaltas.com>
1 parent b44de05 commit 4b0283d

File tree

16 files changed

+299
-21
lines changed

16 files changed

+299
-21
lines changed
 
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
import assert from 'assert'
3+
import { stringify, Stringifier } from 'csv-stringify';
4+
5+
let output: string = '';
6+
// Create the parser
7+
const stringifier: Stringifier = stringify({
8+
delimiter: ':',
9+
encoding: 'utf8'
10+
});
11+
// Use the readable stream api to consume records
12+
stringifier.on('readable', function(){
13+
let record; while ((record = stringifier.read()) !== null) {
14+
output += record
15+
}
16+
});
17+
// Catch any error
18+
stringifier.on('error', function(err){
19+
console.error(err.message)
20+
});
21+
// Test that the parsed records matched what's expected
22+
stringifier.on('end', function(){
23+
assert.deepStrictEqual(
24+
output,
25+
'a:b:c\n1:2:3\n'
26+
)
27+
});
28+
// Write data to the stream
29+
stringifier.write(["a", "b", "c"]);
30+
stringifier.write([1, 2, 3]);
31+
// Close the readable stream
32+
stringifier.end();

‎demo/ts-module-node16/package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "csv-ts-demo-node16",
3+
"version": "0.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"type": "module",
7+
"private": true,
8+
"devDependencies": {
9+
"@types/node": "^18.0.3",
10+
"coffeescript": "^2.7.0",
11+
"mocha": "^10.0.0",
12+
"should": "^13.2.3",
13+
"ts-node": "^10.8.2",
14+
"typescript": "^4.7.4"
15+
},
16+
"mocha": {
17+
"inline-diffs": true,
18+
"loader": "./test/loaders/all.js",
19+
"recursive": true,
20+
"reporter": "spec",
21+
"require": [
22+
"should"
23+
],
24+
"throw-deprecation": true,
25+
"timeout": 40000
26+
},
27+
"scripts": {
28+
"test": "mocha 'test/**/*.coffee'",
29+
"test:legacy": "mocha --loader=./test/loaders/legacy/all.js 'test/**/*.{coffee,ts}'"
30+
}
31+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
import * as coffee from './coffee.js'
3+
import * as ts from 'ts-node/esm'
4+
5+
const coffeeRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;
6+
const tsRegex = /\.ts$/;
7+
8+
export function load(url, context, next) {
9+
if (coffeeRegex.test(url)) {
10+
return coffee.load.apply(this, arguments)
11+
}
12+
if (tsRegex.test(url)) {
13+
return ts.load.apply(this, arguments)
14+
}
15+
return next(url, context, next);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import CoffeeScript from 'coffeescript';
2+
3+
// See https://github.com/nodejs/node/issues/36396
4+
const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;
5+
6+
export async function load(url, context, next) {
7+
if (extensionsRegex.test(url)) {
8+
const format = 'module';
9+
const { source: rawSource } = await next(url, { format });
10+
const source = CoffeeScript.compile(rawSource.toString(), {
11+
bare: true,
12+
inlineMap: true,
13+
filename: url,
14+
header: false,
15+
sourceMap: false,
16+
});
17+
return {format, source};
18+
}
19+
return next(url, context);
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
import * as coffee from './coffee.js'
3+
import * as ts from 'ts-node/esm'
4+
5+
const coffeeRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;
6+
const tsRegex = /\.ts$/;
7+
8+
export function resolve(specifier, context, defaultResolve) {
9+
if (coffeeRegex.test(specifier)) {
10+
return coffee.resolve.apply(this, arguments)
11+
}
12+
if (tsRegex.test(specifier)) {
13+
return ts.resolve.apply(this, arguments)
14+
}
15+
return ts.resolve.apply(this, arguments);
16+
}
17+
18+
export function getFormat(url, context, defaultGetFormat) {
19+
if (coffeeRegex.test(url)) {
20+
return coffee.getFormat.apply(this, arguments)
21+
}
22+
if (tsRegex.test(url)) {
23+
return ts.getFormat.apply(this, arguments)
24+
}
25+
return ts.getFormat.apply(this, arguments);
26+
}
27+
28+
export function transformSource(source, context, defaultTransformSource) {
29+
const { url } = context;
30+
if (coffeeRegex.test(url)) {
31+
return coffee.transformSource.apply(this, arguments)
32+
}
33+
if (tsRegex.test(url)) {
34+
return ts.transformSource.apply(this, arguments)
35+
}
36+
return ts.transformSource.apply(this, arguments);
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// coffeescript-loader.mjs
2+
import { URL, pathToFileURL } from 'url';
3+
import CoffeeScript from 'coffeescript';
4+
import { cwd } from 'process';
5+
6+
const baseURL = pathToFileURL(`${cwd()}/`).href;
7+
8+
// CoffeeScript files end in .coffee, .litcoffee or .coffee.md.
9+
const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;
10+
11+
export function resolve(specifier, context, defaultResolve) {
12+
const { parentURL = baseURL } = context;
13+
// Node.js normally errors on unknown file extensions, so return a URL for
14+
// specifiers ending in the CoffeeScript file extensions.
15+
if (extensionsRegex.test(specifier)) {
16+
return {
17+
url: new URL(specifier, parentURL).href,
18+
stop: true
19+
};
20+
}
21+
// Let Node.js handle all other specifiers.
22+
return defaultResolve(specifier, context, defaultResolve);
23+
}
24+
25+
export function getFormat(url, context, defaultGetFormat) {
26+
// Now that we patched resolve to let CoffeeScript URLs through, we need to
27+
// tell Node.js what format such URLs should be interpreted as. For the
28+
// purposes of this loader, all CoffeeScript URLs are ES modules.
29+
if (extensionsRegex.test(url)) {
30+
return {
31+
format: 'module',
32+
stop: true
33+
};
34+
}
35+
// Let Node.js handle all other URLs.
36+
return defaultGetFormat(url, context, defaultGetFormat);
37+
}
38+
39+
export function transformSource(source, context, defaultTransformSource) {
40+
const { url, format } = context;
41+
42+
if (extensionsRegex.test(url)) {
43+
return {
44+
source: CoffeeScript.compile(String(source), { bare: true })
45+
};
46+
}
47+
48+
// Let Node.js handle all other sources.
49+
return defaultTransformSource(source, context, defaultTransformSource);
50+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
import fs from 'fs'
3+
import path from 'path'
4+
import { exec } from 'child_process'
5+
6+
import { fileURLToPath } from 'url'
7+
__dirname = path.dirname fileURLToPath import.meta.url
8+
dir = path.resolve __dirname, '../lib'
9+
samples = fs.readdirSync dir
10+
11+
describe 'Samples', ->
12+
13+
samples
14+
.filter (sample) ->
15+
return false unless /\.(js|ts)?$/.test sample
16+
true
17+
.map (sample) ->
18+
it "Sample #{sample}", (callback) ->
19+
ext = /\.(\w+)?$/.exec(sample)[0]
20+
bin = switch ext
21+
when '.js'
22+
'node'
23+
when '.ts'
24+
'node --loader ts-node/esm'
25+
exec "#{bin} #{path.resolve dir, sample}", (err, stdout, stderr) ->
26+
callback err

‎demo/ts-module-node16/tsconfig.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"esModuleInterop": true,
4+
"module": "Node16",
5+
"strict": true,
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
import { Options } from './index';
3+
4+
declare function generate(options?: Options): ReadableStream<Buffer>;
5+
// export default generate;
6+
export { generate, Options };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
import { Options } from './index';
3+
4+
declare function generate(options?: Options): ReadableStream<Buffer>;
5+
// export default generate;
6+
export { generate, Options };

‎packages/csv-generate/lib/stream.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
import { Options } from './index';
3+
4+
declare function generate(options?: Options): ReadableStream<Buffer>;
5+
// export default generate;
6+
export { generate, Options };

‎packages/csv-generate/package.json

+14-5
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,27 @@
3030
"exports": {
3131
".": {
3232
"import": "./lib/index.js",
33-
"require": "./dist/cjs/index.cjs"
33+
"require": "./dist/cjs/index.cjs",
34+
"types": "./lib/index.d.ts"
3435
},
3536
"./sync": {
3637
"import": "./lib/sync.js",
37-
"require": "./dist/cjs/sync.cjs"
38+
"require": "./dist/cjs/sync.cjs",
39+
"types": "./lib/sync.d.ts"
3840
},
3941
"./stream": {
4042
"import": "./lib/stream.js",
41-
"require": "./dist/cjs/stream.cjs"
43+
"require": "./dist/cjs/stream.cjs",
44+
"types": "./lib/stream.d.ts"
4245
},
43-
"./browser/esm": "./dist/esm/index.js",
44-
"./browser/esm/sync": "./dist/esm/sync.js"
46+
"./browser/esm": {
47+
"types": "./lib/index.d.ts",
48+
"default": "./dist/esm/index.js"
49+
},
50+
"./browser/esm/sync": {
51+
"types": "./lib/sync.d.ts",
52+
"default": "./dist/esm/sync.js"
53+
}
4554
},
4655
"files": [
4756
"dist",

‎packages/csv-parse/package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,22 @@
3232
"exports": {
3333
".": {
3434
"import": "./lib/index.js",
35-
"require": "./dist/cjs/index.cjs"
35+
"require": "./dist/cjs/index.cjs",
36+
"types": "./lib/index.d.ts"
3637
},
3738
"./sync": {
3839
"import": "./lib/sync.js",
39-
"require": "./dist/cjs/sync.cjs"
40+
"require": "./dist/cjs/sync.cjs",
41+
"types": "./lib/sync.d.ts"
4042
},
41-
"./browser/esm": "./dist/esm/index.js",
42-
"./browser/esm/sync": "./dist/esm/sync.js"
43+
"./browser/esm": {
44+
"types": "./lib/index.d.ts",
45+
"default": "./dist/esm/index.js"
46+
},
47+
"./browser/esm/sync": {
48+
"types": "./lib/sync.d.ts",
49+
"default": "./dist/esm/sync.js"
50+
}
4351
},
4452
"devDependencies": {
4553
"@rollup/plugin-eslint": "^8.0.2",

‎packages/csv-stringify/package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,22 @@
3030
"exports": {
3131
".": {
3232
"import": "./lib/index.js",
33-
"require": "./dist/cjs/index.cjs"
33+
"require": "./dist/cjs/index.cjs",
34+
"types": "./lib/index.d.ts"
3435
},
3536
"./sync": {
3637
"import": "./lib/sync.js",
37-
"require": "./dist/cjs/sync.cjs"
38+
"require": "./dist/cjs/sync.cjs",
39+
"types": "./lib/sync.d.ts"
3840
},
39-
"./browser/esm": "./dist/esm/index.js",
40-
"./browser/esm/sync": "./dist/esm/sync.js"
41+
"./browser/esm": {
42+
"types": "./lib/index.d.ts",
43+
"default": "./dist/esm/index.js"
44+
},
45+
"./browser/esm/sync": {
46+
"types": "./lib/sync.d.ts",
47+
"default": "./dist/esm/sync.js"
48+
}
4149
},
4250
"files": [
4351
"dist",

‎packages/csv/package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,22 @@
4848
"exports": {
4949
".": {
5050
"import": "./lib/index.js",
51-
"require": "./dist/cjs/index.cjs"
51+
"require": "./dist/cjs/index.cjs",
52+
"types": "./lib/index.d.ts"
5253
},
5354
"./sync": {
5455
"import": "./lib/sync.js",
55-
"require": "./dist/cjs/sync.cjs"
56+
"require": "./dist/cjs/sync.cjs",
57+
"types": "./lib/sync.d.ts"
5658
},
57-
"./browser/esm": "./dist/esm/index.js",
58-
"./browser/esm/sync": "./dist/esm/sync.js"
59+
"./browser/esm": {
60+
"types": "./lib/index.d.ts",
61+
"default": "./dist/esm/index.js"
62+
},
63+
"./browser/esm/sync": {
64+
"types": "./lib/sync.d.ts",
65+
"default": "./dist/esm/sync.js"
66+
}
5967
},
6068
"homepage": "https://csv.js.org/",
6169
"files": [

‎packages/stream-transform/package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,22 @@
3030
"exports": {
3131
".": {
3232
"import": "./lib/index.js",
33-
"require": "./dist/cjs/index.cjs"
33+
"require": "./dist/cjs/index.cjs",
34+
"types": "./lib/index.d.ts"
3435
},
3536
"./sync": {
3637
"import": "./lib/sync.js",
37-
"require": "./dist/cjs/sync.cjs"
38+
"require": "./dist/cjs/sync.cjs",
39+
"types": "./lib/sync.d.ts"
3840
},
39-
"./browser/esm": "./dist/esm/index.js",
40-
"./browser/esm/sync": "./dist/esm/sync.js"
41+
"./browser/esm": {
42+
"types": "./lib/index.d.ts",
43+
"default": "./dist/esm/index.js"
44+
},
45+
"./browser/esm/sync": {
46+
"types": "./lib/sync.d.ts",
47+
"default": "./dist/esm/sync.js"
48+
}
4149
},
4250
"files": [
4351
"dist",

0 commit comments

Comments
 (0)
Please sign in to comment.