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

Make dynamic import name relative to the file importing it #2174

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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