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

Make single 'build.js' to handle NPM package build #1841

Merged
merged 1 commit into from May 1, 2019
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
5 changes: 0 additions & 5 deletions .babelrc.js
@@ -1,6 +1,3 @@
//ignore all "__*__" directories, e.g. __tests__, __fixtures__
const ignore = [/\/__[^/]*__\//]; // **/__*__

module.exports = {
presets: ['@babel/preset-env'],
plugins: [
Expand All @@ -15,13 +12,11 @@ module.exports = {
presets: [
['@babel/preset-env', { modules: 'commonjs' }],
],
ignore,
},
mjs: {
presets: [
['@babel/preset-env', { modules: false }],
],
ignore,
},
},
};
9 changes: 1 addition & 8 deletions package.json
Expand Up @@ -32,13 +32,7 @@
"prettier": "prettier --write --list-different 'src/**/*.js'",
"check": "flow check",
"check-cover": "flow batch-coverage --quiet --strip-root --show-all src/ && flow stop --quiet",
"build": "npm run build:clean && npm run build:cp && npm run build:package-json && npm run build:cjs && npm run build:mjs && npm run build:flow",
"build:clean": "rm -rf ./dist && mkdir ./dist",
"build:cp": "cp README.md LICENSE ./dist",
"build:package-json": "node ./resources/copy-package-json.js",
"build:cjs": "babel src --env-name cjs --out-dir dist/",
"build:mjs": "babel src --env-name mjs --out-dir dist/module && for file in $(find dist/module -name '*.js'); do mv \"$file\" `echo \"$file\" | sed 's/dist\\/module/dist/g; s/.js$/.mjs/g'`; done && rm -rf dist/module",
"build:flow": "for file in $(find ./src -name '*.js' -not -path '*/__*__*'); do cp \"$file\" `echo \"$file\" | sed 's/\\/src\\//\\/dist\\//g'`.flow; done",
"build": "node resources/build.js",
"preversion": ". ./resources/checkgit.sh && npm test",
"prepublishOnly": ". ./resources/prepublish.sh",
"gitpublish": ". ./resources/gitpublish.sh"
Expand All @@ -47,7 +41,6 @@
"iterall": "^1.2.2"
},
"devDependencies": {
"@babel/cli": "7.4.4",
"@babel/core": "7.4.4",
"@babel/plugin-transform-flow-strip-types": "7.4.4",
"@babel/polyfill": "7.4.4",
Expand Down
65 changes: 65 additions & 0 deletions resources/build.js
@@ -0,0 +1,65 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
*/

'use strict';

const fs = require('fs');
const path = require('path');
const assert = require('assert');
const babel = require('@babel/core');
const {
copyFile,
writeFile,
rmdirRecursive,
mkdirRecursive,
readdirRecursive,
} = require('./fs-utils');

if (require.main === module) {
rmdirRecursive('./dist');
mkdirRecursive('./dist');

copyFile('./LICENSE', './dist/LICENSE');
copyFile('./README.md', './dist/README.md');
writeFile('./dist/package.json', buildPackageJSON());

const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });
for (const filepath of srcFiles) {
if (filepath.endsWith('.js')) {
buildJSFile(filepath);
}
}
}

function buildJSFile(filepath) {
const srcPath = path.join('./src', filepath);
const destPath = path.join('./dist', filepath);
const cjs = babel.transformFileSync(srcPath, { envName: 'cjs' });
const mjs = babel.transformFileSync(srcPath, { envName: 'mjs' });

copyFile(srcPath, destPath + '.flow');
writeFile(destPath, cjs.code);
writeFile(destPath.replace(/\.js$/, '.mjs'), mjs.code);
}

function buildPackageJSON() {
const packageJSON = require('../package.json');
delete packageJSON.scripts;
delete packageJSON.devDependencies;

const { version } = packageJSON;
const match = /^[0-9]+\.[0-9]+\.[0-9]+-?([^.]*)/.exec(version);
assert(match, 'Version does not match semver spec.');
const tag = match[1];
assert(!tag || tag === 'rc', 'Only "rc" tag is supported.');

assert(!packageJSON.publishConfig, 'Can not override "publishConfig".');
packageJSON.publishConfig = { tag: tag || 'latest' };
return JSON.stringify(packageJSON, null, 2);
}
33 changes: 0 additions & 33 deletions resources/copy-package-json.js

This file was deleted.

65 changes: 65 additions & 0 deletions resources/fs-utils.js
@@ -0,0 +1,65 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
*/

'use strict';

const fs = require('fs');
const path = require('path');

function mkdirRecursive(path) {
fs.mkdirSync(path, { recursive: true });
}

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;
return fs.readdirSync(dirPath, { withFileTypes: true })
.flatMap(dirent => {
const name = dirent.name;
if (dirent.isDirectory()) {
if (ignoreDir && ignoreDir.test(name)) {
return [];
}
return readdirRecursive(path.join(dirPath, name), opts)
.map(f => path.join(name, f));
}
return dirent.name;
});
}

function writeFile(destPath, data) {
mkdirRecursive(path.dirname(destPath));
fs.writeFileSync(destPath, data);
}

function copyFile(srcPath, destPath) {
mkdirRecursive(path.dirname(destPath));
fs.copyFileSync(srcPath, destPath);
}

module.exports = {
copyFile,
writeFile,
rmdirRecursive,
mkdirRecursive,
readdirRecursive,
};