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

fix: not match \n when injecting esbuild helpers #8414

Merged
merged 1 commit into from
Jun 1, 2022

Conversation

ddosakura
Copy link
Contributor

Description

fixes #8412

Additional context

A plugin like this could be used to workaround it.

But, I found that the key to the bug was not rollupOptions.output.banner, but \n, the following code does not match \n correctly:

const INJECT_HELPERS_UMD_RE =
  /(.*)(\(function\([^)]*?\){.+amd.+function\([^)]*?\){"use strict";)(.*)/

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@patak-dev
Copy link
Member

Oh, good catch. This isn't a problem when not using a banner since the result is minified.

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code change looks ok to me.
But the output might be slightly different from the expected output.
The comment will be inside the wrapper function instead of outside.

iife

const MyLib=function(){"use strict";var u=Object.defineProperty;var e=Object.getOwnPropertySymbols;var i=Object.prototype.hasOwnProperty,s=Object.prototype.propertyIsEnumerable;var c=(n,t,o)=>t in n?u(n,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[t]=o,r=(n,t)=>{for(var o in t||(t={}))i.call(t,o)&&c(n,o,t[o]);if(e)for(var o of e(t))s.call(t,o)&&c(n,o,t[o]);return n};/*!
MayLib
*/function n(t){console.log(r({},"foo")),document.querySelector(t).textContent="It works"}return n}();

umd

(function(n,e){typeof exports=="object"&&typeof module<"u"?module.exports=e():typeof define=="function"&&define.amd?define(e):(n=typeof globalThis<"u"?globalThis:n||self,n.MyLib=e())})(this,function(){"use strict";var u=Object.defineProperty;var t=Object.getOwnPropertySymbols;var d=Object.prototype.hasOwnProperty,s=Object.prototype.propertyIsEnumerable;var i=(n,e,o)=>e in n?u(n,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[e]=o,f=(n,e)=>{for(var o in e||(e={}))d.call(e,o)&&i(n,o,e[o]);if(t)for(var o of t(e))s.call(e,o)&&i(n,o,e[o]);return n};/*!
MayLib
*/function n(e){console.log(f({},"foo")),document.querySelector(e).textContent="It works"}return n});

Also there are still some broken niche case. #8412 (comment)

Since it was producing invalid js before, I think it is still good to merge it.

@patak-dev
Copy link
Member

Maybe we could detect the last */ or a line that doesn't start with // in the regex? It would be good to avoid fixing this halfway.

@sapphi-red
Copy link
Member

sapphi-red commented Jun 1, 2022

I think it is difficult to cover all usecases by the current way using regex.

without rollupOptions.output.banner + build.minify: false

var MyLibrary = (function (exports) {
  'use strict';

  exports.foo = { ...'foo' }

  return exports;

})({});

with rollupOptions.output.banner + build.minify: false

var someValueInjected = Object.assign({}, { value: 'by rollup.banner' });
var MyLibrary = (function (exports) {
  'use strict';

  exports.foo = { ...'foo' }

  return exports;

})({});

with rollupOptions.output.banner + build.minify: false

var someValueInjected = Object.assign({}, { value: 'by rollup.banner' });
var u=Object.defineProperty;var i=Object.getOwnPropertySymbols;var a=Object.prototype.hasOwnProperty,c=Object.prototype.propertyIsEnumerable;var n=(r,o,f)=>o in r?u(r,o,{enumerable:!0,configurable:!0,writable:!0,value:f}):r[o]=f,t=(r,o)=>{for(var f in o||(o={}))a.call(o,f)&&n(r,f,o[f]);if(i)for(var f of i(o))c.call(o,f)&&n(r,f,o[f]);return r};var MyLibrary=function(r){"use strict";return r.foo=t({},"foo"),r}({});

without rollupOptions.output.banner + build.minify: false

var u=Object.defineProperty;var i=Object.getOwnPropertySymbols;var a=Object.prototype.hasOwnProperty,c=Object.prototype.propertyIsEnumerable;var n=(r,o,f)=>o in r?u(r,o,{enumerable:!0,configurable:!0,writable:!0,value:f}):r[o]=f,t=(r,o)=>{for(var f in o||(o={}))a.call(o,f)&&n(r,f,o[f]);if(i)for(var f of i(o))c.call(o,f)&&n(r,f,o[f]);return r};var MyLibrary=function(r){"use strict";return r.foo=t({},"foo"),r}({});

IMHO, we should override rollupOptions.output.banner to empty string and then inject it in renderChunks hook by a internal plugin which runs after vite:esbuild-transpile.

@patak-dev
Copy link
Member

You're right. Maybe we could cache the output.banner in case it is a function, and then use the value in renderChunk to skip that part directly.
Let's merge this one in the meantime as it helps with the unminified case.

@patak-dev patak-dev merged commit 5a57626 into vitejs:main Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

当构建库时使用 rollupOptions.output.banner 会导致打包出错误的 umd
3 participants