Skip to content

Commit 95bb728

Browse files
committed
Refine late-entry tests
1 parent dc6a4d8 commit 95bb728

File tree

6 files changed

+93
-3
lines changed

6 files changed

+93
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const path = require('path');
2+
const assert = require('assert');
3+
4+
let referenceId;
5+
const testEntry = 'generated-foo.js';
6+
7+
module.exports = {
8+
description:
9+
'use correct side-effects flags for files that become entry points after they are loaded (strictRequires: true)',
10+
testEntry,
11+
options: {
12+
treeshake: { moduleSideEffects: false },
13+
plugins: [
14+
{
15+
name: 'test',
16+
load(id) {
17+
if (id.endsWith('foo.js')) {
18+
referenceId = this.emitFile({ type: 'chunk', id: path.join(__dirname, 'foo.js') });
19+
}
20+
},
21+
generateBundle() {
22+
assert.strictEqual(this.getFileName(referenceId), testEntry);
23+
},
24+
moduleParsed({ id, code }) {
25+
console.log('\n===', id);
26+
console.log(code);
27+
}
28+
}
29+
],
30+
output: { chunkFileNames: 'generated-[name].js' }
31+
},
32+
pluginOptions: {
33+
strictRequires: true
34+
},
35+
global: (global, t) => {
36+
t.is(global.foo, 'foo');
37+
}
38+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This side-effect will only be respected if this is an entry point
2+
global.foo = 'foo';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './foo.js';
2+
3+
export default 'main';

β€Žpackages/commonjs/test/fixtures/function/module-side-effects-late-entry/_config.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
const path = require('path');
2+
const assert = require('assert');
3+
4+
let referenceId;
5+
const testEntry = 'generated-foo2.js';
26

37
module.exports = {
48
description:
5-
'use correct side-effects flags for files that become entry points after they are loaded (strictRequires: "auto")',
9+
'use correct side-effects flags for files that become entry points after they are loaded (strictRequires: false)',
10+
testEntry,
611
options: {
712
treeshake: { moduleSideEffects: false },
813
plugins: [
914
{
15+
name: 'test',
1016
load(id) {
1117
if (id.endsWith('foo.js')) {
12-
this.emitFile({ type: 'chunk', id: path.join(__dirname, 'foo.js') });
18+
referenceId = this.emitFile({ type: 'chunk', id: path.join(__dirname, 'foo.js') });
1319
}
20+
},
21+
generateBundle() {
22+
assert.strictEqual(this.getFileName(referenceId), testEntry);
1423
}
1524
}
1625
],
1726
output: { chunkFileNames: 'generated-[name].js' }
1827
},
1928
pluginOptions: {
20-
strictRequires: 'auto'
29+
strictRequires: false
2130
},
2231
global: (global, t) => {
2332
t.is(global.foo, 'foo');

β€Žpackages/commonjs/test/snapshots/function.js.md

+38
Original file line numberDiff line numberDiff line change
@@ -6575,6 +6575,44 @@ Generated by [AVA](https://avajs.dev).
65756575
`,
65766576
}
65776577

6578+
## module-side-effects-late-entry-strict
6579+
6580+
> Snapshot 1
6581+
6582+
{
6583+
'generated-foo.js': `'use strict';␊
6584+
␊
6585+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
6586+
␊
6587+
function getDefaultExportFromCjs (x) {␊
6588+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
6589+
}␊
6590+
␊
6591+
var foo$1 = {};␊
6592+
␊
6593+
var hasRequiredFoo;␊
6594+
␊
6595+
function requireFoo () {␊
6596+
if (hasRequiredFoo) return foo$1;␊
6597+
hasRequiredFoo = 1;␊
6598+
// This side-effect will only be respected if this is an entry point␊
6599+
commonjsGlobal.foo = 'foo';␊
6600+
return foo$1;␊
6601+
}␊
6602+
␊
6603+
var fooExports = requireFoo();␊
6604+
var foo = /*@__PURE__*/getDefaultExportFromCjs(fooExports);␊
6605+
␊
6606+
module.exports = foo;␊
6607+
`,
6608+
'main.js': `'use strict';␊
6609+
␊
6610+
var main = 'main';␊
6611+
␊
6612+
module.exports = main;␊
6613+
`,
6614+
}
6615+
65786616
## module_require
65796617

65806618
> Snapshot 1
Binary file not shown.

0 commit comments

Comments
Β (0)