@@ -282,98 +282,100 @@ describe('PreloadPlugin prefetches normal chunks', function() {
282282} ) ;
283283
284284describe ( 'PreloadPlugin filters chunks' , function ( ) {
285- // it('based on chunkname', function(done) {
286- // const compiler = webpack({
287- // entry: path.join(__dirname, 'fixtures', 'file.js'),
288- // output: {
289- // path: OUTPUT_DIR,
290- // filename: 'bundle.js',
291- // chunkFilename: '[name].[chunkhash].js',
292- // publicPath: '/',
293- // },
294- // plugins: [
295- // new HtmlWebpackPlugin(),
296- // new PreloadPlugin({
297- // rel: 'preload',
298- // as: 'script',
299- // include: ['home']
300- // })
301- // ]
302- // }, function(err, result) {
303- // expect(err).toBeFalsy();
304- // expect(JSON.stringify(result.compilation.errors)).toBe('[]');
305- // const html = result.compilation.assets['index.html'].source();
306- // expect(html).toContain('<link rel="preload" as="script" href="/home');
307- // expect(html).not.toContain('<link rel="preload" as="script" href="/bundle.js"');
308- // done();
309- // });
310- // compiler.outputFileSystem = new MemoryFileSystem();
311- // });
312- // it('based on chunkname with sourcemap', function(done) {
313- // const compiler = webpack({
314- // entry: path.join(__dirname, 'fixtures', 'file.js'),
315- // devtool: 'cheap-source-map',
316- // output: {
317- // path: OUTPUT_DIR,
318- // filename: 'bundle.js',
319- // chunkFilename: '[name].js',
320- // publicPath: '/',
321- // },
322- // plugins: [
323- // new HtmlWebpackPlugin(),
324- // new PreloadPlugin({
325- // rel: 'preload',
326- // as: 'script',
327- // include: ['home'],
328- // // disable default file blacklist, to include map file
329- // fileBlacklist: [],
330- // })
331- // ]
332- // }, function(err, result) {
333- // expect(err).toBeFalsy();
334- // expect(JSON.stringify(result.compilation.errors)).toBe('[]');
335- // const html = result.compilation.assets['index.html'].source();
336- // expect(html).toContain('<link rel="preload" as="script" href="/home.js');
337- // expect(html).toContain('<link rel="preload" as="script" href="/home.js.map');
338- // expect(html).not.toContain('<link rel="preload" as="script" href="/bundle.js"');
339- // done();
340- // });
341- // compiler.outputFileSystem = new MemoryFileSystem();
342- // });
343- // it('use fileWhitelist to include only specific files', (done) => {
344- // const compiler = webpack({
345- // entry: path.join(__dirname, 'fixtures', 'file.js'),
346- // devtool: 'cheap-source-map',
347- // output: {
348- // path: OUTPUT_DIR,
349- // filename: 'bundle.js',
350- // chunkFilename: '[name].js',
351- // publicPath: '/',
352- // },
353- // plugins: [
354- // new HtmlWebpackPlugin(),
355- // new PreloadPlugin({
356- // rel: 'preload',
357- // as: 'script',
358- // fileWhitelist: [/home/],
359- // })
360- // ]
361- // }, function(err, result) {
362- // expect(err).toBeFalsy();
363- // expect(JSON.stringify(result.compilation.errors)).toBe('[]');
364- // const html = result.compilation.assets['index.html'].source();
365- // expect(html).toContain('<link rel="preload" as="script" href="/home.js');
366- // // exclude by default fileBlacklist
367- // expect(html).not.toContain('<link rel="preload" as="script" href="/home.js.map');
368- // // not included in fileWhitelist
369- // expect(html).not.toContain('<link rel="preload" as="script" href="/bundle.js"');
370- // done();
371- // });
372- // compiler.outputFileSystem = new MemoryFileSystem();
373- // });
374- // });
285+ it ( 'based on chunkname' , function ( done ) {
286+ const compiler = webpack ( {
287+ entry : path . join ( __dirname , 'fixtures' , 'file.js' ) ,
288+ output : {
289+ path : OUTPUT_DIR ,
290+ filename : 'bundle.js' ,
291+ chunkFilename : '[name].[chunkhash].js' ,
292+ publicPath : '/' ,
293+ } ,
294+ plugins : [
295+ new HtmlWebpackPlugin ( ) ,
296+ new PreloadPlugin ( {
297+ rel : 'preload' ,
298+ as : 'script' ,
299+ include : [ 'home' ]
300+ } )
301+ ]
302+ } , function ( err , result ) {
303+ expect ( err ) . toBeFalsy ( ) ;
304+ expect ( JSON . stringify ( result . compilation . errors ) ) . toBe ( '[]' ) ;
305+ const html = result . compilation . assets [ 'index.html' ] . source ( ) ;
306+ expect ( html ) . toContain ( '<link rel="preload" as="script" href="/home' ) ;
307+ expect ( html ) . not . toContain ( '<link rel="preload" as="script" href="/bundle.js"' ) ;
308+ done ( ) ;
309+ } ) ;
310+ compiler . outputFileSystem = new MemoryFileSystem ( ) ;
311+ } ) ;
312+
313+ it ( 'based on chunkname with sourcemap' , function ( done ) {
314+ const compiler = webpack ( {
315+ entry : path . join ( __dirname , 'fixtures' , 'file.js' ) ,
316+ devtool : 'cheap-source-map' ,
317+ output : {
318+ path : OUTPUT_DIR ,
319+ filename : 'bundle.js' ,
320+ chunkFilename : '[name].js' ,
321+ publicPath : '/' ,
322+ } ,
323+ plugins : [
324+ new HtmlWebpackPlugin ( ) ,
325+ new PreloadPlugin ( {
326+ rel : 'preload' ,
327+ as : 'script' ,
328+ include : [ 'home' ] ,
329+ // disable default file blacklist, to include map file
330+ fileBlacklist : [ ] ,
331+ } )
332+ ]
333+ } , function ( err , result ) {
334+ expect ( err ) . toBeFalsy ( ) ;
335+ expect ( JSON . stringify ( result . compilation . errors ) ) . toBe ( '[]' ) ;
336+ const html = result . compilation . assets [ 'index.html' ] . source ( ) ;
337+ expect ( html ) . toContain ( '<link rel="preload" as="script" href="/home.js' ) ;
338+ expect ( html ) . toContain ( '<link rel="preload" as="script" href="/home.js.map' ) ;
339+ expect ( html ) . not . toContain ( '<link rel="preload" as="script" href="/bundle.js"' ) ;
340+ done ( ) ;
341+ } ) ;
342+ compiler . outputFileSystem = new MemoryFileSystem ( ) ;
343+ } ) ;
344+
345+ it ( 'use fileWhitelist to include only specific files' , ( done ) => {
346+ const compiler = webpack ( {
347+ entry : path . join ( __dirname , 'fixtures' , 'file.js' ) ,
348+ devtool : 'cheap-source-map' ,
349+ output : {
350+ path : OUTPUT_DIR ,
351+ filename : 'bundle.js' ,
352+ chunkFilename : '[name].js' ,
353+ publicPath : '/' ,
354+ } ,
355+ plugins : [
356+ new HtmlWebpackPlugin ( ) ,
357+ new PreloadPlugin ( {
358+ rel : 'preload' ,
359+ as : 'script' ,
360+ fileWhitelist : [ / h o m e / ] ,
361+ } )
362+ ]
363+ } , function ( err , result ) {
364+ expect ( err ) . toBeFalsy ( ) ;
365+ expect ( JSON . stringify ( result . compilation . errors ) ) . toBe ( '[]' ) ;
366+ const html = result . compilation . assets [ 'index.html' ] . source ( ) ;
367+ expect ( html ) . toContain ( '<link rel="preload" as="script" href="/home.js' ) ;
368+ // exclude by default fileBlacklist
369+ expect ( html ) . not . toContain ( '<link rel="preload" as="script" href="/home.js.map' ) ;
370+ // not included in fileWhitelist
371+ expect ( html ) . not . toContain ( '<link rel="preload" as="script" href="/bundle.js"' ) ;
372+ done ( ) ;
373+ } ) ;
374+ compiler . outputFileSystem = new MemoryFileSystem ( ) ;
375+ } ) ;
376+ } ) ;
375377
376- // describe('PreloadPlugin preloads all assets', function() {
378+ describe ( 'PreloadPlugin preloads all assets' , function ( ) {
377379// it('adds preload tags', function(done) {
378380// const compiler = webpack({
379381// entry: path.join(__dirname, 'fixtures', 'load-css.js'),
0 commit comments