Skip to content

Commit

Permalink
Root file shouldn't be hashed. (#270)
Browse files Browse the repository at this point in the history
* This commit should solve the problem of root file being hashed.

* This commit will move the isMainFile check within generateBundleName function.

* Removing the boolean variable. And using the options variable
  • Loading branch information
ssuman authored and devongovett committed Dec 20, 2017
1 parent 7d96281 commit 075190c
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/Asset.js
Expand Up @@ -80,7 +80,7 @@ class Asset {
);
return this.options.parser
.getAsset(resolved, this.package, this.options)
.generateBundleName();
.generateBundleName(resolved);
}

mightHaveDependencies() {
Expand Down Expand Up @@ -144,7 +144,7 @@ class Asset {
this.parentDeps.clear();
}

generateBundleName(isMainAsset) {
generateBundleName(assetPath) {
// Resolve the main file of the package.json
let main =
this.package && this.package.main
Expand All @@ -161,7 +161,7 @@ class Asset {
}

// If this is the entry point of the root bundle, use the original filename
if (isMainAsset) {
if (this.isMainFile(assetPath)) {
return path.basename(this.name, path.extname(this.name)) + ext;
}

Expand All @@ -172,6 +172,10 @@ class Asset {
generateErrorMessage(err) {
return err;
}

isMainFile(assetPath) {
return this.options.mainFile === assetPath;
}
}

module.exports = Asset;
5 changes: 3 additions & 2 deletions src/Bundler.js
Expand Up @@ -63,7 +63,8 @@ class Bundler extends EventEmitter {
minify:
typeof options.minify === 'boolean' ? options.minify : isProduction,
hmr: typeof options.hmr === 'boolean' ? options.hmr : watch,
logLevel: typeof options.logLevel === 'number' ? options.logLevel : 3
logLevel: typeof options.logLevel === 'number' ? options.logLevel : 3,
mainFile: this.mainFile
};
}

Expand Down Expand Up @@ -375,7 +376,7 @@ class Bundler extends EventEmitter {
if (!bundle) {
bundle = new Bundle(
asset.type,
Path.join(this.options.outDir, asset.generateBundleName(true))
Path.join(this.options.outDir, asset.generateBundleName(this.mainFile))
);
bundle.entryAsset = asset;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Server.js
Expand Up @@ -32,7 +32,9 @@ function middleware(bundler) {
function sendIndex() {
// If the main asset is an HTML file, serve it
if (bundler.mainAsset.type === 'html') {
req.url = `/${bundler.mainAsset.generateBundleName(true)}`;
req.url = `/${bundler.mainAsset.generateBundleName(
bundler.options.mainFile
)}`;
serve(req, res, send404);
} else {
send404();
Expand Down
13 changes: 13 additions & 0 deletions test/html.js
Expand Up @@ -134,4 +134,17 @@ describe('html', function() {
let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
assert(html.includes('<a href="#hash_link">'));
});

it('should not update root/main file in the bundles', async function() {
let b = await bundle(__dirname + '/integration/html-root/index.html');

let files = fs.readdirSync(__dirname + '/dist');

for (let file of files) {
if (file !== 'index.html' && file.endsWith('.html')) {
let html = fs.readFileSync(__dirname + '/dist/' + file);
assert(html.includes('index.html'));
}
}
});
});
10 changes: 10 additions & 0 deletions test/integration/html-root/index.html
@@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<a href="./other.html">goto page2</a>
<script type="text/javascript" src="./main.js"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions test/integration/html-root/index2.js
@@ -0,0 +1,3 @@
// index2.js
import util from './util.js'
util.hi()
2 changes: 2 additions & 0 deletions test/integration/html-root/main.js
@@ -0,0 +1,2 @@
import util from './util'
util.hi()
10 changes: 10 additions & 0 deletions test/integration/html-root/other.html
@@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<a href="./index.html">goto page1</a>
<script type="text/javascript" src="./index2.js"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions test/integration/html-root/util.js
@@ -0,0 +1,6 @@
// util.js
export default {
hi() {
console.log('hi')
}
}

0 comments on commit 075190c

Please sign in to comment.