Skip to content

Commit ca3ba73

Browse files
committed
init
0 parents  commit ca3ba73

File tree

20 files changed

+390
-0
lines changed

20 files changed

+390
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
npm-debug.log
3+
test/output
4+
.DS_Store

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 MopTym
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# vue-auto-import-loader
2+
3+
[![Version](https://img.shields.io/npm/v/vue-auto-import-loader.svg?style=flat-square)](https://www.npmjs.com/package/vue-auto-import-loader)
4+
[![License](https://img.shields.io/npm/l/vue-auto-import-loader.svg?style=flat-square)](LICENSE)
5+
6+
> Auto import template / style / script for *.vue file.
7+
8+
## Install
9+
10+
```shell
11+
npm i -D vue-auto-import-loader
12+
```
13+
14+
## webpack config
15+
16+
```js
17+
{
18+
module: {
19+
loaders: [
20+
{
21+
test: /\.vue$/,
22+
loader: 'vue!vue-auto-import'
23+
}
24+
]
25+
},
26+
vueAutoImport: {
27+
scoped: false,
28+
files: { // relative to *.vue file path
29+
html: '[name].html',
30+
css: '[name].css',
31+
js: '[name].js',
32+
}
33+
}
34+
}
35+
```

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/loader')

lib/loader.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
var parse = require('./parse')
4+
5+
module.exports = function (content) {
6+
var filePath = this.resourcePath
7+
var options = resolveOptions(this.options)
8+
var parts = parse(content, filePath)
9+
var output = content
10+
11+
if (!parts.template) {
12+
output += autoImportTemplate(filePath, options)
13+
}
14+
if (!parts.style) {
15+
output += autoImportStyle(filePath, options)
16+
}
17+
if (!parts.script) {
18+
output += autoImportScript(filePath, options)
19+
}
20+
21+
return output
22+
}
23+
24+
function autoImportTemplate(filePath, options) {
25+
var filePathList = resolveFilePathList(filePath, options.files.html)
26+
var filePathToImport = getImportableFilePath(filePathList)
27+
return filePathToImport
28+
? '<template src="' + filePathToImport + '"></template>'
29+
: ''
30+
}
31+
32+
function autoImportStyle(filePath, options) {
33+
var filePathList = resolveFilePathList(filePath, options.files.css)
34+
var filePathToImport = getImportableFilePath(filePathList)
35+
var scopedStr = options.scoped ? 'scoped' : ''
36+
return filePathToImport
37+
? '<style src="' + filePathToImport + '" ' + scopedStr + '></style>'
38+
: ''
39+
}
40+
41+
function autoImportScript(filePath, options) {
42+
var filePathList = resolveFilePathList(filePath, options.files.js)
43+
var filePathToImport = getImportableFilePath(filePathList)
44+
return filePathToImport
45+
? '<script src="' + filePathToImport + '"></script>'
46+
: ''
47+
}
48+
49+
function resolveFilePathList (rootFilePath, list) {
50+
var fileName = path.basename(rootFilePath, '.vue')
51+
var rootPath = path.dirname(rootFilePath)
52+
return list.map(function (filePath) {
53+
return path.resolve(rootPath, filePath.replace(/\[name\]/g, fileName))
54+
})
55+
}
56+
57+
function getImportableFilePath (filePathList) {
58+
for (var i = 0; i < filePathList.length; i++) {
59+
if (fileExistsSync(filePathList[i])) {
60+
return filePathList[i]
61+
}
62+
}
63+
return null
64+
}
65+
66+
function fileExistsSync (filePath) {
67+
try {
68+
fs.statSync(path.resolve(__dirname, filePath))
69+
return true
70+
} catch (err) {
71+
return false
72+
}
73+
}
74+
75+
function resolveOptions (config) {
76+
var options = config.vueAutoImport || {}
77+
options.scoped = !!options.scoped
78+
options.files = options.files || {}
79+
options.files.html = getArray(options.files.html || '[name].html')
80+
options.files.css = getArray(options.files.css || '[name].css')
81+
options.files.js = getArray(options.files.js || '[name].js')
82+
return options
83+
}
84+
85+
function getArray (tar) {
86+
var isArray = Object.prototype.toString.call(tar) === '[object Array]'
87+
return isArray ? tar : [tar]
88+
}

lib/parse.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var parse5 = require('parse5')
2+
var cache = require('lru-cache')(1000)
3+
var hash = require('hash-sum')
4+
5+
module.exports = function (content, filePath) {
6+
var cacheKey = hash(content, filePath)
7+
var output = cache.get(cacheKey)
8+
if (output) { return output }
9+
10+
output = {}
11+
12+
var fragment = parse5.parseFragment(content)
13+
14+
fragment.childNodes.forEach(function (node) {
15+
output[node.tagName] = true
16+
})
17+
18+
cache.set(cacheKey, output)
19+
return output
20+
}

package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "vue-auto-import-loader",
3+
"version": "0.1.0",
4+
"description": "Auto import template / style / script for *.vue file.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha test/test.js --slow 5000 --timeout 10000"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/WEBuster/vue-auto-import-loader.git"
12+
},
13+
"keywords": [
14+
"vue",
15+
"auto",
16+
"import"
17+
],
18+
"author": "MopTym <[email protected]>",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/WEBuster/vue-auto-import-loader/issues"
22+
},
23+
"homepage": "https://github.com/WEBuster/vue-auto-import-loader#readme",
24+
"dependencies": {
25+
"hash-sum": "^1.0.2",
26+
"lru-cache": "^4.0.1",
27+
"parse5": "^2.2.0"
28+
},
29+
"devDependencies": {
30+
"chai": "^3.5.0",
31+
"mocha": "^3.0.2",
32+
"raw-loader": "^0.5.1",
33+
"rimraf": "^2.5.4",
34+
"webpack": "^1.13.2"
35+
},
36+
"peerDependencies": {
37+
"vue-loader": "*"
38+
}
39+
}

test/fixtures/blank-importable/blank.vue

Whitespace-only changes.

test/fixtures/blank-specific-name/blank.vue

Whitespace-only changes.

test/fixtures/blank-specific-name/script.js

Whitespace-only changes.

0 commit comments

Comments
 (0)