Skip to content

Commit c340dec

Browse files
author
Tom Maton
committed
Bumped version, added new option to the code to determine how to generate styles
1 parent d41d882 commit c340dec

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ $ yarn add gulp-css-to-polymer
1919
## Examples
2020

2121
```js
22-
var stylemod = require('gulp-css-to-polymer');
22+
const polymerizeCSS = require('gulp-css-to-polymer');
2323

2424
// Wrap css files
2525
gulp.task("polymerize", () => {
2626
gulp.src("./src/**/*.css")
27-
.pipe(stylemod({
27+
.pipe(polymerizeCSS({
2828
prefix: 'tg-',
29-
suffix: '-styles'
29+
suffix: '-styles',
30+
pwa: true
3031
}))
3132
.pipe(gulp.dest("./dist"));
3233
}
@@ -40,7 +41,9 @@ gulp.task("polymerize", () => {
4041
// string to be used for the beginning of the file name & module ids.
4142
prefix: 'tg-',
4243
// string to be used for the end of the file name & module ids.
43-
suffix: '-styles'
44+
suffix: '-styles',
45+
// boolean, determines how the styles are generated as differentate between Polymer and Polymer PWA
46+
pwa: true // default is false
4447
}
4548
```
4649

index.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,38 @@ const gutil = require('gulp-util');
44

55
const 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

1140
module.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`;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gulp-css-to-polymer",
3-
"version": "1.0.2",
3+
"version": "1.0.4",
44
"description": "Converting CSS to a ES6 Module for Polymer V3.x.x",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)