Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File polyfills-legacy should not be in strict mode (Plugin legacy) #6427

Closed
7 tasks done
pepa-linha opened this issue Jan 10, 2022 · 6 comments
Closed
7 tasks done

File polyfills-legacy should not be in strict mode (Plugin legacy) #6427

pepa-linha opened this issue Jan 10, 2022 · 6 comments
Labels

Comments

@pepa-linha
Copy link

pepa-linha commented Jan 10, 2022

Describe the bug

For older browsers with IE 11 support generated file polyfills-legacy.[hash].js should not contain use strict directive, but it does.

It is required by polyfill regenerator-runtime/runtime which contains a comment: This module should not be running in strict mode, ... (https://github.com/facebook/regenerator/blob/main/packages/runtime/runtime.js#L736)

Because the built-in file contains use strict, it will result in an error.

Even if I have set

        rollupOptions: {
            output: {
                strict: false // no strict mode, ... but maybe Rollup is wrong, I am not sure
            }
        },

Reproduction

https://stackblitz.com/edit/vite-v5p4en

System Info

System:
    OS: Linux undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: Unknown - /bin/jsh
  Binaries:
    Node: 14.16.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /bin/yarn
    npm: 7.17.0 - /bin/npm
  npmPackages:
    @vitejs/plugin-legacy: ^1.6.4 => 1.6.4 
    vite: ^2.6.0 => 2.7.10

Used Package Manager

npm

Logs

No response

Validations

@pepa-linha pepa-linha changed the title File polyfills-legacy contains should not be in strict mode (Plugin legacy) File polyfills-legacy should not be in strict mode (Plugin legacy) Jan 10, 2022
@upcccz
Copy link

upcccz commented Apr 21, 2022

Have you solved it? Or another way?

@pepa-linha
Copy link
Author

pepa-linha commented Apr 21, 2022

I solved it in some ugly way that I wrote my own Vite plugin.

import path from 'path'
import MagicString from 'magic-string'

let config

/**
 * Remove "use strict" from code
 * @param {string} code
 * @param {boolean} generateSourceMap
 * @return {{code: string, map: SourceMap|null}}
 */
export const removeUseStrict = (code, generateSourceMap) => {
    const useStrictCode = 'use strict'

    if (code.includes(useStrictCode)) {
        const indexes = [
            ...code.matchAll(useStrictCode),
        ].map(occurrence => occurrence.index)

        if (indexes.length) {
            const ms = new MagicString(code)

            indexes.forEach(index => ms.overwrite(index, index + useStrictCode.length, ''))

            return {
                code: ms.toString(),
                map: generateSourceMap ? ms.generateMap({ hires: true }) : null,
            }
        }
    }

    return {
        code,
        map: null,
    }
}

// Remove 'use strict' from chunks due legacy Chrome browsers
const vitePluginRemoveStrictMode = {
    name: 'remove-strict-mode',
    enforce: 'post',
    apply: 'build',

    configResolved (_config) {
        config = _config
    },

    renderChunk (code, chunk) {
        if (path.extname(chunk.fileName) !== '.js') {
            return null
        }

        const removedUseStrictResult = removeUseStrict(code, config.build.sourcemap !== false)

        if (removedUseStrictResult.code === code) {
            return null
        }

        if (removedUseStrictResult.map !== null) {
            return {
                code: removedUseStrictResult.code,
                map: removedUseStrictResult.map,
            }
        } else {
            return removedUseStrictResult.code
        }
    },
}

export default vitePluginRemoveStrictMode

@upcccz
Copy link

upcccz commented Apr 22, 2022

I tried too, but because of using transform hook or other hooks, not renderChunk hook , it didn't succeed, so thank you very much for sharing

@upcccz
Copy link

upcccz commented Apr 22, 2022

sometimes you need to use generateBundle hook, such as dealing with polyfills-legacy.js

@sapphi-red
Copy link
Member

I was not able to reproduce it with IE11.

Also regenerator-runtime/runtime states it works if CSP is not enabled. So if CSP is not set, I think it would work.
https://github.com/facebook/regenerator/blob/055b4030219f7cc0af5999ef4431c991697d7ee5/packages/runtime/runtime.js#L739-L748

@sapphi-red sapphi-red added the cannot reproduce The bug cannot be reproduced label May 23, 2022
@bluwy
Copy link
Member

bluwy commented Dec 30, 2022

The latest plugin-legacy doesn't require adding regenerator-runtime anymore (#8007), so I think this is resolved. https://stackblitz.com/edit/vite-h5edus?file=vite.config.js

There is a separate issue with setting output.strict: false but I think that's a separate issue. Running the repro in Ie11 should be working.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Dec 30, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jan 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants