Skip to content

Commit

Permalink
Handle empty html files (#2621)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored and DeMoorJasper committed Feb 7, 2019
1 parent 4c5993f commit bdef68b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
12 changes: 12 additions & 0 deletions packages/core/integration-tests/test/html.js
Expand Up @@ -296,6 +296,18 @@ describe('html', function() {
assert(html.includes('Other page'));
});

it('should work with an empty html file', async function() {
let inputFile = path.join(__dirname, '/integration/html-empty/index.html');
await bundle(inputFile, {
minify: false
});

let outputFile = path.join(__dirname, '/dist/index.html');

let html = await fs.readFile(outputFile, 'utf8');
assert.equal(html.length, 0);
});

it('should read .htmlnanorc and minify HTML in production mode', async function() {
await bundle(
path.join(__dirname, '/integration/htmlnano-config/index.html'),
Expand Down
Empty file.
82 changes: 42 additions & 40 deletions packages/core/parcel-bundler/src/assets/HTMLAsset.js
Expand Up @@ -204,59 +204,61 @@ class HTMLAsset extends Asset {
async generate() {
// Extract inline <script> and <style> tags for processing.
let parts = [];
this.ast.walk(node => {
if (node.tag === 'script' || node.tag === 'style') {
let value = node.content && node.content.join('').trim();
if (value) {
let type;
if (this.ast) {
this.ast.walk(node => {
if (node.tag === 'script' || node.tag === 'style') {
let value = node.content && node.content.join('').trim();
if (value) {
let type;

if (node.tag === 'style') {
if (node.attrs && node.attrs.type) {
type = node.attrs.type.split('/')[1];
} else {
type = 'css';
}
} else if (node.attrs && node.attrs.type) {
// Skip JSON
if (SCRIPT_TYPES[node.attrs.type] === false) {
return node;
}
if (node.tag === 'style') {
if (node.attrs && node.attrs.type) {
type = node.attrs.type.split('/')[1];
} else {
type = 'css';
}
} else if (node.attrs && node.attrs.type) {
// Skip JSON
if (SCRIPT_TYPES[node.attrs.type] === false) {
return node;
}

if (SCRIPT_TYPES[node.attrs.type]) {
type = SCRIPT_TYPES[node.attrs.type];
if (SCRIPT_TYPES[node.attrs.type]) {
type = SCRIPT_TYPES[node.attrs.type];
} else {
type = node.attrs.type.split('/')[1];
}
} else {
type = node.attrs.type.split('/')[1];
type = 'js';
}
} else {
type = 'js';

parts.push({
type,
value,
inlineHTML: true,
meta: {
type: 'tag',
node
}
});
}
}

// Process inline style attributes.
if (node.attrs && node.attrs.style) {
parts.push({
type,
value,
inlineHTML: true,
type: 'css',
value: node.attrs.style,
meta: {
type: 'tag',
type: 'attr',
node
}
});
}
}

// Process inline style attributes.
if (node.attrs && node.attrs.style) {
parts.push({
type: 'css',
value: node.attrs.style,
meta: {
type: 'attr',
node
}
});
}

return node;
});
return node;
});
}

return parts;
}
Expand Down

0 comments on commit bdef68b

Please sign in to comment.