esbuild-plugin-vue3
Advanced tools
Comparing version 0.1.1 to 0.1.2
import * as esbuild from "esbuild"; | ||
declare const vuePlugin: esbuild.Plugin; | ||
import { Options } from "./options"; | ||
declare const vuePlugin: (opts?: Options) => esbuild.Plugin; | ||
export = vuePlugin; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -68,2 +68,13 @@ "use strict"; | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
var path = __importStar(require("path")); | ||
@@ -77,206 +88,233 @@ var fs = __importStar(require("fs")); | ||
var utils_1 = require("./utils"); | ||
var vuePlugin = { | ||
name: "vue", | ||
setup: function (build) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
build.initialOptions.define = __assign(__assign({}, build.initialOptions.define), { "__VUE_OPTIONS_API__": "false", "__VUE_PROD_DEVTOOLS__": "false" }); | ||
return [4 /*yield*/, paths_1.loadRules()]; | ||
case 1: | ||
_a.sent(); | ||
build.onResolve({ filter: /.*/ }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var aliased, fullPath, possible, _i, possible_1, postfix; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
aliased = paths_1.replaceRules(args.path); | ||
fullPath = path.isAbsolute(aliased) ? aliased : path.join(process.cwd(), aliased); | ||
return [4 /*yield*/, utils_1.fileExists(fullPath)]; | ||
case 1: | ||
if (!!(_a.sent())) return [3 /*break*/, 6]; | ||
possible = [ | ||
".ts", | ||
"/index.ts", | ||
".js", | ||
"/index.js", | ||
]; | ||
_i = 0, possible_1 = possible; | ||
_a.label = 2; | ||
case 2: | ||
if (!(_i < possible_1.length)) return [3 /*break*/, 5]; | ||
postfix = possible_1[_i]; | ||
return [4 /*yield*/, utils_1.fileExists(fullPath + postfix)]; | ||
case 3: | ||
if (_a.sent()) { | ||
return [2 /*return*/, { | ||
path: path.normalize(fullPath + postfix), | ||
var html_1 = require("./html"); | ||
var vuePlugin = function (opts) { | ||
if (opts === void 0) { opts = {}; } | ||
return ({ | ||
name: "vue", | ||
setup: function (_a) { | ||
var buildOpts = _a.initialOptions, build = __rest(_a, ["initialOptions"]); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
buildOpts.define = __assign(__assign({}, buildOpts.define), { "__VUE_OPTIONS_API__": opts.enableOptionsApi ? "true" : "false", "__VUE_PROD_DEVTOOLS__": opts.enableDevTools ? "true" : "false" }); | ||
return [4 /*yield*/, paths_1.loadRules()]; | ||
case 1: | ||
_b.sent(); | ||
if (!opts.disableResolving) { | ||
build.onResolve({ filter: /.*/ }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var aliased, fullPath, possible, _i, possible_1, postfix; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
aliased = paths_1.replaceRules(args.path); | ||
fullPath = path.isAbsolute(aliased) ? aliased : path.join(process.cwd(), aliased); | ||
return [4 /*yield*/, utils_1.fileExists(fullPath)]; | ||
case 1: | ||
if (!!(_a.sent())) return [3 /*break*/, 6]; | ||
possible = [ | ||
".ts", | ||
"/index.ts", | ||
".js", | ||
"/index.js", | ||
]; | ||
_i = 0, possible_1 = possible; | ||
_a.label = 2; | ||
case 2: | ||
if (!(_i < possible_1.length)) return [3 /*break*/, 5]; | ||
postfix = possible_1[_i]; | ||
return [4 /*yield*/, utils_1.fileExists(fullPath + postfix)]; | ||
case 3: | ||
if (_a.sent()) { | ||
return [2 /*return*/, { | ||
path: path.normalize(fullPath + postfix), | ||
namespace: "file" | ||
}]; | ||
} | ||
_a.label = 4; | ||
case 4: | ||
_i++; | ||
return [3 /*break*/, 2]; | ||
case 5: return [3 /*break*/, 7]; | ||
case 6: return [2 /*return*/, { | ||
path: path.normalize(fullPath), | ||
namespace: "file" | ||
}]; | ||
case 7: return [2 /*return*/]; | ||
} | ||
_a.label = 4; | ||
case 4: | ||
_i++; | ||
return [3 /*break*/, 2]; | ||
case 5: return [3 /*break*/, 7]; | ||
case 6: return [2 /*return*/, { | ||
path: path.normalize(fullPath), | ||
namespace: "file" | ||
}); | ||
}); }); | ||
} | ||
// Resolve main ".vue" import | ||
build.onResolve({ filter: /\.vue/ }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var params; | ||
return __generator(this, function (_a) { | ||
params = utils_1.getUrlParams(args.path); | ||
return [2 /*return*/, { | ||
path: utils_1.getFullPath(args), | ||
namespace: params.type === "script" ? "sfc-script" : | ||
params.type === "template" ? "sfc-template" : | ||
params.type === "style" ? "sfc-style" : "file", | ||
pluginData: __assign(__assign({}, args.pluginData), { index: params.index }) | ||
}]; | ||
case 7: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
// Resolve main ".vue" import | ||
build.onResolve({ filter: /\.vue/ }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var params; | ||
return __generator(this, function (_a) { | ||
params = utils_1.getUrlParams(args.path); | ||
return [2 /*return*/, { | ||
path: path.isAbsolute(args.path) ? args.path : path.join(args.resolveDir, args.path), | ||
namespace: params.type === "script" ? "sfc-script" : | ||
params.type === "template" ? "sfc-template" : | ||
params.type === "style" ? "sfc-style" : "file", | ||
pluginData: __assign(__assign({}, args.pluginData), { index: params.index }) | ||
}]; | ||
}); | ||
}); }); | ||
// Load stub when .vue is requested | ||
build.onLoad({ filter: /\.vue$/ }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var encPath, source, filename, descriptor, id, code, style; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
encPath = args.path.replace(/\\/g, "\\\\"); | ||
return [4 /*yield*/, fs.promises.readFile(args.path, 'utf8')]; | ||
case 1: | ||
source = _a.sent(); | ||
filename = path.relative(process.cwd(), args.path); | ||
descriptor = sfc.parse(source, { | ||
filename: filename | ||
}).descriptor; | ||
id = "data-v-" + crypto.randomBytes(4).toString("hex"); | ||
code = ""; | ||
if (descriptor.script || descriptor.scriptSetup) { | ||
code += "import script from \"" + encPath + "?type=script\";"; | ||
}); | ||
}); }); | ||
// Load stub when .vue is requested | ||
build.onLoad({ filter: /\.vue$/ }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var encPath, source, filename, descriptor, id, code, style; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
encPath = args.path.replace(/\\/g, "\\\\"); | ||
return [4 /*yield*/, fs.promises.readFile(args.path, 'utf8')]; | ||
case 1: | ||
source = _a.sent(); | ||
filename = path.relative(process.cwd(), args.path); | ||
descriptor = sfc.parse(source, { | ||
filename: filename | ||
}).descriptor; | ||
id = "data-v-" + crypto.randomBytes(4).toString("hex"); | ||
code = ""; | ||
if (descriptor.script || descriptor.scriptSetup) { | ||
code += "import script from \"" + encPath + "?type=script\";"; | ||
} | ||
else { | ||
code += "let script = {};"; | ||
} | ||
for (style in descriptor.styles) { | ||
code += "import \"" + encPath + "?type=style&index=" + style + "\";"; | ||
} | ||
code += "import { render } from \"" + encPath + "?type=template\"; script.render = render;"; | ||
code += "script.__file = " + JSON.stringify(filename) + "; script.__scopeId = " + JSON.stringify(id) + ";"; | ||
code += "export default script;"; | ||
return [2 /*return*/, { | ||
contents: code, | ||
resolveDir: path.dirname(args.path), | ||
pluginData: { descriptor: descriptor, id: id }, | ||
watchFiles: [args.path] | ||
}]; | ||
} | ||
}); | ||
}); }); | ||
build.onLoad({ filter: /.*/, namespace: "sfc-script" }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, descriptor, id, script, code, sourceMap; | ||
return __generator(this, function (_b) { | ||
_a = args.pluginData, descriptor = _a.descriptor, id = _a.id; | ||
if (descriptor.script || descriptor.scriptSetup) { | ||
script = sfc.compileScript(descriptor, { id: id }); | ||
code = script.content; | ||
if (buildOpts.sourcemap && script.map) { | ||
sourceMap = Buffer.from(JSON.stringify(script.map)).toString("base64"); | ||
code += "\n\n//@ sourceMappingURL=data:application/json;charset=utf-8;base64," + sourceMap; | ||
} | ||
else { | ||
code += "let script = {};"; | ||
} | ||
for (style in descriptor.styles) { | ||
code += "import \"" + encPath + "?type=style&index=" + style + "\";"; | ||
} | ||
code += "import { render } from \"" + encPath + "?type=template\"; script.render = render;"; | ||
code += "script.__file = " + JSON.stringify(filename) + "; script.__scopeId = " + JSON.stringify(id) + ";"; | ||
code += "export default script;"; | ||
return [2 /*return*/, { | ||
contents: code, | ||
loader: script.lang === "ts" ? "ts" : "js", | ||
resolveDir: path.dirname(args.path), | ||
pluginData: { descriptor: descriptor, id: id }, | ||
watchFiles: [args.path] | ||
}]; | ||
} | ||
}); | ||
}); }); | ||
build.onLoad({ filter: /.*/, namespace: "sfc-script" }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, descriptor, id, script, code, sourceMap; | ||
return __generator(this, function (_b) { | ||
_a = args.pluginData, descriptor = _a.descriptor, id = _a.id; | ||
if (descriptor.script || descriptor.scriptSetup) { | ||
script = sfc.compileScript(descriptor, { id: id }); | ||
code = script.content; | ||
if (build.initialOptions.sourcemap && script.map) { | ||
sourceMap = Buffer.from(JSON.stringify(script.map)).toString("base64"); | ||
code += "\n\n//@ sourceMappingURL=data:application/json;charset=utf-8;base64," + sourceMap; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
build.onLoad({ filter: /.*/, namespace: "sfc-template" }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, descriptor, id, source, template; | ||
return __generator(this, function (_b) { | ||
_a = args.pluginData, descriptor = _a.descriptor, id = _a.id; | ||
source = descriptor.template.content; | ||
if (descriptor.template.lang === "pug") { | ||
source = pug.render(descriptor.template.content); | ||
// Fix #default="#default" and v-else="v-else" | ||
source = source.replace(/(#.*?|v-.*?)="\1"/g, "$1"); | ||
} | ||
template = sfc.compileTemplate({ | ||
id: id, | ||
source: source, | ||
filename: args.path, | ||
scoped: descriptor.styles.some(function (o) { return o.scoped; }) | ||
}); | ||
return [2 /*return*/, { | ||
contents: code, | ||
loader: script.lang === "ts" ? "ts" : "js", | ||
contents: template.code, | ||
loader: "js", | ||
resolveDir: path.dirname(args.path), | ||
}]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
build.onLoad({ filter: /.*/, namespace: "sfc-template" }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, descriptor, id, source, template; | ||
return __generator(this, function (_b) { | ||
_a = args.pluginData, descriptor = _a.descriptor, id = _a.id; | ||
source = descriptor.template.content; | ||
if (descriptor.template.lang === "pug") { | ||
source = pug.render(descriptor.template.content); | ||
// Fix #default="#default" and v-else="v-else" | ||
source = source.replace(/(#.*?|v-.*?)="\1"/g, "$1"); | ||
} | ||
template = sfc.compileTemplate({ | ||
id: id, | ||
source: source, | ||
filename: args.path, | ||
scoped: descriptor.styles.some(function (o) { return o.scoped; }) | ||
}); | ||
return [2 /*return*/, { | ||
contents: template.code, | ||
loader: "js", | ||
resolveDir: path.dirname(args.path), | ||
}]; | ||
}); | ||
}); }); | ||
build.onLoad({ filter: /.*/, namespace: "sfc-style" }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, descriptor, index, id, style, source, includedFiles, result, template; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a = args.pluginData, descriptor = _a.descriptor, index = _a.index, id = _a.id; | ||
style = descriptor.styles[index]; | ||
source = style.content; | ||
includedFiles = []; | ||
if (!(style.lang === "sass" || style.lang === "scss")) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, new Promise(function (resolve, reject) { return sass.render({ | ||
data: source, | ||
indentedSyntax: style.lang === "sass", | ||
includePaths: [ | ||
path.dirname(args.path) | ||
], | ||
importer: [ | ||
function (url) { | ||
var modulePath = path.join(process.cwd(), "node_modules", url); | ||
if (fs.existsSync(modulePath)) { | ||
return { file: modulePath }; | ||
} | ||
return null; | ||
}, | ||
function (url) { return ({ file: paths_1.replaceRules(url) }); } | ||
] | ||
}, function (ex, res) { return ex ? reject(ex) : resolve(res); }); })]; | ||
case 1: | ||
result = _b.sent(); | ||
includedFiles = result.stats.includedFiles; | ||
source = String(result.css); | ||
_b.label = 2; | ||
case 2: return [4 /*yield*/, sfc.compileStyleAsync({ | ||
filename: args.path, | ||
id: id, | ||
source: source, | ||
scoped: style.scoped, | ||
})]; | ||
case 3: | ||
template = _b.sent(); | ||
return [2 /*return*/, { | ||
contents: template.code, | ||
loader: "css", | ||
resolveDir: path.dirname(args.path), | ||
watchFiles: includedFiles | ||
}]; | ||
}); }); | ||
build.onLoad({ filter: /.*/, namespace: "sfc-style" }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, descriptor, index, id, style, source, includedFiles, result, template; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a = args.pluginData, descriptor = _a.descriptor, index = _a.index, id = _a.id; | ||
style = descriptor.styles[index]; | ||
source = style.content; | ||
includedFiles = []; | ||
if (!(style.lang === "sass" || style.lang === "scss")) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, new Promise(function (resolve, reject) { return sass.render({ | ||
data: source, | ||
indentedSyntax: style.lang === "sass", | ||
includePaths: [ | ||
path.dirname(args.path) | ||
], | ||
importer: [ | ||
function (url) { | ||
var modulePath = path.join(process.cwd(), "node_modules", url); | ||
if (fs.existsSync(modulePath)) { | ||
return { file: modulePath }; | ||
} | ||
return null; | ||
}, | ||
function (url) { return ({ file: paths_1.replaceRules(url) }); } | ||
] | ||
}, function (ex, res) { return ex ? reject(ex) : resolve(res); }); })]; | ||
case 1: | ||
result = _b.sent(); | ||
includedFiles = result.stats.includedFiles; | ||
source = String(result.css); | ||
_b.label = 2; | ||
case 2: return [4 /*yield*/, sfc.compileStyleAsync({ | ||
filename: args.path, | ||
id: id, | ||
source: source, | ||
scoped: style.scoped, | ||
})]; | ||
case 3: | ||
template = _b.sent(); | ||
return [2 /*return*/, { | ||
contents: template.code, | ||
loader: "css", | ||
resolveDir: path.dirname(args.path), | ||
watchFiles: includedFiles | ||
}]; | ||
} | ||
}); | ||
}); }); | ||
build.onEnd(function (result) { | ||
var _a, _b, _c; | ||
var _d, _e, _f; | ||
if (opts === null || opts === void 0 ? void 0 : opts.generateHTML) { | ||
if (typeof opts.generateHTML === "string") { | ||
opts.generateHTML = { | ||
originalFile: opts.generateHTML | ||
}; | ||
} | ||
var outDir = buildOpts.outdir | ||
? buildOpts.outdir | ||
: buildOpts.outfile | ||
? path.dirname(buildOpts.outfile) | ||
: undefined; | ||
(_a = (_d = opts.generateHTML).trimPath) !== null && _a !== void 0 ? _a : (_d.trimPath = outDir); | ||
(_b = (_e = opts.generateHTML).pathPrefix) !== null && _b !== void 0 ? _b : (_e.pathPrefix = "/"); | ||
(_c = (_f = opts.generateHTML).outFile) !== null && _c !== void 0 ? _c : (_f.outFile = outDir && path.join(outDir, "index.html")); | ||
html_1.generateIndexHTML(result, opts.generateHTML); | ||
} | ||
}); | ||
}); }); | ||
return [2 /*return*/]; | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
}); | ||
}; | ||
module.exports = vuePlugin; | ||
//# sourceMappingURL=index.js.map |
/// <reference types="node" /> | ||
import * as fs from 'fs'; | ||
import { OnResolveArgs } from "esbuild"; | ||
export declare function getUrlParams(search: string): Record<string, string>; | ||
export declare function fileExists(path: fs.PathLike): Promise<boolean>; | ||
export declare function getFullPath(args: OnResolveArgs): string; | ||
//# sourceMappingURL=utils.d.ts.map |
@@ -58,4 +58,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fileExists = exports.getUrlParams = void 0; | ||
exports.getFullPath = exports.fileExists = exports.getUrlParams = void 0; | ||
var fs = __importStar(require("fs")); | ||
var path = __importStar(require("path")); | ||
function getUrlParams(search) { | ||
@@ -90,2 +91,6 @@ var hashes = search.slice(search.indexOf('?') + 1).split('&'); | ||
exports.fileExists = fileExists; | ||
function getFullPath(args) { | ||
return path.isAbsolute(args.path) ? args.path : path.join(args.resolveDir, args.path); | ||
} | ||
exports.getFullPath = getFullPath; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "esbuild-plugin-vue3", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "", | ||
@@ -16,2 +16,3 @@ "main": "./dist/index.js", | ||
"@vue/compiler-sfc": "^3.1.5", | ||
"cheerio": "^1.0.0-rc.10", | ||
"esbuild": "^0.12.15", | ||
@@ -27,3 +28,5 @@ "pug": "^3.0.2", | ||
}, | ||
"files": ["dist/*"] | ||
"files": [ | ||
"dist/*" | ||
] | ||
} |
@@ -5,1 +5,9 @@ # esbuild-plugin-vue3 | ||
This plugin is meant to mimick the default [Vue CLI](https://cli.vuejs.org/) behaviour, for example it supports path aliases defined in the tsconfig.json file. | ||
## Supported | ||
* HTML and Pug `<template>` | ||
* JavaScript and TypeScript `<script>` and `<script setup>` (the latter is still experimental) | ||
* CSS, SCSS and SASS `<style>` | ||
* Path aliases from tsconfig.json, e.g. `import "@/Component.vue"` resolves to `import "../../Component.vue` | ||
### The library is still not thoroughly tested, use at your own risk. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
52092
23
645
13
1
5
5
+ Addedcheerio@^1.0.0-rc.10
+ Addedboolbase@1.0.0(transitive)
+ Addedcheerio@1.0.0(transitive)
+ Addedcheerio-select@2.1.0(transitive)
+ Addedcss-select@5.1.0(transitive)
+ Addedcss-what@6.1.0(transitive)
+ Addeddom-serializer@2.0.0(transitive)
+ Addeddomelementtype@2.3.0(transitive)
+ Addeddomhandler@5.0.3(transitive)
+ Addeddomutils@3.2.2(transitive)
+ Addedencoding-sniffer@0.2.0(transitive)
+ Addedhtmlparser2@9.1.0(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addednth-check@2.1.1(transitive)
+ Addedparse5@7.2.1(transitive)
+ Addedparse5-htmlparser2-tree-adapter@7.1.0(transitive)
+ Addedparse5-parser-stream@7.1.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedundici@6.21.0(transitive)
+ Addedwhatwg-encoding@3.1.1(transitive)
+ Addedwhatwg-mimetype@4.0.0(transitive)