Skip to content

Commit 4261472

Browse files
authored
Merge pull request #219 from PepsRyuu/ResolveIdOptions
Added resolve isEntry and custom
2 parents 93f886a + 75f7df9 commit 4261472

File tree

8 files changed

+178
-10
lines changed

8 files changed

+178
-10
lines changed

docs/sdk.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ container.start();
4141
// Call hooks as you need them
4242
container.hooks.buildStart(options);
4343
container.hooks.resolveDynamicImport(id, parentId);
44-
container.hooks.resolveId(id, parentId);
44+
container.hooks.resolveId(id, parentId, options);
4545
container.hooks.load(filepath, parentFilepath);
4646
container.hooks.transform(code, id);
4747
container.hooks.watchChange(id);

lib/impl/NollupContext.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let RollupConfigContainer = require('./RollupConfigContainer');
1010
* @return {Promise<string>}
1111
*/
1212
async function resolveInputId (context, id) {
13-
let resolved = await context.plugins.hooks.resolveId(id, undefined);
13+
let resolved = await context.plugins.hooks.resolveId(id, undefined, { isEntry: true });
1414
if ((typeof resolved === 'object' && resolved.external)) {
1515
throw new Error('Input cannot be external');
1616
}

lib/impl/PluginContext.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ module.exports = {
110110
/**
111111
* @param {string} importee
112112
* @param {string} importer
113-
* @param {{ custom?: import('rollup').CustomPluginOptions, skipSelf?: boolean }} options
113+
* @param {{ isEntry?: boolean, custom?: import('rollup').CustomPluginOptions, skipSelf?: boolean }} options
114114
* @return {Promise<RollupResolveId>}
115115
*/
116116
async resolve (importee, importer, options = {}) {
@@ -119,7 +119,10 @@ module.exports = {
119119
}
120120

121121
try {
122-
return await PluginLifecycle.resolveIdImpl(container, importee, importer);
122+
return await PluginLifecycle.resolveIdImpl(container, importee, importer, {
123+
isEntry: options.isEntry,
124+
custom: options.custom
125+
});
123126
} finally {
124127
if (options.skipSelf) {
125128
PluginLifecycle.resolveIdSkips.remove(plugin, importer, importee);

lib/impl/PluginLifecycle.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,12 @@ const PluginLifecycle = {
197197
* @param {string} parentFilePath
198198
* @return {Promise<RollupResolveIdResult>}
199199
*/
200-
async resolveIdImpl (container, id, parentFilePath) {
200+
async resolveIdImpl (container, id, parentFilePath, options = {}) {
201+
options.isEntry = options.isEntry || false;
202+
options.custom = options.hasOwnProperty('custom')? options.custom : {};
203+
201204
let __plugins = container.__plugins.filter(p => !this.resolveIdSkips.contains(p.execute, parentFilePath, id));
202-
let hr = await callAsyncFirstHook(/** @type {PluginContainer} */ ({ __plugins, __config: container.__config }), 'resolveId', [id, parentFilePath]);
205+
let hr = await callAsyncFirstHook(/** @type {PluginContainer} */ ({ __plugins, __config: container.__config }), 'resolveId', [id, parentFilePath, options]);
203206

204207
if (hr === false || isExternal(container.__config, id)) {
205208
return {
@@ -288,8 +291,8 @@ const PluginLifecycle = {
288291
return await PluginLifecycle.resolveIdImpl(container, id, parentFilePath);
289292
},
290293

291-
async resolveId (id, parentFilePath) {
292-
return await PluginLifecycle.resolveIdImpl(container, id, parentFilePath);
294+
async resolveId (id, parentFilePath, options) {
295+
return await PluginLifecycle.resolveIdImpl(container, id, parentFilePath, options);
293296
},
294297

295298
async load ( filePath, parentFilePath) {

lib/impl/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
* @typedef PluginLifecycleHooks
166166
* @property {(options: RollupConfigContainer) => Promise<void>} buildStart
167167
* @property {(id: string, parentFilePath: string) => Promise<RollupResolveIdResult>} resolveDynamicImport
168-
* @property {(id: string, parentFilePath: string) => Promise<RollupResolveIdResult>} resolveId
168+
* @property {(id: string, parentFilePath: string, options: object) => Promise<RollupResolveIdResult>} resolveId
169169
* @property {(filePath: string, parentFilePath: string) => Promise<RollupSourceDescription>} load
170170
* @property {(code: string, id: string) => Promise<RollupSourceDescription>} transform
171171
* @property {(filePath: string) => void} watchChange

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@
3636
"mocha": "^8.3.0",
3737
"mocha-istanbul-ui": "^0.4.1",
3838
"proxyquire": "^2.0.1",
39-
"rollup": "^2.33.3"
39+
"rollup": "^2.59.0"
4040
}
4141
}

test/cases/api/context.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,112 @@ describe ('API: Plugin Context', () => {
18901890
fs.reset();
18911891
});
18921892

1893+
it ('should accept custom for options', async () => {
1894+
fs.stub('./src/main.js', () => 'export default 123');
1895+
fs.stub('./src/lol.js', () => 'export default 456');
1896+
let passed = false;
1897+
1898+
let bundle = await nollup({
1899+
input: './src/main.js',
1900+
plugins: [{
1901+
resolveId (importee, importer, options) {
1902+
if (importee.indexOf('lol') > -1) {
1903+
expect(options.isEntry).to.be.false;
1904+
expect(options.custom).to.deep.equal({hello: 'world'})
1905+
passed = true;
1906+
}
1907+
},
1908+
1909+
transform () {
1910+
return new Promise(resolve => {
1911+
this.resolve(
1912+
'./lol',
1913+
path.resolve(process.cwd(), './src/main.js'),
1914+
{ custom: { hello: 'world'} }
1915+
).then(resolved => {
1916+
resolve();
1917+
});
1918+
});
1919+
}
1920+
}]
1921+
});
1922+
1923+
let { output } = await bundle.generate({ format: 'esm' });
1924+
expect(passed).to.be.true;
1925+
fs.reset();
1926+
});
1927+
1928+
it ('should accept isEntry for options', async () => {
1929+
fs.stub('./src/main.js', () => 'export default 123');
1930+
fs.stub('./src/lol.js', () => 'export default 456');
1931+
let passed = false;
1932+
1933+
let bundle = await nollup({
1934+
input: './src/main.js',
1935+
plugins: [{
1936+
resolveId (importee, importer, options) {
1937+
if (importee.indexOf('lol') > -1) {
1938+
expect(options.isEntry).to.be.true;
1939+
expect(options.custom).to.be.undefined;
1940+
passed = true;
1941+
}
1942+
},
1943+
1944+
transform () {
1945+
return new Promise(resolve => {
1946+
this.resolve(
1947+
'./lol',
1948+
path.resolve(process.cwd(), './src/main.js'),
1949+
{ isEntry: true }
1950+
).then(resolved => {
1951+
resolve();
1952+
});
1953+
});
1954+
}
1955+
}]
1956+
});
1957+
1958+
let { output } = await bundle.generate({ format: 'esm' });
1959+
expect(passed).to.be.true;
1960+
fs.reset();
1961+
});
1962+
1963+
it ('should not have skipSelf for options in resolveId', async () => {
1964+
fs.stub('./src/main.js', () => 'export default 123');
1965+
fs.stub('./src/lol.js', () => 'export default 456');
1966+
let passed = false;
1967+
1968+
let bundle = await nollup({
1969+
input: './src/main.js',
1970+
plugins: [{
1971+
resolveId (importee, importer, options) {
1972+
if (importee.indexOf('lol') > -1) {
1973+
expect(options.isEntry).to.be.false;
1974+
expect(options.custom).to.be.undefined;
1975+
expect(options.skipSelf).to.be.undefined;
1976+
passed = true;
1977+
}
1978+
}
1979+
}, {
1980+
transform () {
1981+
return new Promise(resolve => {
1982+
this.resolve(
1983+
'./lol',
1984+
path.resolve(process.cwd(), './src/main.js'),
1985+
{ skipSelf: true }
1986+
).then(resolved => {
1987+
resolve();
1988+
});
1989+
});
1990+
}
1991+
}]
1992+
});
1993+
1994+
let { output } = await bundle.generate({ format: 'esm' });
1995+
expect(passed).to.be.true;
1996+
fs.reset();
1997+
});
1998+
18931999
it ('should return null if module cannot be resolved by anyone and isn\'t external');
18942000

18952001
});

test/cases/api/hooks.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,36 @@ describe ('API: Plugin Hooks', () => {
10041004
expect(passed).to.be.true;
10051005
fs.reset();
10061006
});
1007+
1008+
it ('should allow options as third parameter with isEntry and default custom', async () => {
1009+
fs.stub('./src/main.js', () => 'import "./lol";');
1010+
fs.stub('./src/lol.js', () => 'export default 123');
1011+
let passed1 = false;
1012+
let passed2 = false;
1013+
1014+
let bundle = await nollup({
1015+
input: './src/main.js',
1016+
plugins: [{
1017+
resolveId (importee, importer, options) {
1018+
if (importee.indexOf('main.js') > -1) {
1019+
expect(options.isEntry).to.be.true;
1020+
expect(options.custom).to.deep.equal({});
1021+
passed1 = true;
1022+
}
1023+
1024+
if (importee.indexOf('lol') > -1) {
1025+
expect(options.isEntry).to.be.false;
1026+
expect(options.custom).to.deep.equal({});
1027+
passed2 = true;
1028+
}
1029+
}
1030+
}]
1031+
});
1032+
1033+
let { output } = await bundle.generate({ format: 'esm' });
1034+
expect(passed1 && passed2).to.be.true;
1035+
fs.reset();
1036+
});
10071037
});
10081038

10091039
describe ('resolveDynamicImport', () => {
@@ -1253,6 +1283,32 @@ describe ('API: Plugin Hooks', () => {
12531283
expect(output[0].code.indexOf('import(specialvariable)') > -1).to.be.true;
12541284
fs.reset();
12551285
});
1286+
1287+
it ('should have options if it fallbacks to resolveId', async () => {
1288+
fs.stub('./src/main.js', () => 'import("./lol")');
1289+
fs.stub('./src/lol.js', () => 'export default 123');
1290+
let passed = false;
1291+
1292+
let bundle = await nollup({
1293+
input: './src/main.js',
1294+
plugins: [{
1295+
resolveDynamicImport () {
1296+
return null;
1297+
},
1298+
resolveId (importee, importer, options) {
1299+
if (importee.indexOf('lol') > -1) {
1300+
expect(options.isEntry).to.be.false;
1301+
expect(options.custom).to.deep.equal({});
1302+
passed = true;
1303+
}
1304+
}
1305+
}]
1306+
});
1307+
1308+
let { output } = await bundle.generate({ format: 'esm' });
1309+
expect(passed).to.be.true;
1310+
fs.reset();
1311+
})
12561312
});
12571313

12581314
describe ('transform', () => {

0 commit comments

Comments
 (0)