Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Fix issue with serving assets from memory #124

Merged
merged 1 commit into from
Jan 9, 2018
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
8 changes: 6 additions & 2 deletions build/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const webpackDevMiddleware = require('../lib/simple-webpack-dev-middleware');
const ChunkManifestPlugin = require('./external-chunk-manifest-plugin.js');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const composeMiddleware = require('compose-middleware').compose;
const {
//zopfliWebpackPlugin,
brotliWebpackPlugin,
Expand Down Expand Up @@ -612,7 +611,12 @@ function Compiler({
publicPath: '/_static/',
});
const hot = webpackHotMiddleware(compiler, {log: false});
return composeMiddleware([dev, hot]);
return (req, res, next) => {
dev(req, res, err => {
if (err) return next(err);
return hot(req, res, next);
});
};
};

this.clean = () => {
Expand Down
7 changes: 2 additions & 5 deletions lib/simple-webpack-dev-middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,14 @@ module.exports = function(compiler, options) {
let content = context.fs.readFileSync(filename);
content = shared.handleRangeHeaders(content, req, res);
res.setHeader('Access-Control-Allow-Origin', '*'); // To support XHR, etc.
res.setHeader('Content-Type', mime.lookup(filename) + '; charset=UTF-8');
res.setHeader('Content-Type', mime.getType(filename) + '; charset=UTF-8');
res.setHeader('Content-Length', content.length);
if (context.options.headers) {
for (const name in context.options.headers) {
res.setHeader(name, context.options.headers[name]);
}
}
// Express automatically sets the statusCode to 200, but not all servers do (Koa).
res.statusCode = res.statusCode || 200;
if (res.send) res.send(content);
else res.end(content);
res.end(content);
}
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"browser-unhandled-rejection": "^1.0.1",
"case-sensitive-paths-webpack-plugin": "^2.1.1",
"chalk": "^2.3.0",
"compose-middleware": "4.0.0",
"compression-webpack-plugin": "^1.1.3",
"core-js": "^2.5.3",
"enzyme-adapter-react-16": "^1.1.1",
Expand Down
29 changes: 22 additions & 7 deletions test/cli/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path');
const test = require('tape');
const {dev} = require('../run-command');
const {promisify} = require('util');
const request = require('request-promise');

const exists = promisify(fs.exists);
const readFile = promisify(fs.readFile);
Expand All @@ -21,23 +22,37 @@ test('`fusion dev` works', async t => {
t.end();
});

test('`fusion dev` works with assets', async t => {
test.only('`fusion dev` works with assets', async t => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove .only

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(We should implement some sort of linter against this probably)

const dir = path.resolve(__dirname, '../fixtures/assets');
const entryPath = path.resolve(
dir,
'.fusion/dist/development/server/server-main.js'
);
const testFilePath = path.resolve(dir, '.fusion/test-asset');

const {proc} = await dev(`--dir=${dir}`);
const {proc, port} = await dev(`--dir=${dir}`);
t.ok(await exists(testFilePath), 'Generates test file');
t.ok(await exists(entryPath), 'Entry file gets compiled');
const assetPath = await readFile(testFilePath);
t.equal(
assetPath.toString(),
'/_static/d41d8cd98f00b204e9800998ecf8427e.js',
'sets correct asset path'
);
const expectedAssetPath = '/_static/c300a7df05c8142598558365dbdaa451.css';
t.equal(assetPath.toString(), expectedAssetPath, 'sets correct asset path');
try {
t.ok(
await request(`http://localhost:${port}/_static/client-main.js`),
'serves client-main from memory correctly'
);
t.ok(
await request(`http://localhost:${port}/_static/client-vendor.js`),
'serves client-vendor from memory correctly'
);
t.equal(
fs.readFileSync(path.resolve(dir, 'src/static/test.css')).toString(),
await request(`http://localhost:${port}${expectedAssetPath}`),
'serves css file from memory correctly'
);
} catch (e) {
t.iferror(e);
}
proc.kill();
t.end();
});
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/assets/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {assetUrl} from 'fusion-core';
export default async function() {
if (__NODE__) {
const fs = require('fs');
fs.writeFileSync('.fusion/test-asset', assetUrl('./static/test.js'));
fs.writeFileSync('.fusion/test-asset', assetUrl('./static/test.css'));
}
const app = new App('element', el => el);
return app;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/assets/src/static/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
color: black;
}
Empty file.
4 changes: 2 additions & 2 deletions test/run-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ async function start(args, options) {
const port = await getPort();
const {proc} = cmd(`start --port=${port} ${args}`, options);
const res = await waitForServer(port);
return {proc, res};
return {proc, res, port};
}

async function dev(args, options) {
const port = await getPort();
const {proc} = cmd(`dev --port=${port} --no-open ${args}`, options);
const res = await waitForServer(port);
return {proc, res};
return {proc, res, port};
}

async function waitForServer(port) {
Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@
version "0.9.44"
resolved "https://registry.yarnpkg.com/@types/core-js/-/core-js-0.9.44.tgz#0d3b179ffa612898beed19fe8cc863822da507e5"

"@types/[email protected]":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-0.0.29.tgz#a1e514adfbd92f03a224ba54d693111dbf1f3754"

"@types/mkdirp@^0.3.29":
version "0.3.29"
resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-0.3.29.tgz#7f2ad7ec55f914482fc9b1ec4bb1ae6028d46066"
Expand Down Expand Up @@ -1642,14 +1638,6 @@ commondir@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"

[email protected]:
version "4.0.0"
resolved "https://registry.yarnpkg.com/compose-middleware/-/compose-middleware-4.0.0.tgz#1e7298257cd9c841485f3ca1de385d3e7da0dd0f"
dependencies:
"@types/debug" "0.0.29"
array-flatten "^2.1.0"
debug "^3.1.0"

compressible@~2.0.11:
version "2.0.12"
resolved "https://registry.yarnpkg.com/compressible/-/compressible-2.0.12.tgz#c59a5c99db76767e9876500e271ef63b3493bd66"
Expand Down