@@ -5,6 +5,8 @@ const objectHash = require('./utils/objectHash');
55const md5 = require ( './utils/md5' ) ;
66const isURL = require ( './utils/is-url' ) ;
77const config = require ( './utils/config' ) ;
8+ const syncPromise = require ( './utils/syncPromise' ) ;
9+ const logger = require ( './Logger' ) ;
810
911let ASSET_ID = 1 ;
1012
@@ -15,12 +17,11 @@ let ASSET_ID = 1;
1517 * for subclasses to implement.
1618 */
1719class Asset {
18- constructor ( name , pkg , options ) {
20+ constructor ( name , options ) {
1921 this . id = ASSET_ID ++ ;
2022 this . name = name ;
2123 this . basename = path . basename ( this . name ) ;
2224 this . relativeName = path . relative ( options . rootDir , this . name ) ;
23- this . package = pkg || { } ;
2425 this . options = options ;
2526 this . encoding = 'utf8' ;
2627 this . type = path . extname ( this . name ) . slice ( 1 ) ;
@@ -98,13 +99,35 @@ class Asset {
9899 ) ;
99100
100101 parsed . pathname = this . options . parser
101- . getAsset ( resolved , this . package , this . options )
102+ . getAsset ( resolved , this . options )
102103 . generateBundleName ( ) ;
103104
104105 return URL . format ( parsed ) ;
105106 }
106107
108+ get package ( ) {
109+ logger . warn (
110+ '`asset.package` is deprecated. Please use `await asset.getPackage()` instead.'
111+ ) ;
112+ return syncPromise ( this . getPackage ( ) ) ;
113+ }
114+
115+ async getPackage ( ) {
116+ if ( ! this . _package ) {
117+ this . _package = await this . getConfig ( [ 'package.json' ] ) ;
118+ }
119+
120+ return this . _package ;
121+ }
122+
107123 async getConfig ( filenames , opts = { } ) {
124+ if ( opts . packageKey ) {
125+ let pkg = await this . getPackage ( ) ;
126+ if ( pkg && pkg [ opts . packageKey ] ) {
127+ return pkg [ opts . packageKey ] ;
128+ }
129+ }
130+
108131 // Resolve the config file
109132 let conf = await config . resolve ( opts . path || this . name , filenames ) ;
110133 if ( conf ) {
0 commit comments