Skip to content

Commit

Permalink
Revert "Upgrade webpack to 2.x (#14786)"
Browse files Browse the repository at this point in the history
This reverts commit 46d5676.
  • Loading branch information
samouri committed Jun 28, 2017
1 parent f1ff5a5 commit f9a5f24
Show file tree
Hide file tree
Showing 12 changed files with 630 additions and 699 deletions.
861 changes: 378 additions & 483 deletions npm-shrinkwrap.json

Large diffs are not rendered by default.

24 changes: 11 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"async": "0.9.0",
"autoprefixer": "6.3.5",
"autosize": "3.0.15",
"babel-core": "6.24.1",
"babel-loader": "7.0.0",
"babel-core": "6.9.1",
"babel-loader": "6.2.4",
"babel-plugin-add-module-exports": "0.2.1",
"babel-plugin-lodash": "3.2.0",
"babel-plugin-syntax-jsx": "6.8.0",
Expand Down Expand Up @@ -43,7 +43,7 @@
"component-uid": "0.0.2",
"cookie": "0.1.2",
"cookie-parser": "1.3.2",
"copy-webpack-plugin": "4.0.1",
"copy-webpack-plugin": "3.0.1",
"create-react-class": "15.5.3",
"creditcards": "2.1.2",
"cross-env": "4.0.0",
Expand Down Expand Up @@ -71,8 +71,7 @@
"get-video-id": "2.1.4",
"globby": "6.1.0",
"gridicons": "1.1.0",
"happypack": "4.0.0-beta.1",
"hard-source-webpack-plugin": "0.3.12",
"hard-source-webpack-plugin": "0.0.42",
"he": "0.5.0",
"html-loader": "0.4.0",
"i18n-calypso": "1.7.3",
Expand Down Expand Up @@ -118,7 +117,6 @@
"react-day-picker": "6.0.2",
"react-docgen": "2.13.0",
"react-dom": "15.4.0",
"react-hot-loader": "1.3.1",
"react-modal": "1.6.5",
"react-pure-render": "1.0.2",
"react-redux": "5.0.3",
Expand All @@ -145,11 +143,8 @@
"uuid": "2.0.1",
"valid-url": "1.0.9",
"walk": "2.3.4",
"webpack": "2.3.2",
"webpack-chunk-hash": "0.4.0",
"webpack-dashboard": "0.2.1",
"webpack-dev-middleware": "1.9.0",
"webpack-hot-middleware": "2.15.0",
"webpack": "1.13.1",
"webpack-dev-middleware": "1.2.0",
"wpcom": "5.4.0",
"wpcom-oauth": "0.3.3",
"wpcom-proxy-request": "3.0.0",
Expand Down Expand Up @@ -190,7 +185,7 @@
"clean:devdocs": "npm run -s rm -- server/devdocs/search-index.js && npm run -s rm -- server/devdocs/proptypes-index.json && npm run -s rm -- server/devdocs/components-usage-stats.json",
"clean:public": "npm run -s rm -- public .css .css.map .js .js.map",
"predashboard": "npm run -s install-if-deps-outdated",
"dashboard": "DASHBOARD=1 webpack-dashboard -- npm start",
"dashboard": "webpack-dashboard -- npm start",
"distclean": "npm run -s clean && npm run -s rm -- node_modules",
"docker": "docker run -it --name wp-calypso --rm -p 80:3000 -e NODE_ENV=wpcalypso -e CALYPSO_ENV=wpcalypso wp-calypso",
"env": "cross-env NODE_PATH=$NODE_PATH:server:client:.",
Expand Down Expand Up @@ -257,14 +252,17 @@
"prettier": "git+https://github.com/Automattic/calypso-prettier.git#calypso-1.1",
"react-addons-test-utils": "15.4.0",
"react-codemod": "github:reactjs/react-codemod",
"react-hot-loader": "1.3.0",
"react-test-env": "0.2.0",
"readline-sync": "1.4.5",
"sinon": "1.17.7",
"sinon-chai": "2.8.0",
"socket.io": "1.4.5",
"stylelint": "6.9.0",
"supertest": "2.0.0",
"webpack-bundle-analyzer": "2.8.2"
"webpack-bundle-analyzer": "2.2.1",
"webpack-dashboard": "0.2.0",
"webpack-dev-server": "1.11.0"
},
"optionalDependencies": {
"fsevents": "1.1.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const path = require( 'path' );
* @return {String} Module version
*/
function getModuleVersion( id ) {
return require( path.dirname( require.resolve( id ) ).replace( /[\/\\]lib/, '' ) + '/package' ).version;
return require( path.dirname( require.resolve( id ) ) + '/package' ).version;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion server/bundler/bin/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function minify( files ) {
file,
'-m',
'-c',
'-o', file.replace( '.js', '.min.js' )
'-o', file.replace( '.js', '.m.js' )
],
// have to pipe stderr to parent, otherwise large bundles will never finish
// see https://github.com/nodejs/node-v0.x-archive/issues/6764
Expand Down
9 changes: 1 addition & 8 deletions server/bundler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
var webpackMiddleware = require( 'webpack-dev-middleware' ),
webpack = require( 'webpack' ),
chalk = require( 'chalk' );
const hotMiddleware = require( 'webpack-hot-middleware' );

var utils = require( './utils' ),
webpackConfig = require( 'webpack.config' );
Expand All @@ -16,14 +15,8 @@ function middleware( app ) {
beforeFirstCompile = true,
assets;

app.use( hotMiddleware( compiler ) );

app.set( 'compiler', compiler );

compiler.apply( new webpack.ProgressPlugin( {
profile: true,
} ) );

compiler.plugin( 'done', function( stats ) {
built = true;
assets = utils.getAssets( stats.toJson() );
Expand Down Expand Up @@ -84,7 +77,7 @@ function middleware( app ) {
hash: true,
version: false,
timings: true,
assets: false,
assets: true,
chunks: true,
chunkModules: false,
modules: false,
Expand Down
2 changes: 2 additions & 0 deletions server/bundler/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ function singleEnsure( chunkName ) {
module.exports = function( content ) {
var sections;

this.cacheable && this.cacheable();

sections = require( this.resourcePath );

if ( ! Array.isArray( sections ) ) {
Expand Down
22 changes: 8 additions & 14 deletions server/bundler/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ ChunkFileNames.prototype.apply = function( compiler ) {
return this.asString( [
"// \"0\" is the signal for \"already loaded\"",
"if ( installedChunks[ chunkId ] === 0 ) {",
this.indent("return Promise.resolve();"),
this.indent("return callback.call( null, " + this.requireFn + " );"),
"}",
"// a Promise means \"currently loading\".",
"if ( installedChunks[ chunkId ] ) {",
this.indent( "return installedChunks[ chunkId ][2];" ),
"// an array means \"currently loading\".",
"if ( installedChunks[ chunkId ] !== undefined ) {",
this.indent( "installedChunks[ chunkId ].push( callback );" ),
" } else { ",
this.indent( [
"// start chunk loading",
"var promise = new Promise( function( resolve, reject ) {",
this.indent( [
"installedChunks[ chunkId ] = [ resolve, reject ];"
] ),
"} );",
"installedChunks[ chunkId ][2] = promise;",
"installedChunks[ chunkId ] = [ callback ];",
"window.__chunkErrors = window.__chunkErrors || {};",
"window.__chunkErrors[ " + JSON.stringify( chunkMaps.name ) + "[chunkId]||chunkId ]=null;",
"var head = document.getElementsByTagName('head')[0];",
Expand All @@ -35,12 +30,11 @@ ChunkFileNames.prototype.apply = function( compiler ) {
"script.onerror = script.onload = script.onreadystatechange = null;",
"delete installedChunks[ chunkId ];",
"window.__chunkErrors[ " + JSON.stringify( chunkMaps.name ) + "[chunkId]||chunkId ]=new Error();",
"return Promise.resolve();"
"callback.call( null, " + this.requireFn + ")"
] ),
"};",
"script.src = " + this.requireFn + ".p + (" + JSON.stringify( chunkMaps.name ) + "[chunkId]||chunkId) + '.' + (" + JSON.stringify( chunkMaps.hash ) + "[chunkId]||chunkID) + ( isDebug ? '' : '.min' ) + '.js';",
"head.appendChild( script );",
"return promise;"
"script.src = " + this.requireFn + ".p + (" + JSON.stringify( chunkMaps.name ) + "[chunkId]||chunkId) + '.' + (" + JSON.stringify( chunkMaps.hash ) + "[chunkId]||chunkID) + ( isDebug ? '' : '.m' ) + '.js';",
"head.appendChild( script );"
] ),
"}"
] );
Expand Down
26 changes: 0 additions & 26 deletions server/bundler/webpack-plugins/NamedModulesPlugin.js

This file was deleted.

130 changes: 130 additions & 0 deletions server/bundler/webpack/stable-build-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
const crypto = require( 'crypto' );

const md5Cache = {};

function md5( content ) {
if ( ! md5Cache[ content ] ) {
md5Cache[ content ] = crypto.createHash( 'md5' ).update( content, 'utf-8' ).digest( 'hex' );
}
return md5Cache[ content ];
}

function hashToId( hash, seed, hashSize ) {
// Generate a unsigned integer sized <hashSize> bits using a part of the MD5
// hash. Seed is a number 0..31 and the hash is expected to be 32 chars
// (nibbles) long.

// double the hash to allow overflow
hash = hash + hash;

// get lower and upper 28 bits
const lsb = parseInt( hash.substr( seed, 7 ), 16 );
const msb = parseInt( hash.substr( seed + 7, 7 ), 16 );

// combine them to get the ID
// NOTE: Logical operators only work up to 31 bits (because values will be
// casted to 32bit signed integer), so we use classic arithmetic!
const lsbBits = Math.min( 28, hashSize );
const msbBits = Math.max( 0, hashSize - 28 );
const lsbMask = Math.pow( 2, lsbBits ) - 1;
const msbMask = Math.pow( 2, msbBits ) - 1;
return ( lsb & lsbMask ) + ( ( msb & msbMask ) * Math.pow( 2, 28 ) );
}

function sortById( a, b ) {
return a > b ? 1 : a < b ? -1 : 0; //eslint-disable-line no-nested-ternary
}

function getModuleSource( module ) {
return {
id: module.id,
source: ( module._source || {} )._value || '',
dependencies: ( module.dependencies || [] ).map( function( d ) {
return d.module ? d.module.id : '';
} )
};
}

function concatenateSource( result, module ) {
return result + '#' + module.id + ':' + module.source + '$' + ( module.dependencies.join( ',' ) );
}

function WebpackStableBuildPlugin( options ) {
this.options = options || {};

this.algorithm = options.algorithm || 'md5';
this.digest = options.digest || 'hex';
}

WebpackStableBuildPlugin.prototype.apply = function( compiler ) {
const usedChunkIds = {};
const usedModuleIds = {};
const context = compiler.options.context;
const seed = ( +this.options.seed || 0 ) % 32;
const hashSize = ( +this.options.hashSize || 53 );
const _plugin = this;

if ( hashSize > 53 ) {
// In JavaScript, only integers up to 2^53 (exclusive) can be considered
// safe, see http://www.2ality.com/2013/10/safe-integers.html
throw new Error( 'hashSize too large' );
}

function genChunkId( chunkName ) {
const hash = md5( chunkName );
// generate a 28 bit integer using a part of the MD5 hash
const id = hashToId( hash, seed, hashSize );
if ( usedChunkIds.hasOwnProperty( id ) && usedChunkIds[ id ] !== chunkName ) {
throw new Error( 'chunk id collision' );
}
return id;
}

function genModuleId( modulePath ) {
const hash = md5( modulePath );
// generate a 28 bit integer using a part of the MD5 hash
const id = hashToId( hash, seed, hashSize );
if ( usedModuleIds.hasOwnProperty( id ) && usedModuleIds[ id ] !== modulePath ) {
throw new Error( 'module id collision' );
}
return id;
}

compiler.plugin( 'compilation', function( compilation ) {
compilation.plugin( 'before-chunk-ids', function( chunks ) {
chunks.forEach( function( chunk ) {
const chunkName = chunk.name;
if ( ! chunkName ) {
return;
}
chunk.id = genChunkId( chunkName );
usedChunkIds[ chunk.id ] = chunkName;
} );
} );

compilation.plugin( 'before-module-ids', function( modules ) {
modules.forEach( function( module ) {
if ( module.libIdent && module.id === null ) {
const modulePath = module.libIdent( {
context: context
} );
module.id = genModuleId( modulePath );
usedModuleIds[ module.id ] = modulePath;
}
} );
} );

compilation.plugin( 'chunk-hash', function( chunk, chunkHash ) {
const source = chunk.modules.map( getModuleSource ).sort( sortById ).reduce( concatenateSource, '' );
const hash = crypto.createHash( _plugin.algorithm ).update( source );
// have to include the seed to get different hashes for different seeds
hash.update( seed.toString( 16 ) );

chunkHash.digest = function( digest ) {
return hash.digest( digest || _plugin.digest );
};
} );
} );
};

module.exports = WebpackStableBuildPlugin;
2 changes: 1 addition & 1 deletion server/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function generateStaticUrls( request ) {
const name = asset.name;
urls[ name ] = asset.url;
if ( config( 'env' ) !== 'development' ) {
urls[ name + '-min' ] = asset.url.replace( '.js', '.min.js' );
urls[ name + '-min' ] = asset.url.replace( '.js', '.m.js' );
}
} );

Expand Down
Loading

0 comments on commit f9a5f24

Please sign in to comment.