mirror of https://github.com/lukechilds/rollup.git
Rich Harris
8 years ago
committed by
GitHub
52 changed files with 445 additions and 214 deletions
@ -1,11 +1,15 @@ |
|||
export default function getGlobalNameMaker ( globals, onwarn ) { |
|||
export default function getGlobalNameMaker ( globals, bundle ) { |
|||
const fn = typeof globals === 'function' ? globals : id => globals[ id ]; |
|||
|
|||
return function ( module ) { |
|||
const name = fn( module.id ); |
|||
if ( name ) return name; |
|||
|
|||
onwarn( `No name was provided for external module '${module.id}' in options.globals – guessing '${module.name}'` ); |
|||
bundle.warn({ |
|||
code: 'MISSING_GLOBAL_NAME', |
|||
message: `No name was provided for external module '${module.id}' in options.globals – guessing '${module.name}'` |
|||
}); |
|||
|
|||
return module.name; |
|||
}; |
|||
} |
|||
|
@ -0,0 +1,38 @@ |
|||
function spaces ( i ) { |
|||
let result = ''; |
|||
while ( i-- ) result += ' '; |
|||
return result; |
|||
} |
|||
|
|||
|
|||
function tabsToSpaces ( str ) { |
|||
return str.replace( /^\t+/, match => match.split( '\t' ).join( ' ' ) ); |
|||
} |
|||
|
|||
export default function getCodeFrame ( source, line, column ) { |
|||
let lines = source.split( '\n' ); |
|||
|
|||
const frameStart = Math.max( 0, line - 3 ); |
|||
const frameEnd = Math.min( line + 2, lines.length ); |
|||
|
|||
const digits = String( frameEnd + 1 ).length; |
|||
|
|||
lines = lines.slice( frameStart, frameEnd ); |
|||
while ( !/\S/.test( lines[ lines.length - 1 ] ) ) lines.pop(); |
|||
|
|||
return lines |
|||
.map( ( str, i ) => { |
|||
const isErrorLine = frameStart + i + 1 === line; |
|||
|
|||
let lineNum = String( i + frameStart + 1 ); |
|||
while ( lineNum.length < digits ) lineNum = ` ${lineNum}`; |
|||
|
|||
if ( isErrorLine ) { |
|||
const indicator = spaces( digits + 2 + tabsToSpaces( str.slice( 0, column ) ).length ) + '^'; |
|||
return `${lineNum}: ${tabsToSpaces( str )}\n${indicator}`; |
|||
} |
|||
|
|||
return `${lineNum}: ${tabsToSpaces( str )}`; |
|||
}) |
|||
.join( '\n' ); |
|||
} |
@ -1,20 +0,0 @@ |
|||
export default function getLocation ( source, charIndex ) { |
|||
const lines = source.split( '\n' ); |
|||
const len = lines.length; |
|||
|
|||
let lineStart = 0; |
|||
let i; |
|||
|
|||
for ( i = 0; i < len; i += 1 ) { |
|||
const line = lines[i]; |
|||
const lineEnd = lineStart + line.length + 1; // +1 for newline
|
|||
|
|||
if ( lineEnd > charIndex ) { |
|||
return { line: i + 1, column: charIndex - lineStart }; |
|||
} |
|||
|
|||
lineStart = lineEnd; |
|||
} |
|||
|
|||
throw new Error( 'Could not determine location of character' ); |
|||
} |
@ -1,13 +0,0 @@ |
|||
const { resolve } = require('path'); |
|||
const assert = require( 'assert' ); |
|||
|
|||
const r = path => resolve( __dirname, path ); |
|||
|
|||
module.exports = { |
|||
description: 'throws on duplicate export * from', |
|||
warnings ( warnings ) { |
|||
assert.deepEqual( warnings, [ |
|||
`Conflicting namespaces: ${r('main.js')} re-exports 'foo' from both ${r('foo.js')} (will be ignored) and ${r('deep.js')}.` |
|||
]); |
|||
} |
|||
}; |
@ -1,12 +1,23 @@ |
|||
const assert = require( 'assert' ); |
|||
const path = require( 'path' ); |
|||
|
|||
module.exports = { |
|||
description: 'warns on export {}, but does not fail', |
|||
warnings: warnings => { |
|||
assert.deepEqual( warnings, [ |
|||
`Module ${path.resolve( __dirname, 'main.js' )} has an empty export declaration`, |
|||
'Generated an empty bundle' |
|||
]); |
|||
} |
|||
warnings: [ |
|||
{ |
|||
code: 'EMPTY_EXPORT', |
|||
message: 'Empty export declaration', |
|||
pos: 0, |
|||
loc: { |
|||
file: require( 'path' ).resolve( __dirname, 'main.js' ), |
|||
line: 1, |
|||
column: 0 |
|||
}, |
|||
frame: ` |
|||
1: export {}; |
|||
^ |
|||
` |
|||
}, |
|||
{ |
|||
code: 'EMPTY_BUNDLE', |
|||
message: 'Generated an empty bundle' |
|||
} |
|||
] |
|||
}; |
|||
|
@ -1,9 +1,21 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
options: { |
|||
onwarn: function ( msg ) { |
|||
assert.equal( msg, `main.js (3:21) 'foo' is not exported by 'empty.js'. See https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module` ); |
|||
warnings: [ |
|||
{ |
|||
code: 'MISSING_EXPORT', |
|||
message: `'foo' is not exported by 'empty.js'`, |
|||
pos: 61, |
|||
loc: { |
|||
file: require( 'path' ).resolve( __dirname, 'main.js' ), |
|||
line: 3, |
|||
column: 25 |
|||
}, |
|||
frame: ` |
|||
1: import * as mod from './empty.js'; |
|||
2: |
|||
3: assert.equal( typeof mod.foo, 'undefined' ); |
|||
^ |
|||
`,
|
|||
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module` |
|||
} |
|||
} |
|||
] |
|||
}; |
|||
|
@ -1,10 +1,10 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'warns if default and named exports are used in auto mode', |
|||
warnings: function ( warnings ) { |
|||
assert.deepEqual( warnings, [ |
|||
'Using named and default exports together. Consumers of your bundle will have to use bundle[\'default\'] to access the default export, which may not be what you want. Use `exports: \'named\'` to disable this warning. See https://github.com/rollup/rollup/wiki/JavaScript-API#exports for more information' |
|||
]); |
|||
} |
|||
warnings: [ |
|||
{ |
|||
code: 'MIXED_EXPORTS', |
|||
message: `Using named and default exports together. Consumers of your bundle will have to use bundle['default'] to access the default export, which may not be what you want. Use \`exports: 'named'\` to disable this warning`, |
|||
url: `https://github.com/rollup/rollup/wiki/JavaScript-API#exports` |
|||
} |
|||
] |
|||
}; |
|||
|
@ -1,10 +1,9 @@ |
|||
const assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'warns if empty bundle is generated (#444)', |
|||
warnings: warnings => { |
|||
assert.deepEqual( warnings, [ |
|||
'Generated an empty bundle' |
|||
]); |
|||
} |
|||
warnings: [ |
|||
{ |
|||
code: 'EMPTY_BUNDLE', |
|||
message: 'Generated an empty bundle' |
|||
} |
|||
] |
|||
}; |
|||
|
@ -1,16 +1,20 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
var warned = false; |
|||
|
|||
module.exports = { |
|||
description: 'warns about use of eval', |
|||
options: { |
|||
onwarn: function ( message ) { |
|||
warned = true; |
|||
assert.ok( /Use of `eval` \(in .+?main\.js\) is strongly discouraged, as it poses security risks and may cause issues with minification\. See https:\/\/github.com\/rollup\/rollup\/wiki\/Troubleshooting#avoiding-eval for more details/.test( message ) ); |
|||
warnings: [ |
|||
{ |
|||
code: 'EVAL', |
|||
message: `Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification`, |
|||
pos: 13, |
|||
loc: { |
|||
column: 13, |
|||
file: require( 'path' ).resolve( __dirname, 'main.js' ), |
|||
line: 1 |
|||
}, |
|||
frame: ` |
|||
1: var result = eval( '1 + 1' ); |
|||
^ |
|||
`,
|
|||
url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval' |
|||
} |
|||
}, |
|||
exports: function () { |
|||
assert.ok( warned, 'did not warn' ); |
|||
} |
|||
] |
|||
}; |
|||
|
@ -0,0 +1,9 @@ |
|||
module.exports = { |
|||
description: 'warns on duplicate export * from', |
|||
warnings: [ |
|||
{ |
|||
code: 'NAMESPACE_CONFLICT', |
|||
message: `Conflicting namespaces: main.js re-exports 'foo' from both foo.js (will be ignored) and deep.js` |
|||
} |
|||
] |
|||
}; |
Loading…
Reference in new issue