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

Add Vue.js support #1189

Open
michaelw85 opened this issue Apr 8, 2021 · 11 comments
Open

Add Vue.js support #1189

michaelw85 opened this issue Apr 8, 2021 · 11 comments

Comments

@michaelw85
Copy link

michaelw85 commented Apr 8, 2021

As the title explains, it would be nice to have a vue: true option instead of having to manually configure it.

I've got a manual setup working for Vue + TS (with class component and .vue files support) and the following was needed:

NPM:
npm i -D vue vue-template-compiler vue-loader vue-class-component ts-loader babel-preset-typescript-vue @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties

wpackio.project.js:

const { VueLoaderPlugin } = require("vue-loader");

module.exports = {
      files: [
        {
            name: "xyz",
            entry: {},
            webpackConfig: {
                module: {
                    rules: [
                        {
                            test: /\.vue$/,
                            loader: "vue-loader",
                            options: {
                                esModule: true,
                            },
                        },
                        {
                            test: /\.ts$/,
                            loader: "ts-loader",
                            options: {
                                appendTsSuffixTo: [/\.vue$/],
                            },
                        },
                    ],
                },
                plugins: [new VueLoaderPlugin()],
            },
        },
....
    tsBabelOverride: (defaults) => ({
        ...defaults,
        presets: ["babel-preset-typescript-vue"],
        plugins: [
            ["@babel/proposal-decorators", { legacy: true }],
            ["@babel/proposal-class-properties", { loose: true }],
        ],
    }),
...
    alias: {
        vue$: "vue/dist/vue.esm.js",
    },

Add a vue-shim.d.ts to your typings folder:

declare module "*.vue" {
    import Vue from "vue";
    export default Vue;
}

Make sure your typings are loaded in your tsconfig.json, something like:
"typeRoots": ["node_modules/@types", "typings"]

For a working example using a child theme see: https://github.com/michaelw85/wp-bedrock-wpack-vuejs

@swashata
Copy link
Owner

Thank you for this. Maybe you can add it to documentation? Since I don't use vue personally I am reluctant to manage its support. Alternately if you can take this job, then I am happy to review your PR :)

@michaelw85
Copy link
Author

Thank you for this. Maybe you can add it to documentation? Since I don't use vue personally I am reluctant to manage its support. Alternately if you can take this job, then I am happy to review your PR :)

Adding this would be nicer than documenting. I definitely am willing to see if I can add it, but it might take some time. To be honest I find it challenging to find the time to work on code in my spare time.

michaelw85 added a commit to michaelw85/wp-webpack-script that referenced this issue Apr 26, 2021
@michaelw85
Copy link
Author

@swashata I've had a little bit of time tonight. I've done a bare minimum implementation, I think I'm at a point where I can test it locally and then have a look at the unit tests. Please see the linked commit, any pointers would be nice since I'm completely new to this codebase.

@swashata
Copy link
Owner

I took a look. Is ts-loader really necessary? The toolchain support typescript compilation through babel and babel-preset-typescript. Can't that be used instead? We also have support for fork-ts-checker-plugin to report any typing errors.

@michaelw85
Copy link
Author

I took a look. Is ts-loader really necessary? The toolchain support typescript compilation through babel and babel-preset-typescript. Can't that be used instead? We also have support for fork-ts-checker-plugin to report any typing errors.

I was not sure if it's required either. I want to get a working setup first and then try to refactor and see if it can be removed. At least the config telling TS to handle vue files is require.

@swashata
Copy link
Owner

Maybe you can try and figure out if it works with vue-loader and babel-loader with the TS config that it uses. From the documentation I get the idea that, if the webpack config has something to handle .ts files, then vue-loader uses that. So we just need to push vue-loader before the ts config.

Something like

const vueRules = {
  test: /\.vue$/,
  loader: "vue-loader",
  options: {
    esModule: true,
  },
};

// add it to the list of module rules if this.config.hasVue === true.

@michaelw85
Copy link
Author

Thank you for the input! I haven't been able to continue yet. I
would like to try and see if I can find continue on Sunday. I will push updates if there are any and post an update.

@michaelw85
Copy link
Author

@swashata How should I test this locally? I tried using npm link and pointed to the scripts dir.
This seems to add the package. I tried running the bootstrap command but it fails, it doesn't prompt for input.
I tried an existing config and enabled the new vue setting and run a build. This emits an error ts-loader module can't be found.
I've installed the package in the scripts package only, on building it's complaining about the entrypoint package. Should I install packages in a different way than just npm i pkg in the scripts dir?

Sorry, this is probably a noob question and related to using Lerna (this is the first time it's in a project I'm working on).

@swashata
Copy link
Owner

swashata commented May 4, 2021

Hi @michaelw85,

Sorry I got caught up in some other work. So from your findings, it seems like ts-loader is indeed needed for vue-loader to work. Or maybe it is due to some misconfiguration.

In anycase, I think, for now it is better to leave under userland. Extending webpack config is not really a big issue anyway with wpackio.project.js. Once I find some time, I will do another investigation.

In the meanwhile, since I don't use vue at all, a repository of a vue based plugin where I can test the integration would be really helpful.

@michaelw85
Copy link
Author

michaelw85 commented May 13, 2021

@swashata I've set up a repo where I have implemented what I described in this thread. A working WordPress setup with Wpack + VueJS hello world example. I've created the WP setup with Bedrock, to get this setup working you will have to composer install, set .env variables, and run npm ci in the child theme. Go to your site in the browser and perform the wp install to create the DB. Login and switch to the child theme I created. Update the wpackio.server.js with your dev URL. npm run start or npm run build and you should have a site with a hello world component at the top of the page.

https://github.com/michaelw85/wp-bedrock-wpack-vuejs

@the-sides
Copy link

This has all been fantastic to see and am hopeful for anymore updates. Thanks for all your work @michaelw85 and @swashata!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants