-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
b44de05
commit 4b0283d
Showing
16 changed files
with
299 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
import assert from 'assert' | ||
import { stringify, Stringifier } from 'csv-stringify'; | ||
|
||
let output: string = ''; | ||
// Create the parser | ||
const stringifier: Stringifier = stringify({ | ||
delimiter: ':', | ||
encoding: 'utf8' | ||
}); | ||
// Use the readable stream api to consume records | ||
stringifier.on('readable', function(){ | ||
let record; while ((record = stringifier.read()) !== null) { | ||
output += record | ||
} | ||
}); | ||
// Catch any error | ||
stringifier.on('error', function(err){ | ||
console.error(err.message) | ||
}); | ||
// Test that the parsed records matched what's expected | ||
stringifier.on('end', function(){ | ||
assert.deepStrictEqual( | ||
output, | ||
'a:b:c\n1:2:3\n' | ||
) | ||
}); | ||
// Write data to the stream | ||
stringifier.write(["a", "b", "c"]); | ||
stringifier.write([1, 2, 3]); | ||
// Close the readable stream | ||
stringifier.end(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "csv-ts-demo-node16", | ||
"version": "0.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"type": "module", | ||
"private": true, | ||
"devDependencies": { | ||
"@types/node": "^18.0.3", | ||
"coffeescript": "^2.7.0", | ||
"mocha": "^10.0.0", | ||
"should": "^13.2.3", | ||
"ts-node": "^10.8.2", | ||
"typescript": "^4.7.4" | ||
}, | ||
"mocha": { | ||
"inline-diffs": true, | ||
"loader": "./test/loaders/all.js", | ||
"recursive": true, | ||
"reporter": "spec", | ||
"require": [ | ||
"should" | ||
], | ||
"throw-deprecation": true, | ||
"timeout": 40000 | ||
}, | ||
"scripts": { | ||
"test": "mocha 'test/**/*.coffee'", | ||
"test:legacy": "mocha --loader=./test/loaders/legacy/all.js 'test/**/*.{coffee,ts}'" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
import * as coffee from './coffee.js' | ||
import * as ts from 'ts-node/esm' | ||
|
||
const coffeeRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/; | ||
const tsRegex = /\.ts$/; | ||
|
||
export function load(url, context, next) { | ||
if (coffeeRegex.test(url)) { | ||
return coffee.load.apply(this, arguments) | ||
} | ||
if (tsRegex.test(url)) { | ||
return ts.load.apply(this, arguments) | ||
} | ||
return next(url, context, next); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import CoffeeScript from 'coffeescript'; | ||
|
||
// See https://github.com/nodejs/node/issues/36396 | ||
const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/; | ||
|
||
export async function load(url, context, next) { | ||
if (extensionsRegex.test(url)) { | ||
const format = 'module'; | ||
const { source: rawSource } = await next(url, { format }); | ||
const source = CoffeeScript.compile(rawSource.toString(), { | ||
bare: true, | ||
inlineMap: true, | ||
filename: url, | ||
header: false, | ||
sourceMap: false, | ||
}); | ||
return {format, source}; | ||
} | ||
return next(url, context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
import * as coffee from './coffee.js' | ||
import * as ts from 'ts-node/esm' | ||
|
||
const coffeeRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/; | ||
const tsRegex = /\.ts$/; | ||
|
||
export function resolve(specifier, context, defaultResolve) { | ||
if (coffeeRegex.test(specifier)) { | ||
return coffee.resolve.apply(this, arguments) | ||
} | ||
if (tsRegex.test(specifier)) { | ||
return ts.resolve.apply(this, arguments) | ||
} | ||
return ts.resolve.apply(this, arguments); | ||
} | ||
|
||
export function getFormat(url, context, defaultGetFormat) { | ||
if (coffeeRegex.test(url)) { | ||
return coffee.getFormat.apply(this, arguments) | ||
} | ||
if (tsRegex.test(url)) { | ||
return ts.getFormat.apply(this, arguments) | ||
} | ||
return ts.getFormat.apply(this, arguments); | ||
} | ||
|
||
export function transformSource(source, context, defaultTransformSource) { | ||
const { url } = context; | ||
if (coffeeRegex.test(url)) { | ||
return coffee.transformSource.apply(this, arguments) | ||
} | ||
if (tsRegex.test(url)) { | ||
return ts.transformSource.apply(this, arguments) | ||
} | ||
return ts.transformSource.apply(this, arguments); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// coffeescript-loader.mjs | ||
import { URL, pathToFileURL } from 'url'; | ||
import CoffeeScript from 'coffeescript'; | ||
import { cwd } from 'process'; | ||
|
||
const baseURL = pathToFileURL(`${cwd()}/`).href; | ||
|
||
// CoffeeScript files end in .coffee, .litcoffee or .coffee.md. | ||
const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/; | ||
|
||
export function resolve(specifier, context, defaultResolve) { | ||
const { parentURL = baseURL } = context; | ||
// Node.js normally errors on unknown file extensions, so return a URL for | ||
// specifiers ending in the CoffeeScript file extensions. | ||
if (extensionsRegex.test(specifier)) { | ||
return { | ||
url: new URL(specifier, parentURL).href, | ||
stop: true | ||
}; | ||
} | ||
// Let Node.js handle all other specifiers. | ||
return defaultResolve(specifier, context, defaultResolve); | ||
} | ||
|
||
export function getFormat(url, context, defaultGetFormat) { | ||
// Now that we patched resolve to let CoffeeScript URLs through, we need to | ||
// tell Node.js what format such URLs should be interpreted as. For the | ||
// purposes of this loader, all CoffeeScript URLs are ES modules. | ||
if (extensionsRegex.test(url)) { | ||
return { | ||
format: 'module', | ||
stop: true | ||
}; | ||
} | ||
// Let Node.js handle all other URLs. | ||
return defaultGetFormat(url, context, defaultGetFormat); | ||
} | ||
|
||
export function transformSource(source, context, defaultTransformSource) { | ||
const { url, format } = context; | ||
|
||
if (extensionsRegex.test(url)) { | ||
return { | ||
source: CoffeeScript.compile(String(source), { bare: true }) | ||
}; | ||
} | ||
|
||
// Let Node.js handle all other sources. | ||
return defaultTransformSource(source, context, defaultTransformSource); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
import fs from 'fs' | ||
import path from 'path' | ||
import { exec } from 'child_process' | ||
|
||
import { fileURLToPath } from 'url' | ||
__dirname = path.dirname fileURLToPath import.meta.url | ||
dir = path.resolve __dirname, '../lib' | ||
samples = fs.readdirSync dir | ||
|
||
describe 'Samples', -> | ||
|
||
samples | ||
.filter (sample) -> | ||
return false unless /\.(js|ts)?$/.test sample | ||
true | ||
.map (sample) -> | ||
it "Sample #{sample}", (callback) -> | ||
ext = /\.(\w+)?$/.exec(sample)[0] | ||
bin = switch ext | ||
when '.js' | ||
'node' | ||
when '.ts' | ||
'node --loader ts-node/esm' | ||
exec "#{bin} #{path.resolve dir, sample}", (err, stdout, stderr) -> | ||
callback err |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"module": "Node16", | ||
"strict": true, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
import { Options } from './index'; | ||
|
||
declare function generate(options?: Options): ReadableStream<Buffer>; | ||
// export default generate; | ||
export { generate, Options }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
import { Options } from './index'; | ||
|
||
declare function generate(options?: Options): ReadableStream<Buffer>; | ||
// export default generate; | ||
export { generate, Options }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
import { Options } from './index'; | ||
|
||
declare function generate(options?: Options): ReadableStream<Buffer>; | ||
// export default generate; | ||
export { generate, Options }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters