ronami / minipack

📦 A simplified example of a modern module bundler written in JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

a module will be bundled twice?

lessfish opened this issue · comments

I make another file named message2.js, and the content is as below:

import {name} from './name.js';

export default `hello2 ${name}!`;

and the run the script, the variable queue is as below:

[ { id: 0,
    filename: './../example/entry.js',
    dependencies: [ './message.js', './message2.js' ],
    code: '"use strict";\n\nvar _message = require("./message.js");\n\nvar _message2 = _interopRequireDefault(_message);\n\nvar _message3 = require("./message2.js");\n\nvar _message4 = _interopRequireDefault(_message3);\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nconsole.log(_message2.default);\nconsole.log(_message4.default);',
    mapping: { './message.js': 1, './message2.js': 2 } },
  { id: 1,
    filename: '../example/message.js',
    dependencies: [ './name.js' ],
    code: '"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n  value: true\n});\n\nvar _name = require("./name.js");\n\nexports.default = "hello " + _name.name + "!";',
    mapping: { './name.js': 3 } },
  { id: 2,
    filename: '../example/message2.js',
    dependencies: [ './name.js' ],
    code: '"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n  value: true\n});\n\nvar _name = require("./name.js");\n\nexports.default = "hello2 " + _name.name + "!";',
    mapping: { './name.js': 4 } },
  { id: 3,
    filename: '../example/name.js',
    dependencies: [],
    code: '"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n  value: true\n});\nvar name = exports.name = \'world\';',
    mapping: {} },
  { id: 4,
    filename: '../example/name.js',
    dependencies: [],
    code: '"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n  value: true\n});\nvar name = exports.name = \'world\';',
    mapping: {} } ]

so the name.js will be bundled twice?

or it's not considered in the minipack?

thanks

It is skipped in the minipack.

To avoid parsing same module twice, you can simply add checking in the for .. of queue loop of createGraph function.

Hey @hanzichi, thanks for your interest :)

Yes, in this case, the name.js module will be bundled (and parsed) twice.

I chose not to cover this case and other cases (like caching module exports and circular dependencies) to keep minipack's implementation as simple as possible.

@Williammer @ronami

so the case is not considered in the minipack as I thought, thanks for your explanations