Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
feat(project): Add 'invalidateCache()' method that invalidates loaded…
Browse files Browse the repository at this point in the history
… modules from the cache

fix #175
  • Loading branch information
tripodsan committed Apr 1, 2019
1 parent 205af87 commit 73aa3ea
Show file tree
Hide file tree
Showing 9 changed files with 875 additions and 1,387 deletions.
2 changes: 1 addition & 1 deletion .releaserc.js
Expand Up @@ -7,7 +7,7 @@ module.exports = {
}],
"@semantic-release/npm",
["@semantic-release/git", {
"assets": ["package.json", "CHANGELOG.md"],
"assets": ["package.json", "package-lock.json", "CHANGELOG.md"],
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}],
["@semantic-release/github", {}]
Expand Down
2,183 changes: 808 additions & 1,375 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/HelixProject.js
Expand Up @@ -226,6 +226,19 @@ class HelixProject {
return this.config.strains.get('default');
}

/**
* Invalidates the node module cache of the file in the build directory.
*/
invalidateCache() {
// we simple remove all entries from the node cache that fall below the build directory
Object.keys(require.cache).forEach((file) => {
if (file.startsWith(this._buildDir)) {
delete require.cache[file];
this._logger.debug(`evicted ${path.relative(this._buildDir, file)}`);
}
});
}

async init() {
if (!this._logger) {
this._logger = Logger.getLogger('hlx');
Expand Down
10 changes: 0 additions & 10 deletions src/HelixServer.js
Expand Up @@ -45,16 +45,6 @@ function safeCycles() {
* @return {Promise} A promise that resolves to generated output.
*/
function executeTemplate(ctx) {
// invalidate script
// todo: use watcher to invalidate automatically
delete require.cache[require.resolve(ctx.templatePath)];
// temporary workaround: invalidate the pre.js too
try {
delete require.cache[require.resolve(ctx.templatePath.replace('.js', '.pre.js'))];
} catch (e) {
// no pre.js: require.resolve fires an exception if the module does not exist
}

// the compiled script does not bundle the modules that are required for execution, since it
// expects them to be provided by the runtime. We tweak the module loader here in order to
// inject the project module paths.
Expand Down
20 changes: 20 additions & 0 deletions test/hlx_server_test.js
Expand Up @@ -130,6 +130,26 @@ describe('Helix Server', () => {
}
});

it('deliver modified helper', async () => {
const cwd = path.join(SPEC_ROOT, 'local');
const testRoot = await createTestRoot();
await fse.copy(cwd, testRoot);
const project = new HelixProject()
.withCwd(testRoot)
.withBuildDir('./build')
.withHttpPort(0);
await project.init();
try {
await project.start();
await assertHttp(`http://localhost:${project.server.port}/index.html`, 200, 'expected_index.html');
await fse.copy(path.resolve(testRoot, 'build/helper2.js'), path.resolve(testRoot, 'build/helper.js'));
project.invalidateCache();
await assertHttp(`http://localhost:${project.server.port}/index.html`, 200, 'expected_index2.html');
} finally {
await project.stop();
}
});

it('does not start on occupied port', async () => {
const cwd = path.join(SPEC_ROOT, 'local');
const project = new HelixProject()
Expand Down
1 change: 1 addition & 0 deletions test/specs/expected_index2.html
@@ -0,0 +1 @@
<html><head>Test</head><body>Another Hello, world. path=/index.md</body></html>
15 changes: 15 additions & 0 deletions test/specs/local/build/helper.js
@@ -0,0 +1,15 @@
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

module.exports.utils = {
stamp: () => "Hello",
};
15 changes: 15 additions & 0 deletions test/specs/local/build/helper2.js
@@ -0,0 +1,15 @@
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

module.exports.utils = {
stamp: () => "Another Hello",
};
3 changes: 2 additions & 1 deletion test/specs/local/build/html.js
Expand Up @@ -9,10 +9,11 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const { utils } = require('./helper.js');

/* eslint-disable */
module.exports.main = function main(params) {
return {
body: `<html><head>Test</head><body>Hello, world. path=${params.path}</body></html>`,
body: `<html><head>Test</head><body>${utils.stamp()}, world. path=${params.path}</body></html>`,
}
};

0 comments on commit 73aa3ea

Please sign in to comment.