How to abstract from an hard-coded path for the baseUrl parameter of vue.config.js to correctly address...











up vote
0
down vote

favorite












I'm planning to develop wordpress plugins integrating VueJS functionalities. The general idea would be to set up a dev environment where I start a project with vue-cli in the main folder of the plugin.
When the css/js files are generated, I can enqueue the files using the wp_enqueue_scripts hook in the plugin php main file.



This approach has two drawbacks:



1) No hot reload is possible since the page where the plugin is consumed is different from the one served by the webpack environment (webpack_dev_server)



2) In order to have static assets (images, fonts) correctly addressed in the generated JavaScript, I need to specify the final path of my plugin as the baseUrl parameter, like this:



//vue.config.js

module.exports = {
baseUrl: 'wp-content/plugins/VuePress/dist/', // <<--- hardcoded path
productionSourceMap: false,
filenameHashing: false,
lintOnSave: true,

transpileDependencies: [
/bvue-awesomeb/
],

outputDir: undefined,
assetsDir: undefined,
runtimeCompiler: undefined,
parallel: undefined,
css: undefined
}


While the first point is annoying but not blocking, the second represents a serious drawback: The baseUrl is "hardcoded", which means that if the plugin lands in some fancy wp installation where the plugins are not in the standard location, then the static assets links will break.



I've thought of a hack: I could use a fake path as baseUrl (like baseUrl: '__FAKEPATH__/'), and when the plugin is activated I could open the webpack generated js bundle, preg_replace the FAKEPATH and write the modified content (with the correct path) back to its destination, like this (pseudo code):



$js = file_get_contents( __DIR__ . '/js/webpack_bundle.js' );
$js = preg_replace( "__FAKEPATH__/", __DIR__ . "/dist/", $js );
file_put_contents( __DIR__ . '/js/webpack_bundle.js', $js );


