diff --git a/bin/index.js b/bin/index.js index 4c43781..900ae54 100755 --- a/bin/index.js +++ b/bin/index.js @@ -1,13 +1,18 @@ #!/usr/bin/env node -const fs = require('fs'); -const { program } = require('commander'); -const path = require('path'); -const pkg = require('../package.json'); +import fs from 'fs'; +import { program } from 'commander'; +import { SassStyleTemplate } from '../lib/index.js'; +import path from 'path'; +import { fileURLToPath } from 'url'; -const SassStyleTemplate = require('../lib/index.js'); +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); -const customTemplate = path.resolve('.sass-template.tmpl'); +const pkgPath = path.resolve(__dirname, '../package.json'); +const customTemplate = path.resolve(__dirname, '../.sass-template.tmpl'); + +const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); program .version(pkg.version, '-v, --version', 'show version number') @@ -25,6 +30,6 @@ const template = fs.existsSync(customTemplate) ? fs.readFileSync(customTemplate, 'utf8') : undefined; -const config = Object.assign({}, program.opts(), { template: template }); +const config = Object.assign({}, program.opts(), { template }); new SassStyleTemplate(config); diff --git a/lib/index.js b/lib/index.js index be3517c..4252757 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,14 @@ -const fs = require('fs'); -const glob = require('glob'); -const sass = require('sass'); -const path = require('path'); -const chokidar = require('chokidar'); -const mkdirp = require('mkdirp'); -const autoprefixer = require('autoprefixer'); -const postcss = require('postcss'); +import fs from 'fs'; +import path from 'path'; +import { globSync } from 'tinyglobby'; +import chokidar from 'chokidar'; +import { fileURLToPath } from 'url'; +import * as sass from 'sass'; +import autoprefixer from 'autoprefixer'; +import postcss from 'postcss'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.join(__filename, '../..'); const color = { reset: '\x1b[0m', @@ -17,7 +20,7 @@ const color = { const delimTemplate = /<%\s*content\s*%>/; -class SassStyleTemplate { +export class SassStyleTemplate { constructor({ markerStart = 'const styles = css`', markerEnd = '`;', @@ -27,7 +30,7 @@ class SassStyleTemplate { jsFile = 'js', destination = undefined, template = fs.readFileSync( - `${__dirname}/../tpls/sass-template.tmpl`, + path.join(__dirname, 'tpls/sass-template.tmpl'), 'utf8' ), } = {}) { @@ -41,7 +44,8 @@ class SassStyleTemplate { destination, template, }; - this.globFiles = glob.sync(`{${this.options.customGlob},}`); + + this.globFiles = globSync(this.options.customGlob.split(',')); this.init(); } @@ -64,16 +68,16 @@ class SassStyleTemplate { return this._fileInfo; } - set fileInfo(fileInfo = {}) { - this._fileInfo = fileInfo; + set fileInfo(fileInfo) { + this._fileInfo = fileInfo || {}; } get globFiles() { return this._globFiles; } - set globFiles(files = []) { - this._globFiles = files; + set globFiles(files) { + this._globFiles = files || []; } cleanDestinationPath() { @@ -87,8 +91,7 @@ class SassStyleTemplate { if (lastCharacter === path.sep) { strFinal = strFinal.slice(0, strFinal.length - 1); } - mkdirp.sync(strFinal); - + fs.mkdirSync(strFinal, { recursive: true }); return `${path.resolve(strFinal)}`; } @@ -156,6 +159,7 @@ class SassStyleTemplate { } watchSass(glob) { + console.log(`Watching ${glob}`); const watcher = chokidar.watch(glob, { ignoreInitial: true }); watcher.on('change', () => { @@ -194,7 +198,7 @@ class SassStyleTemplate { } updateGlob() { - this.globFiles = glob.sync(`{${this.options.customGlob}}`); + this.globFiles = globSync(this.options.customGlob.split(',')); } globSassFile(cb) { @@ -204,9 +208,7 @@ class SassStyleTemplate { } init() { - this.watchSass(this.options.customGlob.split(',')); + this.watchSass(this.globFiles); this.globSassFile(this.renderStylesTemplate); } } - -module.exports = SassStyleTemplate; diff --git a/package.json b/package.json index 0a6ee68..a39e809 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@blockquote/sass-style-template", - "version": "3.0.5", + "version": "4.0.0-rc.1", "description": "SASS and LitElement for creating Web Components", "keywords": [ "LitElement", @@ -19,6 +19,7 @@ "url": "https://github.com/oscarmarina/sass-style-template" }, "main": "./lib/index.js", + "type": "module", "bin": { "sass-style-template": "./bin/index.js" }, @@ -28,10 +29,9 @@ }, "dependencies": { "autoprefixer": "^10.4.20", - "chokidar": "^3.6.0", + "chokidar": "^4.0.1", "commander": "^12.1.0", - "glob": "^11.0.0", - "mkdirp": "^3.0.1", + "tinyglobby": "^0.2.10", "postcss": "^8.4.47", "sass": "^1.80.6" },