-
Notifications
You must be signed in to change notification settings - Fork 181
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
Add build script #274
Add build script #274
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copied from '.gitignore', please keep it in sync. | ||
node_modules | ||
coverage | ||
lib | ||
dist |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -478,4 +478,21 @@ overrides: | |
no-console: off | ||
- files: '**/__*__/**' | ||
rules: | ||
node/no-unpublished-import: off | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed to add this since there were linting issues with the existing test files There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is something we had added in the other repos as an override for test files. And it makes sense to turn it off in that context but not for other files. |
||
import/no-extraneous-dependencies: [error, { devDependencies: true }] | ||
- files: 'resources/**' | ||
parserOptions: | ||
sourceType: script | ||
rules: | ||
node/no-unpublished-import: off | ||
node/no-unpublished-require: off | ||
node/no-missing-require: off | ||
node/no-sync: off | ||
node/no-unsupported-features/node-builtins: off | ||
node/global-require: off | ||
import/no-dynamic-require: off | ||
import/no-extraneous-dependencies: [error, { devDependencies: true }] | ||
import/no-nodejs-modules: off | ||
import/no-commonjs: off | ||
no-await-in-loop: off | ||
no-console: off |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
|
||
node_modules | ||
coverage | ||
lib | ||
dist |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copied from '.gitignore', please keep it in sync. | ||
node_modules | ||
coverage | ||
lib | ||
dist |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,7 @@ | |
"type": "git", | ||
"url": "http://github.com/graphql/graphql-relay-js.git" | ||
}, | ||
"main": "lib/index.js", | ||
"directories": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"lib": "./lib" | ||
}, | ||
"files": [ | ||
"lib", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"main": "index.js", | ||
"scripts": { | ||
"prepublish": "./resources/prepublish.sh", | ||
"prettier": "prettier --write --list-different .", | ||
|
@@ -29,8 +21,7 @@ | |
"testonly:cover": "nyc npm run testonly", | ||
"lint": "eslint .", | ||
"check": "flow check", | ||
"build": "rm -rf lib/* && babel src --ignore __tests__ --out-dir lib && npm run build:flow", | ||
"build:flow": "find ./src -name '*.js' -not -path '*/__tests__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done" | ||
"build": "node resources/build.js" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^14.0.0 || ^15.0.0" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// @noflow | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const assert = require('assert'); | ||
|
||
const babel = require('@babel/core'); | ||
|
||
const { rmdirRecursive, readdirRecursive } = require('./utils'); | ||
|
||
if (require.main === module) { | ||
rmdirRecursive('./dist'); | ||
fs.mkdirSync('./dist'); | ||
|
||
const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ }); | ||
for (const filepath of srcFiles) { | ||
const srcPath = path.join('./src', filepath); | ||
const destPath = path.join('./dist', filepath); | ||
|
||
fs.mkdirSync(path.dirname(destPath), { recursive: true }); | ||
if (filepath.endsWith('.js')) { | ||
fs.copyFileSync(srcPath, destPath + '.flow'); | ||
|
||
const cjs = babelBuild(srcPath, { envName: 'cjs' }); | ||
fs.writeFileSync(destPath, cjs); | ||
} else if (filepath.endsWith('d.ts')) { | ||
fs.copyFileSync(srcPath, destPath); | ||
} | ||
} | ||
|
||
fs.copyFileSync('./LICENSE', './dist/LICENSE'); | ||
fs.copyFileSync('./README.md', './dist/README.md'); | ||
|
||
// Should be done as the last step so only valid packages can be published | ||
const packageJSON = buildPackageJSON(); | ||
fs.writeFileSync('./dist/package.json', JSON.stringify(packageJSON, null, 2)); | ||
|
||
showStats(); | ||
} | ||
|
||
function babelBuild(srcPath, options) { | ||
return babel.transformFileSync(srcPath, options).code + '\n'; | ||
} | ||
|
||
function buildPackageJSON() { | ||
const packageJSON = require('../package.json'); | ||
delete packageJSON.private; | ||
delete packageJSON.scripts; | ||
delete packageJSON.devDependencies; | ||
|
||
const { version } = packageJSON; | ||
const versionMatch = /^\d+\.\d+\.\d+-?(.*)?$/.exec(version); | ||
if (!versionMatch) { | ||
throw new Error('Version does not match semver spec: ' + version); | ||
} | ||
|
||
const [, preReleaseTag] = versionMatch; | ||
|
||
if (preReleaseTag != null) { | ||
const [tag] = preReleaseTag.split('.'); | ||
assert(['alpha', 'beta', 'rc'].includes(tag), `"${tag}" tag is supported.`); | ||
|
||
assert(!packageJSON.publishConfig, 'Can not override "publishConfig".'); | ||
packageJSON.publishConfig = { tag: tag || 'latest' }; | ||
} | ||
|
||
return packageJSON; | ||
} | ||
|
||
function showStats() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you sync up both this and |
||
const fileTypes = {}; | ||
let totalSize = 0; | ||
|
||
for (const filepath of readdirRecursive('./dist')) { | ||
const name = filepath.split(path.sep).pop(); | ||
const [base, ...splitExt] = name.split('.'); | ||
const ext = splitExt.join('.'); | ||
|
||
const filetype = ext ? '*.' + ext : base; | ||
fileTypes[filetype] = fileTypes[filetype] || { filepaths: [], size: 0 }; | ||
|
||
const { size } = fs.lstatSync(path.join('./dist', filepath)); | ||
totalSize += size; | ||
fileTypes[filetype].size += size; | ||
fileTypes[filetype].filepaths.push(filepath); | ||
} | ||
|
||
let stats = []; | ||
for (const [filetype, typeStats] of Object.entries(fileTypes)) { | ||
const numFiles = typeStats.filepaths.length; | ||
|
||
if (numFiles > 1) { | ||
stats.push([filetype + ' x' + numFiles, typeStats.size]); | ||
} else { | ||
stats.push([typeStats.filepaths[0], typeStats.size]); | ||
} | ||
} | ||
stats.sort((a, b) => b[1] - a[1]); | ||
stats = stats.map(([type, size]) => [type, (size / 1024).toFixed(2) + ' KB']); | ||
|
||
const typeMaxLength = Math.max(...stats.map((x) => x[0].length)); | ||
const sizeMaxLength = Math.max(...stats.map((x) => x[1].length)); | ||
for (const [type, size] of stats) { | ||
console.log( | ||
type.padStart(typeMaxLength) + ' | ' + size.padStart(sizeMaxLength), | ||
); | ||
} | ||
|
||
console.log('-'.repeat(typeMaxLength + 3 + sizeMaxLength)); | ||
const totalMB = (totalSize / 1024 / 1024).toFixed(2) + ' MB'; | ||
console.log( | ||
'Total'.padStart(typeMaxLength) + ' | ' + totalMB.padStart(sizeMaxLength), | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// @noflow | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
function rmdirRecursive(dirPath) { | ||
if (fs.existsSync(dirPath)) { | ||
for (const dirent of fs.readdirSync(dirPath, { withFileTypes: true })) { | ||
const fullPath = path.join(dirPath, dirent.name); | ||
if (dirent.isDirectory()) { | ||
rmdirRecursive(fullPath); | ||
} else { | ||
fs.unlinkSync(fullPath); | ||
} | ||
} | ||
fs.rmdirSync(dirPath); | ||
} | ||
} | ||
|
||
function readdirRecursive(dirPath, opts = {}) { | ||
const { ignoreDir } = opts; | ||
const result = []; | ||
for (const dirent of fs.readdirSync(dirPath, { withFileTypes: true })) { | ||
const name = dirent.name; | ||
if (!dirent.isDirectory()) { | ||
result.push(dirent.name); | ||
continue; | ||
} | ||
|
||
if (ignoreDir && ignoreDir.test(name)) { | ||
continue; | ||
} | ||
const list = readdirRecursive(path.join(dirPath, name), opts).map((f) => | ||
path.join(name, f), | ||
); | ||
result.push(...list); | ||
} | ||
return result; | ||
} | ||
|
||
module.exports = { | ||
rmdirRecursive, | ||
readdirRecursive, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This plugin doesn't exist. Since the babel config wasn't used by the build script, it wasn't erroring out.