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

feat: add exportsIdentifierName option #289

Merged
merged 1 commit into from
Apr 20, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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