Any better idea to address these two point would be greatly appreciated










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm planning to develop wordpress plugins integrating VueJS functionalities. The general idea would be to set up a dev environment where I start a project with vue-cli in the main folder of the plugin.
    When the css/js files are generated, I can enqueue the files using the wp_enqueue_scripts hook in the plugin php main file.



    This approach has two drawbacks:



    1) No hot reload is possible since the page where the plugin is consumed is different from the one served by the webpack environment (webpack_dev_server)



    2) In order to have static assets (images, fonts) correctly addressed in the generated JavaScript, I need to specify the final path of my plugin as the baseUrl parameter, like this:



    //vue.config.js

    module.exports = {
    baseUrl: 'wp-content/plugins/VuePress/dist/', // <<--- hardcoded path
    productionSourceMap: false,
    filenameHashing: false,
    lintOnSave: true,

    transpileDependencies: [
    /bvue-awesomeb/
    ],

    outputDir: undefined,
    assetsDir: undefined,
    runtimeCompiler: undefined,
    parallel: undefined,
    css: undefined
    }


    While the first point is annoying but not blocking, the second represents a serious drawback: The baseUrl is "hardcoded", which means that if the plugin lands in some fancy wp installation where the plugins are not in the standard location, then the static assets links will break.



    I've thought of a hack: I could use a fake path as baseUrl (like baseUrl: '__FAKEPATH__/'), and when the plugin is activated I could open the webpack generated js bundle, preg_replace the FAKEPATH and write the modified content (with the correct path) back to its destination, like this (pseudo code):



    $js = file_get_contents( __DIR__ . '/js/webpack_bundle.js' );
    $js = preg_replace( "__FAKEPATH__/", __DIR__ . "/dist/", $js );
    file_put_contents( __DIR__ . '/js/webpack_bundle.js', $js );


    Any better idea to address these two point would be greatly appreciated










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm planning to develop wordpress plugins integrating VueJS functionalities. The general idea would be to set up a dev environment where I start a project with vue-cli in the main folder of the plugin.
      When the css/js files are generated, I can enqueue the files using the wp_enqueue_scripts hook in the plugin php main file.



      This approach has two drawbacks:



      1) No hot reload is possible since the page where the plugin is consumed is different from the one served by the webpack environment (webpack_dev_server)



      2) In order to have static assets (images, fonts) correctly addressed in the generated JavaScript, I need to specify the final path of my plugin as the baseUrl parameter, like this:



      //vue.config.js

      module.exports = {
      baseUrl: 'wp-content/plugins/VuePress/dist/', // <<--- hardcoded path
      productionSourceMap: false,
      filenameHashing: false,
      lintOnSave: true,

      transpileDependencies: [
      /bvue-awesomeb/
      ],

      outputDir: undefined,
      assetsDir: undefined,
      runtimeCompiler: undefined,
      parallel: undefined,
      css: undefined
      }


      While the first point is annoying but not blocking, the second represents a serious drawback: The baseUrl is "hardcoded", which means that if the plugin lands in some fancy wp installation where the plugins are not in the standard location, then the static assets links will break.



      I've thought of a hack: I could use a fake path as baseUrl (like baseUrl: '__FAKEPATH__/'), and when the plugin is activated I could open the webpack generated js bundle, preg_replace the FAKEPATH and write the modified content (with the correct path) back to its destination, like this (pseudo code):



      $js = file_get_contents( __DIR__ . '/js/webpack_bundle.js' );
      $js = preg_replace( "__FAKEPATH__/", __DIR__ . "/dist/", $js );
      file_put_contents( __DIR__ . '/js/webpack_bundle.js', $js );


      Any better idea to address these two point would be greatly appreciated










      share|improve this question















      I'm planning to develop wordpress plugins integrating VueJS functionalities. The general idea would be to set up a dev environment where I start a project with vue-cli in the main folder of the plugin.
      When the css/js files are generated, I can enqueue the files using the wp_enqueue_scripts hook in the plugin php main file.



      This approach has two drawbacks:



      1) No hot reload is possible since the page where the plugin is consumed is different from the one served by the webpack environment (webpack_dev_server)



      2) In order to have static assets (images, fonts) correctly addressed in the generated JavaScript, I need to specify the final path of my plugin as the baseUrl parameter, like this:



      //vue.config.js

      module.exports = {
      baseUrl: 'wp-content/plugins/VuePress/dist/', // <<--- hardcoded path
      productionSourceMap: false,
      filenameHashing: false,
      lintOnSave: true,

      transpileDependencies: [
      /bvue-awesomeb/
      ],

      outputDir: undefined,
      assetsDir: undefined,
      runtimeCompiler: undefined,
      parallel: undefined,
      css: undefined
      }


      While the first point is annoying but not blocking, the second represents a serious drawback: The baseUrl is "hardcoded", which means that if the plugin lands in some fancy wp installation where the plugins are not in the standard location, then the static assets links will break.



      I've thought of a hack: I could use a fake path as baseUrl (like baseUrl: '__FAKEPATH__/'), and when the plugin is activated I could open the webpack generated js bundle, preg_replace the FAKEPATH and write the modified content (with the correct path) back to its destination, like this (pseudo code):



      $js = file_get_contents( __DIR__ . '/js/webpack_bundle.js' );
      $js = preg_replace( "__FAKEPATH__/", __DIR__ . "/dist/", $js );
      file_put_contents( __DIR__ . '/js/webpack_bundle.js', $js );


      Any better idea to address these two point would be greatly appreciated







      javascript wordpress vue.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 at 10:43

























      asked Nov 12 at 17:30









      Raffaele Candeliere

      14916




      14916





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267247%2fhow-to-abstract-from-an-hard-coded-path-for-the-baseurl-parameter-of-vue-config%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267247%2fhow-to-abstract-from-an-hard-coded-path-for-the-baseurl-parameter-of-vue-config%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?