Skip to content

Commit 17218ea

Browse files
authored
Merge pull request #155 from PepsRyuu/SyntheticExportsCustom
Custom Synthetic Exports
2 parents 011985f + b873be7 commit 17218ea

File tree

3 files changed

+119
-10
lines changed

3 files changed

+119
-10
lines changed

lib/impl/CodeGenerator.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ let path = require('path');
22
let ConvertSourceMap = require('convert-source-map');
33
let PluginLifecycle = require('./PluginLifecycle');
44

5+
function getSyntheticExports (context, filePath) {
6+
let synthetic = context.syntheticNamedExports[filePath];
7+
if (synthetic === true) {
8+
synthetic = 'default';
9+
}
10+
11+
return `if (module.exports.${synthetic}) {
12+
for (var prop in module.exports.${synthetic}) {
13+
prop !== 'default' && __e__(prop, module.exports.${synthetic}[prop]);
14+
}
15+
}`
16+
}
17+
518
function generateFile (context, filePath) {
619
let { code, map, imports, externalImports, dynamicImports } = context.files[filePath];
720

@@ -68,13 +81,7 @@ function generateFile (context, filePath) {
6881
}, function (require, module, __nollup__global__) {
6982
"use strict";
7083
eval('${code}');
71-
${context.syntheticNamedExports[filePath]?
72-
`if (module.exports.default) {
73-
for (var prop in module.exports.default) {
74-
prop !== 'default' && __e__(prop, module.exports.default[prop]);
75-
}
76-
}`
77-
: ''}
84+
${context.syntheticNamedExports[filePath]? getSyntheticExports(context, filePath) : ''}
7885
});
7986
8087
${imports.map(i => {

lib/impl/PluginLifecycle.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ module.exports = {
148148
}
149149

150150
if (hr.syntheticNamedExports) {
151-
context.syntheticNamedExports[hr.id] = true;
151+
context.syntheticNamedExports[hr.id] = hr.syntheticNamedExports;
152152
}
153153

154154
return hr;
@@ -163,7 +163,7 @@ module.exports = {
163163

164164
if (typeof hr === 'object' && hr.code) {
165165
if (hr.syntheticNamedExports) {
166-
context.syntheticNamedExports[filePath] = true;
166+
context.syntheticNamedExports[filePath] = hr.syntheticNamedExports;
167167
}
168168
return hr;
169169
}
@@ -197,7 +197,7 @@ module.exports = {
197197
}
198198

199199
if (result.syntheticNamedExports) {
200-
context.syntheticNamedExports[filePath] = true;
200+
context.syntheticNamedExports[filePath] = result.syntheticNamedExports;
201201
}
202202

203203
mapChain.push({

test/cases/api/hooks.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,39 @@ describe ('API: Plugin Hooks', () => {
517517
fs.reset();
518518
});
519519

520+
it ('should should accept a string for syntheticNamedExports', async () => {
521+
fs.stub('./src/main.js', () => 'export { hello } from "./lol";');
522+
fs.stub('./src/lol.js', () => 'export var __moduleExports = { hello: "world" };')
523+
let phase = 0;
524+
525+
let bundle = await nollup({
526+
input: './src/main.js',
527+
plugins: [{
528+
load (id) {
529+
if (id.indexOf('lol') > -1) {
530+
return {
531+
code: fs.readFileSync(id, 'utf8'),
532+
syntheticNamedExports: phase === 0? false : '__moduleExports'
533+
}
534+
}
535+
536+
}
537+
}]
538+
});
539+
540+
let output = (await bundle.generate({ format: 'iife' })).output;
541+
let result = eval(output[0].code);
542+
expect(result.hello).to.be.undefined;
543+
544+
phase = 1;
545+
bundle.invalidate('./src/lol.js');
546+
output = (await bundle.generate({ format: 'iife' })).output;
547+
result = eval(output[0].code);
548+
expect(result.hello).to.equal('world');
549+
550+
fs.reset();
551+
});
552+
520553
it ('should be allowed to return an AST');
521554
});
522555

@@ -715,6 +748,40 @@ describe ('API: Plugin Hooks', () => {
715748
fs.reset();
716749
});
717750

751+
it ('should allow string for syntheticNamedExports', async () => {
752+
fs.stub('./src/main.js', () => 'export { hello } from "./lol";');
753+
fs.stub('./src/lol.js', () => 'export var __moduleExports = { hello: "world" };')
754+
let phase = 0;
755+
756+
let bundle = await nollup({
757+
input: './src/main.js',
758+
plugins: [{
759+
resolveId (id) {
760+
if (id.indexOf('lol') > -1) {
761+
return {
762+
id: path.resolve(process.cwd(), './src/lol.js'),
763+
syntheticNamedExports: phase === 0? false : '__moduleExports'
764+
}
765+
}
766+
767+
}
768+
}]
769+
});
770+
771+
let output = (await bundle.generate({ format: 'iife' })).output;
772+
let result = eval(output[0].code);
773+
expect(result.hello).to.be.undefined;
774+
775+
phase = 1;
776+
bundle.invalidate('./src/lol.js');
777+
bundle.invalidate('./src/main.js');
778+
output = (await bundle.generate({ format: 'iife' })).output;
779+
result = eval(output[0].code);
780+
expect(result.hello).to.equal('world');
781+
782+
fs.reset();
783+
});
784+
718785
it ('should trigger for input file', async () => {
719786
fs.stub('./src/main.js', () => 'console.log(123)');
720787

@@ -1179,6 +1246,41 @@ describe ('API: Plugin Hooks', () => {
11791246
fs.reset();
11801247
});
11811248

1249+
it ('should allow strings for syntheticNamedExports', async () => {
1250+
fs.stub('./src/main.js', () => `
1251+
export { hello } from './lol'
1252+
`);
1253+
fs.stub('./src/lol.js', () => 'export var __moduleExports = { hello: "world" };')
1254+
let phase = 0;
1255+
1256+
let bundle = await nollup({
1257+
input: './src/main.js',
1258+
plugins: [{
1259+
transform (code, id) {
1260+
if (id.indexOf('lol') > -1) {
1261+
return {
1262+
code,
1263+
syntheticNamedExports: phase === 0? false : '__moduleExports'
1264+
}
1265+
}
1266+
1267+
}
1268+
}]
1269+
});
1270+
1271+
let output = (await bundle.generate({ format: 'iife' })).output;
1272+
let result = eval(output[0].code);
1273+
expect(result.hello).to.be.undefined;
1274+
1275+
phase = 1;
1276+
bundle.invalidate('./src/lol.js');
1277+
output = (await bundle.generate({ format: 'iife' })).output;
1278+
result = eval(output[0].code);
1279+
expect(result.hello).to.equal('world');
1280+
1281+
fs.reset();
1282+
});
1283+
11821284
it ('should not fail if passing null sourcemap');
11831285

11841286
it ('should accept an object with code and map returned');

0 commit comments

Comments
 (0)