-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (54 loc) · 2.14 KB
/
index.js
File metadata and controls
59 lines (54 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const path = require('path')
const fs = require('fs')
const loaderUtils = require("loader-utils")
let mainContents = {}
let getLibMainContent = function (projectDir, libName) {
if (mainContents[libName]) {
return mainContents[libName]
}
let libDir = projectDir + path.sep + 'node_modules' + path.sep + libName
let packageFile = libDir + path.sep + 'package.json'
let pkg = require(packageFile)
let mainFile = pkg.main || 'index.js'
mainFile = path.join(libDir + path.sep + mainFile)
mainContents[libName] = [fs.readFileSync(mainFile).toString().trim(), mainFile]
return mainContents[libName]
}
let getReplacedStatement = function (projectDir, libname, target) {
// import Page from './components/page/index.vue'
let main = getLibMainContent(projectDir, libname)
let mainContent = main[0], mainFile = main[1]
let reg = new RegExp('import[ |\\t]+'+target.trim()+'[ |\\t]+from[ |\\t]+[\'|"][^\'|^"]+[\'|"]', 'i')
let matchs = mainContent.match(reg)
if (matchs) {
let targetPath = matchs[0].split('from')[1].trim().substr(1)
targetPath = targetPath.substr(0, targetPath.length-1)
return matchs[0].split('from')[0] + ' from "' + path.join(path.dirname(mainFile), targetPath) + '"'
}
return null
}
module.exports = function (source) {
const options = loaderUtils.getOptions(this)
if (!options || !options.libname || this.resourcePath.indexOf('node_modules') > -1) {
return source
}
let projectDir = options.projectDir || process.cwd()
console.log("\ndimport-loader " + this.resourcePath)
let libname = options.libname
// import {Page, Spinner} from 'spd-ui'
const reg = new RegExp('import[ |\\t]+{[\\w|,| |\\t]+}[ |\\t]+from[ |\\t]+[\'|"]'+options.libname+'[\'|"]', 'gi')
const matchs = source.match(reg)
if (matchs) {
for (let i=0; i<matchs.length; i++) {
let replacement = []
matchs[i].split('{')[1].split('}')[0].split(',').forEach(target => {
let statement = getReplacedStatement(projectDir, options.libname, target)
if (statement) {
replacement.push(statement)
}
})
source = source.replace(matchs[i], replacement.join(";"))
}
}
return source
}