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
javascript wordpress vue.js
add a comment |
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
javascript wordpress vue.js
add a comment |
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
javascript wordpress vue.js
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
javascript wordpress vue.js
edited Nov 14 at 10:43
asked Nov 12 at 17:30
Raffaele Candeliere
14916
14916
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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