-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from all commits
09f9e8a
c18de6d
ddb5804
6089cc2
386717b
148ebc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' ); | ||
|
@@ -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: | ||
|
@@ -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' } ); | ||
const packageJson = JSON.parse( packageJsonText ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this never throw? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just pondering why we were guarding so much a line below 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, good thinking. OTOH, all those |
||
return packageJson.hasOwnProperty( PROPKEY_ESNEXT ) || packageJson.hasOwnProperty( 'module' ); | ||
} | ||
|
||
const babelLoader = { | ||
loader: 'babel-loader', | ||
options: { | ||
|
@@ -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' ] | ||
}, | ||
{ | ||
|
@@ -110,6 +129,7 @@ const webpackConfig = { | |
}, | ||
resolve: { | ||
extensions: [ '.json', '.js', '.jsx' ], | ||
mainFields: [ PROPKEY_ESNEXT, 'browser', 'module', 'main' ], | ||
modules: [ | ||
path.join( __dirname, 'client' ), | ||
'node_modules', | ||
|
There was a problem hiding this comment.
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…There was a problem hiding this comment.
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...