@@ -4,9 +4,38 @@ const gutil = require('gulp-util');
44
55const PLUGIN_NAME = 'gulp-css-to-polymer' ;
66
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+ ) ;
1039
1140module . exports = opts => through . obj ( ( file , enc , cb ) => {
1241 const fileObj = file ;
@@ -20,19 +49,11 @@ module.exports = opts => through.obj((file, enc, cb) => {
2049
2150 const moduleId = generateModuleName ( opts , file ) ;
2251 const dirname = path . dirname ( file . path ) ;
52+ const isPWA = ! ! opts . pwa ;
2353
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 ) ;
3657
3758 fileObj . contents = Buffer . from ( res ) ;
3859 fileObj . path = `${ path . join ( dirname , moduleId ) } .js` ;
0 commit comments