M
M
MisterTw1ster2020-10-08 14:27:40
JavaScript
MisterTw1ster, 2020-10-08 14:27:40

How to include third party polyfill (classlist-polyfill) in Babel?

the script.js file is processed by running Babel with the npm run build command

here is the package.json code

{
  "name": "video-order",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": "> 0.25%, not dead"
        }
      ]
    ]
  },
  "scripts": {
    "build": "babel script -d dist"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/phillfeel/app_for_video-order.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/phillfeel/app_for_video-order/issues"
  },
  "homepage": "https://github.com/phillfeel/app_for_video-order#readme",
  "devDependencies": {
    "@babel/cli": "^7.11.6",
    "@babel/core": "^7.11.6",
    "@babel/preset-env": "^7.11.5",
    "@babel/preset-es2015": "^7.0.0-beta.53"
  },
  "dependencies": {
    "classlist-polyfill": "^1.2.0"
  }
}


Please tell me where to add the dependency with "classlist-polyfill" so that Babel also uses this package.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
ned4ded, 2020-10-08
@ned4ded

First, classlist-polyfill is not a babel plugin, cl-but it cannot be added to babel.
Secondly, the classlist polifill is a polyfill for the browser dom, not for js (the difference is that dom is an api that can be interacted with via js launchers in the browser environment). Babel polyfills js, but not dom.
Thirdly, in order for a dom-polyfill to work in a browser, it must be launched in this browser, for this it must be imported before the script that will use this polyfill. Babel is a transpiler, it does not concatenate js modules into one file, for this you need to use webpack, rollup or some other bundler.
To sum it up, you first need to set up a webpack that will bundle the js modules into a single file; then import the polyfill for the browser into script.js; then you need to run script.js through Babel to get polyfilled js (for example, webpack does this, it collects scripts into one or more files, runs files through a transpiler).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question