From a31420c9fc6eb7601e2ac552e8af5beeab220ca7 Mon Sep 17 00:00:00 2001 From: zoomdong <1344492820@qq.com> Date: Thu, 12 Nov 2026 11:11:04 +0800 Subject: [PATCH] fix(pack): inherit source map options for inline CSS injectors --- .../shared/transforms/inline_css/module.rs | 3 ++ .../inline_css_minified_no_maps/assert.js | 50 +++++++++++++++++++ .../inline_css_minified_no_maps/config.json | 25 ++++++++++ .../input/index.js | 5 ++ .../input/style.css | 3 ++ .../output/main.min.js | 1 + 6 files changed, 87 insertions(+) create mode 100644 crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/assert.js create mode 100644 crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/config.json create mode 100644 crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/input/index.js create mode 100644 crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/input/style.css create mode 100644 crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/output/main.min.js diff --git a/crates/pack-core/src/shared/transforms/inline_css/module.rs b/crates/pack-core/src/shared/transforms/inline_css/module.rs index dae36cd60..0d8905dfe 100644 --- a/crates/pack-core/src/shared/transforms/inline_css/module.rs +++ b/crates/pack-core/src/shared/transforms/inline_css/module.rs @@ -311,6 +311,9 @@ impl InlineCssModuleType { follow_reexports: module_options_context.follow_reexports, module_fragments_enabled: module_options_context.module_fragments_enabled, mangle_export_names: module_options_context.ecmascript.mangle_export_names, + source_map_generation_options: module_options_context + .ecmascript + .source_map_generation_options, ..Default::default() } .resolved_cell(), diff --git a/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/assert.js b/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/assert.js new file mode 100644 index 000000000..b5ee9dc1f --- /dev/null +++ b/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/assert.js @@ -0,0 +1,50 @@ +const assert = require("node:assert/strict"); +const fs = require("node:fs"); +const path = require("node:path"); +const vm = require("node:vm"); + +const outputDir = path.join(__dirname, "output"); +assert.deepEqual( + fs.readdirSync(outputDir), + ["main.min.js"], + "minified inline CSS must emit its JavaScript bundle without CSS or map files", +); + +const injectedStyles = []; +const document = { + querySelector(selector) { + assert.equal(selector, "head"); + return { + appendChild(style) { + injectedStyles.push(style); + }, + }; + }, + createElement(tagName) { + assert.equal(tagName, "style"); + return { + textContent: "", + appendChild(node) { + this.textContent += node.textContent; + }, + removeAttribute(name) { + assert.equal(name, "media"); + }, + }; + }, + createTextNode(textContent) { + return { textContent }; + }, +}; + +const filename = path.join(outputDir, "main.min.js"); +const bundle = fs.readFileSync(filename, "utf8"); +const sandbox = { URL, window: {}, document }; +vm.runInNewContext(bundle, sandbox, { filename, timeout: 2000 }); + +assert.equal(sandbox.InlineCssMinifiedLibrary.getValue(), 42); +assert.equal(injectedStyles.length, 1, "the stylesheet must auto-inject"); +assert.match( + injectedStyles[0].textContent, + /\.minified-inline-css\s*\{\s*color:\s*red/, +); diff --git a/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/config.json b/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/config.json new file mode 100644 index 000000000..9c755f17b --- /dev/null +++ b/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/config.json @@ -0,0 +1,25 @@ +{ + "runtimeType": "Production", + "config": { + "entry": [ + { + "import": "input/index.js", + "name": "main", + "library": { + "name": "InlineCssMinifiedLibrary" + } + } + ], + "optimization": { + "minify": true, + "moduleIds": "named" + }, + "output": { + "filename": "main.min.js" + }, + "sourceMaps": false, + "styles": { + "inlineCss": {} + } + } +} diff --git a/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/input/index.js b/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/input/index.js new file mode 100644 index 000000000..d07d734cb --- /dev/null +++ b/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/input/index.js @@ -0,0 +1,5 @@ +import "./style.css"; + +export function getValue() { + return 42; +} diff --git a/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/input/style.css b/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/input/style.css new file mode 100644 index 000000000..106d7cd50 --- /dev/null +++ b/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/input/style.css @@ -0,0 +1,3 @@ +.minified-inline-css { + color: red; +} diff --git a/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/output/main.min.js b/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/output/main.min.js new file mode 100644 index 000000000..aec2d94b4 --- /dev/null +++ b/crates/pack-tests/tests/snapshot/library/inline_css_minified_no_maps/output/main.min.js @@ -0,0 +1 @@ +(e=>{let t,n;if(!Array.isArray(e))return;var r,i=((r=i||{})[r.Runtime=0]="Runtime",r[r.Parent=1]="Parent",r[r.Update=2]="Update",r);let o=new Map,l=new WeakMap;function s(e,t){this.m=e,this.e=t}let c=s.prototype;c.M=o;let u=Object.prototype.hasOwnProperty,a="u">typeof Symbol&&Symbol.toStringTag;function f(e,t,n){u.call(e,t)||Object.defineProperty(e,t,n)}function p(e,t){let n=e.get(t);if(n?.error)throw n.error;return n}function d(e,t){let n=e.get(t);return void 0===n&&(n=m(t),e.set(t,n)),n}function m(e){return{exports:{},error:void 0,id:e,namespaceObject:void 0}}function y(e,t,n){f(e,"__esModule",{value:!0}),a&&f(e,a,{value:"Module"});let r=0;for(;rn[r])}c.s=h,c.S=function(e,t){let n=[],r=0;for(;r{if("default"!==e){for(let t of n)if(u.call(t,e))return t}};e.exports=e.namespaceObject=new Proxy(t,{get(e,t){if(u.call(e,t)||"default"===t||"__esModule"===t)return Reflect.get(e,t);let n=r(t);return n&&Reflect.get(n,t)},set:()=>!1,defineProperty:()=>!1,deleteProperty:()=>!1,has:(e,t)=>!!Reflect.has(e,t)||"default"!==t&&"__esModule"!==t&&void 0!==r(t),ownKeys(e){let t=Reflect.ownKeys(e);for(let e of n)for(let n of Reflect.ownKeys(e))"default"===n||t.includes(n)||t.push(n);return t},getOwnPropertyDescriptor(e,t){let n=Reflect.getOwnPropertyDescriptor(e,t);if(n||"default"===t||"__esModule"===t)return n;let i=r(t);if(i)return{enumerable:!0,configurable:!0,get:()=>Reflect.get(i,t)}}})}return n}(n,r);"object"==typeof e&&null!==e&&i.push(e)},c.v=function(e,t){(null!=t?d(this.c,t):this.m).exports=e};c.n=function(e,t){let n;(n=null!=t?d(this.c,t):this.m).exports=n.namespaceObject=e};let _=Object.getPrototypeOf?e=>Object.getPrototypeOf(e):e=>e.__proto__,g=[null,_({}),_([]),_(_)];function j(e,t,n){let r=[],i=-1;for(let t=e;("object"==typeof t||"function"==typeof t)&&!g.includes(t);t=_(t))for(let n of Object.getOwnPropertyNames(t))r.push(n,function(e,t){return()=>e[t]}(e,n)),-1===i&&"default"===n&&(i=r.length-1);return n&&i>=0||(i>=0?r.splice(i,1,0,e):r.push("default",0,e)),y(t,r),t}function O(e){return"function"==typeof e?function(...t){return e.apply(this,t)}:Object.create(null)}function v(e){let t=S(e,this.m);if(t.namespaceObject)return t.namespaceObject;let n=t.exports;return t.namespaceObject=j(n,O(n),n&&n.__esModule)}function w(e){return(0,S(e,this.m).exports)(v.bind(this))}function x(e){let t=e.indexOf("#");-1!==t&&(e=e.substring(0,t));let n=e.indexOf("?");return -1!==n&&(e=e.substring(0,n)),e}c.i=v,c.A=w,c.t="function"==typeof require?require:function(){throw Error("Unexpected use of runtime require")},c.r=function(e){return S(e,this.m).exports},c.f=function(e,t=0){let n=this,r=this.m,i={};for(let o=0;oc,s=0===t?()=>void 0===c?{}:S(c,r).exports:void 0===c?()=>Promise.resolve({}):1===t?()=>Promise.resolve().then(()=>v.call(n,c)):()=>w.call(n,c)),i[e[o]]={id:l,module:s}}function o(e){if(e=x(e),u.call(i,e))return i[e].module();let t=Error(`Cannot find module '${e}'`);throw t.code="MODULE_NOT_FOUND",t}return o.keys=()=>Object.keys(i),o.resolve=e=>{if(e=x(e),u.call(i,e))return i[e].id();let t=Error(`Cannot find module '${e}'`);throw t.code="MODULE_NOT_FOUND",t},o.import=async e=>await o(e),o};let M=function(e){let t=new URL(e,"x:/"),n={};for(let e in t)n[e]=t[e];for(let t in n.href=e,n.pathname=e.replace(/[?#].*/,""),n.origin=n.protocol="",n.toString=n.toJSON=(...t)=>e,n)Object.defineProperty(this,t,{enumerable:!0,configurable:!0,value:n[t]})};M.prototype=URL.prototype,c.U=M,c.z=function(e){throw Error("dynamic usage of require is not supported")},c.g=globalThis;let P="u">typeof globalThis?globalThis.publicPath:void 0;function C(e,t,n=!1){let r;try{r=t()}catch(t){throw Error(`Failed to load external module ${e}: ${t}`)}return!n||r.__esModule?r:j(r,O(r),!0)}Object.defineProperty(c,"runtimePublicPath",{get:()=>P,set:e=>{P=e}}),c.p=function(e){if("auto"===e){let e;if(void 0!==t)return t;if("object"==typeof document){let t=document.currentScript;if(!(e=t?.src)){let t=document.getElementsByTagName("script"),n=t[t.length-1];e=n?.src}}return!e&&"function"==typeof globalThis.importScripts&&globalThis.location&&(e=String(globalThis.location)),t=e?e.replace(/^blob:/,"").replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^/]*$/,"/"):""}if("string"==typeof P){let e=P;return e.endsWith("/")?e:`${e}/`}return"/"},C.resolve=(e,t)=>require.resolve(e,t),c.x=C,c.N=function(e){if(e&&e.__esModule)return e;let t=Object.create(null),n=e&&a&&"Module"===e[a];if(e&&("object"==typeof e||"function"==typeof e))for(let r in e)"__esModule"!==r&&(n||"default"!==r)&&Object.defineProperty(t,r,{enumerable:!0,get:function(e,t){return()=>e[t]}(e,r)});return n||Object.defineProperty(t,"default",{enumerable:!0,value:e}),Object.defineProperty(t,"__esModule",{value:!0}),a&&Object.defineProperty(t,a,{value:"Module"}),t};let E=new Map;c.c=E;let S=(e,t)=>p(E,e)??T(e,i.Parent,t.id);function T(e,t,n){let r=o.get(e);if("function"!=typeof r)throw Error(function(e,t,n){let r;switch(t){case 0:r=`as a runtime entry of chunk ${n}`;break;case 1:r=`because it was required from module ${n}`;break;case 2:r="because of an HMR update";break;default:let i;throw Error(`Invariant: ${(i=t,`Unknown source type: ${i}`)}`)}return`Module ${e} was instantiated ${r}, but the module factory is not available.`}(e,t,n));let i=m(e),l=i.exports;E.set(e,i);let c=new s(i,l);try{r(c,i,l)}catch(e){throw i.error=e,e}return i.namespaceObject&&i.exports!==i.namespaceObject&&j(i.exports,i.namespaceObject),i}function N(e){let t;if(!Array.isArray(e))return n.registerChunk(void 0,e);let r=function(e){if("string"==typeof e)return e;if(e)return{src:e.getAttribute("src")};throw Error("chunk path is empty")}(e[0]);return 2===e.length?t=e[1]:(t=void 0,!function e(t,n,r){let i=n,l=t[i];for(Array.isArray(l)&&(e(l,0,r),i++);i{let r=document.createElement("script");r.src=e,r.onload=()=>t(),r.onerror=()=>n(Error(`Failed to load script: ${e}`)),document.head.appendChild(r)}),k.set(e,t)),t},n={registerChunk(e,t){if(null!=t&&t.runtimeModuleIds.length>0)for(let n of t.runtimeModuleIds)p(E,n)??T(n,i.Runtime,e)}};let R=e;function I(){let e,t=["[project]/library/inline_css_minified_no_maps/input/index.js [library-client] (ecmascript)"];for(let n=0;n{"use strict";e.i('[project]/library/inline_css_minified_no_maps/input/style.css { INLINE_CSS_CONTENT => "[project]/library/inline_css_minified_no_maps/input/style.css [library-client] (css, inline css content)" } [library-client] (inline css, ecmascript)'),e.s(["getValue",0,function(){return 42}])},'[project]/library/inline_css_minified_no_maps/input/style.css { INLINE_CSS_CONTENT => "[project]/library/inline_css_minified_no_maps/input/style.css [library-client] (css, inline css content)" } [library-client] (inline css, ecmascript)',e=>{"use strict";var t=e.i("[project]/library/inline_css_minified_no_maps/input/style.css [library-client] (css, inline css content)"),n=e.i("[@utoo/pack-runtime]/inline_css/injectStylesIntoStyleTag.js [library-client] (ecmascript)"),r={};r.insert="head",r.singleton=!1,(0,n.default)([["library/inline_css_minified_no_maps/input/style.css",t.default,void 0,void 0]],r),e.s([])},"[@utoo/pack-runtime]/inline_css/injectStylesIntoStyleTag.js [library-client] (ecmascript)",(e,t,n)=>{let r,i,o,l=(i={},function(e){if(void 0===i[e]){let t=document.querySelector(e);if(window.HTMLIFrameElement&&t instanceof window.HTMLIFrameElement)try{t=t.contentDocument.head}catch(e){t=null}i[e]=t}return i[e]}),s=[];function c(e){let t=-1;for(let n=0;ntypeof __webpack_nonce__?__webpack_nonce__:null;e&&(n.nonce=e)}if(Object.keys(n).forEach(function(e){t.setAttribute(e,n[e])}),"function"==typeof e.insert)e.insert(t);else{let n=l(e.insert||"head");if(!n)throw Error("Couldn't find a style target. This probably means that the value for the 'insert' parameter is invalid.");n.appendChild(t)}return t}let f=(o=[],function(e,t){return o[e]=t,o.filter(Boolean).join("\n")});function p(e,t,n,r){let i=n?"":r.media?"@media "+r.media+" {"+r.css+"}":r.css;if(e.styleSheet)e.styleSheet.cssText=f(t,i);else{let n=document.createTextNode(i),r=e.childNodes;r[t]&&e.removeChild(r[t]),r.length?e.insertBefore(n,r[t]):e.appendChild(n)}}function d(e,t,n){let r=n.css,i=n.media,o=n.sourceMap;if(i?e.setAttribute("media",i):e.removeAttribute("media"),o&&"u">typeof btoa&&(r+="\n/*# sourceMappingURL=data:application/json;base64,"+btoa(unescape(encodeURIComponent(JSON.stringify(o))))+" */"),e.styleSheet)e.styleSheet.cssText=r;else{for(;e.firstChild;)e.removeChild(e.firstChild);e.appendChild(document.createTextNode(r))}}let m=null,y=0;t.exports=function(e,t){(t=t||{}).singleton||"boolean"==typeof t.singleton||(t.singleton=(void 0===r&&(r=!!(window&&document&&document.all&&!window.atob)),r));let n=u(e=e||[],t);return function(e){if(e=e||[],"[object Array]"!==Object.prototype.toString.call(e))return;for(let e=0;e