11ty / eleventy-plugin-vue

Use Vue.js templates and Vue.js single file components in Eleventy.
195 stars 11 forks source link

Use ~ in `import` urls as project or include root #7

Closed zachleat closed 2 years ago

zachleat commented 3 years ago

Probably something like

Maybe also look at https://github.com/rollup/plugins/tree/master/packages/auto-install

zachleat commented 2 years ago

This is the thing! https://twitter.com/_ovlb/status/1479539567577473029

https://github.com/inframanufaktur/no-11/blob/main/_plugins/vue.js

const path = require('path')
const eleventyVue = require('@11ty/eleventy-plugin-vue')
const alias = require('@rollup/plugin-alias')

module.exports = {
  plugin: eleventyVue,
  pluginOptions: {
    rollupOptions: {
      plugins: [
        alias({
          entries: [
            {
              find: '~components',
              replacement: path.join(
                process.cwd(),
                '_src/_includes/components',
              ),
            },
          ],
        }),
      ],
    },
  },
}
zachleat commented 2 years ago

Note that the master branch holds the 1.x canary versions of this plugin (Vue 3) and 0.x branch is Vue 2.

zachleat commented 2 years ago

⚠️ EDIT WARNING ⚠️ Don’t use this code sample, it causes second build errors. Use this one instead: https://github.com/11ty/eleventy-plugin-vue/issues/7#issuecomment-1113550527

Here’s the sample I got working on 1.x:

const path = require('path');
const eleventyVue = require("@11ty/eleventy-plugin-vue");
const alias = require('@rollup/plugin-alias')

module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(eleventyVue, {
    rollupOptions: {
      plugins: [
        alias({
          entries: [
            {
              find: /^\~components\/(.*)/,
              replacement: './_includes/$1',
            },
          ],
        }),
      ],
    },
  });
};

and import include from "~components/include.vue";

zachleat commented 2 years ago

Eleventy Plugin Vue 0.7.3 is out which includes support for rollupOptions—thank you @hexagoncircle!

zachleat commented 2 years ago

One caveat I found here, if you’re running into File not processed yet issues:

[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] File not processed yet, /Users/zachleat/Temp/eleventy-vue-aliases/_includes/include.vue (via Error)
[11ty] 
[11ty] Original error stack trace: Error: File not processed yet, /Users/zachleat/Temp/eleventy-vue-aliases/_includes/include.vue
[11ty]     at resolveVuePart (/Users/zachleat/Temp/eleventy-vue-aliases/node_modules/rollup-plugin-vue/dist/rollup-plugin-vue.js:92:15)
[11ty]     at Object.resolveId (/Users/zachleat/Temp/eleventy-vue-aliases/node_modules/rollup-plugin-vue/dist/rollup-plugin-vue.js:217:33)
[11ty]     at /Users/zachleat/Temp/eleventy-vue-aliases/node_modules/rollup/dist/shared/rollup.js:22823:37
[11ty] Wrote 0 files in 0.04 seconds (v1.0.1)

Make sure you’re returning an absolute path from the aliased replacement. This works better:

const path = require('path');
const eleventyVue = require("@11ty/eleventy-plugin-vue");
const alias = require('@rollup/plugin-alias')

module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(eleventyVue, {
    rollupOptions: {
      plugins: [
        alias({
          entries: [
            {
              find: "~components",
              replacement: path.join(process.cwd(), '/_includes/'),
            },
          ],
        }),
      ],
    },
  });
};

with import include from "~components/include.vue";