Skip to content

Commit 1d4ab5a

Browse files
committed
Switch to .mjs for ES module output for Node.js compat
This also reverts commit 60cf26c.
1 parent 6868e4a commit 1d4ab5a

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"name": "node-fetch",
33
"version": "2.1.2",
44
"description": "A light-weight module that brings window.fetch to node.js",
5-
"main": "lib/index.js",
5+
"main": "lib/index",
66
"browser": "./browser.js",
7-
"module": "lib/index.es.js",
7+
"module": "lib/index.mjs",
88
"files": [
99
"lib/index.js",
10+
"lib/index.mjs",
1011
"lib/index.es.js",
1112
"browser.js"
1213
],
@@ -47,6 +48,7 @@
4748
"codecov": "^3.0.0",
4849
"cross-env": "^5.1.3",
4950
"form-data": "^2.3.1",
51+
"is-builtin-module": "^1.0.0",
5052
"mocha": "^5.0.0",
5153
"nyc": "11.9.0",
5254
"parted": "^0.1.1",

rollup.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import isBuiltin from 'is-builtin-module';
12
import babel from 'rollup-plugin-babel';
23
import tweakDefault from './build/rollup-plugin';
34

@@ -7,7 +8,8 @@ export default {
78
input: 'src/index.js',
89
output: [
910
{ file: 'lib/index.js', format: 'cjs', exports: 'named' },
10-
{ file: 'lib/index.es.js', format: 'es', exports: 'named' }
11+
{ file: 'lib/index.es.js', format: 'es', exports: 'named', intro: 'process.emitWarning("The .es.js file is deprecated. Use .mjs instead.");' },
12+
{ file: 'lib/index.mjs', format: 'es', exports: 'named' },
1113
],
1214
plugins: [
1315
babel({
@@ -16,6 +18,9 @@ export default {
1618
tweakDefault()
1719
],
1820
external: function (id) {
21+
if (isBuiltin(id)) {
22+
return true;
23+
}
1924
id = id.split('/').slice(0, id[0] === '@' ? 2 : 1).join('/');
2025
return !!require('./package.json').dependencies[id];
2126
}

src/body.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
* Body interface provides common methods for Request and Response
66
*/
77

8+
import Stream, { PassThrough } from 'stream';
89
import Blob, { BUFFER } from './blob.js';
910
import FetchError from './fetch-error.js';
1011

11-
const Stream = require('stream');
12-
const { PassThrough } = require('stream');
13-
1412
let convert;
1513
try { convert = require('encoding').convert; } catch(e) {}
1614

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
* All spec algorithm step numbers are based on https://fetch.spec.whatwg.org/commit-snapshots/ae716822cb3a61843226cd090eefc6589446c1d2/.
88
*/
99

10+
import { resolve as resolve_url } from 'url';
11+
import http from 'http';
12+
import https from 'https';
13+
import zlib from 'zlib';
14+
import { PassThrough } from 'stream';
15+
1016
import Body, { writeToStream, getTotalBytes } from './body';
1117
import Response from './response';
1218
import Headers, { createHeadersLenient } from './headers';
1319
import Request, { getNodeRequestOptions } from './request';
1420
import FetchError from './fetch-error';
1521

16-
const http = require('http');
17-
const https = require('https');
18-
const { PassThrough } = require('stream');
19-
const { resolve: resolve_url } = require('url');
20-
const zlib = require('zlib');
21-
2222
/**
2323
* Fetch function
2424
*

src/request.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
* All spec algorithm step numbers are based on https://fetch.spec.whatwg.org/commit-snapshots/ae716822cb3a61843226cd090eefc6589446c1d2/.
88
*/
99

10+
import { format as format_url, parse as parse_url } from 'url';
1011
import Headers, { exportNodeCompatibleHeaders } from './headers.js';
1112
import Body, { clone, extractContentType, getTotalBytes } from './body';
1213

13-
const { format: format_url, parse: parse_url } = require('url');
14-
1514
const INTERNALS = Symbol('Request internals');
1615

1716
/**

src/response.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
* Response class provides content decoding
66
*/
77

8+
import { STATUS_CODES } from 'http';
89
import Headers from './headers.js';
910
import Body, { clone } from './body';
1011

11-
const { STATUS_CODES } = require('http');
12-
1312
const INTERNALS = Symbol('Response internals');
1413

1514
/**

0 commit comments

Comments
 (0)