Skip to content

Commit

Permalink
(chore) no longer bundle Vue.js plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Mar 22, 2021
1 parent bfa6600 commit 78a0453
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@ div.code {

## Using with Vue.js

Simply register the plugin with Vue:
Simply build & register the plugin with Vue:

```js
Vue.use(hljs.vuePlugin);
import { buildVuePlugin } from "highlight.js/lib/vue_plugin.js";
Vue.use(buildVuePlugin(hljs).VuePlugin);
```

And you'll be provided with a `highlightjs` component for use
Expand Down
5 changes: 1 addition & 4 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import * as utils from './lib/utils.js';
import * as MODES from './lib/modes.js';
import { compileLanguage } from './lib/mode_compiler.js';
import * as packageJSON from '../package.json';
import { BuildVuePlugin } from "./plugins/vue.js";
import * as logger from "./lib/logger.js";

const escape = utils.escapeHTML;
Expand Down Expand Up @@ -898,9 +897,7 @@ const HLJS = function(hljs) {
registerAliases,
autoDetection,
inherit,
addPlugin,
// plugins for frameworks
vuePlugin: BuildVuePlugin(hljs).VuePlugin
addPlugin
});

hljs.debugMode = function() { SAFE_MODE = false; };
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function hasValueOrEmptyAttribute(value) {
return Boolean(value || value === "");
}

export function BuildVuePlugin(hljs) {
export default function BuildVuePlugin(hljs) {
const Component = {
props: ["language", "code", "autodetect"],
data: function() {
Expand Down
24 changes: 24 additions & 0 deletions tools/build_browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ async function buildBrowser(options) {

detailedGrammarSizes(languages);

await buildVuePluginJS({ minify: options.minify });
const size = await buildBrowserHighlightJS(languages, { minify: options.minify });

log("-----");
Expand Down Expand Up @@ -154,6 +155,29 @@ function installDemoStyles() {
});
}

async function buildVuePluginJS({ minify }) {
log("Building vue_plugin.js.");

const outFile = `${process.env.BUILD_DIR}/vue_plugin.js`;
const minifiedFile = outFile.replace(/js$/, "min.js");

const input = { ...config.rollup.browser_core.input, input: `src/plugins/vue.js`};
const output = config.rollup.browser_core.output;
// output.footer = output.footer.replace("hljs", "hljsVue");
let pluginSrc = await rollupCode(input,
{ ...output, file: outFile, name: "hljsVue", footer: null });

const tasks = [];
tasks.push(fs.writeFile(outFile, pluginSrc, { encoding: "utf8" }));

if (minify) {
const tersed = await Terser.minify(pluginSrc, config.terser);
tasks.push(fs.writeFile(minifiedFile, tersed.code, { encoding: "utf8" }));
}

await Promise.all(tasks);
}

async function buildBrowserHighlightJS(languages, { minify }) {
log("Building highlight.js.");

Expand Down
9 changes: 9 additions & 0 deletions tools/build_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ async function buildNodeLanguage(language) {
await rollupWrite(input, output);
}

async function buildNodeVuePluginJS() {
const input = { ...config.rollup.node.input, input: `src/plugins/vue.js` };
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/vue_plugin.js` };
await rollupWrite(input, output);
}


async function buildNodeHighlightJS() {
const input = { ...config.rollup.node.input, input: `src/highlight.js` };
const output = { ...config.rollup.node.output, file: `${process.env.BUILD_DIR}/lib/core.js` };
Expand Down Expand Up @@ -81,9 +88,11 @@ async function buildNode(options) {

await buildNodeIndex(languages);
await buildLanguages(languages);
await buildNodeVuePluginJS();

log("Writing highlight.js");
await buildNodeHighlightJS();

}

module.exports.build = buildNode;
Expand Down
4 changes: 3 additions & 1 deletion tools/developer.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ <h3>Markup</h3>
</div>

<script src="../build/highlight.js"></script>
<script src="../build/vue_plugin.js"></script>
<script src="vendor/jquery-2.1.1.min.js"></script>
<script src="vendor/vue.js"></script>

Expand Down Expand Up @@ -273,7 +274,8 @@ <h3>Markup</h3>
});
}());

Vue.use(hljs.vuePlugin);
let plugin = hljsVue(hljs).VuePlugin;
Vue.use(plugin);
let vue = new Vue({
el: '#app',
data: { code: "", language: "" },
Expand Down

0 comments on commit 78a0453

Please sign in to comment.