1
- function getDevlandFile ( name ) {
2
- return require (
3
- require . resolve ( name , {
4
- paths : [ __dirname ]
5
- } )
6
- )
7
- }
1
+ const stringifyRequest = require ( 'loader-utils/lib/stringifyRequest' )
2
+ const getDevlandFile = require ( './get-devland-file.js' )
8
3
9
4
const data = getDevlandFile ( 'quasar/dist/babel-transforms/auto-import.json' )
5
+ const importTransform = getDevlandFile ( 'quasar/dist/babel-transforms/imports.js' )
6
+ const runtimePath = require . resolve ( './runtime.auto-import.js' )
10
7
11
8
const compRegex = {
12
9
'?kebab' : new RegExp ( data . regex . kebabComponents || data . regex . components , 'g' ) ,
@@ -21,68 +18,76 @@ const funcCompRegex = new RegExp(
21
18
22
19
const dirRegex = new RegExp ( data . regex . directives , 'g' )
23
20
24
- function extract ( content , form ) {
25
- let comp = content . match ( compRegex [ form ] )
26
- const directives = content . match ( dirRegex )
21
+ function transform ( itemArray ) {
22
+ return itemArray
23
+ . map ( name => `import ${ name } from '${ importTransform ( name ) } ';` )
24
+ . join ( `\n` )
25
+ }
26
+
27
+ function extract ( content , ctx ) {
28
+ let comp = content . match ( compRegex [ ctx . query ] )
29
+ let dir = content . match ( dirRegex )
30
+
31
+ if ( comp === null && dir === null ) {
32
+ return
33
+ }
34
+
35
+ let importStatements = ''
36
+ let installStatements = ''
27
37
28
38
if ( comp !== null ) {
29
39
// avoid duplicates
30
40
comp = Array . from ( new Set ( comp ) )
31
41
32
42
// map comp names only if not pascal-case already
33
- if ( form !== '?pascal' ) {
43
+ if ( ctx . query !== '?pascal' ) {
34
44
comp = comp . map ( name => data . importName [ name ] )
35
45
}
36
46
37
- if ( form === '?combined' ) {
47
+ if ( ctx . query === '?combined' ) {
38
48
// could have been transformed QIcon and q-icon too,
39
49
// so avoid duplicates
40
50
comp = Array . from ( new Set ( comp ) )
41
51
}
42
52
43
- comp = comp . join ( ',' )
44
- }
45
- else {
46
- comp = ''
53
+ importStatements += transform ( comp )
54
+ installStatements += `qInstall(component, 'components', {${ comp . join ( ',' ) } });`
47
55
}
48
56
49
- return {
50
- comp,
57
+ if ( dir !== null ) {
58
+ dir = Array . from ( new Set ( dir ) )
59
+ . map ( name => data . importName [ name ] )
51
60
52
- dir : directives !== null
53
- ? Array . from ( new Set ( directives ) )
54
- . map ( name => data . importName [ name ] )
55
- . join ( ',' )
56
- : ''
61
+ importStatements += transform ( dir )
62
+ installStatements += `qInstall(component, 'directives', {${ dir . join ( ',' ) } });`
57
63
}
64
+
65
+ // stringifyRequest needed so it doesn't
66
+ // messes up consistency of hashes between builds
67
+ return `
68
+ ${ importStatements }
69
+ import qInstall from ${ stringifyRequest ( ctx , runtimePath ) } ;
70
+ ${ installStatements }
71
+ `
58
72
}
59
73
60
- module . exports = function ( content ) {
74
+ module . exports = function ( content , map ) {
75
+ let newContent = content
76
+
61
77
if ( ! this . resourceQuery && funcCompRegex . test ( content ) === false ) {
62
78
const file = this . fs . readFileSync ( this . resource , 'utf-8' ) . toString ( )
63
- const { comp , dir } = extract ( file , this . query )
79
+ const code = extract ( file , this )
64
80
65
- const hasComp = comp !== ''
66
- const hasDir = dir !== ''
67
-
68
- if ( hasComp === true || hasDir === true ) {
81
+ if ( code !== void 0 ) {
69
82
const index = this . mode === 'development'
70
83
? content . indexOf ( '/* hot reload */' )
71
84
: - 1
72
85
73
- const code = `\nimport {${ comp } ${ hasComp === true && hasDir === true ? ',' : '' } ${ dir } } from 'quasar'\n` +
74
- ( hasComp === true
75
- ? `component.options.components = Object.assign(Object.create(component.options.components || null), component.options.components || {}, {${ comp } })\n`
76
- : '' ) +
77
- ( hasDir === true
78
- ? `component.options.directives = Object.assign(Object.create(component.options.directives || null), component.options.directives || {}, {${ dir } })\n`
79
- : '' )
80
-
81
- return index === - 1
86
+ newContent = index === - 1
82
87
? content + code
83
88
: content . slice ( 0 , index ) + code + content . slice ( index )
84
89
}
85
90
}
86
91
87
- return content
92
+ return this . callback ( null , newContent , map )
88
93
}
0 commit comments