@@ -4,9 +4,38 @@ const gutil = require('gulp-util');
4
4
5
5
const PLUGIN_NAME = 'gulp-css-to-polymer' ;
6
6
7
- function generateModuleName ( options , file ) {
8
- return `${ options . prefix } ${ path . basename ( file . path , path . extname ( file . path ) ) } ${ options . suffix } ` ;
9
- }
7
+ const camelCaseModuleId = ( moduleId ) => {
8
+ return moduleId . replace ( / ^ ( [ A - Z ] ) | [ \s - _ ] + ( \w ) / g, ( match , p1 , p2 ) => {
9
+ if ( p2 ) return p2 . toUpperCase ( ) ;
10
+ return p1 . toLowerCase ( ) ;
11
+ } ) ;
12
+ } ;
13
+
14
+ const generateModuleName = ( options , file ) => `${ options . prefix } ${ path . basename ( file . path , path . extname ( file . path ) ) } ${ options . suffix } ` ;
15
+
16
+ const generatePolymerStyle = ( styles , moduleId ) => ( `import '@polymer/polymer/polymer-element';
17
+
18
+ const $_documentContainer = document.createElement('template');
19
+ $_documentContainer.innerHTML = \`<dom-module id="${ moduleId } ">
20
+ <template>
21
+ <style>
22
+ ${ styles . toString ( 'utf8' ) }
23
+ </style>
24
+ </template>
25
+ </dom-module>\`;
26
+
27
+ document.head.appendChild($_documentContainer.content);
28
+ `
29
+ ) ;
30
+
31
+ const generatePWAStyle = ( styles , moduleId ) => ( `import { html } from '@polymer/lit-element';
32
+
33
+ export const ${ camelCaseModuleId ( moduleId ) } = html \`
34
+ <style>
35
+ ${ styles . toString ( 'utf8' ) }
36
+ </style>\`;
37
+ `
38
+ ) ;
10
39
11
40
module . exports = opts => through . obj ( ( file , enc , cb ) => {
12
41
const fileObj = file ;
@@ -20,19 +49,11 @@ module.exports = opts => through.obj((file, enc, cb) => {
20
49
21
50
const moduleId = generateModuleName ( opts , file ) ;
22
51
const dirname = path . dirname ( file . path ) ;
52
+ const isPWA = ! ! opts . pwa ;
23
53
24
- const res = `import '@polymer/polymer/polymer-element';
25
-
26
- const $_documentContainer = document.createElement('template');
27
- $_documentContainer.innerHTML = \`<dom-module id=${ moduleId } >
28
- <template>
29
- <style>
30
- ${ file . contents . toString ( 'utf8' ) }
31
- </style>
32
- </template>
33
- </dom-module>\`;
34
-
35
- document.head.appendChild($_documentContainer.content);` ;
54
+ const res = ( isPWA )
55
+ ? generatePWAStyle ( file . contents , moduleId )
56
+ : generatePolymerStyle ( file . contents , moduleId ) ;
36
57
37
58
fileObj . contents = Buffer . from ( res ) ;
38
59
fileObj . path = `${ path . join ( dirname , moduleId ) } .js` ;
0 commit comments