vue-replace-loader
Vue-replace-loader
Webpack loader for replacing strings of vue single-file component.
Install
npm install vue-replace-loader --save-dev
# or yarn
yarn add vue-replace-loader --dev
Usage
Suppose there are some special placeholders in your template, like __ENV__
, __PORT__
, __REMOTE_CSS__
, and you want to replace them with variable value when compiling.
// my-component.vue
<template>
<div data-env="__ENV__">
System is running on port: {{port}}
</div>
</template>
<script>
export default {
data() {
return {
port: '__PORT__',
}
},
}
</script>
<style>
@import "__REMOTE_CSS__";
</style>
All you need to do is define the regular expression and the value you want to replace in your webpack configuration file.
// webpack.config.js
module.exports = {
// ...
module: {
rules: [
{
test: /.vue$/,
use: [
'vue-loader',
{
loader: 'vue-replace-loader',
options: {
values: [
{ regex: /__ENV__/g, replacement: process.env.NODE_ENV },
{ regex: /__PORT__/g, replacement: process.env.PORT },
{ regex: /__REMOTE_CSS__/g, replacement: process.env.REMOTE_CSS },
],
},
},
],
},
],
},
// ...
}
Dependencies
@babel/core: ^7.14.6@babel/preset-env: ^7.14.7babel-jest: ^27.0.6babel-loader: ^8.2.2css-loader: ^5.2.6eslint: ^7.29.0eslint-config-airbnb-base: ^14.2.1eslint-plugin-import: ^2.23.4eslint-plugin-vue: ^7.12.1husky: >=6jest: ^27.0.6lint-staged: >=10memfs: ^3.2.2vue-loader: ^15.9.7vue-style-loader: ^4.1.3vue-template-compiler: ^2.6.14webpack: ^5.41.0