From 465e1e810fc4ca7f46b68c024ba9c47a184a6935 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 16 Apr 2018 09:40:37 +0200 Subject: [PATCH 01/10] Remove explicit '.js' extension from all imports --- src/__tests__/starWarsConnectionTests.js | 2 +- src/__tests__/starWarsMutationTests.js | 2 +- src/__tests__/starWarsObjectIdentificationTests.js | 2 +- src/__tests__/starWarsSchema.js | 10 +++++----- src/connection/__tests__/connection.js | 4 ++-- src/connection/arrayconnection.js | 2 +- src/index.js | 14 +++++++------- src/node/node.js | 2 +- 8 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/__tests__/starWarsConnectionTests.js b/src/__tests__/starWarsConnectionTests.js index 6642d3c..327272e 100644 --- a/src/__tests__/starWarsConnectionTests.js +++ b/src/__tests__/starWarsConnectionTests.js @@ -9,7 +9,7 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; -import { StarWarsSchema } from './starWarsSchema.js'; +import { StarWarsSchema } from './starWarsSchema'; import { graphql } from 'graphql'; // 80+ char lines are useful in describe/it, so ignore in this file. diff --git a/src/__tests__/starWarsMutationTests.js b/src/__tests__/starWarsMutationTests.js index dcf9f74..66b435d 100644 --- a/src/__tests__/starWarsMutationTests.js +++ b/src/__tests__/starWarsMutationTests.js @@ -9,7 +9,7 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; -import { StarWarsSchema } from './starWarsSchema.js'; +import { StarWarsSchema } from './starWarsSchema'; import { graphql } from 'graphql'; // 80+ char lines are useful in describe/it, so ignore in this file. diff --git a/src/__tests__/starWarsObjectIdentificationTests.js b/src/__tests__/starWarsObjectIdentificationTests.js index 7fe42e7..eca659f 100644 --- a/src/__tests__/starWarsObjectIdentificationTests.js +++ b/src/__tests__/starWarsObjectIdentificationTests.js @@ -9,7 +9,7 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; -import { StarWarsSchema } from './starWarsSchema.js'; +import { StarWarsSchema } from './starWarsSchema'; import { graphql } from 'graphql'; // 80+ char lines are useful in describe/it, so ignore in this file. diff --git a/src/__tests__/starWarsSchema.js b/src/__tests__/starWarsSchema.js index 9f55601..ab69db9 100644 --- a/src/__tests__/starWarsSchema.js +++ b/src/__tests__/starWarsSchema.js @@ -19,20 +19,20 @@ import { nodeDefinitions, globalIdField, fromGlobalId -} from '../node/node.js'; +} from '../node/node'; import { connectionFromArray -} from '../connection/arrayconnection.js'; +} from '../connection/arrayconnection'; import { connectionArgs, connectionDefinitions -} from '../connection/connection.js'; +} from '../connection/connection'; import { mutationWithClientMutationId -} from '../mutation/mutation.js'; +} from '../mutation/mutation'; import { getFaction, @@ -40,7 +40,7 @@ import { getRebels, getEmpire, createShip, -} from './starWarsData.js'; +} from './starWarsData'; /** * This is a basic end-to-end test, designed to demonstrate the various diff --git a/src/connection/__tests__/connection.js b/src/connection/__tests__/connection.js index 76f01d8..dd21bf1 100644 --- a/src/connection/__tests__/connection.js +++ b/src/connection/__tests__/connection.js @@ -17,14 +17,14 @@ import { import { connectionFromArray, -} from '../arrayconnection.js'; +} from '../arrayconnection'; import { backwardConnectionArgs, connectionArgs, connectionDefinitions, forwardConnectionArgs, -} from '../connection.js'; +} from '../connection'; import { expect } from 'chai'; import { describe, it } from 'mocha'; diff --git a/src/connection/arrayconnection.js b/src/connection/arrayconnection.js index 2ec1056..ea6e3c8 100644 --- a/src/connection/arrayconnection.js +++ b/src/connection/arrayconnection.js @@ -16,7 +16,7 @@ import type { import { base64, unbase64, -} from '../utils/base64.js'; +} from '../utils/base64'; type ArraySliceMetaInfo = { sliceStart: number; diff --git a/src/index.js b/src/index.js index c2c449a..2d6c1f6 100644 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,7 @@ export type { ConnectionCursor, Edge, PageInfo, -} from './connection/connectiontypes.js'; +} from './connection/connectiontypes'; // Helpers for creating connection types in the schema export { @@ -22,7 +22,7 @@ export { connectionArgs, connectionDefinitions, forwardConnectionArgs, -} from './connection/connection.js'; +} from './connection/connection'; // Helpers for creating connections from arrays export { @@ -34,26 +34,26 @@ export { cursorToOffset, getOffsetWithDefault, offsetToCursor, -} from './connection/arrayconnection.js'; +} from './connection/arrayconnection'; // Helper for creating mutations with client mutation IDs export { mutationWithClientMutationId, -} from './mutation/mutation.js'; +} from './mutation/mutation'; // Helper for creating node definitions export { nodeDefinitions, -} from './node/node.js'; +} from './node/node'; // Helper for creating plural identifying root fields export { pluralIdentifyingRootField, -} from './node/plural.js'; +} from './node/plural'; // Utilities for creating global IDs in systems that don't have them. export { fromGlobalId, globalIdField, toGlobalId, -} from './node/node.js'; +} from './node/node'; diff --git a/src/node/node.js b/src/node/node.js index 3755b1b..4718eff 100644 --- a/src/node/node.js +++ b/src/node/node.js @@ -23,7 +23,7 @@ import type { import { base64, unbase64 -} from '../utils/base64.js'; +} from '../utils/base64'; type GraphQLNodeDefinitions = { nodeInterface: GraphQLInterfaceType, From f14c55ac0cc15ba447bd43242e49884c88b89560 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 16 Apr 2018 09:22:16 +0200 Subject: [PATCH 02/10] Break out current Babel invocation as build:cjs --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8c1ef4e..53c72c8 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "testonly": "babel-node ./node_modules/.bin/_mocha $npm_package_options_mocha", "lint": "eslint src", "check": "flow check", - "build": "rm -rf lib/* && babel src --ignore __tests__ --out-dir lib && npm run build:flow", + "build": "rm -rf lib/* && npm run build:cjs && npm run build:flow", + "build:cjs": "babel src --ignore __tests__ --out-dir lib", "build:flow": "find ./src -name '*.js' -not -path '*/__tests__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done", "watch": "babel-node scripts/watch.js", "cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha", From 1dfedb4cb439495a1d1349154fc8af49d00b5cd0 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 16 Apr 2018 09:23:15 +0200 Subject: [PATCH 03/10] Transform modules to CommonJS if BABEL_MODULES not set --- .babelrc | 2 +- resources/common-js-modules.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 resources/common-js-modules.js diff --git a/.babelrc b/.babelrc index a7f45b7..73274c2 100644 --- a/.babelrc +++ b/.babelrc @@ -19,7 +19,7 @@ "transform-es2015-parameters", ["transform-es2015-destructuring", {loose: true}], "transform-es2015-block-scoping", - "transform-es2015-modules-commonjs", + "./resources/common-js-modules", "transform-regenerator", ] } diff --git a/resources/common-js-modules.js b/resources/common-js-modules.js new file mode 100644 index 0000000..d9838b3 --- /dev/null +++ b/resources/common-js-modules.js @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +/** + * Determines whether to transform modules to commonjs based on an + * environment variable. Module import/export statements are not transformed + * if the `BABEL_MODULES` env variable is set. + */ +module.exports = process.env.BABEL_MODULES ? + () => ({}) : + require('babel-plugin-transform-es2015-modules-commonjs'); From 675edfc1def4a1300d12095a8236149f9c2a9ebd Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 16 Apr 2018 09:27:37 +0200 Subject: [PATCH 04/10] Create build:clean script in the style of graphql-js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This isn’t technically necessary for publishing ES modules, but I’ve gone ahead and aligned this with graphql-js anyway. --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 53c72c8..0cc6794 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "testonly": "babel-node ./node_modules/.bin/_mocha $npm_package_options_mocha", "lint": "eslint src", "check": "flow check", - "build": "rm -rf lib/* && npm run build:cjs && npm run build:flow", + "build": "npm run build:clean && npm run build:cjs && npm run build:flow", + "build:clean": "rm -rf ./lib && mkdir ./lib", "build:cjs": "babel src --ignore __tests__ --out-dir lib", "build:flow": "find ./src -name '*.js' -not -path '*/__tests__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done", "watch": "babel-node scripts/watch.js", From d19581f36a21d75666a53bc18211469eaabd847e Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 16 Apr 2018 09:30:06 +0200 Subject: [PATCH 05/10] Build .mjs files in lib/ --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 0cc6794..4bdcb33 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "url": "http://github.com/graphql/graphql-relay-js.git" }, "main": "lib/index.js", + "module": "lib/index.mjs", "directories": { "lib": "./lib" }, @@ -32,9 +33,10 @@ "testonly": "babel-node ./node_modules/.bin/_mocha $npm_package_options_mocha", "lint": "eslint src", "check": "flow check", - "build": "npm run build:clean && npm run build:cjs && npm run build:flow", + "build": "npm run build:clean && npm run build:cjs && npm run build:mjs && npm run build:flow", "build:clean": "rm -rf ./lib && mkdir ./lib", "build:cjs": "babel src --ignore __tests__ --out-dir lib", + "build:mjs": "BABEL_MODULES=1 babel src --optional runtime --ignore __tests__ --out-dir lib/module/ && for file in $(find lib/module -name '*.js'); do mv \"$file\" `echo \"$file\" | sed 's/lib\\/module/lib/g; s/.js$/.mjs/g'`; done && rm -rf lib/module", "build:flow": "find ./src -name '*.js' -not -path '*/__tests__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done", "watch": "babel-node scripts/watch.js", "cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha", From e00547e575347ad7513d55540986125418a82536 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 16 Apr 2018 09:34:37 +0200 Subject: [PATCH 06/10] Mark all modules as having no side effects for bundling --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 4bdcb33..2b3dc4a 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ }, "main": "lib/index.js", "module": "lib/index.mjs", + "sideEffects": false, "directories": { "lib": "./lib" }, From efdb7ddfc8c7f00ec1fb3709d4c25f91a1809514 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Thu, 19 Apr 2018 09:45:46 +0300 Subject: [PATCH 07/10] Import node buffer module explicitly --- src/utils/base64.js | 2 ++ src/utils/buffer.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/utils/buffer.js diff --git a/src/utils/base64.js b/src/utils/base64.js index 0213ec0..f93f8a8 100644 --- a/src/utils/base64.js +++ b/src/utils/base64.js @@ -7,6 +7,8 @@ * @flow */ +import { Buffer } from './buffer'; + export type Base64String = string; export function base64(i: string): Base64String { diff --git a/src/utils/buffer.js b/src/utils/buffer.js new file mode 100644 index 0000000..0c30f2c --- /dev/null +++ b/src/utils/buffer.js @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +/** + * This file exists to work around a number of issues with importing Buffer + * across Webpack, Node and Flow. + * + * Webpack (tested with 4.5.0) doesn't currently support use of Node globals, + * such as Buffer, in ES modules. + * See https://github.com/webpack/webpack/issues/7032 + * + * Hence we want to import it explicitly e.g. + * + * import { Buffer } from 'buffer'; + * + * But Node (tested with version 9.11.1 and --experimental-modules) only has + * Buffer as a property of the default export, not as a separate named export. + */ + +import NodeBuffer from 'buffer'; + +/** + * An alias for Node's Buffer class. + * + * Flow (tested with 0.69.0) knows about global.Buffer but not about + * require('buffer').Buffer. + * https://github.com/facebook/flow/issues/3723 + */ + +// $FlowFixMe because of the above. +export const Buffer: typeof global.Buffer = NodeBuffer.Buffer; From 608c2a1b999626a6b08169f4d8e59b9ce6d273c8 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Sat, 26 May 2018 20:01:41 +0100 Subject: [PATCH 08/10] Upgrade flow@0.73.0 --- .flowconfig | 2 +- package.json | 2 +- yarn.lock | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.flowconfig b/.flowconfig index da0ffce..971ec52 100644 --- a/.flowconfig +++ b/.flowconfig @@ -10,4 +10,4 @@ [options] [version] -^0.69.0 +^0.73.0 diff --git a/package.json b/package.json index 8c1ef4e..085ee1e 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ "eslint": "^4.17.0", "eslint-plugin-babel": "^4.1.2", "eslint-plugin-flowtype": "^2.44.0", - "flow-bin": "^0.69.0", + "flow-bin": "^0.73.0", "graphql": "^0.13.1", "isparta": "4.0.0", "mocha": "^5.0.1", diff --git a/yarn.lock b/yarn.lock index 9c9e37b..9eec94b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1384,9 +1384,9 @@ flat-cache@^1.2.1: graceful-fs "^4.1.2" write "^0.2.1" -flow-bin@^0.69.0: - version "0.69.0" - resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.69.0.tgz#053159a684a6051fcbf0b71a2eb19a9679082da6" +flow-bin@^0.73.0: + version "0.73.0" + resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.73.0.tgz#da1b90a02b0ef9c439f068c2fc14968db83be425" for-in@^1.0.1, for-in@^1.0.2: version "1.0.2" From d699ee41cf476ac35d2a0e11892d65376bc1e72c Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Sat, 26 May 2018 20:10:41 +0100 Subject: [PATCH 09/10] Simplify buffer.js after Flow bugfix --- src/utils/buffer.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/utils/buffer.js b/src/utils/buffer.js index 0c30f2c..34a2a73 100644 --- a/src/utils/buffer.js +++ b/src/utils/buffer.js @@ -8,8 +8,8 @@ */ /** - * This file exists to work around a number of issues with importing Buffer - * across Webpack, Node and Flow. + * This file exists to work around an issue with importing Buffer across + * Webpack and Node. * * Webpack (tested with 4.5.0) doesn't currently support use of Node globals, * such as Buffer, in ES modules. @@ -27,11 +27,6 @@ import NodeBuffer from 'buffer'; /** * An alias for Node's Buffer class. - * - * Flow (tested with 0.69.0) knows about global.Buffer but not about - * require('buffer').Buffer. - * https://github.com/facebook/flow/issues/3723 */ -// $FlowFixMe because of the above. -export const Buffer: typeof global.Buffer = NodeBuffer.Buffer; +export const Buffer = NodeBuffer.Buffer; From 93befafc061e8ba1a69ae2d9c42e736d35c650fa Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Sat, 26 May 2018 20:12:34 +0100 Subject: [PATCH 10/10] Remove .js extension from package.json "main" --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eba83f3..35dc5b9 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "type": "git", "url": "http://github.com/graphql/graphql-relay-js.git" }, - "main": "lib/index.js", + "main": "lib/index", "module": "lib/index.mjs", "sideEffects": false, "directories": {