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' )
83
94const 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' )
107
118const compRegex = {
129 '?kebab' : new RegExp ( data . regex . kebabComponents || data . regex . components , 'g' ) ,
@@ -21,68 +18,76 @@ const funcCompRegex = new RegExp(
2118
2219const dirRegex = new RegExp ( data . regex . directives , 'g' )
2320
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 = ''
2737
2838 if ( comp !== null ) {
2939 // avoid duplicates
3040 comp = Array . from ( new Set ( comp ) )
3141
3242 // map comp names only if not pascal-case already
33- if ( form !== '?pascal' ) {
43+ if ( ctx . query !== '?pascal' ) {
3444 comp = comp . map ( name => data . importName [ name ] )
3545 }
3646
37- if ( form === '?combined' ) {
47+ if ( ctx . query === '?combined' ) {
3848 // could have been transformed QIcon and q-icon too,
3949 // so avoid duplicates
4050 comp = Array . from ( new Set ( comp ) )
4151 }
4252
43- comp = comp . join ( ',' )
44- }
45- else {
46- comp = ''
53+ importStatements += transform ( comp )
54+ installStatements += `qInstall(component, 'components', {${ comp . join ( ',' ) } });`
4755 }
4856
49- return {
50- comp,
57+ if ( dir !== null ) {
58+ dir = Array . from ( new Set ( dir ) )
59+ . map ( name => data . importName [ name ] )
5160
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 ( ',' ) } });`
5763 }
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+ `
5872}
5973
60- module . exports = function ( content ) {
74+ module . exports = function ( content , map ) {
75+ let newContent = content
76+
6177 if ( ! this . resourceQuery && funcCompRegex . test ( content ) === false ) {
6278 const file = this . fs . readFileSync ( this . resource , 'utf-8' ) . toString ( )
63- const { comp , dir } = extract ( file , this . query )
79+ const code = extract ( file , this )
6480
65- const hasComp = comp !== ''
66- const hasDir = dir !== ''
67-
68- if ( hasComp === true || hasDir === true ) {
81+ if ( code !== void 0 ) {
6982 const index = this . mode === 'development'
7083 ? content . indexOf ( '/* hot reload */' )
7184 : - 1
7285
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
8287 ? content + code
8388 : content . slice ( 0 , index ) + code + content . slice ( index )
8489 }
8590 }
8691
87- return content
92+ return this . callback ( null , newContent , map )
8893}
0 commit comments