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

feat: change the behavior of html minify #2013

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/short-moles-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/core": patch
---

disabled quotes & closed tag removal behavior in html minify
2 changes: 1 addition & 1 deletion crates/compiler/tests/fixtures/minify/html/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/toolkit/src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub fn codegen_html_document(document: &Document, minify: bool) -> String {
html_writer,
CodegenConfig {
minify,
tag_omission: Some(false),
quotes: Some(true),
..Default::default()
},
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Js Plugin Execution - transformHtml 1`] = `
"<!doctype html><html lang=en><head>
<meta charset=UTF-8>
<meta name=viewport content="width=device-width, initial-scale=1.0">
"<!doctype html><html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{head} {style}
<title>Document</title>
<script>
Expand All @@ -12,8 +12,8 @@ global['f081367f80fe14896375a9f8b8918ca3'] = {
__FARM_TARGET_ENV__: 'browser',
};</script><script>const e="undefined"!=typeof globalThis?globalThis:window;class t{constructor(e,t){this.resource_pot="",this.id=e,this.exports={},this.meta={env:{}},this.require=t;}o(e,t,r){Object.defineProperty(e,t,{enumerable:!0,get:r});}d(e,t,r){this.o(e,t,function(){return r;});}_m(e){let t="__esModule";e[t]||Object.defineProperty(e,t,{value:!0});}_e(e,t){return Object.keys(t).forEach(function(r){"default"===r||Object.prototype.hasOwnProperty.call(e,r)||Object.defineProperty(e,r,{value:t[r],enumerable:!0,configurable:!0});}),t;}i(e){return e&&e.__esModule?e:{default:e};}_g(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,r=new WeakMap;return(this._g=function(e){return e?r:t;})(e);}w(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var r=this._g(t);if(r&&r.has(e))return r.get(e);var i={__proto__:null},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var s in e)if("default"!==s&&Object.prototype.hasOwnProperty.call(e,s)){var a=o?Object.getOwnPropertyDescriptor(e,s):null;a&&(a.get||a.set)?Object.defineProperty(i,s,a):i[s]=e[s];}return i.default=e,r&&r.set(e,i),i;}_(e,t,r,i){this.d(e,t,r[i||t]);}p(e,t){for(let r of Object.keys(t)){let i=e[r];i&&!Object.prototype.hasOwnProperty.call(t,i)&&this.d(t,i,t[r]);}}f(e){return void 0!==e.default?e.default:e;}}class r{constructor(e){this.plugins=[],this.plugins=e;}hookSerial(e,...t){for(let r of this.plugins){let i=r[e];i&&i.apply(r,t);}}hookBail(e,...t){for(let r of this.plugins){let i=r[e];if(i){let e=i.apply(r,t);if(e)return e;}}}}const i=global.f081367f80fe14896375a9f8b8918ca3,o="undefined"!=typeof window?window:"undefined"!=typeof global?global:{},s=i.__FARM_TARGET_ENV__||"node",a="browser"===s&&o.document;class l{constructor(e,t){this.moduleSystem=e,this._loadedResources={},this._loadingResources={},this.publicPaths=t;}load(e,t=0){if(!a){let t=this.moduleSystem.pluginContainer.hookBail("loadResource",e);if(t)return t.then(t=>{if(!t.success&&t.retryWithDefaultResourceLoader){if(0===e.type)return this._loadScript(\`./\${e.path}\`);if(1===e.type)return this._loadLink(\`./\${e.path}\`);}else if(!t.success)throw Error(\`[Farm] Failed to load resource: "\${e.path}, type: \${e.type}". Original Error: \${t.err}\`);});if(0===e.type)return this._loadScript(\`./\${e.path}\`);if(1===e.type)return this._loadLink(\`./\${e.path}\`);}let r=this.publicPaths[t],i=\`\${r.endsWith("/")?r.slice(0,-1):r}/\${e.path}\`;if(this._loadedResources[e.path])return Promise.resolve();if(this._loadingResources[e.path])return this._loadingResources[e.path];let o=this.moduleSystem.pluginContainer.hookBail("loadResource",e);return o?o.then(r=>{if(r.success)this.setLoadedResource(e.path);else if(r.retryWithDefaultResourceLoader)return this._load(i,e,t);else throw Error(\`[Farm] Failed to load resource: "\${e.path}, type: \${e.type}". Original Error: \${r.err}\`);}):this._load(i,e,t);}setLoadedResource(e,t=!0){this._loadedResources[e]=t;}isResourceLoaded(e){return this._loadedResources[e];}_load(e,t,r){let i=Promise.resolve();return 0===t.type?i=this._loadScript(e):1===t.type&&(i=this._loadLink(e)),this._loadingResources[t.path]=i,i.then(()=>{this._loadedResources[t.path]=!0,this._loadingResources[t.path]=null;}).catch(i=>{if(console.warn(\`[Farm] Failed to load resource "\${e}" using publicPath: \${this.publicPaths[r]}\`),++r<this.publicPaths.length)return this._load(e,t,r);throw this._loadingResources[t.path]=null,Error(\`[Farm] Failed to load resource: "\${t.path}, type: \${t.type}". \${i}\`);}),i;}_loadScript(e){return import(e);}_loadLink(e){return Promise.resolve();}}class n{constructor(){this.dynamicResources=[],this.modules={},this.cache={},this.publicPaths=[],this.dynamicModuleResourcesMap={},this.resourceLoader=new l(this,this.publicPaths),this.pluginContainer=new r([]),this.targetEnv=s,this.externalModules={},this.reRegisterModules=!1;}require(e,r=!1){if(this.cache[e]&&!this.pluginContainer.hookBail("readModuleCache",this.cache[e])){let t=this.cache[e];return t.initializer||t.exports;}let i=this.modules[e];if(!i){if(this.externalModules[e]){let t=this.externalModules[e];return r&&t.default||t;}return("node"===this.targetEnv||!a)&&nodeRequire?nodeRequire(e):(this.pluginContainer.hookSerial("moduleNotFound",e),console.debug(\`[Farm] Module "\${e}" is not registered\`),{});}let s=new t(e,this.require.bind(this));s.resource_pot=i.__farm_resource_pot__,this.pluginContainer.hookSerial("moduleCreated",s),this.cache[e]=s,o.require||(o.require=this.require.bind(this));let l=i(s,s.exports,this.require.bind(this),this.farmDynamicRequire.bind(this));return l&&l instanceof Promise?(s.initializer=l.then(()=>(this.pluginContainer.hookSerial("moduleInitialized",s),s.initializer=void 0,s.exports)),s.initializer):(this.pluginContainer.hookSerial("moduleInitialized",s),s.exports);}farmDynamicRequire(e){if(this.modules[e]){let t=this.require(e);return t.__farm_async?t.default:Promise.resolve(t);}return this.loadDynamicResources(e);}loadDynamicResourcesOnly(e,t=!1){let r=this.dynamicModuleResourcesMap[e].map(e=>this.dynamicResources[e]);if(!r||0===r.length)throw Error(\`Dynamic imported module "\${e}" does not belong to any resource\`);return t&&this.clearCache(e),Promise.all(r.map(e=>{if(t){let t=this.resourceLoader.isResourceLoaded(e.path);if(this.resourceLoader.setLoadedResource(e.path,!1),t)return this.resourceLoader.load({...e,path:\`\${e.path}?t=\${Date.now()}\`});}return this.resourceLoader.load(e);}));}loadDynamicResources(e,t=!1){let r=this.dynamicModuleResourcesMap[e].map(e=>this.dynamicResources[e]);return this.loadDynamicResourcesOnly(e,t).then(()=>{if(r.every(e=>0!==e.type))return;if(!this.modules[e])throw Error(\`Dynamic imported module "\${e}" is not registered.\`);let t=this.require(e);return t.__farm_async?t.default:t;}).catch(t=>{throw console.error(\`[Farm] Error loading dynamic module "\${e}"\`,t),t;});}register(e,t){if(this.modules[e]&&!this.reRegisterModules){console.warn(\`Module "\${e}" has registered! It should not be registered twice\`);return;}this.modules[e]=t;}update(e,t){this.modules[e]=t,this.clearCache(e);}delete(e){return!!this.modules[e]&&(this.clearCache(e),delete this.modules[e],!0);}getModuleUrl(e){let t=this.publicPaths[0]??"";return a?\`\${window.location.protocol}//\${window.location.host}\${t.endsWith("/")?t.slice(0,-1):t}/\${this.modules[e].__farm_resource_pot__}\`:this.modules[e].__farm_resource_pot__;}getCache(e){return this.cache[e];}clearCache(e){return!!this.cache[e]&&(delete this.cache[e],!0);}setInitialLoadedResources(e){for(let t of e)this.resourceLoader.setLoadedResource(t);}setDynamicModuleResourcesMap(e,t){this.dynamicResources=e,this.dynamicModuleResourcesMap=t;}setPublicPaths(e){this.publicPaths=e,this.resourceLoader.publicPaths=this.publicPaths;}setPlugins(e){this.pluginContainer.plugins=e;}addPlugin(e){this.pluginContainer.plugins.every(t=>t.name!==e.name)&&this.pluginContainer.plugins.push(e);}removePlugin(e){this.pluginContainer.plugins=this.pluginContainer.plugins.filter(t=>t.name!==e);}setExternalModules(e){Object.assign(this.externalModules,e||{});}bootstrap(){this.pluginContainer.hookSerial("bootstrap",this);}}i.__farm_module_system__=(function(){let e=new n;return function(){return e;};})()(),global.f081367f80fe14896375a9f8b8918ca3.__farm_module_system__.setPlugins([{name:"farm-runtime-import-meta",_moduleSystem:{},bootstrap(e){this._moduleSystem=e;},moduleCreated(t){let r;let i=this._moduleSystem.publicPaths?.[0]||"",o="node"===this._moduleSystem.targetEnv,{location:s}=e;try{r=(s?new URL(i,\`\${s.protocol}//\${s.host}\`):new URL(t.resource_pot)).pathname;}catch(e){r="/";}t.meta.env={...{NODE_ENV:"production",mode:"production"}??{},dev:"development"===process.env.NODE_ENV,prod:"production"===process.env.NODE_ENV,BASE_URL:r,SSR:o};let a=s?\`\${s.protocol}//\${s.host}\${i.replace(/\\/$/,"")}/\${t.id}?t=\${Date.now()}\`:t.resource_pot;t.meta.url=a;}}]);</script></head>
<body>
<div id=app-container>{ssr}</div>
<div id="app-container">{ssr}</div>


<script>global['f081367f80fe14896375a9f8b8918ca3'].__farm_module_system__.setInitialLoadedResources([]);</script><script>global['f081367f80fe14896375a9f8b8918ca3'].__farm_module_system__.setDynamicModuleResourcesMap([],{ });</script><script>global['f081367f80fe14896375a9f8b8918ca3'].__farm_module_system__.setPublicPaths(['./']);</script><script>global['f081367f80fe14896375a9f8b8918ca3'].__farm_module_system__.bootstrap();</script>"
<script>global['f081367f80fe14896375a9f8b8918ca3'].__farm_module_system__.setInitialLoadedResources([]);</script><script>global['f081367f80fe14896375a9f8b8918ca3'].__farm_module_system__.setDynamicModuleResourcesMap([],{ });</script><script>global['f081367f80fe14896375a9f8b8918ca3'].__farm_module_system__.setPublicPaths(['./']);</script><script>global['f081367f80fe14896375a9f8b8918ca3'].__farm_module_system__.bootstrap();</script></body></html>"
`;
Loading