Skip to content
This repository has been archived by the owner on Jul 9, 2018. It is now read-only.

Url Module: Bootstrap the repository and add the first module #1

Merged
merged 3 commits into from
Jun 28, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"presets": [
[ "env", {
"targets": {
"browsers": [
"last 2 Chrome versions",
"last 2 Firefox versions",
"last 2 Safari versions",
"last 2 Edge versions",
"last 2 iOS versions",
"last 1 Android version",
"last 1 ChromeAndroid version",
"ie 11"
]
}
} ]
],
"plugins": [
"transform-object-rest-spread"
]
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/

/packages/*/build/
/packages/*/build-module/
/packages/*/build-browser/
7 changes: 7 additions & 0 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"lerna": "2.0.0-rc.5",
"packages": [
"packages/*"
],
"version": "0.0.0"
}
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"devDependencies": {
"babel-core": "^6.25.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.5.2",
"chalk": "^1.1.3",
"glob": "^7.1.2",
"lerna": "^2.0.0-rc.5",
"mkdirp": "^0.5.1",
"rimraf": "^2.6.1"
},
"scripts": {
"build-clean": "rimraf ./packages/*/build ./packages/*/build-browser ./packages/*/build-module",
"build": "node ./scripts/build.js"
}
}
18 changes: 18 additions & 0 deletions packages/url/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@wordpress/url",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/packages.git"
},
"description": "WordPress URL utilities",
"main": "build/index.js",
"module": "build-module/index.js",
"browser": "build-browser/index.js",
"author": "WordPress",
"license": "GPL-2.0+",
"dependencies": {
"querystring": "^0.2.0",
"url": "^0.11.0"
}
}
21 changes: 21 additions & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* External dependencies
*/
import { parse, format } from 'url';
import { parse as parseQueryString, stringify } from 'querystring';

/**
* Appends arguments to the query string of the url
*
* @param {String} url URL
* @param {Object} args Query Args
*
* @return {String} Updated URL
*/
export function addQueryArgs( url, args ) {
const parsedURL = parse( url, true );
const query = { ...parsedURL.query, ...args };
delete parsedURL.search;

return format( { ...parsedURL, query } );
}
151 changes: 151 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* script to build WordPress packages into `build/` directory.
*
* Example:
* node ./scripts/build.js
*/

/**
* External Dependenceis
*/
const fs = require( 'fs' );
const path = require( 'path' );
const glob = require( 'glob' );
const babel = require( 'babel-core' );
const chalk = require( 'chalk' );
const mkdirp = require( 'mkdirp' );

/**
* Module Constants
*/
const PACKAGES_DIR = path.resolve( __dirname, '../packages' );
const SRC_DIR = 'src';
const BUILD_DIR = {
main: 'build',
module: 'build-module',
browser: 'build-browser',
};
const OK = chalk.reset.inverse.bold.green( ' DONE ' );

/**
* Babel Configuration
*/
const babelDefaultConfig = JSON.parse(
fs.readFileSync( path.resolve( __dirname, '..', '.babelrc' ), 'utf8' )
);
babelDefaultConfig.babelrc = false;
const presetEnvConfig = babelDefaultConfig.presets[ 0 ][ 1 ];
const babelConfigs = {
main: babelDefaultConfig,
module: Object.assign(
{},
babelDefaultConfig,
{ presets: [
[ "env", Object.assign( {},
presetEnvConfig,
{ modules: false }
) ],
] }
),
browser: Object.assign(
{},
babelDefaultConfig,
{ plugins: [ ...babelDefaultConfig.plugins, 'transform-runtime' ] }
)
};

/**
* Returns the absolute path of all WordPress packages
*
* @return {Array} Package paths
*/
function getAllPackages() {
return fs
.readdirSync( PACKAGES_DIR )
.map( ( file ) => path.resolve( PACKAGES_DIR, file ) )
.filter( ( f ) => fs.lstatSync( path.resolve( f ) ).isDirectory() );
}

/**
* Get the package name for a specified file
*
* @param {String} file File name
* @return {String} Package name
*/
function getPackageName(file) {
return path.relative( PACKAGES_DIR, file ).split( path.sep )[ 0 ];
}

/**
* Get Build Path for a specified file
*
* @param {String} file File to build
* @param {String} buildFolder Output folder
* @return {String} Build path
*/
function getBuildPath( file, buildFolder ) {
const pkgName = getPackageName( file );
const pkgSrcPath = path.resolve( PACKAGES_DIR, pkgName, SRC_DIR );
const pkgBuildPath = path.resolve( PACKAGES_DIR, pkgName, buildFolder );
const relativeToSrcPath = path.relative( pkgSrcPath, file );
return path.resolve( pkgBuildPath, relativeToSrcPath );
}

/**
* Build a file for the required environments (node and ES5)
*
* @param {String} file File path to build
* @param {Boolean} silent Show logs
*/
function buildFile( file, silent ) {
buildFileFor( file, silent, 'main' );
buildFileFor( file, silent, 'module' );
buildFileFor( file, silent, 'browser' );
}

/**
* Build a file for a specific environment
*
* @param {String} file File path to build
* @param {Boolean} silent Show logs
* @param {String} environment Dist environment (node or es5)
*/
function buildFileFor( file, silent, environment ) {
const buildDir = BUILD_DIR[ environment ];
const destPath = getBuildPath( file, buildDir );
const babelOptions = babelConfigs[ environment ];

mkdirp.sync( path.dirname( destPath ) );
const transformed = babel.transformFileSync( file, babelOptions ).code;
fs.writeFileSync(destPath, transformed);
if ( ! silent ) {
process.stdout.write(
chalk.green(' \u2022 ') +
path.relative(PACKAGES_DIR, file) +
chalk.green(' \u21D2 ') +
path.relative(PACKAGES_DIR, destPath) +
'\n'
);
}
}

/**
* Build the provided package path
*
* @param {String} packagePath absolute package path
*/
function buildPackage( packagePath ) {
const srcDir = path.resolve( packagePath, SRC_DIR );
const pattern = path.resolve( srcDir, '**/*' );
const files = glob.sync( pattern, { nodir: true } );

process.stdout.write( `${ path.basename( packagePath ) }\n` );

files.forEach( file => buildFile( file, true ) );
process.stdout.write( `${ OK }\n` );
}

process.stdout.write( chalk.inverse( '>> Building packages \n' ) );
getAllPackages()
.forEach( buildPackage );
process.stdout.write('\n');