Skip to content

Commit

Permalink
Merge branch 'master' into css-sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Mar 5, 2019
2 parents 9d5ec0b + f68f0df commit ae13f8a
Show file tree
Hide file tree
Showing 25 changed files with 134 additions and 40 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Expand Up @@ -13,5 +13,8 @@
"globals": {
"parcelRequire": true,
"define": true
},
"rules": {
"no-return-await": "error"
}
}
39 changes: 39 additions & 0 deletions packages/core/integration-tests/test/hmr.js
Expand Up @@ -392,6 +392,45 @@ describe('hmr', function() {
assert.deepEqual(outputs, [3, 10]);
});

it('should bubble up HMR events to a page reload', async function() {
await ncp(
path.join(__dirname, '/integration/hmr-reload'),
path.join(__dirname, '/input')
);

b = bundler(path.join(__dirname, '/input/index.js'), {
watch: true,
hmr: true
});
let bundle = await b.bundle();

let outputs = [];
let ctx = await run(
bundle,
{
output(o) {
outputs.push(o);
}
},
{require: false}
);
let spy = sinon.spy(ctx.location, 'reload');

await sleep(50);
assert.deepEqual(outputs, [3]);
assert(spy.notCalled);

await sleep(100);
fs.writeFile(
path.join(__dirname, '/input/local.js'),
'exports.a = 5; exports.b = 5;'
);

await nextEvent(b, 'bundled');
assert.deepEqual(outputs, [3]);
assert(spy.calledOnce);
});

it('should log emitted errors and show an error overlay', async function() {
await ncp(
path.join(__dirname, '/integration/commonjs'),
Expand Down
Expand Up @@ -4,6 +4,8 @@ function run() {
output(local.a + local.b);
}

module.hot.accept();

run();

module.exports = 'value';
Expand Up @@ -6,4 +6,6 @@ function run() {
});
};

module.hot.accept();

run();
@@ -0,0 +1,7 @@
var local = require('./local');

function run() {
output(local.a + local.b);
}

run();
@@ -0,0 +1,2 @@
exports.a = 1;
exports.b = 2;
2 changes: 2 additions & 0 deletions packages/core/integration-tests/test/integration/hmr/index.js
Expand Up @@ -4,4 +4,6 @@ function run() {
output(local.a + local.b);
}

module.hot.accept();

run();
@@ -0,0 +1 @@
export default import("./b.js").then(b => b.default);
@@ -0,0 +1 @@
export default import('./c.js').then(b => b.default + 1);
@@ -0,0 +1 @@
export default 122;
12 changes: 12 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -264,6 +264,18 @@ describe('scope hoisting', function() {
assert.equal(await output.default, 5);
});

it('supports nested dynamic imports', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/dynamic-import-dynamic/a.js'
)
);

let output = await run(b);
assert.equal(await output.default, 123);
});

it('should not export function arguments', async function() {
let b = await bundle(
path.join(
Expand Down
4 changes: 2 additions & 2 deletions packages/core/parcel-bundler/src/Asset.js
Expand Up @@ -160,7 +160,7 @@ class Asset {
return conf;
}

return await config.load(opts.path || this.name, filenames);
return config.load(opts.path || this.name, filenames);
}

return null;
Expand All @@ -171,7 +171,7 @@ class Asset {
}

async load() {
return await fs.readFile(this.name, this.encoding);
return fs.readFile(this.name, this.encoding);
}

parse() {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/parcel-bundler/src/Bundler.js
Expand Up @@ -490,7 +490,7 @@ class Bundler extends EventEmitter {
this.options.autoinstall &&
install
) {
return await this.installDep(asset, dep);
return this.installDep(asset, dep);
}

err.message = `Cannot resolve dependency '${dep.name}'`;
Expand Down Expand Up @@ -521,7 +521,7 @@ class Bundler extends EventEmitter {
}
}

return await this.resolveDep(asset, dep, false);
return this.resolveDep(asset, dep, false);
}

async throwDepError(asset, dep, err) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/parcel-bundler/src/Resolver.js
Expand Up @@ -159,7 +159,7 @@ class Resolver {
// First try as a file, then as a directory.
return (
(await this.loadAsFile(filename, extensions, pkg)) ||
(await this.loadDirectory(filename, extensions, pkg))
(await this.loadDirectory(filename, extensions, pkg)) // eslint-disable-line no-return-await
);
}

Expand Down Expand Up @@ -251,7 +251,7 @@ class Resolver {
}

// Fall back to an index file inside the directory.
return await this.loadAsFile(path.join(dir, 'index'), extensions, pkg);
return this.loadAsFile(path.join(dir, 'index'), extensions, pkg);
}

async readPackage(dir) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/SourceMap.js
Expand Up @@ -35,7 +35,7 @@ class SourceMap {
}
map = typeof map === 'string' ? JSON.parse(map) : map;
if (map.sourceRoot) delete map.sourceRoot;
return await new SourceMapConsumer(map);
return new SourceMapConsumer(map);
}

