Giter VIP home page Giter VIP logo

babel-engine-plugin's Introduction

babel-engine-plugin Build Status

Webpack plugin that transpiles dependencies targeting Node.js versions newer than Node.js 0.10

Install

$ npm install --save-dev babel-engine-plugin

Usage

Within your webpack configuration object, you'll need to add the babel-engine-plugin to the list of plugins.

const BabelEnginePlugin = require('babel-engine-plugin');

module.exports = {
    entry: './app.js',
    output: {
        filename: 'bundle.js'
    },
    plugins: [
        new BabelEnginePlugin({
            presets: ['env']
        })
    ]
}

Why

Module maintainers often only target Node.js and don't really care about browser support. Tools like Webpack allow you to easily bundle these modules and run them in the browser. But since Node.js 0.12 became deprecated, module maintainers started to leverage the new ES2016 features, for example fat-arrow functions. This becomes a problem because not all of these features are supported in the browser. Tools like UglifyJS doesn't like them either as it will fail with an Unexpected Token error.

The most popular Webpack loader for Babel, babel-loader, describes that you should exclude node_modules because you should transpile as few files as possible. This means, you will also exclude all the modules that utilize the new ES2016 features.

This Webpack plugin only transpiles modules in node_modules, if you need to transpile your source files as well, use babel-loader. This plugin checks the engines field in package.json and only transpiles the dependency if it does not support Node.js 0.10.

The full discussion can be found here.

API

new BabelEnginePlugin([babelOptions, [pluginOptions]])

babelOptions

See babel options.

pluginOptions

verbose

Type: boolean
Default: true

By disabling verbose logging, the plugin will only print the warning per package once.

Related

License

MIT © Sam Verschueren

babel-engine-plugin's People

Contributors

arzafran avatar dmikheev avatar jdufresne avatar oligot avatar samverschueren avatar winstonewert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

babel-engine-plugin's Issues

`verbose` option has no effect

I'm still getting all the warnings, even with verbose: false. Did you perhaps mean to use an || operator instead of && on this line? Happy to submit a PR if that's helpful.

Doesn't work with html-webpack-plugin

I'm getting the following error while trying to use the library

node_modules\html-webpack-plugin\lib\compiler.js:194

return childCompilation.assets[helperFileName].source();
                                                                           ^
TypeError: Cannot read property 'source' of undefined

Deprecation warnings when used with webpack 4

I get this warnings during webpack compilation. More info can be found here: webpack/webpack#6568

