Skip to content

Commit

Permalink
🔥🔥🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Dec 21, 2023
1 parent 26780a1 commit 4d90068
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
"<?php return array('dependencies' => array('wp-blob'), 'version' => '385771ebe58754ea37f8');
"<?php return array('dependencies' => array('wp-blob'), 'version' => 'c0e8a6f22065ea096606');
"
`;

Expand All @@ -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`] = `
"<?php return array('dependencies' => 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`] = `
"<?php return array('dependencies' => 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`] = `
"<?php return array('dependencies' => array(), 'version' => '43880e6c42e7c39fcdf1');
"
Expand Down Expand Up @@ -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`] = `
"<?php return array('dependencies' => array('wp-blob'), 'version' => '3be3f4673a8366a0c065');
"<?php return array('dependencies' => 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`] = `
"<?php return array('dependencies' => array('lodash', 'wp-blob'), 'version' => '27acdd396d9f87e81bbc');
"<?php return array('dependencies' => 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`] = `
"<?php return array('dependencies' => array(), 'version' => '0e89c281d72bbb150fd7');
"<?php return array('dependencies' => array(), 'version' => 'dc673033633cae5c6702');
"
`;

Expand Down
103 changes: 7 additions & 96 deletions packages/dependency-extraction-webpack-plugin/test/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -47,16 +45,15 @@ describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )(
chunkIds: 'named',
moduleIds: 'named',
},
output: {
chunkFormat: 'commonjs',
},
output: {},
experiments: {},
},
require( path.join( testDirectory, 'webpack.config.js' ) )
);
options.output.path = outputDirectory;

if ( moduleMode === 'modules' ) {
options.target = 'es2024';
options.output.module = true;
options.output.chunkFormat = 'module';
options.output.library = options.output.library || {};
Expand Down Expand Up @@ -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'
);
} );

0 comments on commit 4d90068

Please sign in to comment.