From d4518d90ee4a7cdda6b4df7ac28916e32703bc02 Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Wed, 29 Aug 2018 16:58:53 -0700 Subject: [PATCH] Refactor babel configuration --- build/babel-fusion-preset.js | 62 ---------- .../test/index.js | 13 +-- build/babel-preset.js | 19 ---- build/babel-transpilation-preset.js | 57 ---------- build/browser-support.js | 18 --- build/compiler.js | 106 ++++++++---------- build/jest/jest-transformer.js | 33 +++--- entries/client-entry.js | 7 +- test/index.js | 3 - 9 files changed, 66 insertions(+), 252 deletions(-) delete mode 100644 build/babel-fusion-preset.js delete mode 100644 build/babel-preset.js delete mode 100644 build/babel-transpilation-preset.js delete mode 100644 build/browser-support.js diff --git a/build/babel-fusion-preset.js b/build/babel-fusion-preset.js deleted file mode 100644 index 433db77b..00000000 --- a/build/babel-fusion-preset.js +++ /dev/null @@ -1,62 +0,0 @@ -/** Copyright (c) 2018 Uber Technologies, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - */ - -/* eslint-env node */ - -/** - * This babel config is used for fusion-related compilation, - * including non-standard language features (e.g. React and Flow). - * This should only be applied when compiling files in src/ - */ - -// Needs to be a preset, because tree shaking must run after JSX plugin (in React preset) -function globalsPreset( - context /*: any */, - {target, transformGlobals, assumeNoImportSideEffects} /*: any */ -) { - return { - plugins: [ - ...(transformGlobals - ? [ - [require.resolve('babel-plugin-transform-cup-globals'), {target}], - assumeNoImportSideEffects && [ - require.resolve( - './babel-plugins/babel-plugin-transform-tree-shake' - ), - {target}, - ], - ].filter(Boolean) - : []), - ], - }; -} - -module.exports = function buildPreset( - context /*: any */, - { - targets, - transformGlobals = true, - assumeNoImportSideEffects = false, - } /*: any */ -) { - const target = targets.hasOwnProperty('node') ? 'node' : 'browser'; - - return { - presets: [ - require('@babel/preset-react'), - require('@babel/preset-flow'), - [globalsPreset, {target, transformGlobals, assumeNoImportSideEffects}], - ], - plugins: [ - require('@babel/plugin-syntax-object-rest-spread'), - require('@babel/plugin-syntax-dynamic-import'), - ], - }; -}; - -module.exports.globalsPreset = globalsPreset; diff --git a/build/babel-plugins/babel-plugin-transform-tree-shake/test/index.js b/build/babel-plugins/babel-plugin-transform-tree-shake/test/index.js index 95ca224e..a0054dff 100644 --- a/build/babel-plugins/babel-plugin-transform-tree-shake/test/index.js +++ b/build/babel-plugins/babel-plugin-transform-tree-shake/test/index.js @@ -4,21 +4,14 @@ const fs = require('fs'); const test = require('tape'); const {transformFileSync} = require('@babel/core'); const plugin = require('../'); -const {globalsPreset} = require('../../../babel-fusion-preset.js'); test('boolean expression transformed', t => { const output = transformFileSync( __dirname + '/fixtures/input-boolean-expression', { - presets: [ - [ - globalsPreset, - { - target: 'browser', - transformGlobals: true, - assumeNoImportSideEffects: true, - }, - ], + plugins: [ + [require('babel-plugin-transform-cup-globals'), {target: 'browser'}], + [plugin, {target: 'browser'}], ], } ); diff --git a/build/babel-preset.js b/build/babel-preset.js deleted file mode 100644 index caad9d1e..00000000 --- a/build/babel-preset.js +++ /dev/null @@ -1,19 +0,0 @@ -/** Copyright (c) 2018 Uber Technologies, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - */ - -/* eslint-env node */ - -module.exports = function buildPreset(context /*: any */, opts /*: any */) { - return { - plugins: [], - presets: [ - [require('./babel-transpilation-preset'), opts], - [require('./babel-fusion-preset'), opts], - ], - }; -}; diff --git a/build/babel-transpilation-preset.js b/build/babel-transpilation-preset.js deleted file mode 100644 index 92f53fa2..00000000 --- a/build/babel-transpilation-preset.js +++ /dev/null @@ -1,57 +0,0 @@ -/** Copyright (c) 2018 Uber Technologies, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - */ - -/* eslint-env node */ - -/** - * This babel config is used for transpiling valid spec-compliant ES2017+. - * This should be universally applied for all code, including node_modules. - */ - -module.exports = function buildPreset( - context /*: any */, - {targets, modules = false} /*: any */ -) { - const target = targets.hasOwnProperty('node') ? 'node' : 'browser'; - - return { - presets: [ - [ - require('@babel/preset-env'), - { - targets: targets, - modules: modules, - exclude: ['transform-regenerator', 'transform-async-to-generator'], - }, - ], - require('@babel/preset-react'), - require('@babel/preset-flow'), - ], - plugins: [ - require('@babel/plugin-syntax-dynamic-import'), - require('@babel/plugin-proposal-async-generator-functions'), - require('@babel/plugin-proposal-class-properties'), - [ - require('@babel/plugin-proposal-object-rest-spread'), - { - useBuiltIns: true, - }, - ], - ...(target === 'browser' - ? [ - [ - require.resolve('fast-async'), - { - spec: true, - }, - ], - ] - : []), - ], - }; -}; diff --git a/build/browser-support.js b/build/browser-support.js deleted file mode 100644 index 698eaf07..00000000 --- a/build/browser-support.js +++ /dev/null @@ -1,18 +0,0 @@ -/** Copyright (c) 2018 Uber Technologies, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - */ - -/* eslint-env node */ - -const conservative = [ - '>1%', - 'last 4 versions', - 'Firefox ESR', - 'not ie < 9', // React doesn't support IE8 anyway -]; - -module.exports.conservative = conservative; diff --git a/build/compiler.js b/build/compiler.js index af42fdea..70f75f18 100644 --- a/build/compiler.js +++ b/build/compiler.js @@ -25,6 +25,7 @@ const { } = require('../lib/compression'); const resolveFrom = require('resolve-from'); +const getBabelConfig = require('./get-babel-config.js'); const LoaderContextProviderPlugin = require('./plugins/loader-context-provider-plugin.js'); const {chunkIdsLoader, fileLoader} = require('./loaders/index.js'); const {DeferredState} = require('./shared-state-containers.js'); @@ -34,7 +35,6 @@ const InstrumentedImportDependencyTemplatePlugin = require('./plugins/instrument const I18nDiscoveryPlugin = require('./i18n-discovery-plugin.js'); const ClientChunkBundleUrlMapPlugin = require('./client-chunk-bundle-url-map-plugin'); const SyncChunkIdsPlugin = require('./sync-chunk-ids-plugin'); -const browserSupport = require('./browser-support'); const chalk = require('chalk'); const webpackHotMiddleware = require('webpack-hot-middleware'); const loadFusionRC = require('./load-fusionrc.js'); @@ -94,15 +94,6 @@ function getConfig({target, env, dir, watch, state}) { `process.env.NODE_ENV = '${nodeEnv}';` + `}`; - const targets = - target === 'node' - ? { - node: 'current', - } - : { - browsers: browserSupport.conservative, - }; - return { name, target, @@ -204,25 +195,19 @@ function getConfig({target, env, dir, watch, state}) { loader: require.resolve('babel-loader'), options: { cacheDirectory: `${dir}/node_modules/.fusion_babel_cache`, - plugins: [ - // Note: plugins run first to last, so user-defined plugins go first - ...(fusionConfig.babel && fusionConfig.babel.plugins - ? fusionConfig.babel.plugins - : []), - ], - presets: [ - // Note: presets run last to first, so user-defined presets go last - [ - require.resolve('./babel-transpilation-preset.js'), - { - targets, - }, - ], - ...(fusionConfig.babel && fusionConfig.babel.presets - ? fusionConfig.babel.presets - : []), - ], - + ...getBabelConfig({ + runtime: + target === 'node' ? 'node-bundled' : 'browser-legacy', + specOnly: true, + plugins: + fusionConfig.babel && fusionConfig.babel.plugins + ? fusionConfig.babel.plugins + : [], + presets: + fusionConfig.babel && fusionConfig.babel.presets + ? fusionConfig.babel.presets + : [], + }), /** * Fusion-specific transforms (not applied to node_modules) */ @@ -235,40 +220,39 @@ function getConfig({target, env, dir, watch, state}) { entry, /fusion-cli\/entries/, ], - plugins: [ - //cup-globals works with webpack.EnvironmentPlugin(['NODE_ENV']) to implement static conditionals - require.resolve('./babel-plugins/babel-plugin-asseturl'), - require.resolve( - './babel-plugins/babel-plugin-pure-create-plugin' - ), - require.resolve( - './babel-plugins/babel-plugin-sync-chunk-ids' - ), - require.resolve( - './babel-plugins/babel-plugin-sync-chunk-paths' - ), - require.resolve('./babel-plugins/babel-plugin-chunkid'), - pragma && [ - require.resolve('@babel/plugin-transform-react-jsx'), - {pragma}, - ], - target === 'web' && - require.resolve('./babel-plugins/babel-plugin-i18n'), - ].filter(Boolean), - presets: [ - [ - require.resolve('./babel-fusion-preset.js'), - { - targets, - assumeNoImportSideEffects: - fusionConfig.assumeNoImportSideEffects, - }, - ], - ], + ...getBabelConfig({ + dev: env === 'development', + transformGlobals: true, + assumeNoImportSideEffects: + fusionConfig.assumeNoImportSideEffects, + runtime: + target === 'node' ? 'node-bundled' : 'browser-legacy', + specOnly: false, + plugins: [ + //cup-globals works with webpack.EnvironmentPlugin(['NODE_ENV']) to implement static conditionals + require.resolve( + './babel-plugins/babel-plugin-asseturl' + ), + require.resolve( + './babel-plugins/babel-plugin-pure-create-plugin' + ), + require.resolve( + './babel-plugins/babel-plugin-sync-chunk-ids' + ), + require.resolve( + './babel-plugins/babel-plugin-sync-chunk-paths' + ), + require.resolve('./babel-plugins/babel-plugin-chunkid'), + pragma && [ + require.resolve('@babel/plugin-transform-react-jsx'), + {pragma}, + ], + target === 'web' && + require.resolve('./babel-plugins/babel-plugin-i18n'), + ].filter(Boolean), + }), }, ], - - babelrc: false, }, }, ], diff --git a/build/jest/jest-transformer.js b/build/jest/jest-transformer.js index dc730900..a04c9121 100644 --- a/build/jest/jest-transformer.js +++ b/build/jest/jest-transformer.js @@ -9,30 +9,27 @@ /* eslint-env node */ const loadFusionRC = require('../load-fusionrc.js'); - -const babelConfig = require('../babel-preset.js')(null, { - targets: { - node: 'current', - }, - modules: 'commonjs', - transformGlobals: false, -}); +const getBabelConfig = require('../get-babel-config.js'); const fusionConfig = loadFusionRC(process.cwd()); -if (!babelConfig.plugins) { - babelConfig.plugins = []; -} - -babelConfig.plugins.push(require.resolve('babel-plugin-dynamic-import-node')); +let customPlugins; +let customPresets; -if (fusionConfig.babel && fusionConfig.babel.plugins) { - // Run user-defined plugins first - babelConfig.plugins = fusionConfig.babel.plugins.concat( - ...babelConfig.plugins - ); +if (fusionConfig.babel) { + customPlugins = fusionConfig.babel.plugins; + customPresets = fusionConfig.babel.presets; } +const babelConfig = getBabelConfig({ + runtime: 'node-native', + specOnly: false, + plugins: customPlugins, + presets: customPresets, + dev: false, + transformGlobals: false, +}); + const transformer = require('babel-jest').createTransformer(babelConfig); module.exports = transformer; diff --git a/entries/client-entry.js b/entries/client-entry.js index 3e3bad22..fc1dda3b 100644 --- a/entries/client-entry.js +++ b/entries/client-entry.js @@ -11,8 +11,7 @@ // Require is used to opt out of webpack tree-shaking of ununsed imports // See: https://github.com/webpack/webpack/issues/6571 -require('core-js/es6'); // eslint-disable-line -require('core-js/es7'); // eslint-disable-line +require("core-js"); // eslint-disable-line /* Webpack has a configuration option called `publicPath`, which determines the @@ -35,12 +34,12 @@ into `__webpack_require__.p = ...` and uses it for HMR manifest requests /* eslint-disable */ // $FlowFixMe -__webpack_public_path__ = window.__WEBPACK_PUBLIC_PATH__ + '/'; +__webpack_public_path__ = window.__WEBPACK_PUBLIC_PATH__ + "/"; /* eslint-enable */ function reload() { // $FlowFixMe - const main = require('__FRAMEWORK_SHARED_ENTRY__'); // eslint-disable-line + const main = require("__FRAMEWORK_SHARED_ENTRY__"); // eslint-disable-line const initialize = main.default || main; Promise.resolve(initialize()).then(app => { app.callback().call(); diff --git a/test/index.js b/test/index.js index e31dbca7..2b8bc807 100644 --- a/test/index.js +++ b/test/index.js @@ -16,9 +16,6 @@ require('./compiler/errors'); require('./hmr'); require('./route-prefix.js'); -/* -require('./browser-support'); -*/ require('../build/babel-plugins/babel-plugin-pure-create-plugin/test'); require('../build/babel-plugins/babel-plugin-asseturl/test'); require('../build/babel-plugins/babel-plugin-chunkid/test');