Skip to content

Commit

Permalink
Sync package building process with 'graphql-js' (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Mar 18, 2021
1 parent 11e6288 commit 87e62e7
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 68 deletions.
9 changes: 9 additions & 0 deletions .babelrc-npm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": ["@babel/plugin-transform-flow-strip-types"],
"presets": [
[
"@babel/preset-env",
{ "modules": "commonjs", "targets": { "node": "12" } }
]
]
}
28 changes: 1 addition & 27 deletions .babelrc.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
{
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }]],
"plugins": ["@babel/plugin-transform-flow-strip-types"],
"overrides": [
{
"exclude": ["**/__tests__/**/*"],
"presets": ["@babel/preset-env"],
"plugins": [
["@babel/plugin-transform-classes", { "loose": true }],
["@babel/plugin-transform-destructuring", { "loose": true }],
["@babel/plugin-transform-spread", { "loose": true }],
["@babel/plugin-transform-for-of", { "assumeArray": true }]
],
"env": {
"cjs": {
"presets": [["@babel/preset-env", { "modules": "commonjs" }]]
},
"mjs": {
"presets": [["@babel/preset-env", { "modules": false }]],
"plugins": [
[
"./resources/add-extension-to-import-paths",
{ "extension": "mjs" }
]
]
}
}
}
]
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
}
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copied from '.gitignore', please keep it in sync.
node_modules
coverage
dist
npmDist

# Ignore Flow typings for 3rd-party libraries
/flow-typed
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

node_modules
coverage
dist
npmDist
2 changes: 1 addition & 1 deletion .prettierignore
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
dist
npmDist
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
".eslintcache",
"node_modules",
"coverage",
"dist",
"npmDist",

// Excluded from spelling check
"flow-typed",
Expand Down
42 changes: 26 additions & 16 deletions resources/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @noflow

'use strict';

const fs = require('fs');
Expand All @@ -8,40 +6,49 @@ const assert = require('assert');

const babel = require('@babel/core');

const { rmdirRecursive, readdirRecursive, showStats } = require('./utils');
const { readdirRecursive, showDirStats } = require('./utils');

if (require.main === module) {
rmdirRecursive('./dist');
fs.mkdirSync('./dist');
fs.rmSync('./npmDist', { recursive: true, force: true });
fs.mkdirSync('./npmDist');

const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });
for (const filepath of srcFiles) {
const srcPath = path.join('./src', filepath);
const destPath = path.join('./dist', filepath);
const destPath = path.join('./npmDist', filepath);

fs.mkdirSync(path.dirname(destPath), { recursive: true });
if (filepath.endsWith('.js')) {
fs.copyFileSync(srcPath, destPath + '.flow');
const flowBody = '// @flow strict\n' + fs.readFileSync(srcPath, 'utf-8');
fs.writeFileSync(destPath + '.flow', flowBody);

const cjs = babelBuild(srcPath, { envName: 'cjs' });
fs.writeFileSync(destPath, cjs);
} else if (filepath.endsWith('d.ts')) {
} else if (filepath.endsWith('.d.ts')) {
fs.copyFileSync(srcPath, destPath);
}
}

fs.copyFileSync('./LICENSE', './dist/LICENSE');
fs.copyFileSync('./README.md', './dist/README.md');
fs.copyFileSync('./LICENSE', './npmDist/LICENSE');
fs.copyFileSync('./README.md', './npmDist/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));
fs.writeFileSync(
'./npmDist/package.json',
JSON.stringify(packageJSON, null, 2),
);

showStats();
showDirStats('./npmDist');
}

function babelBuild(srcPath, options) {
return babel.transformFileSync(srcPath, options).code + '\n';
const { code } = babel.transformFileSync(srcPath, {
babelrc: false,
configFile: './.babelrc-npm.json',
...options,
});
return code + '\n';
}

function buildPackageJSON() {
Expand All @@ -54,16 +61,19 @@ function buildPackageJSON() {
delete packageJSON.devDependencies;

const { version } = packageJSON;
const versionMatch = /^\d+\.\d+\.\d+-?(.*)?$/.exec(version);
const versionMatch = /^\d+\.\d+\.\d+-?(?<preReleaseTag>.*)?$/.exec(version);
if (!versionMatch) {
throw new Error('Version does not match semver spec: ' + version);
}

const [, preReleaseTag] = versionMatch;
const { preReleaseTag } = versionMatch.groups;

if (preReleaseTag != null) {
const [tag] = preReleaseTag.split('.');
assert(['alpha', 'beta', 'rc'].includes(tag), `"${tag}" tag is supported.`);
assert(
tag.startsWith('experimental-') || ['alpha', 'beta', 'rc'].includes(tag),
`"${tag}" tag is supported.`,
);

assert(!packageJSON.publishConfig, 'Can not override "publishConfig".');
packageJSON.publishConfig = { tag: tag || 'latest' };
Expand Down
25 changes: 4 additions & 21 deletions resources/utils.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
// @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 = [];
Expand All @@ -40,19 +24,19 @@ function readdirRecursive(dirPath, opts = {}) {
return result;
}

function showStats() {
function showDirStats(dirPath) {
const fileTypes = {};
let totalSize = 0;

for (const filepath of readdirRecursive('./dist')) {
for (const filepath of readdirRecursive(dirPath)) {
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));
const { size } = fs.lstatSync(path.join(dirPath, filepath));
totalSize += size;
fileTypes[filetype].size += size;
fileTypes[filetype].filepaths.push(filepath);
Expand Down Expand Up @@ -87,7 +71,6 @@ function showStats() {
}

module.exports = {
rmdirRecursive,
readdirRecursive,
showStats,
showDirStats,
};

0 comments on commit 87e62e7

Please sign in to comment.