From ae9de93a45dc45edb57532f205112fee606c1446 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Fri, 26 Oct 2018 18:58:21 +0100 Subject: [PATCH] feat: move @nuxtjs/babel-preset-app into nuxt mono-repo (#4205) --- packages/babel-preset-app/package.json | 24 +++++ packages/babel-preset-app/src/index.js | 97 +++++++++++++++++++ .../babel-preset-app/src/polyfills-plugin.js | 24 +++++ packages/webpack/package.json | 2 +- packages/webpack/src/config/base.js | 2 +- scripts/dev | 2 +- yarn.lock | 16 --- 7 files changed, 148 insertions(+), 19 deletions(-) create mode 100644 packages/babel-preset-app/package.json create mode 100644 packages/babel-preset-app/src/index.js create mode 100644 packages/babel-preset-app/src/polyfills-plugin.js diff --git a/packages/babel-preset-app/package.json b/packages/babel-preset-app/package.json new file mode 100644 index 0000000000..b3466d6efd --- /dev/null +++ b/packages/babel-preset-app/package.json @@ -0,0 +1,24 @@ +{ + "name": "@nuxt/babel-preset-app", + "version": "2.2.0", + "description": "babel-preset-app for nuxt.js", + "repository": "nuxt/nuxt.js", + "license": "MIT", + "author": "Evan You", + "contributors": [ + "Clark Du" + ], + "main": "src/index.js", + "dependencies": { + "@babel/core": "^7.1.0", + "@babel/plugin-proposal-class-properties": "^7.1.0", + "@babel/plugin-proposal-decorators": "^7.1.0", + "@babel/plugin-syntax-dynamic-import": "^7.0.0", + "@babel/plugin-syntax-jsx": "^7.0.0", + "@babel/plugin-transform-runtime": "^7.1.0", + "@babel/preset-env": "^7.1.0", + "@babel/runtime": "^7.0.0", + "babel-helper-vue-jsx-merge-props": "^2.0.3", + "babel-plugin-transform-vue-jsx": "^4.0.1" + } +} diff --git a/packages/babel-preset-app/src/index.js b/packages/babel-preset-app/src/index.js new file mode 100644 index 0000000000..db9673d723 --- /dev/null +++ b/packages/babel-preset-app/src/index.js @@ -0,0 +1,97 @@ +const defaultPolyfills = [ + // Promise polyfill alone doesn't work in IE, + // Needs this as well. see: #1642 + 'es6.array.iterator', + // This is required for webpack code splitting, vuex etc. + 'es6.promise', + // #2012 es6.promise replaces native Promise in FF and causes missing finally + 'es7.promise.finally' +] + +function getPolyfills(targets, includes, { ignoreBrowserslistConfig, configPath }) { + const { isPluginRequired } = require('@babel/preset-env') + const builtInsList = require('@babel/preset-env/data/built-ins.json') + const getTargets = require('@babel/preset-env/lib/targets-parser').default + const builtInTargets = getTargets(targets, { + ignoreBrowserslistConfig, + configPath + }) + + return includes.filter(item => isPluginRequired(builtInTargets, builtInsList[item])) +} + +module.exports = (context, options = {}) => { + const presets = [] + const plugins = [] + + // JSX + if (options.jsx !== false) { + plugins.push( + require('@babel/plugin-syntax-jsx'), + require('babel-plugin-transform-vue-jsx') + // require('babel-plugin-jsx-event-modifiers'), + // require('babel-plugin-jsx-v-model') + ) + } + + const modern = !!options.modern + + const { + buildTarget, + loose = false, + useBuiltIns = 'usage', + modules = false, + polyfills: userPolyfills, + ignoreBrowserslistConfig = modern, + configPath, + forceAllTransforms, + decoratorsLegacy + } = options + + let targets = options.targets + if (modern === true) { + targets = { esmodules: true } + } else if (targets === undefined) { + targets = buildTarget === 'server' ? { node: 'current' } : { ie: 9 } + } + + let polyfills + if (modern === false && useBuiltIns === 'usage' && buildTarget === 'client') { + polyfills = getPolyfills(targets, userPolyfills || defaultPolyfills, { + ignoreBrowserslistConfig, + configPath + }) + plugins.push([require('./polyfills-plugin'), { polyfills }]) + } else { + polyfills = [] + } + + // Pass options along to babel-preset-env + presets.push([ + require('@babel/preset-env'), { + loose, + modules, + targets, + useBuiltIns, + forceAllTransforms, + ignoreBrowserslistConfig, + exclude: polyfills + } + ]) + + plugins.push( + require('@babel/plugin-syntax-dynamic-import'), + [require('@babel/plugin-proposal-decorators'), { legacy: decoratorsLegacy !== false }], + [require('@babel/plugin-proposal-class-properties'), { loose }] + ) + + // Transform runtime, but only for helpers + plugins.push([require('@babel/plugin-transform-runtime'), { + regenerator: useBuiltIns !== 'usage' + }]) + + return { + presets, + plugins + } +} diff --git a/packages/babel-preset-app/src/polyfills-plugin.js b/packages/babel-preset-app/src/polyfills-plugin.js new file mode 100644 index 0000000000..8ff6350d68 --- /dev/null +++ b/packages/babel-preset-app/src/polyfills-plugin.js @@ -0,0 +1,24 @@ +// Add polyfill imports to the first file encountered. +module.exports = ({ types }) => { + let entryFile + return { + name: 'inject-polyfills', + visitor: { + Program(path, state) { + if (!entryFile) { + entryFile = state.filename + } else if (state.filename !== entryFile) { + return + } + + const { polyfills } = state.opts + const { createImport } = require('@babel/preset-env/lib/utils') + + // Imports are injected in reverse order + polyfills.slice().reverse().forEach((p) => { + createImport(path, p) + }) + } + } + } +} diff --git a/packages/webpack/package.json b/packages/webpack/package.json index 53bd488578..a9274c6e69 100644 --- a/packages/webpack/package.json +++ b/packages/webpack/package.json @@ -10,8 +10,8 @@ "dependencies": { "@babel/core": "^7.1.2", "@babel/polyfill": "^7.0.0", + "@nuxt/babel-preset-app": "^2.2.0", "@nuxt/common": "^2.2.0", - "@nuxtjs/babel-preset-app": "^0.7.0", "@nuxtjs/friendly-errors-webpack-plugin": "^2.0.2", "babel-loader": "^8.0.4", "cache-loader": "^1.2.2", diff --git a/packages/webpack/src/config/base.js b/packages/webpack/src/config/base.js index d98762fd8e..106ce56374 100644 --- a/packages/webpack/src/config/base.js +++ b/packages/webpack/src/config/base.js @@ -43,7 +43,7 @@ export default class WebpackBaseConfig { if (!options.babelrc && !options.presets) { options.presets = [ [ - require.resolve('@nuxtjs/babel-preset-app'), + require.resolve('@nuxt/babel-preset-app'), { buildTarget: this.isServer ? 'server' : 'client' } diff --git a/scripts/dev b/scripts/dev index a0d428793a..890acce2fe 100755 --- a/scripts/dev +++ b/scripts/dev @@ -12,7 +12,7 @@ async function main() { // Create a dev-only entrypoint to the src for (const pkg of workspacePackages) { - if (!pkg.pkg.main) { + if (!pkg.pkg.main || !pkg.options.build) { continue } consola.info(pkg.pkg.main) diff --git a/yarn.lock b/yarn.lock index c74fe7c89a..faa98098b0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1333,22 +1333,6 @@ resolved "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-1.1.2.tgz#54c5a964462be3d4d78af631363c18d6fa91ac26" integrity sha512-yprFYuno9FtNsSHVlSWd+nRlmGoAbqbeCwOryP6sC/zoCjhpArcRMYp19EvpSUSizJAlsXEwJv+wcWS9XaXdMw== -"@nuxtjs/babel-preset-app@^0.7.0": - version "0.7.0" - resolved "https://registry.npmjs.org/@nuxtjs/babel-preset-app/-/babel-preset-app-0.7.0.tgz#b208a95e0a053259c29b99a9e4ca9ea2604dbdd2" - integrity sha512-gYyFNa+etbsYs5SN7KT2SJxARPgKptBh5N3r66TzSE4kerFdSn7Gd324FoNKfqAnkXv7FsHZflKWoLFxYgn8UQ== - dependencies: - "@babel/core" "^7.1.0" - "@babel/plugin-proposal-class-properties" "^7.1.0" - "@babel/plugin-proposal-decorators" "^7.1.0" - "@babel/plugin-syntax-dynamic-import" "^7.0.0" - "@babel/plugin-syntax-jsx" "^7.0.0" - "@babel/plugin-transform-runtime" "^7.1.0" - "@babel/preset-env" "^7.1.0" - "@babel/runtime" "^7.0.0" - babel-helper-vue-jsx-merge-props "^2.0.3" - babel-plugin-transform-vue-jsx "^4.0.1" - "@nuxtjs/devalue@^1.0.1": version "1.0.1" resolved "https://registry.npmjs.org/@nuxtjs/devalue/-/devalue-1.0.1.tgz#661f16ed4c5d4bf64ab8b6e66fa1aa16bc57d504"