async addMap(map, lineOffset = 0, columnOffset = 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/assets/GLSLAsset.js
Expand Up @@ -37,7 +37,7 @@ class GLSLAsset extends Asset {
}
});

return await promisify(depper.inline.bind(depper))(this.contents, cwd);
return promisify(depper.inline.bind(depper))(this.contents, cwd);
}

collectDependencies() {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/assets/LESSAsset.js
Expand Up @@ -26,7 +26,7 @@ class LESSAsset extends Asset {
opts.sourceMap = {outputSourceFiles: true};
}

return await render(code, opts);
return render(code, opts);
}

collectDependencies() {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/assets/RawAsset.js
Expand Up @@ -27,7 +27,7 @@ class RawAsset extends Asset {
}

async generateHash() {
return await md5.file(this.name);
return md5.file(this.name);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/assets/SASSAsset.js
Expand Up @@ -102,7 +102,7 @@ async function getSassRuntime(searchPath) {
return await localRequire('node-sass', searchPath, true);
} catch (e) {
// If node-sass is not used locally, install dart-sass, as this causes no freezing issues
return await localRequire('sass', searchPath);
return localRequire('sass', searchPath);
}
}

Expand Down
61 changes: 42 additions & 19 deletions packages/core/parcel-bundler/src/builtins/hmr-runtime.js
Expand Up @@ -20,30 +20,43 @@ function Module(moduleName) {
}

module.bundle.Module = Module;
var updatedAssets;
var checkedAssets, assetsToAccept;

var parent = module.bundle.parent;
if ((!parent || !parent.isParcelRequire) && typeof WebSocket !== 'undefined') {
var hostname = process.env.HMR_HOSTNAME || location.hostname;
var protocol = location.protocol === 'https:' ? 'wss' : 'ws';
var ws = new WebSocket(protocol + '://' + hostname + ':' + process.env.HMR_PORT + '/');
ws.onmessage = function(event) {
updatedAssets = {};

checkedAssets = {};
assetsToAccept = [];

var data = JSON.parse(event.data);

if (data.type === 'update') {
console.clear();

data.assets.forEach(function (asset) {
hmrApply(global.parcelRequire, asset);
});

data.assets.forEach(function (asset) {
var handled = false;
data.assets.forEach(function(asset) {
if (!asset.isNew) {
hmrAccept(global.parcelRequire, asset.id);
var didAccept = hmrAcceptCheck(global.parcelRequire, asset.id);
if (didAccept) {
handled = true;
}
}
});

if(handled){
console.clear();

data.assets.forEach(function (asset) {
hmrApply(global.parcelRequire, asset);
});

assetsToAccept.forEach(function (v) {
hmrAcceptRun(v[0], v[1]);
});
} else {
window.location.reload();
}
}

if (data.type === 'reload') {
Expand Down Expand Up @@ -140,21 +153,35 @@ function hmrApply(bundle, asset) {
}
}

function hmrAccept(bundle, id) {
function hmrAcceptCheck(bundle, id) {
var modules = bundle.modules;
if (!modules) {
return;
}

if (!modules[id] && bundle.parent) {
return hmrAccept(bundle.parent, id);
return hmrAcceptCheck(bundle.parent, id);
}

if (updatedAssets[id]) {
if (checkedAssets[id]) {
return;
}
updatedAssets[id] = true;
checkedAssets[id] = true;

var cached = bundle.cache[id];

assetsToAccept.push([bundle, id])

if (cached && cached.hot && cached.hot._acceptCallbacks.length) {
return true;
}

return getParents(global.parcelRequire, id).some(function (id) {
return hmrAcceptCheck(global.parcelRequire, id)
});
}

function hmrAcceptRun(bundle, id) {
var cached = bundle.cache[id];
bundle.hotData = {};
if (cached) {
Expand All @@ -177,8 +204,4 @@ function hmrAccept(bundle, id) {
});
return true;
}

return getParents(global.parcelRequire, id).some(function (id) {
return hmrAccept(global.parcelRequire, id)
});
}
Expand Up @@ -384,6 +384,7 @@ class JSConcatPackager extends Packager {
if (this.bundle.assets.has(asset)) {
return;
}
this.assets.set(asset.id, asset);
this.bundle.addAsset(asset);
if (!asset.parentBundle) {
asset.parentBundle = this.bundle;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/parcel-bundler/src/transforms/babel/babelrc.js
Expand Up @@ -53,12 +53,12 @@ async function getBabelRc(asset, isSource) {
}

// Otherwise, return the .babelrc if babelify was found
return babelify ? await findBabelRc(asset) : null;
return babelify ? findBabelRc(asset) : null;
}

// If this asset is not in node_modules, always use the .babelrc
if (isSource) {
return await findBabelRc(asset);
return findBabelRc(asset);
}

// Otherwise, don't load .babelrc for node_modules.
Expand Down Expand Up @@ -266,7 +266,7 @@ async function installPlugins(asset, babelrc) {
let plugins = (babelrc.plugins || []).map(p =>
resolveModule('plugin', getPluginName(p), asset.name)
);
return await Promise.all([...presets, ...plugins]);
return Promise.all([...presets, ...plugins]);
}

async function resolveModule(type, name, path) {
Expand Down
8 changes: 3 additions & 5 deletions packages/core/parcel-bundler/src/utils/loadPlugins.js
Expand Up @@ -2,14 +2,12 @@ const localRequire = require('./localRequire');

module.exports = async function loadPlugins(plugins, relative) {
if (Array.isArray(plugins)) {
return await Promise.all(
plugins.map(async p => await loadPlugin(p, relative)).filter(Boolean)
return Promise.all(
plugins.map(p => loadPlugin(p, relative)).filter(Boolean)
);
} else if (typeof plugins === 'object') {
let mapPlugins = await Promise.all(
Object.keys(plugins).map(
async p => await loadPlugin(p, relative, plugins[p])
)
Object.keys(plugins).map(p => loadPlugin(p, relative, plugins[p]))
);
return mapPlugins.filter(Boolean);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/utils/localRequire.js
Expand Up @@ -22,7 +22,7 @@ async function localResolve(name, path, triedInstall = false) {
if (e.code === 'MODULE_NOT_FOUND' && !triedInstall) {
const packageName = getModuleParts(name)[0];
await installPackage(packageName, path);
return await localResolve(name, path, true);
return localResolve(name, path, true);
}
throw e;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test-utils/src/utils.js
Expand Up @@ -234,7 +234,7 @@ function prepareBrowserContext(bundle, globals) {
document: fakeDocument,
WebSocket,
console,
location: {hostname: 'localhost'},
location: {hostname: 'localhost', reload() {}},
fetch(url) {
return Promise.resolve({
arrayBuffer() {
Expand Down

0 comments on commit ae13f8a

Please sign in to comment.