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

Framework: Transpile untranspiled node_modules #16870

Merged
merged 6 commits into from
Nov 13, 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
6 changes: 5 additions & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"ms": "0.7.1",
"name-all-modules-plugin": "1.0.1",
"node-sass": "4.5.3",
"notifications-panel": "1.2.7",
"notifications-panel": "2.1.0",
"npm-run-all": "4.0.2",
"numeral": "2.0.4",
"page": "1.6.4",
Expand Down Expand Up @@ -262,6 +262,7 @@
"eslint-plugin-jest": "21.2.0",
"eslint-plugin-react": "7.1.0",
"eslint-plugin-wpcalypso": "3.4.1",
"find-root": "1.1.0",
"glob": "7.0.3",
"husky": "0.13.3",
"ignore": "3.3.5",
Expand Down
22 changes: 21 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
const _ = require( 'lodash' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const DashboardPlugin = require( 'webpack-dashboard/plugin' );
const findRoot = require( 'find-root' );
const fs = require( 'fs' );
const HappyPack = require( 'happypack' );
const HardSourceWebpackPlugin = require( 'hard-source-webpack-plugin' );
const path = require( 'path' );
const pathIsInside = require( 'path-is-inside' );
const webpack = require( 'webpack' );
const NameAllModulesPlugin = require( 'name-all-modules-plugin' );
const AssetsPlugin = require( 'assets-webpack-plugin' );
Expand All @@ -24,8 +26,10 @@ const UseMinifiedFiles = require( './server/bundler/webpack-plugins/use-minified
/**
* Internal variables
*/
const PROPKEY_ESNEXT = 'esnext';
const calypsoEnv = config( 'env_id' );
const bundleEnv = config( 'env' );
const nodeModulesDir = path.resolve( __dirname, 'node_modules' );

/**
* This function scans the /client/extensions directory in order to generate a map that looks like this:
Expand All @@ -51,6 +55,20 @@ function getAliasesForExtensions() {
return aliasesMap;
}

/*
* Find package.json for file at `filepath`.
* Return `true` if it has a property whose key is `PROPKEY_ESNEXT` or 'module'.
* Taken from http://2ality.com/2017/06/pkg-esnext.html
*/
function hasPkgEsnext( filepath ) {
const pkgRoot = findRoot( filepath );
const packageJsonPath = path.resolve( pkgRoot, 'package.json' );
const packageJsonText = fs.readFileSync( packageJsonPath,
{ encoding: 'utf-8' } );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fun fact! I recently discovered that we can pass the options value as a string, in which case it will be interpreted as the encoding…

fs.readFileSync( packageJsonPath, 'utf-8' );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer that notation? I like the explicit encoding object key...

const packageJson = JSON.parse( packageJsonText );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this never throw?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it doesn't the build should stop - what would we do otherwise?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pondering why we were guarding so much a line below 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good thinking. OTOH, all those package.jsons are also used by npm, so I'd hope they're well-formed...

return packageJson.hasOwnProperty( PROPKEY_ESNEXT ) || packageJson.hasOwnProperty( 'module' );
}

const babelLoader = {
loader: 'babel-loader',
options: {
Expand Down Expand Up @@ -81,7 +99,8 @@ const webpackConfig = {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules[\/\\](?!notifications-panel)/,
// Should Babel transpile the file at `filepath`?
include: ( filepath ) => ( ! pathIsInside( filepath, nodeModulesDir ) || hasPkgEsnext( filepath ) ),
loader: [ 'happypack/loader' ]
},
{
Expand Down Expand Up @@ -110,6 +129,7 @@ const webpackConfig = {
},
resolve: {
extensions: [ '.json', '.js', '.jsx' ],
mainFields: [ PROPKEY_ESNEXT, 'browser', 'module', 'main' ],
modules: [
path.join( __dirname, 'client' ),
'node_modules',
Expand Down