@stylin/msa-loader
Advanced tools
Comparing version 0.1.1 to 0.2.0
import { MSABase } from './parser'; | ||
export interface MSA extends MSABase { | ||
className: string; | ||
compName: string; | ||
componentName: string; | ||
tagName: string; | ||
@@ -6,0 +6,0 @@ } |
@@ -21,18 +21,12 @@ "use strict"; | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.pitch = void 0; | ||
const R = __importStar(require("ramda")); | ||
const Handlebars = __importStar(require("handlebars")); | ||
const parser_1 = require("./parser"); | ||
const path_1 = require("path"); | ||
const fs_1 = require("fs"); | ||
Handlebars.registerHelper(`json`, JSON.stringify); | ||
const template = fs_1.readFileSync(path_1.join(__dirname, `template.hbs`), `utf8`); | ||
const toDTS = Handlebars.compile(template, { noEscape: true }); | ||
const pickRawValues = R.applySpec({ | ||
@@ -63,37 +57,9 @@ defValue: R.nth(1), | ||
} | ||
const pickLocals = R.pipe(R.match(/export default (.+);/), R.nth(1)); | ||
const parseCommentOnly = R.pipe(R.replace(/@.+/g, ``), R.when(R.includes(`{`), R.replace(/\n|\r/g, ` `)), parseComment); | ||
function extract(name, text) { | ||
const re = new RegExp(`@${name}[:\\s]+(\\w+)`); | ||
const [, value] = text.match(re) || []; | ||
return value; | ||
const re = new RegExp(`@${name}[:\\s]+(.+)[\n|\r]+`); | ||
const [, value = ``] = text.match(re) || []; | ||
return value.trim(); | ||
} | ||
const defineComponents = R.compose(R.join(`\n`), R.map((_a) => { | ||
var { compName } = _a, msa = __rest(_a, ["compName"]); | ||
return `export const ${compName} = styled(${JSON.stringify(msa)});`; | ||
})); | ||
const parseStyleLoaderContent = (text) => ` | ||
${text} | ||
const styled = createComponent(content.locals); | ||
`; | ||
function parseMiniCssContent(text) { | ||
const [, cssList] = text.match(/export default (.+);/); | ||
return ` | ||
const cssList = ${cssList}; | ||
const styled = createComponent(cssList); | ||
export default cssList; | ||
`; | ||
} | ||
const parseContent = R.ifElse(R.includes(`import content`), parseStyleLoaderContent, parseMiniCssContent); | ||
function loader(content, sourceMap, meta = {}) { | ||
meta.msa = this.data.msa; | ||
this.addDependency(this.resource); | ||
const onComplete = this.async(); | ||
const exports = defineComponents(this.data.msa); | ||
const result = ` | ||
import {createComponent} from "@stylin/style"; | ||
${parseContent(content)} | ||
${exports}; | ||
`; | ||
onComplete(null, result, sourceMap, meta); | ||
} | ||
const parseCommentOnly = R.pipe(R.replace(/@\w+[:\s]+(\w+)/g, ``), R.when(R.includes(`{`), R.replace(/\n|\r/g, ` `)), parseComment); | ||
function parseComments(text) { | ||
@@ -106,3 +72,3 @@ const reComment = /\/\*+((\r|\n|.[^*])+)\*+\/[\n\r]+\.([\w-]+)/gm; | ||
const [, comment, , className] = matches; | ||
const pack = R.append(Object.assign({ className, compName: extract(`component`, comment), tagName: extract(`tag`, comment) || `div` }, parseCommentOnly(comment))); | ||
const pack = R.append(Object.assign({ className, componentName: extract(`component`, comment), tagName: extract(`tag`, comment) }, parseCommentOnly(comment))); | ||
return pack(match(re)); | ||
@@ -112,2 +78,14 @@ }; | ||
} | ||
function loader(content, sourceMap, meta = {}) { | ||
meta.msa = this.data.msa; | ||
this.addDependency(this.resource); | ||
const onComplete = this.async(); | ||
const dts = toDTS({ | ||
content, | ||
isStyleLoader: content.includes(`import content`), | ||
locals: pickLocals(content), | ||
msa: meta.msa, | ||
}); | ||
onComplete(null, dts, sourceMap, meta); | ||
} | ||
function pitch(skip, me, sharedData) { | ||
@@ -114,0 +92,0 @@ const onComplete = this.async(); |
{ | ||
"name": "@stylin/msa-loader", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Stylin loader module for webpack", | ||
@@ -20,4 +20,5 @@ "license": "MIT", | ||
"build": "npm run clean && npm run compile", | ||
"clean": "rm -rf ./dist", | ||
"compile": "tsc", | ||
"clean": "rm -rf ./dist && npm run copy-files", | ||
"compile": "tsc -b", | ||
"copy-files": "mkdir dist && cp ./src/*.hbs ./dist/", | ||
"lint": "eslint . --ext .tsx,.ts --fix", | ||
@@ -28,2 +29,3 @@ "prepublishOnly": "npm run build", | ||
"devDependencies": { | ||
"@types/node": "^14.14.31", | ||
"@types/ramda": "^0.27.38", | ||
@@ -37,2 +39,3 @@ "@typescript-eslint/eslint-plugin": "^4.15.0", | ||
"dependencies": { | ||
"handlebars": "^4.7.7", | ||
"ramda": "^0.27.1" | ||
@@ -43,3 +46,3 @@ }, | ||
}, | ||
"gitHead": "0c908a5f4fd303ebf6e2fd84255b4dfbc75ffe87" | ||
"gitHead": "226894fec336c3f532652ca9f9c62297e2a2d242" | ||
} |
Sorry, the diff of this file is not supported yet
9
18955
2
7
213
+ Addedhandlebars@^4.7.7
+ Addedhandlebars@4.7.8(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedneo-async@2.6.2(transitive)
+ Addedsource-map@0.6.1(transitive)
+ Addeduglify-js@3.19.3(transitive)
+ Addedwordwrap@1.0.0(transitive)