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

Root file shouldn't be hashed. #270

Merged
merged 3 commits into from
Dec 20, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Asset {
);
return this.options.parser
.getAsset(resolved, this.package, this.options)
.generateBundleName();
.generateBundleName(resolved);
}

mightHaveDependencies() {
Expand Down Expand Up @@ -143,7 +143,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 @@ -157,7 +157,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 @@ -168,6 +168,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
Original file line number Diff line number Diff line change
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 @@ -366,7 +367,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import util from './util'
util.hi()
10 changes: 10 additions & 0 deletions test/integration/html-root/other.html
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// util.js
export default {
hi() {
console.log('hi')
}
}