Skip to content

Commit

Permalink
Adapt test assertions for wrapped CommonJS
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 18, 2024
1 parent 119958f commit 6f5d440
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@ const ID_MAIN = path.join(__dirname, 'main.js');
const ID_OTHER = path.join(__dirname, 'other.js');

module.exports = {
description: 'exposes cjs file type detection to other plugins (strictRequires: "auto")',
description: 'exposes cjs file type detection to other plugins',
options: {
input: [ID_MAIN, ID_OTHER],
plugins: [
{
moduleParsed({ id, meta: { commonjs } }) {
if (id === ID_OTHER) {
if (commonjs.isCommonJS !== true) {
if (commonjs.isCommonJS !== 'withRequireFunction') {
throw new Error(
`File "${id}" wrongly detected: isCommonJS === ${JSON.stringify(
commonjs.isCommonJS
)}`
)} instead of "withRequireFunction"`
);
}
} else if (commonjs && commonjs.isCommonJS !== false) {
} else if (commonjs && !!commonjs.isCommonJS) {
throw new Error(
`File "${id}" wrongly detected: isCommonJS === ${JSON.stringify(commonjs.isCommonJS)}`
`File "${id}" wrongly detected: isCommonJS === ${JSON.stringify(
commonjs.isCommonJS
)} instead of false`
);
}
}
}
]
},
pluginOptions: {
strictRequires: 'auto'
}
};
31 changes: 25 additions & 6 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -4789,21 +4789,40 @@ Generated by [AVA](https://avajs.dev).
{
'main.js': `'use strict';␊
␊
var other = require('./other.js');␊
var other = require('./other-UXsUMMAz.js');␊
␊
t.is(other, 'foo');␊
var otherExports = other.requireOther();␊
var foo = /*@__PURE__*/other.getDefaultExportFromCjs(otherExports);␊
␊
t.is(foo, 'foo');␊
`,
'other.js': `'use strict';␊
'other-UXsUMMAz.js': `'use strict';␊
␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
var other = 'foo';␊
var other;␊
var hasRequiredOther;␊
␊
var foo = /*@__PURE__*/getDefaultExportFromCjs(other);␊
function requireOther () {␊
if (hasRequiredOther) return other;␊
hasRequiredOther = 1;␊
other = 'foo';␊
return other;␊
}␊
␊
module.exports = foo;␊
exports.getDefaultExportFromCjs = getDefaultExportFromCjs;␊
exports.requireOther = requireOther;␊
`,
'other.js': `'use strict';␊
␊
var other$1 = require('./other-UXsUMMAz.js');␊
␊
var otherExports = other$1.requireOther();␊
var other = /*@__PURE__*/other$1.getDefaultExportFromCjs(otherExports);␊
␊
module.exports = other;␊
`,
}

Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 6f5d440

Please sign in to comment.