Skip to content

Wrong cjs global module variable generated in esm files #1028

@Mister-Hope

Description

@Mister-Hope
Image

Vite 8 has replaced esbuild and rollup with rolldown powered by oxc (log here):

demo/components demo:vite-build: [COMMONJS_VARIABLE_IN_ESM] Warning: The CommonJS `module` variable is treated as a global variable in an ECMAScript module and may not work as expected
demo/components demo:vite-build:  -  in ../../node_modules/.pnpm/artplayer-plugin-danmuku@5.2.0/node_modules/artplayer-plugin-danmuku/dist/artplayer-plugin-danmuku.mjs at 1377..1383
demo/components demo:vite-build:    ╭─[ ../../node_modules/.pnpm/artplayer-plugin-danmuku@5.2.0/node_modules/artplayer-plugin-danmuku/dist/artplayer-plugin-danmuku.mjs:8:52778 ]
demo/components demo:vite-build:    │
demo/components demo:vite-build:  8 │ !function(e,t,i,a,n,o,s,l){var r="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{},d="function"==typeof r[a]&&r[a],p=d.i||{},u=d.cache||{},h="undefined"!=typeof module&&"function"==typeof module.require&&module.require.bind(module);function m(t,i){if(!u[t]){if(!e[t]){if(n[t])return n[t];var o="function"==typeof r[a]&&r[a];if(!i&&o)return o(t,!0);if(d)return d(t,!0);if(h&&"string"==typeof t)return h(t);var s=Error("Cannot find module '"+t+"'");throw s.code="MODULE_NOT_FOUND",s}p.resolve=function(i){var a=e[t][1][i];return null!=a?a:i},p.cache={};var l=u[t]=new m.Module(t);e[t][0].call(l.exports,p,l,l.exports,r)}return u[t].exports;function p(e){var t=p.resolve(e);return!1===t?{}:m(t)}}m.isParcelRequire=!0,m.Module=function(e){this.id=e,this.bundle=m,this.require=h,this.exports={}},m.modules=e,m.cache=u,m.parent=d,m.distDir=void 0,m.publicUrl=void 0,m.devServer=void 0,m.i=p,m.register=function(t,i){e[t]=[function(e,t){t.exports=i},{}]},Object.defineProperty(m,"root",{get:function(){return r[a]}}),r[a]=m;for(var c=0;c<t.length;c++)m(t[c]);if(i){var f=m(i);"object"==typeof exports&&"undefined"!=typeof module?module.exports=f:"function"==typeof define&&define.amd&&define(function(){return f})}}({"7ICE7":[function(e,t,i,a){var n=e("@parcel/transformer-js/src/esmodule-helpers.js");n.defineInteropFlag(i),n.export(i,"default",()=>u);var o=e("./danmuku"),s=n.interopDefault(o),l=e("./heatmap"),r=n.interopDefault(l),d=e("./setting"),p=n.interopDefault(d);function u(e){return t=>{let i=new(0,s.default)(t,e),a=new(0,p.default)(t,i);return i.option.heatmap&&(0,r.default)(t,i,i.option.heatmap),{name:"artplayerPluginDanmuku",emit:i.emit.bind(i),load:i.load.bind(i),config:i.config.bind(i),hide:i.hide.bind(i),show:i.show.bind(i),reset:i.reset.bind(i),mount:a.mount.bind(a),get option(){return i.option},get isHide(){return i.isHide},get isStop(){return i.isStop}}}}u.icons=p.default.icons,"undefined"!=typeof window&&(window.artplayerPluginDanmuku=u)},{"./danmuku":"9bpce","./heatmap":"9KNY2","./setting":"94xSe","@parcel/transformer-js/src/esmodule-helpers.js":"loqXi"}],"9bpce":[function(e,t,i,a){var n=e("@parcel/transformer-js/src/esmodule-helpers.js");n.defineInteropFlag(i);var o=e("bundle-text:./worker"),s=n.interopDefault(o),l=e("./bilibili");class r{constructor(e,t){let{constructor:i,template:a}=e;this.utils=i.utils,this.validator=i.validator,this.$danmuku=a.$danmuku,this.$player=a.$player,this.art=e,this.queue=[],this.$refs=[],this.isStop=!1,this.isHide=!1,this.timer=null,this.index=0,this.option=r.option,this.states={wait:[],ready:[],emit:[],stop:[]},this.config(t,!0);let n=new Blob([s.default.replace("export{e as default};","")],{type:"application/javascript"});this.worker=new Worker(URL.createObjectURL(n)),this.start=t�

Oxc will throw a warning about this (and end up building in CI), as there shall not be any CommonJS variables in ESM files.

Currently I can try to bypass this with the following:

export default defineConfig({
  build: {
    rolldownOptions: {
      onLog(level, log, defaultHandler) {
        if (log.code === "COMMONJS_VARIABLE_IN_ESM" && log.id.includes("artplayer")) return;
        defaultHandler(level, log);
      },
    },
  },
});

But to make your package work with the coming Vite 8, you MUST fix your packing logic with ESM files.

Note: tested with vite@8.0.0-beta.16, issue appearing in artplayer and all plugins

Metadata

Metadata

Assignees

No one assigned

    Labels

    Ready for releaseReady for releasebugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions