Skip to content
Open
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
4 changes: 2 additions & 2 deletions lib/impl/ImportExportResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ async function resolveDynamicImport (context, input, output, node, currentpath)

let resolved = await PluginLifecycle.hooks.resolveDynamicImport(context, value, currentpath);

if (resolved === false || (typeof resolved === 'object' && resolved.external)) {
output.externalDynamicImports.push(resolved.id || value);
if (isExternal(context, value) || resolved === false || (typeof resolved === 'object' && resolved.external)) {
output.externalDynamicImports.push(value);
return;
}

Expand Down
15 changes: 14 additions & 1 deletion lib/impl/NollupCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let PluginLifecycle = require('./PluginLifecycle');
let ImportExportResolver = require('./ImportExportResolver');
let CodeGenerator = require('./CodeGenerator');
let ParseError = require('./ParseError');
let { applyOutputFileNames, getNameFromFileName, emitAssetToBundle } = require('./utils');
let { applyOutputFileNames, getNameFromFileName, emitAssetToBundle, applyBundleMetadata } = require('./utils');
let path = require('path');
let ErrorHandling = require('./ErrorHandling');

Expand Down Expand Up @@ -76,6 +76,12 @@ async function compileModule (context, filePath, parentFilePath, depth, isEntry,
}
});

file.externalDynamicImports.forEach(di => {
if (emitted.externalDynamicImports.indexOf(di) === -1) {
emitted.externalDynamicImports.push(di);
}
});

file.externalImports.forEach(ei => {
let foundSource = emitted.externalImports.find(other => other.source === ei.source);
if (!foundSource) {
Expand Down Expand Up @@ -128,6 +134,7 @@ async function compileInputTarget (context, filePath, isEntry, bundleModuleIds)
modules: {}, // modules id this input contains
dynamicImports: [], // emitted dynamic ids
externalImports: [],
externalDynamicImports: [],
metaProperties: [],
assets: [],
chunks: []
Expand Down Expand Up @@ -217,10 +224,13 @@ module.exports = {
type: 'chunk',
map: null,
modules: emitted.modules,
dynamicImports: [],
imports: [],
exports: context.files[file].exports,
__entryModule: file,
__entryModuleId: context.files[file].moduleId,
__dynamicImports: emitted.dynamicImports,
__externalDynamicImports: emitted.externalDynamicImports,
__externalImports: emitted.externalImports,
__metaProperties: emitted.metaProperties,
});
Expand All @@ -241,13 +251,15 @@ module.exports = {
type: 'chunk',
map: null,
modules: emitted.modules,
dynamicImports: [],
imports: [],
exports: context.files[chunk.id].exports,
referenceId: chunk.referenceId,
fileName: chunk.fileName,
__entryModule: chunk.id,
__entryModuleId: context.files[chunk.id].moduleId,
__dynamicImports: emitted.dynamicImports,
__externalDynamicImports: emitted.externalDynamicImports,
__externalImports: emitted.externalImports,
__metaProperties: emitted.metaProperties,
});
Expand All @@ -264,6 +276,7 @@ module.exports = {
}

applyOutputFileNames(context, bundle);
applyBundleMetadata(context, bundle);

PluginLifecycle.setCurrentPhase(context, 'generate');

Expand Down
13 changes: 11 additions & 2 deletions lib/impl/PluginContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,24 @@ module.exports = {
id: id,
isEntry: file.isEntry,
isExternal: false,
importedIds: file.externalImports.map(i => i.source).concat(file.imports.map(i => i.source))
importedIds: file.externalImports.map(i => i.source).concat(file.imports.map(i => i.source)),
dynamicallyImportedIds: file.externalDynamicImports.concat(file.dynamicImports),
// For importers, need to implement something
// along the lines of "emitted.moduleImporters = {}"
// that's refreshed on each compilation.
importers: [],
dynamicImporters: []
};
} else {
// Probably external
return {
id: id,
isEntry: false,
isExternal: true,
importedIds: []
importedIds: [],
dynamicallyImportedIds: [],
importers: [],
dynamicImporters: []
}
}
},
Expand Down
13 changes: 12 additions & 1 deletion lib/impl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ function applyOutputFileNames (context, bundle) {
});
}

function applyBundleMetadata (context, bundle) {
bundle.forEach(entry => {
if (entry.type === 'chunk') {
entry.dynamicImports = entry.__externalDynamicImports.concat(entry.__dynamicImports.map(di => {
return bundle.find(b => b.__entryModule === di).fileName;
}));
}
});
}