(node:16392) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
    at BabelEnginePlugin.apply (./node_modules/babel-engine-plugin/index.js:13:12)
    at webpack (./node_modules/webpack/lib/webpack.js:37:12)
    at startDevServer (./node_modules/webpack-dev-server/bin/webpack-dev-server.js:335:16)
    at processOptions (./node_modules/webpack-dev-server/bin/webpack-dev-server.js:309:5)
    at Object.<anonymous> (./node_modules/webpack-dev-server/bin/webpack-dev-server.js:424:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3
(node:16392) DeprecationWarning: Compilation.moduleTemplate: Use Compilation.moduleTemplates.javascript instead
    at compiler.plugin.compilation (./node_modules/babel-engine-plugin/index.js:14:16)
    at SyncHook.eval [as call] (eval at create (./node_modules/tapable/lib/HookCodeFactory.js:17:12), <anonymous>:13:1)
    at SyncHook.lazyCompileHook [as _call] (./node_modules/tapable/lib/Hook.js:35:21)
    at Compiler.newCompilation (./node_modules/webpack/lib/Compiler.js:504:26)
    at hooks.beforeCompile.callAsync.err (./node_modules/webpack/lib/Compiler.js:540:29)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (./node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (./node_modules/tapable/lib/Hook.js:35:21)
    at Compiler.compile (./node_modules/webpack/lib/Compiler.js:535:28)
    at compiler.hooks.watchRun.callAsync.err (./node_modules/webpack/lib/Watching.js:77:18)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (./node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:24:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (./node_modules/tapable/lib/Hook.js:35:21)
    at Watching._go (./node_modules/webpack/lib/Watching.js:40:32)
    at Watching.compiler.readRecords.err (./node_modules/webpack/lib/Watching.js:32:9)
    at Compiler.readRecords (./node_modules/webpack/lib/Compiler.js:402:11)
    at new Watching (./node_modules/webpack/lib/Watching.js:29:17)
    at Compiler.watch (./node_modules/webpack/lib/Compiler.js:201:10)
(node:16392) DeprecationWarning: Tapable.apply is deprecated. Call apply on the plugin directly instead
    at compiler.plugin.compilation (./node_modules/babel-engine-plugin/index.js:14:31)
    at SyncHook.eval [as call] (eval at create (./node_modules/tapable/lib/HookCodeFactory.js:17:12), <anonymous>:13:1)
    at SyncHook.lazyCompileHook [as _call] (./node_modules/tapable/lib/Hook.js:35:21)
    at Compiler.newCompilation (./node_modules/webpack/lib/Compiler.js:504:26)
    at hooks.beforeCompile.callAsync.err (./node_modules/webpack/lib/Compiler.js:540:29)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (./node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (./node_modules/tapable/lib/Hook.js:35:21)
    at Compiler.compile (./node_modules/webpack/lib/Compiler.js:535:28)
    at compiler.hooks.watchRun.callAsync.err (./node_modules/webpack/lib/Watching.js:77:18)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (./node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:24:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (./node_modules/tapable/lib/Hook.js:35:21)
    at Watching._go (./node_modules/webpack/lib/Watching.js:40:32)
    at Watching.compiler.readRecords.err (./node_modules/webpack/lib/Watching.js:32:9)
    at Compiler.readRecords (./node_modules/webpack/lib/Compiler.js:402:11)
    at new Watching (./node_modules/webpack/lib/Watching.js:29:17)
    at Compiler.watch (./node_modules/webpack/lib/Compiler.js:201:10)

Less verbose logging

Currently the warning messages WARNING in The package ...doesn't specify the targeted Node.js version in its package.jsonengines field. Open an issue and ask them to do it, or even better, do a pull request. are being printed on every compilation-run when running webpack with --watch, which prints a lot of messages and pollutes the console window.

Is there currently a way to have a less verbose logging or print the warnings only once?

Full of warnings

How can I suppress or remove the following warnings? Thanks

WARNING in The package @storybook/ui doesn't specify the targeted Node.js version in its package.json engines field. Open an issue and ask them to do it, or even better, do a pull request.

WARNING in The package babel-runtime doesn't specify the targeted Node.js version in its package.json engines field. Open an issue and ask them to do it, or even better, do a pull request.

WARNING in The package fuse.js doesn't specify the targeted Node.js version in its package.json engines field. Open an issue and ask them to do it, or even better, do a pull request.

WARNING in The package @storybook/react doesn't specify the targeted Node.js version in its package.json engines field. Open an issue and ask them to do it, or even better, do a pull request.

WARNING in The package @storybook/addon-actions doesn't specify the targeted Node.js version in its package.json engines field. Open an issue and ask them to do it, or even better, do a pull request.

WARNING in The package @storybook/addon-links doesn't specify the targeted Node.js version in its package.json engines field. Open an issue and ask them to do it, or even better, do a pull request.

WARNING in The package core-js doesn't specify the targeted Node.js version in its package.json engines field. Open an issue and ask them to do it, or even better, do a pull request.
Child html-webpack-plugin for "index.html":
Asset Size Chunks Chunk Names
index.html 569 kB 0
[0] ./node_modules/@storybook/react/node_modules/html-webpack-plugin/lib/loader.js!./node_modules/@storybook/react/dist/server/index.html.ejs 1.69 kB {0} [built]
[1] ./node_modules/@storybook/react/node_modules/lodash/lodash.js 540 kB {0} [built]
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
[3] (webpack)/buildin/module.js 517 bytes {0} [built]

WARNING in The package `@storybook/react` doesn't specify the targeted Node.js version in its package.json `engines` field. Open an issue and ask them to do it, or even better, do a pull request.

WARNING in The package `lodash` doesn't specify the targeted Node.js version in its package.json `engines` field. Open an issue and ask them to do it, or even better, do a pull request.

Child html-webpack-plugin for "iframe.html":
Asset Size Chunks Chunk Names
iframe.html 568 kB 0
[0] ./node_modules/@storybook/react/node_modules/html-webpack-plugin/lib/loader.js!./node_modules/@storybook/react/dist/server/iframe.html.ejs 973 bytes {0} [built]
[1] ./node_modules/@storybook/react/node_modules/lodash/lodash.js 540 kB {0} [built]
[2] (webpack)/buildin/global.js 509 bytes {0} [built]
[3] (webpack)/buildin/module.js 517 bytes {0} [built]

Struggling to compile

I am using your plugin in combination with create-react-app-typescript, in order to compile mem in production. When adding babel-engine-plugin I am left with a very vague webpack compiler error:

Failed to compile.

chunk main [entry]
static/js/[name].[chunkhash:8].js
Cannot read property 'indexOf' of null

I've been diving into various bits of code, trying out different things, but I am running out of ideas and energy. I'm hoping anyone might recognise this and point me in the right direction.

My setup has been compiling fine without the plugin. I can still do the non-production build, because it doesn't use the Uglify plugin. It's the Uglify plugin that clashes with the syntax of mem, but I guess that is not something unusual.

Below is my config-overrides file, in case that helps.

const { injectBabelPlugin, getLoader } = require("react-app-rewired");
const path = require("path");
const BabelEnginePlugin = require("babel-engine-plugin");

module.exports = function override(config, env) {
  const babelLoader = {
    loader: "babel-loader",
    options: {
      presets: ["react-app"],
    },
  };

  const babelEnginePlugin = new BabelEnginePlugin({
    presets: ["env"],
  });

  const tsLoader = getLoader(
    config.module.rules,
    rule =>
      rule.loader &&
      typeof rule.loader === "string" &&
      rule.loader.indexOf(`${path.sep}ts-loader${path.sep}`) !== -1
  );

  if (!tsLoader) {
    throw new Error("Unable to find ts-loader in the webpack config base");
  }
  tsLoader.loader = undefined; // remove loader in favor of use
  tsLoader.use = [
    babelLoader,
    {
      loader: "ts-loader",
    },
  ];

  config.plugins.unshift(babelEnginePlugin);

  // For imports with non-relative path from /src
  config.resolve.modules = [path.resolve("src")].concat(config.resolve.modules);
  config.resolve.extensions = [".tsx", ".ts", ".json", ".js"];
  return config;
};

`babel-engine-plugin` does not use `.babelrc`

It seems babel-engine-plugin currently uses it's own babel-core version, which prevents proper use of the project's .babelrc file, in my case.

.babelrc looks like this:

{
    "presets": [
        ["@babel/preset-env", {
            "useBuiltIns": "usage",
            "loose": true,
            "modules": false
        }],
        "@babel/preset-stage-3"
    ]
}

When adding the plugin by calling new BabelEnginePlugin() it seems that the settings in the .babelrc file are ignored - not sure why, but I guess as it's using an older babel-core version, it's not working.

When adding the plugin by calling new BabelEnginePlugin({presets: ['env']}) it works "properly", but it's not using the configured presets in .babelrc either.

I'm wondering how this would work properly. Is there a way to get it to use the .babelrc settings?

Good defaults

Currently the user has to add babel-preset-env every single time. Should we bake this in and provide good defaults? I think it's always good to have good defaults.

Keyword `require` is undefined

Apparently I'm listening to the wrong compilation step because this plugin somehow gets require calls in your bundle instead of the __webpack_require function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.