diff --git a/.vscode/launch.json b/.vscode/launch.json index ece0aaa..062a1b0 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -3,13 +3,17 @@ // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", - "configurations": [{ + "configurations": [ + + { "name": "Attach to Process", "type": "node", "request": "attach", "port": 9229, "protocol": "inspector", - "restart": true + "restart": true, + "outFiles": ["${workspaceRoot}/lib"], + "sourceMaps": true }, { "type": "node", diff --git a/example/app.js b/example/app.js index d418ab3..a47ceff 100644 --- a/example/app.js +++ b/example/app.js @@ -15,7 +15,7 @@ var exampleMixin = { }; const options = { - rootPath: path.join(__dirname, '/vueFiles'), + rootPath: path.join(__dirname, ''), vue: { head: { meta: [{ @@ -66,7 +66,7 @@ app.get('/', (req, res) => { ] } } - res.renderVue('main/main.vue', data, vueOptions) + res.renderVue('main.vue', data, vueOptions) }); app.listen(3000); diff --git a/example/components/uuid.vue b/example/components/uuid.vue index e4e7d6c..5bc401a 100644 --- a/example/components/uuid.vue +++ b/example/components/uuid.vue @@ -1,12 +1,12 @@ diff --git a/example/vueFiles/main.vue b/example/main.vue similarity index 100% rename from example/vueFiles/main.vue rename to example/main.vue diff --git a/example/vueFiles/components/uuid.vue b/example/vueFiles/components/uuid.vue deleted file mode 100644 index 99e296f..0000000 --- a/example/vueFiles/components/uuid.vue +++ /dev/null @@ -1,20 +0,0 @@ - - - - - diff --git a/example/vueFiles/components/uuid2.vue b/example/vueFiles/components/uuid2.vue deleted file mode 100644 index 1a007d3..0000000 --- a/example/vueFiles/components/uuid2.vue +++ /dev/null @@ -1,20 +0,0 @@ - - - - - diff --git a/example/vueFiles/main/main.vue b/example/vueFiles/main/main.vue deleted file mode 100644 index 7fe51bf..0000000 --- a/example/vueFiles/main/main.vue +++ /dev/null @@ -1,37 +0,0 @@ - - - - - diff --git a/example/vueFiles/mixins/exampleMixin.js b/example/vueFiles/mixins/exampleMixin.js deleted file mode 100644 index b89cc67..0000000 --- a/example/vueFiles/mixins/exampleMixin.js +++ /dev/null @@ -1,10 +0,0 @@ -const exampleMixin = { - methods: { - hello: function () { - console.log('Hello'); - } - } -}; - -module.exports = exampleMixin; -exports.default = exampleMixin; diff --git a/src/parser/component.js b/src/parser/component.js index 9f5cfff..06e9491 100644 --- a/src/parser/component.js +++ b/src/parser/component.js @@ -3,6 +3,7 @@ const fs = require('fs'); const camelCase = require('camel-case'); const compiler = require('vue-template-compiler'); +const CleanCSS = require('clean-css'); const styleParser = require('./style'); const htmlParser = require('./html'); const scriptParser = require('./script'); @@ -16,6 +17,7 @@ function componentParser(templatePath: string, defaults: Object, type: string, C scriptParser(cachedComponentContentObject.parsedContent.script, defaults, type, Cache).then(parsedScriptObject => { cachedComponentContentObject.script = parsedScriptObject; cachedComponentContentObject.script.template = cachedComponentContentObject.template; + cachedComponentContentObject.styles = parsedScriptObject.styles; resolve(cachedComponentContentObject); }).catch(error => { reject(error); @@ -64,7 +66,12 @@ function parseContent(content: string, templatePath: string, defaults: Object, t Promise.all(promiseArray).then(resultsArray => { const template = resultsArray[0]; const script = resultsArray[1]; - const style = resultsArray[2]; + let style = ''; + if (resultsArray[2]) { + style = resultsArray[2]; + } else { + style = new CleanCSS({}).minify(resultsArray[1].styles ? resultsArray[1].styles : '').styles; + } script.template = template; diff --git a/src/parser/style.js b/src/parser/style.js index de50467..bdd9ba7 100644 --- a/src/parser/style.js +++ b/src/parser/style.js @@ -12,17 +12,16 @@ type StyleObjectType = { function styleParser(styleObjectArray: StyleObjectType[]): Promise { return new Promise((resolve) => { - if (!styleObjectArray || styleObjectArray.length === 0) { - resolve(''); - } else { + let output = ''; + if (styleObjectArray && styleObjectArray.length > 0) { for (const styleObject of styleObjectArray) { if(styleObject.lang === 'scss' || styleObject.lang === 'less') { console.error('Sorry please only use plain CSS in your files for now'); } - const output = new CleanCSS({}).minify(styleObject.content); - resolve(output.styles); + output += new CleanCSS({}).minify(styleObject.content).styles; } } + resolve(output); }); } diff --git a/src/utils/require.js b/src/utils/require.js index bda0d54..9809e4c 100644 --- a/src/utils/require.js +++ b/src/utils/require.js @@ -104,14 +104,19 @@ function requireFromString(code: string, filename: string = '', optsObj: Object } Promise.all(promiseArray) .then(renderedItemArray => { + let styles = ''; for (var renderedItem of renderedItemArray) { const rawString = renderedItem.rendered.scriptStringRaw; code = code.replace(renderedItem.match, rawString); + if (renderedItem.rendered.layout && renderedItem.rendered.layout.style) { + styles += renderedItem.rendered.layout.style; + } } //check if its the last element and then render const last_element = code.match(options.vueFileRegex); if (last_element === undefined || last_element === null) { m._compile(code, filename); + m.exports.default.styles = styles; resolve(m.exports.default); } }) diff --git a/tests/index.js b/tests/index.js index a9c6df8..d63f734 100644 --- a/tests/index.js +++ b/tests/index.js @@ -5,7 +5,7 @@ const Models = require('../src/models'); const path = require('path'); const options = { - rootPath: path.join(__dirname, '../example/vueFiles') + rootPath: path.join(__dirname, '../example') }; const data = { @@ -20,8 +20,8 @@ const vueOptions = { } } -const exampleHead = `\nPage Title\n`; -const exampleScript = `` +const exampleHead = `\nPage Title\n`; +const exampleScript = ``; test('renders App object', t => { const renderer = new ExpressVueRenderer(options); diff --git a/tests/parser/index.js b/tests/parser/index.js index 21a6687..3f7bc31 100644 --- a/tests/parser/index.js +++ b/tests/parser/index.js @@ -20,9 +20,9 @@ var cacheOptions = { var lruCache = LRU(cacheOptions); let types = new Types(); -const component = __dirname + '/../../example/vueFiles/components/uuid.vue'; +const component = __dirname + '/../../example/components/uuid.vue'; const options = { - rootPath: path.join(__dirname, 'tests'), + rootPath: path.join(__dirname, '../../example'), component: 'uuid.vue' }; diff --git a/tests/utils/checkPathUtils.js b/tests/utils/checkPathUtils.js index 52180f9..5c80dd6 100644 --- a/tests/utils/checkPathUtils.js +++ b/tests/utils/checkPathUtils.js @@ -5,7 +5,7 @@ import { } from '../../src/utils'; import Models from '../../src/models'; -const rootPath = path.join(__dirname, '../../example/vueFiles/'); +const rootPath = path.join(__dirname, '../../example/'); const defaults = new Models.Defaults(); var LRU = require('lru-cache'); var cacheOptions = { @@ -15,7 +15,7 @@ var cacheOptions = { var lruCache = LRU(cacheOptions); test('correctPath Path', t => { - const filePath = path.join(rootPath, '../../example/vueFiles/components/uuid.vue'); + const filePath = path.join(rootPath, '../example/components/uuid.vue'); const correctPath = rootPath + 'components/uuid.vue'; return PathUtils.getCorrectPathForFile(filePath, 'view', defaults, lruCache)