Skip to content

Commit

Permalink
fix: fixed non-self-closing script tags failing to load
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Ellis committed Feb 27, 2018
1 parent 463b527 commit f1cf57b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
3 changes: 3 additions & 0 deletions changelog.md
@@ -1,5 +1,8 @@
# Change Log

## 0.4.2
- Non-self-closing external script tags (i.e. `<script src="foo"></script>`) failed to load

## 0.4.1
- Support for css modules [18](https://github.com/jackmellis/require-extension-hooks-vue/issues/18)

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "require-extension-hooks-vue",
"version": "0.4.1",
"version": "0.4.2",
"description": "Simple server parser for .vue files",
"main": "src/index.js",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions spec/external.spec.js
Expand Up @@ -2,6 +2,7 @@ import test from 'ava';
import Vue from 'vue';
import c from './vue-files/external';
import c2 from './vue-files/external/pug';
import c3 from './vue-files/non-self-closing';

test('imports component with external html', t => {
t.true(c !== undefined);
Expand All @@ -13,6 +14,11 @@ test('imports component with external js', t => {
t.is(typeof c.data, 'function');
});

test('imports component with non-self-closing tags', t => {
t.is(c3.name, 'external');
t.is(typeof c3.data, 'function');
});

test('imports component with external css-module', t => {
t.is(c.name, 'external');
t.is(typeof c.computed, 'object');
Expand Down
4 changes: 4 additions & 0 deletions spec/vue-files/non-self-closing.vue
@@ -0,0 +1,4 @@
<template>
<div>Non-self-closing script tag</div>
</template>
<script src="./external/external-js.js"></script>
24 changes: 13 additions & 11 deletions src/index.js
Expand Up @@ -12,13 +12,12 @@ const defaultConfig = {
let globalConfig = defaultConfig;

module.exports = function ({ content, filename, hook }) {

function retrieveContent(config) {
if (config.content) {
return config.content;
} else if (config.attrs.src) {
let fullPath = join(dirname(filename), config.attrs.src);
if (config.attrs.src) {
const fullPath = join(dirname(filename), config.attrs.src);
return readFileSync(fullPath, 'utf8');
} else if (config.content) {
return config.content;
} else {
return '';
}
Expand All @@ -37,6 +36,7 @@ module.exports = function ({ content, filename, hook }) {
? transpileSpecial(content, lang)
: hook(lang, content);
}

return content
}

Expand All @@ -57,11 +57,13 @@ module.exports = function ({ content, filename, hook }) {
lang,
content
],
{ encoding: 'utf-8' });
{ encoding: 'utf-8' }
);

if (result.stderr) {
throw new Error(result.stderr);
}

return result.stdout;
}

Expand All @@ -71,7 +73,7 @@ module.exports = function ({ content, filename, hook }) {
// If there is a template then compile to render functions
// This avoids the need for a runtime compilation
// ES2015 template compiler to support es2015 features
let content = retrieveAndTranspileContent(template, ['html'],
const content = retrieveAndTranspileContent(template, ['html'],
globalConfig.transpileTemplates ? transpileTemplateSpecial : null);

const compiled = compiler.compile(content, { preserveWhitespace: false });
Expand All @@ -90,11 +92,11 @@ module.exports = function ({ content, filename, hook }) {
if (!styles)
return ''

let computedProps = []
const computedProps = []
for (let i = 0; i < styles.length; i++) {
const style = styles[i];
if (style.module !== undefined && style.module !== false) {
const moduleName = typeof style.module === 'string' ? style.module : '$style';
const moduleName = (typeof style.module) === 'string' ? style.module : '$style';
const content = retrieveAndTranspileContent(style, ['css']);
let cssClasses;
postcss(postcssModules(
Expand All @@ -106,9 +108,9 @@ module.exports = function ({ content, filename, hook }) {
computedProps.push(`${exportsTarget}.computed.${moduleName} = function(){ return ${cssClassesStr}; };`)
}
}
return computedProps.length > 0
return (computedProps.length > 0)
? `${exportsTarget}.computed = ${exportsTarget}.computed || {};` + computedProps.join(' ')
: ''
: '';
}

const { template, script, styles } = compiler.parseComponent(content, { pad: 'line' });
Expand Down

0 comments on commit f1cf57b

Please sign in to comment.