Skip to content

Commit

Permalink
live references
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Dec 22, 2019
1 parent 85f1899 commit 004fb25
Show file tree
Hide file tree
Showing 24 changed files with 130 additions and 62 deletions.
4 changes: 0 additions & 4 deletions src/Chunk.ts
Expand Up @@ -593,10 +593,6 @@ export default class Chunk {
else magicString.addSource(new MagicString(rendered));
}
}
module.syntheticExports.forEach(s => {
const rendered = s.renderBlock(renderOptions);
magicString.addSource(new MagicString(rendered));
});
}
const { renderedExports, removedExports } = module.getRenderedExports();
renderedModules[module.id] = {
Expand Down
41 changes: 30 additions & 11 deletions src/ModuleLoader.ts
Expand Up @@ -244,7 +244,7 @@ export class ModuleLoader {
module.id,
(module.resolvedIds[source] =
module.resolvedIds[source] ||
this.handleMissingImports(await this.resolveId(source, module.id), source, module.id))
this.handleResolveId(await this.resolveId(source, module.id), source, module.id))
)
) as Promise<unknown>[]),
...module.getDynamicImportExpressions().map((specifier, index) =>
Expand Down Expand Up @@ -411,6 +411,33 @@ export class ModuleLoader {
return resolvedId;
}

private handleResolveId(
resolvedId: ResolvedId | null,
source: string,
importer: string
): ResolvedId {
return this.handleSyntheticExports(
this.handleMissingImports(resolvedId, source, importer),
source,
importer
);
}

private handleSyntheticExports(
resolvedId: ResolvedId,
source: string,
importer: string
): ResolvedId {
if (resolvedId.external && resolvedId.syntheticNamedExports) {
this.graph.warn(errExternalSyntheticExports(source, importer));
return {
...resolvedId,
syntheticNamedExports: false
};
}
return resolvedId;
}

private loadEntryModule = (unresolvedId: string, isEntry: boolean): Promise<Module> =>
this.pluginDriver.hookFirst('resolveId', [unresolvedId, undefined]).then(resolveIdResult => {
if (
Expand Down Expand Up @@ -464,10 +491,6 @@ export class ModuleLoader {
}
external = true;
}
if (external && syntheticNamedExports) {
syntheticNamedExports = false;
this.graph.warn(errExternalSyntheticExports(source, importer));
}
return {
external,
id,
Expand Down Expand Up @@ -505,13 +528,9 @@ export class ModuleLoader {
if (resolution == null) {
return (module.resolvedIds[specifier] =
module.resolvedIds[specifier] ||
this.handleMissingImports(
await this.resolveId(specifier, module.id),
specifier,
module.id
));
this.handleResolveId(await this.resolveId(specifier, module.id), specifier, module.id));
}
return this.handleMissingImports(
return this.handleResolveId(
this.normalizeResolveIdResult(resolution, importer, specifier),
specifier,
importer
Expand Down
9 changes: 1 addition & 8 deletions src/ast/variables/SyntheticNamedExport.ts
Expand Up @@ -14,6 +14,7 @@ export default class SyntheticNamedExport extends Variable {
this.context = context;
this.module = context.module;
this.defaultVariable = defaultVariable;
this.setRenderNames(defaultVariable.getName(), name);
}

include(context: InclusionContext) {
Expand All @@ -22,12 +23,4 @@ export default class SyntheticNamedExport extends Variable {
this.context.includeVariable(context, this.defaultVariable);
}
}

renderBlock(options: RenderOptions) {
const _ = options.compact ? '' : ' ';
const name = this.getName();
const defaultVariable = this.defaultVariable.getName();
const output = `${options.varOrConst} ${name}${_}=${_}${defaultVariable}.${this.name};`;
return output;
}
}
2 changes: 1 addition & 1 deletion src/utils/error.ts
Expand Up @@ -268,7 +268,7 @@ export function errExternalSyntheticExports(source: string, importer: string) {
return {
code: Errors.EXTERNAL_SYNTHETIC_EXPORTS,
importer: relativeId(importer),
message: `External '${source}' can not have syntheticNamedExports`,
message: `External '${source}' can not have 'syntheticNamedExports' enabled`,
source
};
}
Expand Down
@@ -0,0 +1,19 @@
module.exports = {
description: 'mixed synthetic named exports',
options: {
input: ['main.js'],
plugins: [
{
resolveId(id) {
if (id === './dep1.js' || id === './dep2.js') {
return {
id,
syntheticNamedExports: true
};
}
return null;
}
}
]
}
};
@@ -0,0 +1,7 @@
define(function () { 'use strict';

var dep2 = {bar: {foo: 'works'}};

console.log(dep2.bar.foo);

});
@@ -0,0 +1,5 @@
'use strict';

var dep2 = {bar: {foo: 'works'}};

console.log(dep2.bar.foo);
@@ -0,0 +1,3 @@
var dep2 = {bar: {foo: 'works'}};

console.log(dep2.bar.foo);
@@ -0,0 +1,12 @@
System.register([], function () {
'use strict';
return {
execute: function () {

var dep2 = {bar: {foo: 'works'}};

console.log(dep2.bar.foo);

}
};
});
@@ -0,0 +1 @@
export {bar as default} from './dep2.js';
@@ -0,0 +1 @@
export default {bar: {foo: 'works'}};
@@ -0,0 +1,2 @@
import { foo } from './dep1.js';
console.log(foo);
@@ -1,5 +1,5 @@
module.exports = {
description: 'simple chunking',
description: 'mixed synthetic named exports',
options: {
input: ['main.js'],
plugins: [
Expand Down
Expand Up @@ -11,9 +11,7 @@ define(function () { 'use strict';
foo: foo
}, d));

var fn = d.fn;

console.log(fn);
console.log(d.fn);
console.log(foo);
console.log(ns);

Expand Down
Expand Up @@ -11,8 +11,6 @@ var ns = /*#__PURE__*/Object.freeze(Object.assign({
foo: foo
}, d));

var fn = d.fn;

console.log(fn);
console.log(d.fn);
console.log(foo);
console.log(ns);
Expand Up @@ -9,8 +9,6 @@ var ns = /*#__PURE__*/Object.freeze(Object.assign({
foo: foo
}, d));

var fn = d.fn;

console.log(fn);
console.log(d.fn);
console.log(foo);
console.log(ns);
Expand Up @@ -14,9 +14,7 @@ System.register([], function () {
foo: foo
}, d));

var fn = d.fn;

console.log(fn);
console.log(d.fn);
console.log(foo);
console.log(ns);

Expand Down
@@ -1,5 +1,5 @@
module.exports = {
description: 'simple chunking',
description: 'synthetic named exports',
options: {
input: ['main.js'],
plugins: [
Expand Down
Expand Up @@ -7,12 +7,8 @@ define(function () { 'use strict';

var ns = d;

var fn = d.fn;

var foo = d.foo;

console.log(fn);
console.log(foo);
console.log(d.fn);
console.log(d.foo);
console.log(ns);

});
Expand Up @@ -7,10 +7,6 @@ const d = {

var ns = d;

var fn = d.fn;

var foo = d.foo;

console.log(fn);
console.log(foo);
console.log(d.fn);
console.log(d.foo);
console.log(ns);
Expand Up @@ -5,10 +5,6 @@ const d = {

var ns = d;

var fn = d.fn;

var foo = d.foo;

console.log(fn);
console.log(foo);
console.log(d.fn);
console.log(d.foo);
console.log(ns);
Expand Up @@ -10,12 +10,8 @@ System.register([], function () {

var ns = d;

var fn = d.fn;

var foo = d.foo;

console.log(fn);
console.log(foo);
console.log(d.fn);
console.log(d.foo);
console.log(ns);

}
Expand Down
32 changes: 32 additions & 0 deletions test/function/samples/external-synthetic-exports/_config.js
@@ -0,0 +1,32 @@
module.exports = {
description: 'external modules can not have syntheticNamedExports',
solo: true,
options: {
plugins: [
{
resolveId(id) {
if (id === 'dep') {
return {
id,
external: true,
syntheticNamedExports: true
};
}
}
}
]
},
warnings: [
{
code: 'EXTERNAL_SYNTHETIC_EXPORTS',
importer: 'main.js',
source: 'dep',
message: "External 'dep' can not have 'syntheticNamedExports' enabled"
}
],
context: {
require() {
return 1;
}
}
};
2 changes: 2 additions & 0 deletions test/function/samples/external-synthetic-exports/main.js
@@ -0,0 +1,2 @@
import { foo } from 'dep';
foo;

0 comments on commit 004fb25

Please sign in to comment.