Skip to content

Commit 2193df1

Browse files
Add support for src imports
1 parent 41b8163 commit 2193df1

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ You can either use this loader inline, or add it to your `webpack.config.js` fil
4242
01. [Basic](./examples/01-base/) - A basic example which shows how to use this loader.
4343
02. [vue-play](./examples/02-play) - Shows how to define an alias for the loader, so that you can `import "vue-play-loader!./component.vue"`.
4444
03. [Testing](./examples/03-play) - Sets the loader in `webpack.config.js`, without applying it to too much paths. (see [the previous warning](#warning-inline-loader))
45+
04. [Src import](./examples/04-src-import/) - This loader also works if the custom block specifies its value using the `src` attribute.

examples/04-src-import/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "vue-extract-loader.04-src-import",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"build": "webpack"
8+
},
9+
"devDependencies": {
10+
"css-loader": "^0.28.2",
11+
"vue-loader": "^12.1.0",
12+
"vue-template-compiler": "^2.3.3",
13+
"webpack": "^2.6.0"
14+
},
15+
"dependencies": {
16+
"vue": "^2.3.3"
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
name: "component",
3+
author: "Nicolò Ribaudo",
4+
description: "An Hello World component",
5+
version: "0.1.0",
6+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<template>
2+
<p class="text">{{ message }}</p>
3+
</template>
4+
5+
<script>
6+
7+
export default {
8+
data: () => ({
9+
message: "Hello World!",
10+
}),
11+
}
12+
13+
</script>
14+
15+
<style>
16+
17+
.text {
18+
color: blue;
19+
}
20+
21+
</style>
22+
23+
<info src="./component.info.js"></info>

examples/04-src-import/src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// This only imports the <info> block of the component.vue file
2+
import info from "vue-extract-loader?block=info!./component.vue";
3+
4+
console.log(info);
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
module.exports = {
6+
entry: "./src/index.js",
7+
output: {
8+
filename: "./bundle.js",
9+
path: path.resolve("dist"),
10+
},
11+
12+
resolve: {
13+
extensions: [ ".js", ".vue" ],
14+
},
15+
16+
resolveLoader: {
17+
alias: {
18+
// This is only needed in order to alias "vue-extract-loader" to
19+
// the root folder of this package.
20+
// You shouldn't do this in your project.
21+
"vue-extract-loader": require.resolve("../../"),
22+
},
23+
},
24+
25+
module: {
26+
rules: [ {
27+
test: /\.vue$/,
28+
loader: "vue-loader",
29+
} ],
30+
},
31+
};

lib/loader.js

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ module.exports = function loader(source, map) {
1515

1616
void map;
1717

18+
if (block.attrs.src) {
19+
return `module.exports = require(${JSON.stringify(block.attrs.src)})`;
20+
}
21+
1822
return block.content;
1923
};
2024

0 commit comments

Comments
 (0)