function emitAssetToBundle (context, bundle, asset) {
let extensionlessName = getNameFromFileName(asset.name);
let extension = path.extname(asset.name);
Expand All @@ -162,5 +172,6 @@ module.exports = {
combineSourceMapChain,
combineSourceMapChainFast,
applyOutputFileNames,
emitAssetToBundle
emitAssetToBundle,
applyBundleMetadata
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
"chai": "^4.1.2",
"mocha-istanbul-ui": "^0.4.0",
"proxyquire": "^2.0.1",
"rollup": "^1.28.0"
"rollup": "^2.26.8"
}
}
85 changes: 42 additions & 43 deletions test/cases/api/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -1674,59 +1674,58 @@ describe ('API: Plugin Context', () => {

// local module
let local_info = fn(path.resolve(process.cwd(), './src/main.js'));
expect(local_info).to.deep.equal({
id: path.resolve(process.cwd(), './src/main.js'),
isEntry: true,
isExternal: false,
importedIds: ['jquery', 'underscore', path.resolve(process.cwd(), './src/lol.js')],
// hasModuleSideEffects: true
});
expect(local_info.id).to.equal(path.resolve(process.cwd(), './src/main.js'));
expect(local_info.isEntry).to.equal(true);
expect(local_info.isExternal).to.equal(false);
expect(local_info.importedIds).to.deep.equal(['jquery', 'underscore', path.resolve(process.cwd(), './src/lol.js')]);
// expect(local_info.importers).to.deep.equal([]);
expect(local_info.dynamicallyImportedIds).to.deep.equal(['backbone', path.resolve(process.cwd(), './src/rofl.js')]);
// expect(local_info.dynamicImporters).to.deep.equal([]);

let local_info_dep = fn(path.resolve(process.cwd(), './src/lol.js'));
expect(local_info_dep).to.deep.equal({
id: path.resolve(process.cwd(), './src/lol.js'),
isEntry: false,
isExternal: false,
importedIds: [],
// hasModuleSideEffects: true
});
expect(local_info_dep.id).to.equal(path.resolve(process.cwd(), './src/lol.js'));
expect(local_info_dep.isEntry).to.equal(false);
expect(local_info_dep.isExternal).to.equal(false);
expect(local_info_dep.importedIds).to.deep.equal([]);
// expect(local_info_dep.importers).to.deep.equal([path.resolve(process.cwd(), './src/main.js')]);
expect(local_info_dep.dynamicallyImportedIds).to.deep.equal([]);
// expect(local_info_dep.dynamicImporters).to.deep.equal([]);

let external_info = fn('jquery');
expect(external_info).to.deep.equal({
id: 'jquery',
isEntry: false,
isExternal: true,
importedIds: [],
// hasModuleSideEffects: true
});
expect(external_info.id).to.equal('jquery');
expect(external_info.isEntry).to.equal(false);
expect(external_info.isExternal).to.equal(true);
expect(external_info.importedIds).to.deep.equal([]);
// expect(external_info.importers).to.deep.equal([path.resolve(process.cwd(), './src/main.js')]);
expect(external_info.dynamicallyImportedIds).to.deep.equal([]);
// expect(external_info.dynamicImporters).to.deep.equal([]);

let external_resolve_info = fn('underscore');
expect(external_resolve_info).to.deep.equal({
id: 'underscore',
isEntry: false,
isExternal: true,
importedIds: [],
// hasModuleSideEffects: true
});
expect(external_resolve_info.id).to.equal('underscore');
expect(external_resolve_info.isEntry).to.equal(false);
expect(external_resolve_info.isExternal).to.equal(true);
expect(external_resolve_info.importedIds).to.deep.equal([]);
// expect(external_resolve_info.importers).to.deep.equal([path.resolve(process.cwd(), './src/main.js')])
expect(external_resolve_info.dynamicallyImportedIds).to.deep.equal([]);
// expect(external_resolve_info.dynamicImporters).to.deep.equal([]);

let dynamic_external_resolve_info = fn('backbone');
expect(dynamic_external_resolve_info).to.deep.equal({
id: 'backbone',
isEntry: false,
isExternal: true,
importedIds: [],
// hasModuleSideEffects: true
});

expect(dynamic_external_resolve_info.id).to.equal('backbone');
expect(dynamic_external_resolve_info.isEntry).to.equal(false);
expect(dynamic_external_resolve_info.isExternal).to.equal(true);
expect(dynamic_external_resolve_info.importedIds).to.deep.equal([]);
// expect(dynamic_external_resolve_info.importers).to.deep.equal([]);
expect(dynamic_external_resolve_info.dynamicallyImportedIds).to.deep.equal([]);
// expect(dynamic_external_resolve_info.dynamicImporters).to.deep.equal([path.resolve(process.cwd(), './src/main.js')]);

let dynamic_import_info = fn(path.resolve(process.cwd(), './src/rofl.js'));
expect(dynamic_import_info).to.deep.equal({
id: path.resolve(process.cwd(), './src/rofl.js'),
isEntry: false,
isExternal: false,
importedIds: [],
// hasModuleSideEffects: true
});
expect(dynamic_import_info.id).to.equal(path.resolve(process.cwd(), './src/rofl.js'));
expect(dynamic_import_info.isEntry).to.equal(false);
expect(dynamic_import_info.isExternal).to.equal(false);
expect(dynamic_import_info.importedIds).to.deep.equal([]);
// expect(dynamic_import_info.importers).to.deep.equal([]);
expect(dynamic_import_info.dynamicallyImportedIds).to.deep.equal([]);
// expect(dynamic_import_info.dynamicImporters).to.deep.equal([path.resolve(process.cwd(), './src/main.js')]);

fs.reset();
});
Expand Down
21 changes: 21 additions & 0 deletions test/cases/api/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,25 @@ describe ('API: generate', () => {
fs.reset();
});

it ('should include deconflicted external dynamic imports in outputted chunkinfos', async () => {
fs.stub('./src/a/dep.js', () => 'export default 123; import("jquery");');
fs.stub('./src/b/dep.js', () => 'export default 456; import("../c/dep");');
fs.stub('./src/c/dep.js', () => 'export default 789;');
fs.stub('./src/main.js', () => 'import("./a/dep"); import("./b/dep"); ');

let bundle = await nollup({
input: './src/main.js',
external: ['jquery']
});

let { output } = await bundle.generate({ format: 'esm', chunkFileNames: '[name].js' });

expect(output.length).to.equal(4);
expect(output[0].dynamicImports).to.deep.equal(['dep.js', 'dep2.js']);
expect(output[1].dynamicImports).to.deep.equal(['jquery']);
expect(output[2].dynamicImports).to.deep.equal(['dep3.js']);
expect(output[3].dynamicImports).to.deep.equal([]);
fs.reset();
});

});