From 4d900686dfd644853cb42b685183bb3cd069f38e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 21 Dec 2023 20:16:35 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=F0=9F=94=A5=F0=9F=94=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/__snapshots__/build.js.snap | 54 ++++++++- .../test/build.js | 103 ++---------------- 2 files changed, 57 insertions(+), 100 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap index e68592520d3e6..4d0b69ca2a278 100644 --- a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap +++ b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap @@ -257,7 +257,7 @@ exports[`DependencyExtractionWebpackPlugin scripts Webpack \`combine-assets\` sh `; exports[`DependencyExtractionWebpackPlugin scripts Webpack \`dynamic-import\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` -" array('wp-blob'), 'version' => '385771ebe58754ea37f8'); +" array('wp-blob'), 'version' => 'c0e8a6f22065ea096606'); " `; @@ -274,6 +274,52 @@ exports[`DependencyExtractionWebpackPlugin scripts Webpack \`dynamic-import\` sh ] `; +exports[`DependencyExtractionWebpackPlugin scripts Webpack \`function-output-filename\` should produce expected output: Asset file 'chunk--main--main.asset.php' should match snapshot 1`] = ` +" array('lodash', 'wp-blob'), 'version' => 'fc2d750fc9e08c5749db'); +" +`; + +exports[`DependencyExtractionWebpackPlugin scripts Webpack \`function-output-filename\` should produce expected output: External modules should match snapshot 1`] = ` +[ + { + "externalType": "window", + "request": "lodash", + "userRequest": "lodash", + }, + { + "externalType": "window", + "request": [ + "wp", + "blob", + ], + "userRequest": "@wordpress/blob", + }, +] +`; + +exports[`DependencyExtractionWebpackPlugin scripts Webpack \`has-extension-suffix\` should produce expected output: Asset file 'index.min.asset.php' should match snapshot 1`] = ` +" array('lodash', 'wp-blob'), 'version' => 'c3f17b34fdd974d57d0f'); +" +`; + +exports[`DependencyExtractionWebpackPlugin scripts Webpack \`has-extension-suffix\` should produce expected output: External modules should match snapshot 1`] = ` +[ + { + "externalType": "window", + "request": "lodash", + "userRequest": "lodash", + }, + { + "externalType": "window", + "request": [ + "wp", + "blob", + ], + "userRequest": "@wordpress/blob", + }, +] +`; + exports[`DependencyExtractionWebpackPlugin scripts Webpack \`no-default\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` " array(), 'version' => '43880e6c42e7c39fcdf1'); " @@ -386,17 +432,17 @@ exports[`DependencyExtractionWebpackPlugin scripts Webpack \`overrides\` should `; exports[`DependencyExtractionWebpackPlugin scripts Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'a.asset.php' should match snapshot 1`] = ` -" array('wp-blob'), 'version' => '3be3f4673a8366a0c065'); +" array('wp-blob'), 'version' => 'd3cda564b538b44d38ef'); " `; exports[`DependencyExtractionWebpackPlugin scripts Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'b.asset.php' should match snapshot 1`] = ` -" array('lodash', 'wp-blob'), 'version' => '27acdd396d9f87e81bbc'); +" array('lodash', 'wp-blob'), 'version' => '420d636da562e71648f7'); " `; exports[`DependencyExtractionWebpackPlugin scripts Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'runtime.asset.php' should match snapshot 1`] = ` -" array(), 'version' => '0e89c281d72bbb150fd7'); +" array(), 'version' => 'dc673033633cae5c6702'); " `; diff --git a/packages/dependency-extraction-webpack-plugin/test/build.js b/packages/dependency-extraction-webpack-plugin/test/build.js index f85a065d2e1d1..8a9c607eb5378 100644 --- a/packages/dependency-extraction-webpack-plugin/test/build.js +++ b/packages/dependency-extraction-webpack-plugin/test/build.js @@ -11,14 +11,12 @@ const webpack = require( 'webpack' ); const fixturesPath = path.join( __dirname, 'fixtures' ); const configFixtures = fs.readdirSync( fixturesPath ).sort(); -// afterAll( () => rimraf( path.join( __dirname, 'build' ) ) ); +afterAll( () => rimraf( path.join( __dirname, 'build' ) ) ); describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )( 'DependencyExtractionWebpackPlugin %s', ( moduleMode ) => { - describe.each( - configFixtures /** .filter( ( x ) => x.includes( 'interactivity' ) ) /**/ - )( 'Webpack `%s`', ( configCase ) => { + describe.each( configFixtures )( 'Webpack `%s`', ( configCase ) => { const testDirectory = path.join( fixturesPath, configCase ); const outputDirectory = path.join( __dirname, @@ -28,17 +26,17 @@ describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )( ); beforeEach( () => { - // rimraf( outputDirectory ); + rimraf( outputDirectory ); mkdirp( outputDirectory ); } ); // This afterEach is necessary to prevent watched tests from retriggering on every run. - // afterEach( () => rimraf( outputDirectory ) ); + afterEach( () => rimraf( outputDirectory ) ); test( 'should produce expected output', async () => { const options = Object.assign( { - target: 'es2024', + target: 'web', context: testDirectory, entry: './index.js', mode: 'production', @@ -47,9 +45,7 @@ describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )( chunkIds: 'named', moduleIds: 'named', }, - output: { - chunkFormat: 'commonjs', - }, + output: {}, experiments: {}, }, require( path.join( testDirectory, 'webpack.config.js' ) ) @@ -57,6 +53,7 @@ describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )( options.output.path = outputDirectory; if ( moduleMode === 'modules' ) { + options.target = 'es2024'; options.output.module = true; options.output.chunkFormat = 'module'; options.output.library = options.output.library || {}; @@ -121,89 +118,3 @@ describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )( } ); } ); - -test.skip( 'module build works as expected', async () => { - const testDirectory = path.join( fixturesPath, 'wordpress-interactivity' ); - const outputDirectory = path.join( - __dirname, - 'build', - 'wordpress-interactivity' - ); - - mkdirp( outputDirectory ); - - const options = Object.assign( - { - context: testDirectory, - entry: './index.js', - mode: 'production', - optimization: { - minimize: false, - chunkIds: 'named', - moduleIds: 'named', - }, - output: {}, - experiments: {}, - }, - require( path.join( testDirectory, 'webpack.config.js' ) ) - ); - options.output.path = outputDirectory; - options.output.module = true; - options.experiments.outputModule = true; - - const stats = await new Promise( ( resolve, reject ) => - webpack( options, ( err, _stats ) => { - if ( err ) { - return reject( err ); - } - resolve( _stats ); - } ) - ); - - console.log( 'complete' ); - console.log( { stats } ); - - console.log( 1 ); - const assetFiles = glob( - `${ outputDirectory }/+(*.asset|assets).@(json|php)` - ); - - console.log( 2 ); - expect( assetFiles.length ).toBeGreaterThan( 0 ); - - console.log( 3 ); - // Asset files should match. - assetFiles.forEach( ( assetFile ) => { - const assetBasename = path.basename( assetFile ); - - expect( fs.readFileSync( assetFile, 'utf-8' ) ).toMatchSnapshot( - `Asset file '${ assetBasename }' should match snapshot` - ); - } ); - console.log( 4 ); - - const compareByModuleIdentifier = ( m1, m2 ) => { - const i1 = m1.identifier(); - const i2 = m2.identifier(); - if ( i1 < i2 ) return -1; - if ( i1 > i2 ) return 1; - return 0; - }; - console.log( 5 ); - - // Webpack stats external modules should match. - const externalModules = Array.from( stats.compilation.modules ) - .filter( ( { externalType } ) => externalType ) - .sort( compareByModuleIdentifier ) - .map( ( module ) => ( { - externalType: module.externalType, - request: module.request, - userRequest: module.userRequest, - } ) ); - console.log( 6 ); - - console.log( { externalModules } ); - expect( externalModules ).toMatchSnapshot( - 'External modules should match snapshot' - ); -} );