scottcharlesworth / laravel-mix-polyfill

A Laravel Mix extension to include polyfills by using Babel, core-js, and regenerator-runtime
MIT License
50 stars 7 forks source link

[Question] laravel-mix-polyfill finds the correct transform but it is not applied #10

Closed dotnetCarpenter closed 5 years ago

dotnetCarpenter commented 5 years ago

@scottcharlesworth Thank you for creating this.

I have a question though because I can not get it to work properly.

I'm using template literals and classes and with the debug flag, I seems that laravel-mix-polyfill also detects that but the transforms are never applied. However transforms like proposal-async-generator-functions and proposal-object-rest-spread are.

In my webpack.mix.js I have:

mix.js('resources/assets/js/app.js', 'public/js')
  .polyfill({
    enabled: true,
    useBuiltIns: 'usage',
    targets: '> 1%, not IE < 11',
    corejs: 3,
    debug: true
  })

Output:

npm run dev

> @1.6.1 dev /home/dotnet/napp/newPDFViewer
> npm run development

> @1.6.1 development /home/dotnet/napp/newPDFViewer
> cross-env NODE_ENV=development webpack --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js

Compiling for development
 10% building 1/2 modules 1 active ...newPDFViewer/resources/assets/js/app.js@babel/preset-env: `DEBUG` option

Using targets:
{
  "chrome": "71",
  "edge": "17",
  "firefox": "65",
  "ie": "11",
  "ios": "12",
  "safari": "12",
  "samsung": "8.2"
}

Using modules transform: auto

Using plugins:
  transform-template-literals { "ie":"11", "ios":"12", "safari":"12" }
  transform-literals { "ie":"11" }
  transform-function-name { "edge":"17", "ie":"11" }
  transform-arrow-functions { "ie":"11" }
  transform-classes { "ie":"11" }
  transform-object-super { "ie":"11" }
  transform-shorthand-properties { "ie":"11" }
  transform-duplicate-keys { "ie":"11" }
  transform-computed-properties { "ie":"11" }
  transform-for-of { "ie":"11" }
  transform-sticky-regex { "ie":"11" }
  transform-dotall-regex { "edge":"17", "firefox":"65", "ie":"11" }
  transform-unicode-regex { "ie":"11" }
  transform-spread { "ie":"11" }
  transform-parameters { "edge":"17", "ie":"11" }
  transform-destructuring { "edge":"17", "ie":"11" }
  transform-block-scoping { "ie":"11" }
  transform-typeof-symbol { "ie":"11" }
  transform-new-target { "ie":"11" }
  transform-regenerator { "ie":"11" }
  transform-exponentiation-operator { "ie":"11" }
  transform-async-to-generator { "ie":"11" }
  proposal-async-generator-functions { "edge":"17", "ie":"11" }
  proposal-object-rest-spread { "edge":"17", "ie":"11" }
  proposal-unicode-property-regex { "edge":"17", "firefox":"65", "ie":"11", "samsung":"8.2" }
  proposal-json-strings { "edge":"17", "ie":"11", "samsung":"8.2" }
  proposal-optional-catch-binding { "edge":"17", "ie":"11", "samsung":"8.2" }
  transform-named-capturing-groups-regex { "edge":"17", "firefox":"65", "ie":"11", "samsung":"8.2" }
/***/ "./resources/assets/js/save-warning/index.mjs":
/*!****************************************************!*\
  !*** ./resources/assets/js/save-warning/index.mjs ***!
  \****************************************************/
/*! exports provided: template, Commander */
/***/ (function(__webpack_module__, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "template", function() { return template; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "Commander", function() { return Commander; });
const template = `<style>
  .save-warning {
    position: absolute;
    z-index: 100;

    top: 0;
    left: 50%;
    max-height: 50%;
    transform: translateX(-50%);

    opacity: 1;
    transition: 500ms opacity;
  }
  .save-warning__icon {
    height: 20vh;
    width: 100%;
    margin: 0 auto;
  }
  .save-warning__text {
    padding: 1em;
    margin: 0.2em;

    background: #0d1b2a;
    border-radius: 5px;

    color: #f57900;
    font-size: 2.5em;
    text-align: center;
  }
  .save-warning__emphasis {
    display: block;
  }
  .save-warning--hide {
    opacity: 0;
    pointer-events: none;
  }
</style>
<section class="save-warning">
  <img class="save-warning__icon" src="/images/svg/warning.svg">
  <p class="save-warning__text">
    <strong>The PDF will not work if you save this page locally.</strong>
    <em class="save-warning__emphasis">use the download button</em>
  </p>
</section>`

class Invoker {
  constructor (name, seq, action) {
    this.name = name
    this.seq = seq
    this.execute = action
    this.seqNumber = 0
  }

  check (event) {
    const code = this.seq[this.seqNumber].code
    const ctrl = this.seq[this.seqNumber].ctrl || false

    if (
      event.code === code &&
      (event.ctrlKey || event.metaKey) === ctrl
    ) {
      return true
    } else {
      return false
    }
  }

  sequenceMatching (event) {
    if (this.check(event)) {
      if (this.seqNumber === this.seq.length - 1) {
        this.execute(event)
        this.seqNumber = 0
      } else {
        this.seqNumber += 1
      }
    } else {
      this.seqNumber = 0
    }
  }

  matching (event) {
    if (this.check(event)) {
      this.execute(event)
    }
  }
}
// etc.

I have node_modules/@babel/plugin-transform-template-literals etc, so I don't think I'm missing babel modules.

@scottcharlesworth Do you have an idea to what I'm missing or how to debug this?

Any help is greatly appreciated!

dotnetCarpenter commented 5 years ago

Looking a bit close to the terminal output, I seems that laravel-mix-polyfill does not find the correct transforms.

[/home/dotnet/napp/newPDFViewer/resources/assets/js/save-warning/index.js] Added following core-js polyfills:
  es.function.name { "ie":"11" }
  web.dom-collections.for-each { "ie":"11" }

And the original file is:

export const template =
`<style>
  .save-warning {
    position: absolute;
    z-index: 100;

    top: 0;
    left: 50%;
    max-height: 50%;
    transform: translateX(-50%);

    opacity: 1;
    transition: 500ms opacity;
  }
  .save-warning__icon {
    height: 20vh;
    width: 100%;
    margin: 0 auto;
  }
  .save-warning__text {
    padding: 1em;
    margin: 0.2em;

    background: #0d1b2a;
    border-radius: 5px;

    color: #f57900;
    font-size: 2.5em;
    text-align: center;
  }
  .save-warning__emphasis {
    display: block;
  }
  .save-warning--hide {
    opacity: 0;
    pointer-events: none;
  }
</style>
<section class="save-warning">
  <img class="save-warning__icon" src="/images/svg/warning.svg">
  <p class="save-warning__text">
    <strong>The PDF will not work if you save this page locally.</strong>
    <em class="save-warning__emphasis">use the download button</em>
  </p>
</section>`

class Invoker {
  constructor (name, seq, action) {
    this.name = name
    this.seq = seq
    this.execute = action
    this.seqNumber = 0
  }

  check (event) {
    const code = this.seq[this.seqNumber].code
    const ctrl = this.seq[this.seqNumber].ctrl || false

    if (
      event.code === code &&
      (event.ctrlKey || event.metaKey) === ctrl
    ) {
      return true
    } else {
      return false
    }
  }

  sequenceMatching (event) {
    if (this.check(event)) {
      if (this.seqNumber === this.seq.length - 1) {
        this.execute(event)
        this.seqNumber = 0
      } else {
        this.seqNumber += 1
      }
    } else {
      this.seqNumber = 0
    }
  }

  matching (event) {
    if (this.check(event)) {
      this.execute(event)
    }
  }
}

export class Commander {
  constructor () {
    this.commands = []
  }

  addCommand (name, seq, action) {
    this.commands.push(new Invoker(name, seq, action))
  }

  keyHandler (event) {
    this.commands.forEach(c => {
      if (c.seq.length > 1) {
        c.sequenceMatching(event)
      } else {
        c.matching(event)
      }
    })
  }
}
dotnetCarpenter commented 5 years ago

Actually, on second thought. This is not an issue with a missing polyfil but with missing transforms. I'll probably need to dig somewhere else

dotnetCarpenter commented 5 years ago

I'm trying to create a simple reproduction, so far it works fine without webpack, using @babel/cli. I'll post when I find something...

dotnetCarpenter commented 5 years ago

I found the issue. Some of my files have .mjs extension to be able to run in nodejs.

See here for more info: https://github.com/dotnetCarpenter/babel-transform-issue#readme