Skip to content

Commit

Permalink
feat: add exportsIdentifierName option (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
fossamagna authored Apr 20, 2023
1 parent d0cbdbe commit 381ffe1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,18 @@ class EntryPointFunctions {
}

class GlobalAssignments {
constructor() {
constructor(exportsIdentifierName = 'exports') {
this.stubs = [];
this.functionNames = new Set();
this.exportsIdentifierName = exportsIdentifierName;
}

add(functionName) {
if (this.functionNames.has(functionName)) {
return;
}
this.functionNames.add(functionName);
this.stubs.push(createGlobalAssignmentASTNode(functionName));
this.stubs.push(createGlobalAssignmentASTNode(functionName, this.exportsIdentifierName));
}

getGlobalAssignments() {
Expand Down Expand Up @@ -147,8 +148,8 @@ function generateStubs(ast, options) {
return escodegen.generate(baseAST, { comment: !!options.comment });
}

function generateGlobalAssignments(ast) {
const globalAssignments = new GlobalAssignments();
function generateGlobalAssignments(ast, { exportsIdentifierName = 'exports'}) {
const globalAssignments = new GlobalAssignments(exportsIdentifierName);
estraverse.traverse(ast, {
leave: (node) => {
if (node.type === 'ExpressionStatement'
Expand All @@ -174,7 +175,7 @@ function generateGlobalAssignments(ast) {
return escodegen.generate(baseAST);
}

function createGlobalAssignmentASTNode(functionName) {
function createGlobalAssignmentASTNode(functionName, exportsIdentifierName) {
const node = {
type: "ExpressionStatement",
expression: {
Expand All @@ -197,7 +198,7 @@ function createGlobalAssignmentASTNode(functionName) {
computed: false,
object: {
type: "Identifier",
name: "exports"
name: exportsIdentifierName // TODO: この名前を外部から指定できるようにしないとダメ。webpackでesnextなら `__webpack_exports__`とする必要がある。
},
property: {
type: "Identifier",
Expand All @@ -209,7 +210,7 @@ function createGlobalAssignmentASTNode(functionName) {
return node;
}

exports.generate = function(source, options = { comment: false, autoGlobalExports: false }){
exports.generate = function(source, options = { comment: false, autoGlobalExports: false, exportsIdentifierName: "exports" }){
const ast = esprima.parseModule(source, { attachComment: options.comment });
const functions = generateStubs(ast, options);
const globalAssignments = options.autoGlobalExports ? generateGlobalAssignments(ast, options) : undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
global.bar = exports.bar;
global.bar = __webpack_exports__.bar;
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test('generate function generate entry functions from export with autoGlobalExpo
const source = fs.readFileSync(__dirname + '/fixtures/esm-source.js', {encoding: 'utf8'});
const expected = fs.readFileSync(__dirname + '/fixtures/esm-expected-autoGlobalExports.js', {encoding: 'utf8'});
const expectedGlobalAssignments = fs.readFileSync(__dirname + '/fixtures/esm-exports-generated-global-assignments-expected.js', {encoding: 'utf8'});
const output = generate(source, { comment: true, autoGlobalExports: true });
const output = generate(source, { comment: true, autoGlobalExports: true, exportsIdentifierName: '__webpack_exports__'});
t.equal(output.entryPointFunctions, expected, 'actual output will match expected');
t.equal(output.globalAssignments, expectedGlobalAssignments, 'actual output will match expected');
t.end();
Expand Down

0 comments on commit 381ffe1

Please sign in to comment.