Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty html files #2621

Merged
merged 1 commit into from Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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