From e61b35151a13933a0406694c2380443395f5174b Mon Sep 17 00:00:00 2001 From: Maciej Adamczak Date: Wed, 31 May 2017 22:39:58 +0200 Subject: [PATCH] Clean-up and upgrade to CanJS3 --- README.md | 12 ++++++- clean-line-endings.js | 47 ---------------------------- index.js | 14 +++------ intermediate_and_imports.js | 62 ------------------------------------- package.json | 3 +- 5 files changed, 17 insertions(+), 121 deletions(-) delete mode 100644 clean-line-endings.js delete mode 100644 intermediate_and_imports.js diff --git a/README.md b/README.md index e46cff6..887206a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,13 @@ [![dependencies](https://img.shields.io/david/macku/can-stache-loader.svg)](https://david-dm.org/macku/can-stache-loader) [![dev dependencies](https://img.shields.io/david/dev/macku/can-stache-loader.svg)](https://david-dm.org/macku/can-stache-loader?type=dev) +
+ + + +
+ # CanJS Stache template loader for webpack Compiles [CanJS Stache](https://github.com/canjs/can-stache) templates with [can-view-parser](https://github.com/canjs/can-view-parser) and allows to load them with [webpack](https://webpack.github.io/) @@ -28,6 +35,8 @@ yarn add can-stache-loader ### Configure webpack 2+ +**webpack.config.js** + ```js { module: { @@ -46,7 +55,8 @@ yarn add can-stache-loader ### Import stache templates in your [CanJS](https://canjs.com/) project ```js -const tpl = require('./template.stache'); +import tpl from './template.stache'; + const html = tpl({ foo: 'bar' }); diff --git a/clean-line-endings.js b/clean-line-endings.js deleted file mode 100644 index 330b18f..0000000 --- a/clean-line-endings.js +++ /dev/null @@ -1,47 +0,0 @@ -var mustacheLineBreakRegExp = /(?:(?:^|(\r?)\n)(\s*)(\{\{([^\}]*)\}\}\}?)([^\S\n\r]*)($|\r?\n))|(\{\{([^\}]*)\}\}\}?)/g; -/** - * @hide - * Returns the mustache mode split from the rest of the expression. - * @param {can.stache.Expression} expression - * @param {Object} state The state of HTML where the expression was found. - */ -function splitModeFromExpression (expression, state){ - expression = expression.trim(); - var mode = expression.charAt(0); - - if( "#/{&^>!".indexOf(mode) >= 0 ) { - expression = expression.substr(1).trim(); - } else { - mode = null; - } - // Triple braces do nothing within a tag. - if(mode === "{" && state.node) { - mode = null; - } - return { - mode: mode, - expression: expression - }; -}; - -/** - * @hide - * Prunes line breaks accoding to the mustache specification. - * @param {String} template - * @return {String} - */ -module.exports = function cleanLineEndings(template) { - return template.replace(mustacheLineBreakRegExp, function (whole, returnBefore, spaceBefore, special, expression, spaceAfter, returnAfter, spaceLessSpecial, spaceLessExpression, matchIndex) { - spaceAfter = spaceAfter || ''; - returnBefore = returnBefore || ''; - spaceBefore = spaceBefore || ''; - var modeAndExpression = splitModeFromExpression(expression || spaceLessExpression, {}); - if (spaceLessSpecial || '>{'.indexOf(modeAndExpression.mode) >= 0) { - return whole; - } else if ('^#!/'.indexOf(modeAndExpression.mode) >= 0) { - return special + (matchIndex !== 0 && returnAfter.length ? returnBefore + '\n' : ''); - } else { - return spaceBefore + special + spaceAfter + (spaceBefore.length || matchIndex !== 0 ? returnBefore + '\n' : ''); - } - }); -} diff --git a/index.js b/index.js index 47254a5..411ee8d 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,11 @@ -const getIntermediateAndImports = require('./intermediate_and_imports'); -// const getIntermediateAndImports = require('can-stache/src/intermediate_and_imports'); -// const getIntermediateAndImports = require('can/dist/cjs/view/stache/intermediate_and_imports'); - +const getIntermediateAndImports = require('can-stache/src/intermediate_and_imports'); const getTemplate = (source, imports) => { const requires = imports.map(i => `require('${i}');`).join('\n'); - return `var stache = require('can/dist/cjs/view/stache/stache'); -var mustacheCore = require('can/dist/cjs/view/stache/mustache_core'); -var getIntermediateAndImports = require('can/dist/cjs/view/stache/intermediate_and_imports'); + return `var stache = require('can-stache'); +var mustacheCore = require('can-stache/src/mustache_core'); +var getIntermediateAndImports = require('can-stache/src/intermediate_and_imports'); ${requires} @@ -27,11 +24,10 @@ module.exports = function (scope, options, nodeList) { return renderer(scope, options.add(moduleOptions), nodeList); };`; -} +}; module.exports = function canStacheLoader(source) { const src = JSON.stringify(source); - const intermediateAndImports = getIntermediateAndImports(source); return getTemplate(src, intermediateAndImports.imports); diff --git a/intermediate_and_imports.js b/intermediate_and_imports.js deleted file mode 100644 index 3f1633c..0000000 --- a/intermediate_and_imports.js +++ /dev/null @@ -1,62 +0,0 @@ -const parser = require('can-view-parser'); -const cleanLineEndings = require('./clean-line-endings'); - -module.exports = function(source){ - var template = cleanLineEndings(source); - var imports = [], dynamicImports = [], ases = {}, inImport = false, inFrom = false, inAs = false, isUnary = false, currentAs = '', currentFrom = ''; - var intermediate = parser(template, { - start: function (tagName, unary) { - isUnary = unary; - if (tagName === 'can-import') { - inImport = true; - } else if (inImport) { - inImport = false; - } - }, - attrStart: function (attrName) { - if (attrName === 'from') { - inFrom = true; - } else if (attrName === 'as' || attrName === 'export-as') { - inAs = true; - } - }, - attrEnd: function (attrName) { - if (attrName === 'from') { - inFrom = false; - } else if (attrName === 'as' || attrName === 'export-as') { - inAs = false; - } - }, - attrValue: function (value) { - if (inFrom && inImport) { - imports.push(value); - if (!isUnary) { - dynamicImports.push(value); - } - currentFrom = value; - } else if (inAs && inImport) { - currentAs = value; - } - }, - end: function (tagName) { - if (tagName === 'can-import') { - if (currentAs) { - ases[currentAs] = currentFrom; - currentAs = ''; - } - } - }, - close: function (tagName) { - if (tagName === 'can-import') { - imports.pop(); - } - } - }, true); - return { - intermediate: intermediate, - imports: imports, - dynamicImports: dynamicImports, - ases: ases, - exports: ases - }; -}; diff --git a/package.json b/package.json index 521ff34..a627e6c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "can-stache-loader", - "version": "0.2.1", + "version": "1.0.0-beta", "description": "CanJS Stache loader for WebPack", "main": "index.js", "scripts": { @@ -20,7 +20,6 @@ "mustache" ], "dependencies": { - "can": "^2.3.31", "can-stache": "^3.0.25", "can-view-parser": "^3.3.0" },