Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add type declaration paths to exports field #341

Merged
merged 3 commits into from Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions demo/ts-module-node16/lib/stringify.ts
@@ -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();
31 changes: 31 additions & 0 deletions demo/ts-module-node16/package.json
@@ -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}'"
}
}
16 changes: 16 additions & 0 deletions demo/ts-module-node16/test/loaders/all.js
@@ -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);
}
20 changes: 20 additions & 0 deletions demo/ts-module-node16/test/loaders/coffee.js
@@ -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);
}
37 changes: 37 additions & 0 deletions demo/ts-module-node16/test/loaders/legacy/all.js
@@ -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);
}
50 changes: 50 additions & 0 deletions demo/ts-module-node16/test/loaders/legacy/coffee.js
@@ -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);
}
26 changes: 26 additions & 0 deletions demo/ts-module-node16/test/samples.coffee
@@ -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
7 changes: 7 additions & 0 deletions demo/ts-module-node16/tsconfig.json
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"module": "Node16",
"strict": true,
}
}
6 changes: 6 additions & 0 deletions packages/csv-generate/dist/cjs/stream.d.ts
@@ -0,0 +1,6 @@

import { Options } from './index';

declare function generate(options?: Options): ReadableStream<Buffer>;
// export default generate;
export { generate, Options };
6 changes: 6 additions & 0 deletions packages/csv-generate/dist/esm/stream.d.ts
@@ -0,0 +1,6 @@

import { Options } from './index';

declare function generate(options?: Options): ReadableStream<Buffer>;
// export default generate;
export { generate, Options };
6 changes: 6 additions & 0 deletions packages/csv-generate/lib/stream.d.ts
@@ -0,0 +1,6 @@

import { Options } from './index';

declare function generate(options?: Options): ReadableStream<Buffer>;
// export default generate;
export { generate, Options };
19 changes: 14 additions & 5 deletions packages/csv-generate/package.json
Expand Up @@ -30,18 +30,27 @@
"exports": {
".": {
"import": "./lib/index.js",
"require": "./dist/cjs/index.cjs"
"require": "./dist/cjs/index.cjs",
"types": "./lib/index.d.ts"
},
"./sync": {
"import": "./lib/sync.js",
"require": "./dist/cjs/sync.cjs"
"require": "./dist/cjs/sync.cjs",
"types": "./lib/sync.d.ts"
},
"./stream": {
"import": "./lib/stream.js",
"require": "./dist/cjs/stream.cjs"
"require": "./dist/cjs/stream.cjs",
"types": "./lib/stream.d.ts"
},
"./browser/esm": "./dist/esm/index.js",
"./browser/esm/sync": "./dist/esm/sync.js"
"./browser/esm": {
"types": "./lib/index.d.ts",
"default": "./dist/esm/index.js"
},
"./browser/esm/sync": {
"types": "./lib/sync.d.ts",
"default": "./dist/esm/sync.js"
}
},
"files": [
"dist",
Expand Down
16 changes: 12 additions & 4 deletions packages/csv-parse/package.json
Expand Up @@ -32,14 +32,22 @@
"exports": {
".": {
"import": "./lib/index.js",
"require": "./dist/cjs/index.cjs"
"require": "./dist/cjs/index.cjs",
"types": "./lib/index.d.ts"
},
"./sync": {
"import": "./lib/sync.js",
"require": "./dist/cjs/sync.cjs"
"require": "./dist/cjs/sync.cjs",
"types": "./lib/sync.d.ts"
},
"./browser/esm": "./dist/esm/index.js",
"./browser/esm/sync": "./dist/esm/sync.js"
"./browser/esm": {
"types": "./lib/index.d.ts",
"default": "./dist/esm/index.js"
},
"./browser/esm/sync": {
"types": "./lib/sync.d.ts",
"default": "./dist/esm/sync.js"
}
},
"devDependencies": {
"@rollup/plugin-eslint": "^8.0.2",
Expand Down
16 changes: 12 additions & 4 deletions packages/csv-stringify/package.json
Expand Up @@ -30,14 +30,22 @@
"exports": {
".": {
"import": "./lib/index.js",
"require": "./dist/cjs/index.cjs"
"require": "./dist/cjs/index.cjs",
"types": "./lib/index.d.ts"
},
"./sync": {
"import": "./lib/sync.js",
"require": "./dist/cjs/sync.cjs"
"require": "./dist/cjs/sync.cjs",
"types": "./lib/sync.d.ts"
},
"./browser/esm": "./dist/esm/index.js",
"./browser/esm/sync": "./dist/esm/sync.js"
"./browser/esm": {
"types": "./lib/index.d.ts",
"default": "./dist/esm/index.js"
},
"./browser/esm/sync": {
"types": "./lib/sync.d.ts",
"default": "./dist/esm/sync.js"
}
},
"files": [
"dist",
Expand Down
16 changes: 12 additions & 4 deletions packages/csv/package.json
Expand Up @@ -48,14 +48,22 @@
"exports": {
".": {
"import": "./lib/index.js",
"require": "./dist/cjs/index.cjs"
"require": "./dist/cjs/index.cjs",
"types": "./lib/index.d.ts"
},
"./sync": {
"import": "./lib/sync.js",
"require": "./dist/cjs/sync.cjs"
"require": "./dist/cjs/sync.cjs",
"types": "./lib/sync.d.ts"
},
"./browser/esm": "./dist/esm/index.js",
"./browser/esm/sync": "./dist/esm/sync.js"
"./browser/esm": {
"types": "./lib/index.d.ts",
"default": "./dist/esm/index.js"
},
"./browser/esm/sync": {
"types": "./lib/sync.d.ts",
"default": "./dist/esm/sync.js"
}
},
"homepage": "https://csv.js.org/",
"files": [
Expand Down
16 changes: 12 additions & 4 deletions packages/stream-transform/package.json
Expand Up @@ -30,14 +30,22 @@
"exports": {
".": {
"import": "./lib/index.js",
"require": "./dist/cjs/index.cjs"
"require": "./dist/cjs/index.cjs",
"types": "./lib/index.d.ts"
},
"./sync": {
"import": "./lib/sync.js",
"require": "./dist/cjs/sync.cjs"
"require": "./dist/cjs/sync.cjs",
"types": "./lib/sync.d.ts"
},
"./browser/esm": "./dist/esm/index.js",
"./browser/esm/sync": "./dist/esm/sync.js"
"./browser/esm": {
"types": "./lib/index.d.ts",
"default": "./dist/esm/index.js"
},
"./browser/esm/sync": {
"types": "./lib/sync.d.ts",
"default": "./dist/esm/sync.js"
}
},
"files": [
"dist",
Expand Down