vite-plugin-generate-file
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,1 +0,1 @@ | ||
"use strict";var e=require("fs"),t=require("path"),n=require("chalk"),r=require("js-yaml"),l=require("ejs");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=i(e),o=i(t),s=i(r),u=i(l);function f(e){const t=o.default.dirname(e);a.default.existsSync(t)||(f(t),a.default.mkdirSync(t))}let c,d;const h=new Map;function p(e){const n={output:"./output.txt",type:"json",template:"",...e},r=t.resolve(d,n.output),l=`/${t.relative(d,r)}`;return{...n,fullPath:r,relativePath:l}}function y(n){if(!n.type)return"";if("json"===n.type)return n.data?JSON.stringify(n.data):"";if("yaml"===n.type)return n.data?s.default.dump(n.data):"";if("template"===n.type){const r=t.resolve(c.root,n.template),l=e.readFileSync(r,{encoding:"utf8"});return u.default.render(l,n.data)}return console.warn(`Unknown type [${n.type}]`),""}function m(t){const r=t.fullPath,l=y(t);f(r),e.writeFile(r,l,{flag:"w"},(e=>{if(e)throw e;console.log(`Generate File to ${n.green(r)}`)}))}function v(e){e.middlewares.use("/__generate_file_list",((e,t)=>{t.writeHead(200,{"Content-Type":"text/html"}),t.write(u.default.render('<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>Generate File List</title>\n<body>\n<h1>Generate File List</h1>\n<div>\n <ul>\n <% Object.keys(generateFiles).forEach(key => { %>\n <li><a href=\'<%= key %>\'><%= generateFiles[key].output %></a></li>\n <% }) %>\n </ul>\n</div>\n</body>\n</html>\n',{generateFiles:Object.fromEntries(h)})),t.end()})),e.middlewares.use(((e,t,n)=>{const r=new URL(e.originalUrl,`http://${e.headers.host}`).pathname;if(h.has(r)){const e=y(h.get(r));t.write(e),t.end()}else n()}));const t=e,r=e.httpServer.listen;let l,i=c.server.port||3e3;t.httpServer.listen=function(...e){return i||(i=e[0]),clearTimeout(l),l=setTimeout((()=>{console.log(` > Generate File List: ${n.yellow(`http://localhost:${i}/__generate_file_list/`)}\n`)}),0),r.apply(this,e)}}module.exports=function(e){return{name:"vite-plugin-generate-file",configResolved(n){if(c=n,d=t.resolve(c.root,c.build.outDir),Array.isArray(e))e.forEach((e=>{const t=p(e);h.set(t.relativePath,t)}));else{const t=p(e);h.set(t.relativePath,t)}},closeBundle(){if("serve"!==c.command)for(const e of h.values())m(e)},configureServer:v}}; | ||
"use strict";var e=require("fs"),t=require("path"),n=require("chalk"),r=require("js-yaml"),i=require("ejs"),l=require("mime-types");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function a(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var u=o(e),s=o(t),c=o(r),f=o(i),d=a(l);function p(e){const t=s.default.dirname(e);u.default.existsSync(t)||(p(t),u.default.mkdirSync(t))}let y,h;const m=new Map;function g(e){const n={output:"./output.txt",type:"json",template:"",...e},r=t.resolve(h,n.output),i=`/${t.relative(h,r)}`,l=n.contentType||d.lookup(n.output||"")||"text/plain";return{...n,contentType:l,fullPath:r,relativePath:i}}function v(n){if(!n.type)return"";if("json"===n.type)return n.data?JSON.stringify(n.data):"";if("yaml"===n.type)return n.data?c.default.dump(n.data):"";if("template"===n.type){const r=t.resolve(y.root,n.template),i=e.readFileSync(r,{encoding:"utf8"});return f.default.render(i,n.data)}return console.warn(`Unknown type [${n.type}]`),""}function w(t){const r=t.fullPath,i=v(t);p(r),e.writeFile(r,i,{flag:"w"},(e=>{if(e)throw e;console.log(`Generate File to ${n.green(r)}`)}))}function b(e){e.middlewares.use("/__generate_file_list",((e,t)=>{t.writeHead(200,{"Content-Type":"text/html"}),t.write(f.default.render('<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>Generate File List</title>\n<body>\n<h1>Generate File List</h1>\n<div>\n <ul>\n <% Object.keys(generateFiles).forEach(key => { %>\n <li><a href=\'<%= key %>\'><%= generateFiles[key].output %></a></li>\n <% }) %>\n </ul>\n</div>\n</body>\n</html>\n',{generateFiles:Object.fromEntries(m)})),t.end()})),e.middlewares.use(((e,t,n)=>{const r=new URL(e.originalUrl,`http://${e.headers.host}`).pathname;if(m.has(r)){const e=m.get(r),n=v(e);t.writeHead(200,{"Content-Type":e.contentType}),t.write(n),t.end()}else n()}));const t=e,r=e.httpServer.listen;let i,l=y.server.port||3e3;t.httpServer.listen=function(...e){return l||(l=e[0]),clearTimeout(i),i=setTimeout((()=>{console.log(` > Generate File List: ${n.yellow(`http://localhost:${l}/__generate_file_list/`)}\n`)}),0),r.apply(this,e)}}module.exports=function(e){return{name:"vite-plugin-generate-file",configResolved(n){if(y=n,h=t.resolve(y.root,y.build.outDir),Array.isArray(e))e.forEach((e=>{const t=g(e);m.set(t.relativePath,t)}));else{const t=g(e);m.set(t.relativePath,t)}},closeBundle(){if("serve"!==y.command)for(const e of m.values())w(e)},configureServer:b}}; |
@@ -16,2 +16,6 @@ import { Plugin } from 'vite'; | ||
/** | ||
* devServer访问时返回的ContentType,默认根据output路径扩展名进行猜测 | ||
*/ | ||
contentType?: string; | ||
/** | ||
* 输出使用的模板文件,在type为template时有效,无默认值 | ||
@@ -18,0 +22,0 @@ */ |
@@ -1,1 +0,1 @@ | ||
import e,{writeFile as t,readFileSync as n}from"fs";import r,{resolve as o,relative as i}from"path";import{green as a,yellow as l}from"chalk";import s from"js-yaml";import u from"ejs";function f(t){const n=r.dirname(t);e.existsSync(n)||(f(n),e.mkdirSync(n))}let c,p;const m=new Map;function d(e){const t={output:"./output.txt",type:"json",template:"",...e},n=o(p,t.output),r=`/${i(p,n)}`;return{...t,fullPath:n,relativePath:r}}function h(e){if(!e.type)return"";if("json"===e.type)return e.data?JSON.stringify(e.data):"";if("yaml"===e.type)return e.data?s.dump(e.data):"";if("template"===e.type){const t=o(c.root,e.template),r=n(t,{encoding:"utf8"});return u.render(r,e.data)}return console.warn(`Unknown type [${e.type}]`),""}function y(e){const n=e.fullPath,r=h(e);f(n),t(n,r,{flag:"w"},(e=>{if(e)throw e;console.log(`Generate File to ${a(n)}`)}))}function g(e){e.middlewares.use("/__generate_file_list",((e,t)=>{t.writeHead(200,{"Content-Type":"text/html"}),t.write(u.render('<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>Generate File List</title>\n<body>\n<h1>Generate File List</h1>\n<div>\n <ul>\n <% Object.keys(generateFiles).forEach(key => { %>\n <li><a href=\'<%= key %>\'><%= generateFiles[key].output %></a></li>\n <% }) %>\n </ul>\n</div>\n</body>\n</html>\n',{generateFiles:Object.fromEntries(m)})),t.end()})),e.middlewares.use(((e,t,n)=>{const r=new URL(e.originalUrl,`http://${e.headers.host}`).pathname;if(m.has(r)){const e=h(m.get(r));t.write(e),t.end()}else n()}));const t=e,n=e.httpServer.listen;let r,o=c.server.port||3e3;t.httpServer.listen=function(...e){return o||(o=e[0]),clearTimeout(r),r=setTimeout((()=>{console.log(` > Generate File List: ${l(`http://localhost:${o}/__generate_file_list/`)}\n`)}),0),n.apply(this,e)}}function v(e){return{name:"vite-plugin-generate-file",configResolved(t){if(c=t,p=o(c.root,c.build.outDir),Array.isArray(e))e.forEach((e=>{const t=d(e);m.set(t.relativePath,t)}));else{const t=d(e);m.set(t.relativePath,t)}},closeBundle(){if("serve"!==c.command)for(const e of m.values())y(e)},configureServer:g}}export{v as default}; | ||
import e,{writeFile as t,readFileSync as n}from"fs";import r,{resolve as o,relative as i}from"path";import{green as a,yellow as l}from"chalk";import s from"js-yaml";import u from"ejs";import*as p from"mime-types";function c(t){const n=r.dirname(t);e.existsSync(n)||(c(n),e.mkdirSync(n))}let f,m;const d=new Map;function h(e){const t={output:"./output.txt",type:"json",template:"",...e},n=o(m,t.output),r=`/${i(m,n)}`,a=t.contentType||p.lookup(t.output||"")||"text/plain";return{...t,contentType:a,fullPath:n,relativePath:r}}function y(e){if(!e.type)return"";if("json"===e.type)return e.data?JSON.stringify(e.data):"";if("yaml"===e.type)return e.data?s.dump(e.data):"";if("template"===e.type){const t=o(f.root,e.template),r=n(t,{encoding:"utf8"});return u.render(r,e.data)}return console.warn(`Unknown type [${e.type}]`),""}function g(e){const n=e.fullPath,r=y(e);c(n),t(n,r,{flag:"w"},(e=>{if(e)throw e;console.log(`Generate File to ${a(n)}`)}))}function v(e){e.middlewares.use("/__generate_file_list",((e,t)=>{t.writeHead(200,{"Content-Type":"text/html"}),t.write(u.render('<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>Generate File List</title>\n<body>\n<h1>Generate File List</h1>\n<div>\n <ul>\n <% Object.keys(generateFiles).forEach(key => { %>\n <li><a href=\'<%= key %>\'><%= generateFiles[key].output %></a></li>\n <% }) %>\n </ul>\n</div>\n</body>\n</html>\n',{generateFiles:Object.fromEntries(d)})),t.end()})),e.middlewares.use(((e,t,n)=>{const r=new URL(e.originalUrl,`http://${e.headers.host}`).pathname;if(d.has(r)){const e=d.get(r),n=y(e);t.writeHead(200,{"Content-Type":e.contentType}),t.write(n),t.end()}else n()}));const t=e,n=e.httpServer.listen;let r,o=f.server.port||3e3;t.httpServer.listen=function(...e){return o||(o=e[0]),clearTimeout(r),r=setTimeout((()=>{console.log(` > Generate File List: ${l(`http://localhost:${o}/__generate_file_list/`)}\n`)}),0),n.apply(this,e)}}function w(e){return{name:"vite-plugin-generate-file",configResolved(t){if(f=t,m=o(f.root,f.build.outDir),Array.isArray(e))e.forEach((e=>{const t=h(e);d.set(t.relativePath,t)}));else{const t=h(e);d.set(t.relativePath,t)}},closeBundle(){if("serve"!==f.command)for(const e of d.values())g(e)},configureServer:v}}export{w as default}; |
{ | ||
"name": "vite-plugin-generate-file", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"license": "MIT", | ||
@@ -40,3 +40,4 @@ "description": "Generate extra file to dist folder.", | ||
"ejs": "^3.1.6", | ||
"js-yaml": "^4.1.0" | ||
"js-yaml": "^4.1.0", | ||
"mime-types": "^2.1.35" | ||
}, | ||
@@ -47,2 +48,3 @@ "devDependencies": { | ||
"@types/js-yaml": "^4.0.3", | ||
"@types/mime-types": "^2.1.1", | ||
"@types/node": "^16.9.2", | ||
@@ -49,0 +51,0 @@ "@typescript-eslint/eslint-plugin": "^4.31.1", |
@@ -35,8 +35,9 @@ # vite-plugin-generate-file | ||
| Name | Description | Options | Defaults | | ||
|------|-------------|--------- |----------| | ||
| `type` | Generate file format type. | `json` `yaml` `template` | `json` | | ||
| `output` | Path to the output file the plugin will generate. It relative to `dist` folder. | - | `./output.txt` | | ||
| `template` | Path to the template file. Support `ejs` format template. Available while `type` is `template`. | - | - | | ||
| `data` | Data to use in generated file or be passed in to the template. | - | - | | ||
| Name | Description | Options | Defaults | | ||
|---------------|-------------------------------------------------------------------------------------------------------------|---------------------------|----------------| | ||
| `type` | Generate file format type. | `json` `yaml` `template` | `json` | | ||
| `output` | Path to the output file the plugin will generate. It relative to `dist` folder. | - | `./output.txt` | | ||
| `template` | Path to the template file. Support `ejs` format template. Available while `type` is `template`. | - | - | | ||
| `data` | Data to use in generated file or be passed in to the template. | - | - | | ||
| `contentType` | Response Content-Type return from dev server. If empty it will be guessed from the `output` path extension. | - | - | | ||
@@ -43,0 +44,0 @@ In dev mode, plugin will mock file by dev server. See [localhost:3000/__generate_file_list/](http://localhost:3000/__generate_file_list/) for more detail. |
10195
60
54
4
20
+ Addedmime-types@^2.1.35
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)