diff --git a/packages/core/src/asset/assetConfig.ts b/packages/core/src/asset/assetConfig.ts index 0c5cbf9a1..514b70b8f 100644 --- a/packages/core/src/asset/assetConfig.ts +++ b/packages/core/src/asset/assetConfig.ts @@ -13,14 +13,15 @@ const RSBUILD_SVGR_PLUGIN_NAME = 'rsbuild:svgr'; /** * Be compatible to css-extract importModule and experimentalLibPreserveExports * when set experimentalLibPreserveExports to true, the css-loader result can not executed in node side, so clone the assets rule - * 1. js assets: original rule set issuer and experimentalLibPreserveExports: true - * 2. css assets: a copy of original rule + * 1. js assets: original rule set issuer and experimentalLibPreserveExports: true, `import url from './foo.svg'` + * 2. css assets: a copy of original rule, `url('./foo.svg')` */ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({ name: PLUGIN_NAME, pre: [RSBUILD_SVGR_PLUGIN_NAME], setup(api) { api.modifyBundlerChain((config, { CHAIN_ID }) => { + // for separating css assets and js assets // 1. modify svg rule first, svg is special because of svgr const svgAssetRule = config.module.rules .get(CHAIN_ID.RULE.SVG) @@ -92,14 +93,17 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({ } // for svgr + const isUsingSvgr: boolean = config.module + .rule(CHAIN_ID.RULE.SVG) + .oneOf(CHAIN_ID.RULE.SVG) + .uses.has(CHAIN_ID.USE.SVGR); + const isUsingSvgrUrlLoader = config.module + .rule(CHAIN_ID.RULE.SVG) + .oneOf(CHAIN_ID.ONE_OF.SVG) + .uses.has(CHAIN_ID.USE.URL); + // 1. remove __webpack_require__.p in svgr url-loader and file-loader - const isUsingSvgr = Boolean( - config.module - .rule(CHAIN_ID.RULE.SVG) - .oneOf(CHAIN_ID.RULE.SVG) - .uses.has(CHAIN_ID.USE.SVGR), - ); - if (isUsingSvgr) { + if (isUsingSvgr && isUsingSvgrUrlLoader) { const urlLoaderRule = config.module .rule(CHAIN_ID.RULE.SVG) .oneOf(CHAIN_ID.ONE_OF.SVG) @@ -113,8 +117,8 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({ }); config.plugin(LibSvgrPatchPlugin.name).use(LibSvgrPatchPlugin, []); } - // 2. in bundleless, only support transform the svg asset to mixedImport svgr file - // remove issuer to make every svg asset is transformed + // 2. in bundleless, do not support queryImport + // remove issuer to make every svg asset is transformed by svgr-loader if (!bundle) { if (isUsingSvgr) { const rule = config.module diff --git a/tests/integration/asset/__snapshots__/index.test.ts.snap b/tests/integration/asset/__snapshots__/index.test.ts.snap index 4193dd100..800bcb601 100644 --- a/tests/integration/asset/__snapshots__/index.test.ts.snap +++ b/tests/integration/asset/__snapshots__/index.test.ts.snap @@ -37,6 +37,224 @@ export { draft_rslib_entry_namespaceObject as default }; " `; +exports[`use svgr > mixedImport: true should contain export { url as default, ReactComponent } 1`] = ` +"import __rslib_svgr_url__0__ from "../static/svg/logo.svg"; +import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__ from "react/jsx-runtime"; +import "react"; +const SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("svg", { + xmlns: "http://www.w3.org/2000/svg", + viewBox: "0 0 841.9 595.3", + ...props, + children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsxs)("g", { + fill: "#61DAFB", + children: [ + /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("path", { + d: "M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3m-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9m-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9m32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1M421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32m-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24q7.05 12 14.4 23.4M420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32m-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9m-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6s22.9-35.6 58.3-50.6c8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2M310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7m237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1m38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6M320.8 78.4" + }), + /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("circle", { + cx: 420.9, + cy: 296.5, + r: 45.7 + }), + /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("path", { + d: "M520.5 78.1" + }) + ] + }) + }); +const logo_rslib_entry_ = __rslib_svgr_url__0__; +export { SvgLogo as ReactComponent, logo_rslib_entry_ as default }; +" +`; + +exports[`use svgr > mixedImport: true should contain export { url as default, ReactComponent } 2`] = ` +""use strict"; +var __webpack_require__ = {}; +(()=>{ + __webpack_require__.d = function(exports1, definition) { + for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, { + enumerable: true, + get: definition[key] + }); + }; +})(); +(()=>{ + __webpack_require__.o = function(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); + }; +})(); +(()=>{ + __webpack_require__.r = function(exports1) { + if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, { + value: 'Module' + }); + Object.defineProperty(exports1, '__esModule', { + value: true + }); + }; +})(); +var __webpack_exports__ = {}; +__webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + ReactComponent: ()=>SvgLogo, + default: ()=>logo_rslib_entry_ +}); +const jsx_runtime_namespaceObject = require("react/jsx-runtime"); +require("react"); +const SvgLogo = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("svg", { + xmlns: "http://www.w3.org/2000/svg", + viewBox: "0 0 841.9 595.3", + ...props, + children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("g", { + fill: "#61DAFB", + children: [ + /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("path", { + d: "M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3m-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9m-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9m32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1M421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32m-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24q7.05 12 14.4 23.4M420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32m-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9m-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6s22.9-35.6 58.3-50.6c8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2M310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7m237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1m38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6M320.8 78.4" + }), + /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("circle", { + cx: 420.9, + cy: 296.5, + r: 45.7 + }), + /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("path", { + d: "M520.5 78.1" + }) + ] + }) + }); +const logo_rslib_entry_ = require("../static/svg/logo.svg"); +var __webpack_export_target__ = exports; +for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__]; +if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', { + value: true +}); +" +`; + +exports[`use svgr > should only contain svgr default export 1`] = ` +"import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__ from "react/jsx-runtime"; +import "react"; +const SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("svg", { + xmlns: "http://www.w3.org/2000/svg", + viewBox: "0 0 841.9 595.3", + ...props, + children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsxs)("g", { + fill: "#61DAFB", + children: [ + /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("path", { + d: "M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3m-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9m-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9m32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1M421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32m-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24q7.05 12 14.4 23.4M420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32m-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9m-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6s22.9-35.6 58.3-50.6c8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2M310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7m237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1m38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6M320.8 78.4" + }), + /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("circle", { + cx: 420.9, + cy: 296.5, + r: 45.7 + }), + /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("path", { + d: "M520.5 78.1" + }) + ] + }) + }); +const logo_rslib_entry_ = SvgLogo; +export { logo_rslib_entry_ as default }; +" +`; + +exports[`use svgr > should only contain svgr default export 2`] = ` +""use strict"; +var __webpack_require__ = {}; +(()=>{ + __webpack_require__.d = function(exports1, definition) { + for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, { + enumerable: true, + get: definition[key] + }); + }; +})(); +(()=>{ + __webpack_require__.o = function(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); + }; +})(); +(()=>{ + __webpack_require__.r = function(exports1) { + if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, { + value: 'Module' + }); + Object.defineProperty(exports1, '__esModule', { + value: true + }); + }; +})(); +var __webpack_exports__ = {}; +__webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + default: ()=>logo_rslib_entry_ +}); +const jsx_runtime_namespaceObject = require("react/jsx-runtime"); +require("react"); +const SvgLogo = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("svg", { + xmlns: "http://www.w3.org/2000/svg", + viewBox: "0 0 841.9 595.3", + ...props, + children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("g", { + fill: "#61DAFB", + children: [ + /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("path", { + d: "M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3m-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9m-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9m32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1M421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32m-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24q7.05 12 14.4 23.4M420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32m-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9m-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6s22.9-35.6 58.3-50.6c8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2M310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7m237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1m38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6M320.8 78.4" + }), + /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("circle", { + cx: 420.9, + cy: 296.5, + r: 45.7 + }), + /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("path", { + d: "M520.5 78.1" + }) + ] + }) + }); +const logo_rslib_entry_ = SvgLogo; +var __webpack_export_target__ = exports; +for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__]; +if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', { + value: true +}); +" +`; + +exports[`use svgr > should only contain url default export 1`] = ` +"import logo2_rslib_entry_namespaceObject from "../static/svg/logo2.svg"; +export { logo2_rslib_entry_namespaceObject as default }; +" +`; + +exports[`use svgr > should only contain url default export 2`] = ` +""use strict"; +var __webpack_modules__ = { + "./src/assets/logo2.svg?__rslib_entry__": function(module) { + module.exports = require("../static/svg/logo2.svg"); + } +}; +var __webpack_module_cache__ = {}; +function __webpack_require__(moduleId) { + var cachedModule = __webpack_module_cache__[moduleId]; + if (void 0 !== cachedModule) return cachedModule.exports; + var module = __webpack_module_cache__[moduleId] = { + exports: {} + }; + __webpack_modules__[moduleId](module, module.exports, __webpack_require__); + return module.exports; +} +var __webpack_exports__ = __webpack_require__("./src/assets/logo2.svg?__rslib_entry__"); +var __webpack_export_target__ = exports; +for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__]; +if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', { + value: true +}); +" +`; + exports[`use svgr 1`] = ` "import __rslib_svgr_url__0__ from "./static/svg/logo.svg"; import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__ from "react/jsx-runtime"; @@ -150,41 +368,3 @@ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_targe }); " `; - -exports[`use svgr 3`] = ` -"import * as __WEBPACK_EXTERNAL_MODULE__assets_logo_js_450929b7__ from "./assets/logo.js"; -console.log('namedImport', 'ReactComponent', __WEBPACK_EXTERNAL_MODULE__assets_logo_js_450929b7__.ReactComponent); -" -`; - -exports[`use svgr 4`] = ` -"import * as __WEBPACK_EXTERNAL_MODULE__assets_logo_js_450929b7__ from "./assets/logo.js"; -console.log('namedImport', 'ReactComponent', __WEBPACK_EXTERNAL_MODULE__assets_logo_js_450929b7__.ReactComponent); -" -`; - -exports[`use svgr 5`] = ` -""use strict"; -var __webpack_exports__ = {}; -const logo_cjs_namespaceObject = require("./assets/logo.cjs"); -console.log('namedImport', 'ReactComponent', logo_cjs_namespaceObject.ReactComponent); -var __webpack_export_target__ = exports; -for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__]; -if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', { - value: true -}); -" -`; - -exports[`use svgr 6`] = ` -""use strict"; -var __webpack_exports__ = {}; -const logo_cjs_namespaceObject = require("./assets/logo.cjs"); -console.log('namedImport', 'ReactComponent', logo_cjs_namespaceObject.ReactComponent); -var __webpack_export_target__ = exports; -for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__]; -if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', { - value: true -}); -" -`; diff --git a/tests/integration/asset/index.test.ts b/tests/integration/asset/index.test.ts index 0f0b87298..15cdc6a49 100644 --- a/tests/integration/asset/index.test.ts +++ b/tests/integration/asset/index.test.ts @@ -284,29 +284,46 @@ test('use svgr', async () => { const { content: indexCjs } = queryContent(contents.cjs0!, /index\.cjs/); expect(indexCjs).matchSnapshot(); - // 1. bundleless + // 1. bundleless mixedImport // esm - const { content: namedImportJs } = queryContent( - contents.esm1!, - /namedImport\.js/, + const { content: logoJs } = queryContent(contents.esm1!, /assets\/logo\.js/); + expect(logoJs).toMatchSnapshot( + 'mixedImport: true should contain export { url as default, ReactComponent }', ); - expect(namedImportJs).toMatchSnapshot(); - const { content: defaultImportJs } = queryContent( - contents.esm1!, - /namedImport\.js/, - ); - expect(defaultImportJs).toMatchSnapshot(); // cjs - const { content: namedImportCjs } = queryContent( + const { content: logoCjs } = queryContent( contents.cjs1!, - /namedImport\.cjs/, + /assets\/logo\.cjs/, ); - expect(namedImportCjs).toMatchSnapshot(); - const { content: defaultImportCjs } = queryContent( - contents.cjs1!, - /namedImport\.cjs/, + expect(logoCjs).toMatchSnapshot( + 'mixedImport: true should contain export { url as default, ReactComponent }', + ); + + // 2. bundleless only svgr + const { content: svgrLogoJs } = queryContent( + contents.esm2!, + /assets\/logo\.js/, + ); + expect(svgrLogoJs).toMatchSnapshot('should only contain svgr default export'); + const { content: urlLogoJs } = queryContent( + contents.esm2!, + /assets\/logo2\.js/, + ); + expect(urlLogoJs).toMatchSnapshot('should only contain url default export'); + + // cjs + const { content: svgrLogoCjs } = queryContent( + contents.cjs2!, + /assets\/logo\.cjs/, + ); + expect(svgrLogoCjs).toMatchSnapshot( + 'should only contain svgr default export', + ); + const { content: urlLogoCjs } = queryContent( + contents.cjs2!, + /assets\/logo2\.cjs/, ); - expect(defaultImportCjs).toMatchSnapshot(); + expect(urlLogoCjs).toMatchSnapshot('should only contain url default export'); }); test('use asset/source', async () => { diff --git a/tests/integration/asset/svgr/rslib.config.ts b/tests/integration/asset/svgr/rslib.config.ts index 987972fca..a19c5d527 100644 --- a/tests/integration/asset/svgr/rslib.config.ts +++ b/tests/integration/asset/svgr/rslib.config.ts @@ -32,13 +32,13 @@ export default defineConfig({ }), ], }), - // 1. bundleless + // 1. bundleless mixedImport // esm generateBundleEsmConfig({ bundle: false, output: { distPath: { - root: './dist/esm/bundleless-default', + root: './dist/esm/bundleless-mixed', }, }, plugins: [ @@ -52,7 +52,7 @@ export default defineConfig({ bundle: false, output: { distPath: { - root: './dist/cjs/bundleless-default', + root: './dist/cjs/bundleless-mixed', }, }, plugins: [ @@ -61,6 +61,41 @@ export default defineConfig({ }), ], }), + // 2. bundleless only svgr + // esm + generateBundleEsmConfig({ + bundle: false, + output: { + distPath: { + root: './dist/esm/bundleless-only-svgr', + }, + }, + plugins: [ + pluginSvgr({ + svgrOptions: { + exportType: 'default', + }, + exclude: /logo2\.svg$/, + }), + ], + }), + // cjs + generateBundleCjsConfig({ + bundle: false, + output: { + distPath: { + root: './dist/cjs/bundleless-only-svgr', + }, + }, + plugins: [ + pluginSvgr({ + svgrOptions: { + exportType: 'default', + }, + exclude: /logo2\.svg$/, + }), + ], + }), ], output: { target: 'web', diff --git a/tests/integration/asset/svgr/src/assets/logo2.svg b/tests/integration/asset/svgr/src/assets/logo2.svg new file mode 100644 index 000000000..6b60c1042 --- /dev/null +++ b/tests/integration/asset/svgr/src/assets/logo2.svg @@ -0,0 +1,7 @@ + + + + + + +