Skip to content

Commit

Permalink
Make dynamic import name relative to the file importing it (#2174)
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens authored and devongovett committed Oct 20, 2018
1 parent cbeadd7 commit 3d6a621
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
Expand Up @@ -372,7 +372,7 @@ class JSConcatPackager extends Packager {
}

getBundleSpecifier(bundle) {
let name = path.basename(bundle.name);
let name = path.relative(path.dirname(this.bundle.name), bundle.name);
if (bundle.entryAsset) {
return [name, bundle.entryAsset.id];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/parcel-bundler/src/packagers/JSPackager.js
Expand Up @@ -94,7 +94,7 @@ class JSPackager extends Packager {
}

getBundleSpecifier(bundle) {
let name = path.basename(bundle.name);
let name = path.relative(path.dirname(this.bundle.name), bundle.name);
if (bundle.entryAsset) {
return [name, bundle.entryAsset.id];
}
Expand Down
@@ -0,0 +1,2 @@
exports.a = 1;
exports.b = 2;
@@ -0,0 +1,7 @@
var local = import('../local');

module.exports = function () {
return local.then(function (l) {
return l.a + l.b;
});
};
30 changes: 29 additions & 1 deletion packages/core/parcel-bundler/test/javascript.js
@@ -1,7 +1,14 @@
const assert = require('assert');
const fs = require('../src/utils/fs');
const path = require('path');
const {bundle, run, assertBundleTree, deferred, ncp} = require('./utils');
const {
bundle,
bundler,
run,
assertBundleTree,
deferred,
ncp
} = require('./utils');
const {mkdirp} = require('../src/utils/fs');
const {symlinkSync} = require('fs');

Expand Down Expand Up @@ -314,6 +321,27 @@ describe('javascript', function() {
assert.equal(await output(), 3);
});

it('should load dynamic bundle when entry is in a subdirectory', async function() {
let bu = await bundler(
path.join(
__dirname,
'/integration/dynamic-subdirectory/subdirectory/index.js'
),
{
target: 'browser'
}
);
// Set the rootDir to make sure subdirectory is preserved
bu.options.rootDir = path.join(
__dirname,
'/integration/dynamic-subdirectory'
);
let b = await bu.bundle();
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(await output(), 3);
});

it('should support bundling workers', async function() {
let b = await bundle(path.join(__dirname, '/integration/workers/index.js'));

Expand Down
8 changes: 5 additions & 3 deletions packages/core/parcel-bundler/test/utils.js
Expand Up @@ -82,7 +82,9 @@ function prepareBrowserContext(bundle, globals) {
setTimeout(function() {
if (el.tag === 'script') {
vm.runInContext(
nodeFS.readFileSync(path.join(__dirname, 'dist', el.src)),
nodeFS.readFileSync(
path.join(path.dirname(bundle.name), el.src)
),
ctx
);
}
Expand Down Expand Up @@ -119,13 +121,13 @@ function prepareBrowserContext(bundle, globals) {
arrayBuffer() {
return Promise.resolve(
new Uint8Array(
nodeFS.readFileSync(path.join(__dirname, 'dist', url))
nodeFS.readFileSync(path.join(path.dirname(bundle.name), url))
).buffer
);
},
text() {
return Promise.resolve(
nodeFS.readFileSync(path.join(__dirname, 'dist', url), 'utf8')
nodeFS.readFileSync(path.join(path.dirname(bundle.name), url), 'utf8')
);
}
});
Expand Down

0 comments on commit 3d6a621

Please sign in to comment.