|
|
@ -9,14 +9,13 @@ import ensureArray from './utils/ensureArray.js'; |
|
|
|
import { load, makeOnwarn, resolveId } from './utils/defaults.js'; |
|
|
|
import getExportMode from './utils/getExportMode.js'; |
|
|
|
import getIndentString from './utils/getIndentString.js'; |
|
|
|
import { unixizePath } from './utils/normalizePlatform.js'; |
|
|
|
import { mapSequence } from './utils/promise.js'; |
|
|
|
import transform from './utils/transform.js'; |
|
|
|
import transformBundle from './utils/transformBundle.js'; |
|
|
|
import collapseSourcemaps from './utils/collapseSourcemaps.js'; |
|
|
|
import SOURCEMAPPING_URL from './utils/sourceMappingURL.js'; |
|
|
|
import callIfFunction from './utils/callIfFunction.js'; |
|
|
|
import { dirname, isRelative, isAbsolute, relative, resolve } from './utils/path.js'; |
|
|
|
import { dirname, isRelative, isAbsolute, normalize, relative, resolve } from './utils/path.js'; |
|
|
|
|
|
|
|
export default class Bundle { |
|
|
|
constructor ( options ) { |
|
|
@ -35,7 +34,7 @@ export default class Bundle { |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
this.entry = unixizePath( options.entry ); |
|
|
|
this.entry = normalize( options.entry ); |
|
|
|
this.entryId = null; |
|
|
|
this.entryModule = null; |
|
|
|
|
|
|
@ -64,7 +63,7 @@ export default class Bundle { |
|
|
|
if ( typeof options.external === 'function' ) { |
|
|
|
this.isExternal = options.external; |
|
|
|
} else { |
|
|
|
const ids = ensureArray( options.external ).map( id => id.replace( /[\/\\]/g, '/' ) ); |
|
|
|
const ids = ensureArray( options.external ); |
|
|
|
this.isExternal = id => ids.indexOf( id ) !== -1; |
|
|
|
} |
|
|
|
|
|
|
@ -227,40 +226,28 @@ export default class Bundle { |
|
|
|
return mapSequence( module.sources, source => { |
|
|
|
return this.resolveId( source, module.id ) |
|
|
|
.then( resolvedId => { |
|
|
|
let externalName; |
|
|
|
if ( resolvedId ) { |
|
|
|
// If the `resolvedId` is supposed to be external, make it so.
|
|
|
|
externalName = resolvedId.replace( /[\/\\]/g, '/' ); |
|
|
|
} else if ( isRelative( source ) ) { |
|
|
|
// This could be an external, relative dependency, based on the current module's parent dir.
|
|
|
|
externalName = resolve( module.id, '..', source ); |
|
|
|
} |
|
|
|
const forcedExternal = externalName && this.isExternal( externalName ); |
|
|
|
const externalId = resolvedId || ( |
|
|
|
isRelative( source ) ? resolve( module.id, '..', source ) : source |
|
|
|
); |
|
|
|
|
|
|
|
if ( !resolvedId || forcedExternal ) { |
|
|
|
let normalizedExternal = source; |
|
|
|
let isExternal = this.isExternal( externalId ); |
|
|
|
|
|
|
|
if ( !forcedExternal ) { |
|
|
|
if ( !resolvedId && !isExternal ) { |
|
|
|
if ( isRelative( source ) ) throw new Error( `Could not resolve ${source} from ${module.id}` ); |
|
|
|
if ( !this.isExternal( source ) ) this.onwarn( `Treating '${source}' as external dependency` ); |
|
|
|
} else if ( resolvedId ) { |
|
|
|
if ( isRelative(resolvedId) || isAbsolute(resolvedId) ) { |
|
|
|
// Try to deduce relative path from entry dir if resolvedId is defined as a relative path.
|
|
|
|
normalizedExternal = this.getPathRelativeToEntryDirname( resolvedId ); |
|
|
|
} else { |
|
|
|
normalizedExternal = resolvedId; |
|
|
|
} |
|
|
|
|
|
|
|
this.onwarn( `Treating '${source}' as external dependency` ); |
|
|
|
isExternal = true; |
|
|
|
} |
|
|
|
module.resolvedIds[ source ] = normalizedExternal; |
|
|
|
|
|
|
|
if ( !this.moduleById.has( normalizedExternal ) ) { |
|
|
|
const module = new ExternalModule( normalizedExternal ); |
|
|
|
if ( isExternal ) { |
|
|
|
module.resolvedIds[ source ] = externalId; |
|
|
|
|
|
|
|
if ( !this.moduleById.has( externalId ) ) { |
|
|
|
const module = new ExternalModule( externalId, this.getPathRelativeToEntryDirname( externalId ) ); |
|
|
|
this.externalModules.push( module ); |
|
|
|
this.moduleById.set( normalizedExternal, module ); |
|
|
|
} |
|
|
|
this.moduleById.set( externalId, module ); |
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
} else { |
|
|
|
if ( resolvedId === module.id ) { |
|
|
|
throw new Error( `A module cannot import itself (${resolvedId})` ); |
|
|
|
} |
|
|
@ -273,16 +260,14 @@ export default class Bundle { |
|
|
|
} |
|
|
|
|
|
|
|
getPathRelativeToEntryDirname ( resolvedId ) { |
|
|
|
// Get a path relative to the resolved entry directory
|
|
|
|
if ( isRelative( resolvedId ) || isAbsolute( resolvedId ) ) { |
|
|
|
const entryDirname = dirname( this.entryId ); |
|
|
|
const relativeToEntry = relative( entryDirname, resolvedId ); |
|
|
|
const relativeToEntry = normalize( relative( entryDirname, resolvedId ) ); |
|
|
|
|
|
|
|
if ( isRelative( relativeToEntry )) { |
|
|
|
return relativeToEntry; |
|
|
|
return isRelative( relativeToEntry ) ? relativeToEntry : `./${relativeToEntry}`; |
|
|
|
} |
|
|
|
|
|
|
|
// The path is missing the `./` prefix
|
|
|
|
return `./${relativeToEntry}`; |
|
|
|
return resolvedId; |
|
|
|
} |
|
|
|
|
|
|
|
render ( options = {} ) { |
|
|
@ -357,7 +342,7 @@ export default class Bundle { |
|
|
|
map = magicString.generateMap({ file, includeContent: true }); |
|
|
|
} |
|
|
|
|
|
|
|
map.sources = map.sources.map( unixizePath ); |
|
|
|
map.sources = map.sources.map( normalize ); |
|
|
|
} |
|
|
|
|
|
|
|
return { code, map }; |
|
|
|