/
index.js
160 lines (148 loc) · 5.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const assert = require('node:assert');
const path = require('node:path');
const { loadConfigFile } = require('../../dist/loadConfigFile.js');
const { compareError } = require('../utils');
describe('loadConfigFile', () => {
const defaultConfigs = [
{
external: [],
input: 'my-input',
output: [
{
file: 'my-file',
format: 'es',
plugins: []
}
],
plugins: [
{
name: 'stdin'
}
]
}
];
it('loads an ESM config file', async () => {
const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/esm-via-pkg/rollup.config.js')
);
assert.strictEqual(warnings.count, 0);
// Remove undefined values and functions before checking
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), defaultConfigs);
});
it('loads an ESM config file with mjs extension', async () => {
const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/esm-via-ext/rollup.config.mjs')
);
assert.strictEqual(warnings.count, 0);
// Remove undefined values and functions before checking
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), defaultConfigs);
});
it('loads an ESM config file exporting a config as a function with defineConfig()', async () => {
const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/esm-defineconfig-as-fn/rollup.config.mjs')
);
assert.strictEqual(warnings.count, 0);
// Remove undefined values and functions before checking
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), defaultConfigs);
});
it('loads a CommonJS config file', async () => {
const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/cjs-via-pkg/rollup.config.js')
);
assert.strictEqual(warnings.count, 0);
// Remove undefined values and functions before checking
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), defaultConfigs);
});
it('loads a CommonJS config file with cjs extension', async () => {
const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/cjs-via-ext/rollup.config.cjs')
);
assert.strictEqual(warnings.count, 0);
// Remove undefined values and functions before checking
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), defaultConfigs);
});
it('loads a CommonJS config file exporting a config as a function with defineConfig()', async () => {
const { options, warnings } = await loadConfigFile(
path.resolve(__dirname, 'samples/cjs-defineconfig-as-fn/rollup.config.cjs')
);
assert.strictEqual(warnings.count, 0);
// Remove undefined values and functions before checking
assert.deepStrictEqual(JSON.parse(JSON.stringify(options)), defaultConfigs);
});
it('throws a helpful error when loading an ES module that should actually be CommonJS', async () => {
let caughtError;
try {
await loadConfigFile(path.resolve(__dirname, 'samples/cjs-as-esm/rollup.config.js'));
} catch (error) {
caughtError = error;
}
compareError(caughtError, {
cause: {
message: "Unexpected token 'export'"
},
code: 'INVALID_CONFIG_MODULE_FORMAT',
message:
'Node tried to load your configuration file as CommonJS even though it is likely an ES module. To resolve this, change the extension of your configuration to ".mjs", set "type": "module" in your package.json file or pass the "--bundleConfigAsCjs" flag.\n\nOriginal error: Unexpected token \'export\'',
url: 'https://rollupjs.org/guide/en/#--bundleconfigascjs'
});
});
it('just throws the error if it is not accompanied by the proper warning', async () => {
let caughtError;
try {
const promise = loadConfigFile(
path.resolve(__dirname, 'samples/esm-with-error/rollup.config.js')
);
process.emit('warning', { message: 'Another warning.' });
await promise;
} catch (error) {
caughtError = error;
}
compareError(caughtError, {
message: 'Config broken.'
});
});
it('throws a helpful error when loading a CommonJS module that should actually be ES', async () => {
let caughtError;
try {
await loadConfigFile(path.resolve(__dirname, 'samples/esm-as-cjs/rollup.config.js'));
} catch (error) {
caughtError = error;
}
assert.strictEqual(
caughtError.message.slice(0, 256),
'Node tried to load your configuration as an ES module even though it is likely CommonJS. To resolve this, change the extension of your configuration to ".cjs" or pass the "--bundleConfigAsCjs" flag.\n\nOriginal error: module is not defined in ES module scope'
);
});
it('throws a helpful error when loading a CJS module that should actually be ESM while bundling', async () => {
let caughtError;
try {
await loadConfigFile(path.resolve(__dirname, 'samples/esm-as-cjs/rollup.config.js'), {
configPlugin: '{transform: c => c}'
});
} catch (error) {
caughtError = error;
}
compareError(caughtError, {
cause: {
message: 'module is not defined in ES module scope'
},
code: 'INVALID_CONFIG_MODULE_FORMAT',
message:
'Rollup transpiled your configuration to an ES module even though it appears to contain CommonJS elements. To resolve this, you can pass the "--bundleConfigAsCjs" flag to Rollup or change your configuration to only contain valid ESM code.\n\nOriginal error: module is not defined in ES module scope',
url: 'https://rollupjs.org/guide/en/#--bundleconfigascjs'
});
});
it('just throws other errors while bundling', async () => {
let caughtError;
try {
await loadConfigFile(path.resolve(__dirname, 'samples/esm-with-error/rollup.config.js'), {
configPlugin: '{transform: c => c}'
});
} catch (error) {
caughtError = error;
}
compareError(caughtError, {
message: 'Config broken.'
});
});
});