mirror of https://github.com/rollup/rollup.git
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>pull/3943/head
parent
4519b11274
commit
b99eea8e70
@ -1,3 +0,0 @@
|
||||
{
|
||||
"spec": "test/test.js"
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
{
|
||||
"exclude": ["*commonjsHelpers.js", "test"],
|
||||
"exclude": ["test"],
|
||||
"extension": [".ts", ""]
|
||||
}
|
||||
|
@ -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'
|
||||
});
|
||||
};
|
@ -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');
|
||||
|
@ -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;
|
||||
}
|
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -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);
|
||||
}
|
@ -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;`
|
||||
})
|
||||
}
|
||||
};
|
@ -0,0 +1,3 @@
|
||||
const foo = 42;
|
||||
|
||||
console.log(foo);
|
@ -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).'
|
||||
}
|
||||
};
|
@ -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']
|
||||
}
|
||||
};
|
@ -0,0 +1,42 @@
|
||||
const { join, dirname } = require('path').posix;
|
||||
|
||||
module.exports = {
|
||||
description: 'renormalizes external paths if possible',
|
||||
options: {
|
||||
input: ['/main.js', '/nested/entry.js'],
|
||||
external(id) {
|
||||
return id.endsWith('ext');
|
||||
},
|
||||
plugins: {
|
||||
resolveId(source, importer) {
|
||||
if (source.endsWith('ext.js')) {
|
||||
return false;
|
||||
}
|
||||
if (!importer) {
|
||||
return source;
|
||||
}
|
||||
return join(dirname(importer), source);
|
||||
},
|
||||
load(id) {
|
||||
switch (id) {
|
||||
case '/main.js':
|
||||
return `import './nested/dep.js';
|
||||
import './ext.js';
|
||||
import './nested/nested-ext.js';`;
|
||||
case '/dep.js':
|
||||
return `import './ext.js';
|
||||
import './nested/nested-ext.js';`;
|
||||
case '/nested/dep.js':
|
||||
return `import '../ext.js';
|
||||
import './nested-ext.js';`;
|
||||
case '/nested/entry.js':
|
||||
return `import '../dep.js';
|
||||
import '../ext.js';
|
||||
import './nested-ext.js';`;
|
||||
default:
|
||||
throw new Error(`Unexpected id ${id}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
@ -0,0 +1,2 @@
|
||||
import './ext.js';
|
||||
import './nested/nested-ext.js';
|
@ -0,0 +1,2 @@
|
||||
import './ext.js';
|
||||
import './nested/nested-ext.js';
|
@ -0,0 +1,16 @@
|
||||
const { loader } = require('../../../utils.js');
|
||||
|
||||
module.exports = {
|
||||
description: 'bundles files for the browser',
|
||||
options: {
|
||||
input: ['main', 'dep'],
|
||||
plugins: loader({
|
||||
main: `import {foo} from 'dep';
|
||||
console.log(foo);`,
|
||||
dep: `export const foo = 42;`
|
||||
}),
|
||||
output: {
|
||||
entryFileNames: '[name]-[hash].js'
|
||||
}
|
||||
}
|
||||
};
|
@ -0,0 +1,3 @@
|
||||
const foo = 42;
|
||||
|
||||
export { foo };
|
@ -0,0 +1,3 @@
|
||||
import { foo } from './dep-cf8755fa.js';
|
||||
|
||||
console.log(foo);
|
Loading…
Reference in new issue