Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove isolated resolve() for compat with browser distribution (#…
…3935) * fix: remove isolated resolve() This is incompatible with the browser implementation of the method. * Set up basic browser tests * Update test suites * Update browser/error.ts Co-authored-by: Craig Morten <cmorten@users.noreply.github.com> * Use posix paths in test config Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com> Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
- Loading branch information
1 parent
4519b11
commit b99eea8
Showing
26 changed files
with
292 additions
and
51 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"exclude": ["*commonjsHelpers.js", "test"], | ||
"exclude": ["test"], | ||
"extension": [".ts", ""] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { error } from '../src/utils/error'; | ||
|
||
export const throwNoFileSystem = (method: string) => (..._args: any[]): never => { | ||
error({ | ||
code: 'NO_FS_IN_BROWSER', | ||
message: `Cannot access the file system (via "${method}") when using the browser build of Rollup. Make sure you supply a plugin with custom resolveId and load hooks to Rollup.`, | ||
url: 'https://rollupjs.org/guide/en/#a-simple-example' | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,4 @@ | ||
const nope = (method: string) => (..._args: any[]): never => { | ||
throw Object.assign( | ||
new Error( | ||
`Cannot access the file system (via "fs.${method}") when using the browser build of Rollup. Make sure you supply a plugin with custom resolveId and load hooks to Rollup.` | ||
), | ||
{ code: 'NO_FS_IN_BROWSER', url: 'https://rollupjs.org/guide/en/#a-simple-example' } | ||
); | ||
}; | ||
import { throwNoFileSystem } from './error'; | ||
|
||
export const lstatSync = nope('lstatSync'); | ||
export const readdirSync = nope('readdirSync'); | ||
export const readFile = nope('readFile'); | ||
export const realpathSync = nope('realpathSync'); | ||
export const writeFile = nope('writeFile'); | ||
export const readFile = throwNoFileSystem('fs.readFile'); | ||
export const writeFile = throwNoFileSystem('fs.writeFile'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { CustomPluginOptions } from '../src/rollup/types'; | ||
import { PluginDriver } from '../src/utils/PluginDriver'; | ||
import { throwNoFileSystem } from './error'; | ||
|
||
export async function resolveId( | ||
source: string, | ||
importer: string | undefined, | ||
_preserveSymlinks: boolean, | ||
pluginDriver: PluginDriver, | ||
skip: number | null, | ||
customOptions: CustomPluginOptions | undefined | ||
) { | ||
const pluginResult = await pluginDriver.hookFirst( | ||
'resolveId', | ||
[source, importer, { custom: customOptions }], | ||
null, | ||
skip | ||
); | ||
if (pluginResult == null) { | ||
throwNoFileSystem('path.resolve'); | ||
} | ||
return pluginResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import path from 'path'; | ||
|
||
const ID_CRYPTO = path.resolve('src/utils/crypto'); | ||
const ID_FS = path.resolve('src/utils/fs'); | ||
const ID_PATH = path.resolve('src/utils/path'); | ||
const ID_RESOLVEID = path.resolve('src/utils/resolveId'); | ||
|
||
export default function replaceBrowserModules() { | ||
return { | ||
name: 'replace-browser-modules', | ||
resolveId: (source, importee) => { | ||
if (importee && source[0] === '.') { | ||
const resolved = path.join(path.dirname(importee), source); | ||
switch (resolved) { | ||
case ID_CRYPTO: | ||
return path.resolve('browser/crypto.ts'); | ||
case ID_FS: | ||
return path.resolve('browser/fs.ts'); | ||
case ID_PATH: | ||
return path.resolve('browser/path.ts'); | ||
case ID_RESOLVEID: | ||
return path.resolve('browser/resolveId.ts'); | ||
} | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const fixturify = require('fixturify'); | ||
const { basename, resolve } = require('path'); | ||
const { rollup } = require('../../dist/rollup.browser.js'); | ||
const { assertFilesAreEqual, runTestSuiteWithSamples, compareError } = require('../utils.js'); | ||
|
||
runTestSuiteWithSamples('browser', resolve(__dirname, 'samples'), (dir, config) => { | ||
(config.skip ? it.skip : config.solo ? it.only : it)( | ||
basename(dir) + ': ' + config.description, | ||
async () => { | ||
let bundle; | ||
try { | ||
bundle = await rollup({ | ||
input: 'main', | ||
onwarn: warning => { | ||
if (!(config.expectedWarnings && config.expectedWarnings.indexOf(warning.code) >= 0)) { | ||
throw new Error( | ||
`Unexpected warnings (${warning.code}): ${warning.message}\n` + | ||
'If you expect warnings, list their codes in config.expectedWarnings' | ||
); | ||
} | ||
}, | ||
strictDeprecations: true, | ||
...config.options | ||
}); | ||
} catch (error) { | ||
if (config.error) { | ||
compareError(error, config.error); | ||
return; | ||
} else { | ||
throw error; | ||
} | ||
} | ||
if (config.error) { | ||
throw new Error('Expected an error while rolling up'); | ||
} | ||
let output; | ||
try { | ||
({ output } = await bundle.generate({ | ||
exports: 'auto', | ||
format: 'es', | ||
...(config.options || {}).output | ||
})); | ||
} catch (error) { | ||
if (config.generateError) { | ||
compareError(error, config.generateError); | ||
return; | ||
} else { | ||
throw error; | ||
} | ||
} | ||
if (config.generateError) { | ||
throw new Error('Expected an error while generating output'); | ||
} | ||
assertOutputMatches(output, dir); | ||
} | ||
); | ||
}); | ||
|
||
function assertOutputMatches(output, dir) { | ||
const actual = {}; | ||
for (const file of output) { | ||
const filePath = file.fileName.split('/'); | ||
const fileName = filePath.pop(); | ||
let currentDir = actual; | ||
for (const pathElement of filePath) { | ||
if (!currentDir[pathElement]) { | ||
currentDir[pathElement] = {}; | ||
} | ||
currentDir = currentDir[pathElement] = currentDir[pathElement] || {}; | ||
} | ||
currentDir[fileName] = file.source || file.code; | ||
} | ||
fixturify.writeSync(resolve(dir, '_actual'), actual); | ||
const expected = fixturify.readSync(resolve(dir, '_expected')); | ||
assertFilesAreEqual(actual, expected); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const { loader } = require('../../../utils.js'); | ||
|
||
module.exports = { | ||
description: 'bundles files for the browser', | ||
options: { | ||
plugins: loader({ | ||
main: `import {foo} from 'dep'; | ||
console.log(foo);`, | ||
dep: `export const foo = 42;` | ||
}) | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const foo = 42; | ||
|
||
console.log(foo); |
16 changes: 16 additions & 0 deletions
16
test/browser/samples/missing-dependency-resolution/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { loader } = require('../../../utils.js'); | ||
|
||
module.exports = { | ||
description: 'fails if a dependency cannot be resolved', | ||
options: { | ||
plugins: loader({ | ||
main: `import {foo} from 'dep'; | ||
console.log(foo);` | ||
}) | ||
}, | ||
error: { | ||
message: | ||
"Unexpected warnings (UNRESOLVED_IMPORT): 'dep' is imported by main, but could not be resolved – treating it as an external dependency\nIf you expect warnings, list their codes in config.expectedWarnings", | ||
watchFiles: ['main'] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
description: 'fails if an entry cannot be resolved', | ||
error: { | ||
code: 'UNRESOLVED_ENTRY', | ||
message: 'Could not resolve entry module (main).' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = { | ||
description: 'fails if a file cannot be loaded', | ||
options: { | ||
plugins: { | ||
resolveId(source) { | ||
return source; | ||
} | ||
} | ||
}, | ||
error: { | ||
code: 'NO_FS_IN_BROWSER', | ||
message: | ||
'Could not load main: Cannot access the file system (via "fs.readFile") when using the browser build of Rollup. Make sure you supply a plugin with custom resolveId and load hooks to Rollup.', | ||
url: 'https://rollupjs.org/guide/en/#a-simple-example', | ||
watchFiles: ['main'] | ||
} | ||
}; |
Oops, something went wrong.