
Research
npm Malware Targets Telegram Bot Developers with Persistent SSH Backdoors
Malicious npm packages posing as Telegram bot libraries install SSH backdoors and exfiltrate data from Linux developer machines.
vue-loader
Advanced tools
vue-loader is a loader for webpack that allows you to author Vue components in a format called Single-File Components (SFCs). It provides the ability to transform and load .vue files directly into webpack. It handles the parsing of .vue files, which can contain template, script, and style elements, and it applies other webpack loaders to the respective parts, such as babel-loader for the script section and css-loader for the style section.
Parsing Single-File Components
vue-loader parses .vue files, extracting the script, template, and style sections. This code snippet shows how to configure webpack to use vue-loader for .vue files.
module.exports = {
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
}
]
},
plugins: [
new VueLoaderPlugin()
]
}
Scoped CSS
vue-loader enables scoped CSS for Vue components, ensuring styles only apply to the current component. The code sample shows a scoped style block within a .vue file.
<style scoped>
p[data-v-f3f3eg9] {
color: red;
}
</style>
Hot Module Replacement
vue-loader supports Hot Module Replacement (HMR) for Vue components, allowing components to be updated in real-time without a full page reload. The code sample demonstrates how to set up HMR for a Vue component.
if (module.hot) {
module.hot.accept('./components/MyComponent.vue', function () {
// Any required update logic...
})
}
Pre-Processors
vue-loader allows the use of pre-processors like SCSS, LESS, and Stylus within the style section of a .vue file. The code sample shows how to use SCSS within a Vue component.
<style lang="scss">
$color: red;
.style-class {
color: $color;
}
</style>
Asset URL Handling
vue-loader can transform asset URLs found in a Vue component's template into webpack module requests. The code sample demonstrates how to require an image asset in a .vue file's template.
<template>
<img :src="require('./assets/logo.png')">
</template>
react-hot-loader is similar to vue-loader in that it provides hot module replacement for React components. However, it is specific to React and does not handle single-file components like vue-loader does for Vue.
angular2-template-loader is a loader for webpack that inlines HTML and CSS into Angular components. It is similar to vue-loader in that it processes component templates and styles, but it is designed for Angular rather than Vue.
svelte-loader is a webpack loader for Svelte components, which are single-file components similar to Vue's SFCs. It compiles Svelte components into JavaScript modules, much like vue-loader does for Vue components.
webpack loader for Vue Single-File Components
experimentalInlineMatchResource: boolean
: enable Inline matchResource for rule matching for vue-loader.reactivityTransform: boolean
: enable Vue Reactivity Transform (SFCs only).
refSugar: boolean
: removed. use reactivityTransform
instead.
customElement: boolean | RegExp
: enable custom elements mode. An SFC loaded in custom elements mode inlines its <style>
tags as strings under the component's styles
option. When used with defineCustomElement
from Vue core, the styles will be injected into the custom element's shadow root.
/\.ce\.vue$/
true
will process all .vue
files in custom element mode.enableTsInTemplate: boolean
(16.8+): allow TS expressions in templates when <script>
has lang="ts"
. Defaults to true
.
When used with ts-loader
, due to ts-loader
's cache invalidation behavior, it sometimes prevents the template from being hot-reloaded in isolation, causing the component to reload despite only the template being edited. If this is annoying, you can set this option to false
(and avoid using TS expressions in templates).
Alternatively, leave this option on (by default) and use esbuild-loader
to transpile TS instead, which doesn't suffer from this problem (it's also a lot faster). However, do note you will need to rely on TS type checking from other sources (e.g. IDE or vue-tsc
).
vue-loader
is a loader for webpack that allows you to author Vue components in a format called Single-File Components (SFCs):
<template>
<div class="example">{{ msg }}</div>
</template>
<script>
export default {
data() {
return {
msg: 'Hello world!',
}
},
}
</script>
<style>
.example {
color: red;
}
</style>
There are many cool features provided by vue-loader
:
<style>
and Pug for <template>
;.vue
file that can have custom loader chains applied to them;<style>
and <template>
as module dependencies and handle them with webpack loaders;In a nutshell, the combination of webpack and vue-loader
gives you a modern, flexible and extremely powerful front-end workflow for authoring Vue.js applications.
The following section is for maintainers and contributors who are interested in the internal implementation details of
vue-loader
, and is not required knowledge for end users.
vue-loader
is not a simple source transform loader. It handles each language blocks inside an SFC with its own dedicated loader chain (you can think of each block as a "virtual module"), and finally assembles the blocks together into the final module. Here's a brief overview of how the whole thing works:
vue-loader
parses the SFC source code into an SFC Descriptor using @vue/compiler-sfc
. It then generates an import for each language block so the actual returned module code looks like this:
// code returned from the main loader for 'source.vue'
// import the <template> block
import render from 'source.vue?vue&type=template'
// import the <script> block
import script from 'source.vue?vue&type=script'
export * from 'source.vue?vue&type=script'
// import <style> blocks
import 'source.vue?vue&type=style&index=1'
script.render = render
export default script
Notice how the code is importing source.vue
itself, but with different request queries for each block.
We want the content in script
block to be treated like .js
files (and if it's <script lang="ts">
, we want to to be treated like .ts
files). Same for other language blocks. So we want webpack to apply any configured module rules that matches .js
also to requests that look like source.vue?vue&type=script
. This is what VueLoaderPlugin
(src/plugins.ts
) does: for each module rule in the webpack config, it creates a modified clone that targets corresponding Vue language block requests.
Suppose we have configured babel-loader
for all *.js
files. That rule will be cloned and applied to Vue SFC <script>
blocks as well. Internally to webpack, a request like
import script from 'source.vue?vue&type=script'
Will expand to:
import script from 'babel-loader!vue-loader!source.vue?vue&type=script'
Notice the vue-loader
is also matched because vue-loader
are applied to .vue
files.
Similarly, if you have configured style-loader
+ css-loader
+ sass-loader
for *.scss
files:
<style scoped lang="scss">
Will be returned by vue-loader
as:
import 'source.vue?vue&type=style&index=1&scoped&lang=scss'
And webpack will expand it to:
import 'style-loader!css-loader!sass-loader!vue-loader!source.vue?vue&type=style&index=1&scoped&lang=scss'
When processing the expanded requests, the main vue-loader
will get invoked again. This time though, the loader notices that the request has queries and is targeting a specific block only. So it selects (src/select.ts
) the inner content of the target block and passes it on to the loaders matched after it.
For the <script>
block, this is pretty much it. For <template>
and <style>
blocks though, a few extra tasks need to be performed:
<style scoped>
blocks, after css-loader
but before style-loader
.Technically, these are additional loaders (src/templateLoader.ts
and src/stylePostLoader.ts
) that need to be injected into the expanded loader chain. It would be very complicated if the end users have to configure this themselves, so VueLoaderPlugin
also injects a global Pitching Loader (src/pitcher.ts
) that intercepts Vue <template>
and <style>
requests and injects the necessary loaders. The final requests look like the following:
// <template lang="pug">
import 'vue-loader/template-loader!pug-loader!source.vue?vue&type=template'
// <style scoped lang="scss">
import 'style-loader!vue-loader/style-post-loader!css-loader!sass-loader!vue-loader!source.vue?vue&type=style&index=1&scoped&lang=scss'
FAQs
> webpack loader for Vue Single-File Components
The npm package vue-loader receives a total of 2,173,353 weekly downloads. As such, vue-loader popularity was classified as popular.
We found that vue-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Malicious npm packages posing as Telegram bot libraries install SSH backdoors and exfiltrate data from Linux developer machines.
Security News
pip, PDM, pip-audit, and the packaging library are already adding support for Python’s new lock file format.
Product
Socket's Go support is now generally available, bringing automatic scanning and deep code analysis to all users with Go projects.