Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Feb 11, 2021
1 parent de93c82 commit d83e6db
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 25 deletions.
10 changes: 10 additions & 0 deletions fixtures/js/import_node_modules_image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// this helps us trigger a manifest.json bug
// https://github.com/shellscape/webpack-manifest-plugin/pull/249
import 'mocha/assets/growl/ok.png';
import '../images/symfony_logo.png';


// module.userRequest
// growl: /Users/weaverryan/Sites/os/webpack-encore/node_modules/mocha/assets/growl/ok.png
// logo: /Users/weaverryan/Sites/os/webpack-encore/test_tmp/51witp/images/symfony_logo.png

2 changes: 1 addition & 1 deletion lib/plugins/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'use strict';

const WebpackConfig = require('../WebpackConfig'); //eslint-disable-line no-unused-vars
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const { WebpackManifestPlugin } = require('../webpack-manifest-plugin/index');
const PluginPriorities = require('./plugin-priorities');
const applyOptionsCallback = require('../utils/apply-options-callback');
const copyEntryTmpName = require('../utils/copyEntryTmpName');
Expand Down
68 changes: 45 additions & 23 deletions lib/webpack-manifest-plugin/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { dirname, join, basename } = require('path');

const generateManifest = (compilation, files, { generate, seed = {} }) => {
let result;
if (generate) {
Expand Down Expand Up @@ -25,7 +27,13 @@ const getFileType = (fileName, { transformExtensions }) => {
};

const reduceAssets = (files, asset, moduleAssets) => {
const name = moduleAssets[asset.name] ? moduleAssets[asset.name] : asset.info.sourceFilename;
let name;
if (moduleAssets[asset.name]) {
name = moduleAssets[asset.name];
} else if (asset.info.sourceFilename) {
name = join(dirname(asset.name), basename(asset.info.sourceFilename));
}

if (name) {
return files.concat({
path: asset.name,
Expand All @@ -52,29 +60,43 @@ const reduceAssets = (files, asset, moduleAssets) => {
});
};

const reduceChunk = (files, chunk, options) =>
Array.of(...Array.from(chunk.files), ...Array.from(chunk.auxiliaryFiles || [])).reduce(
(prev, path) => {
let name = chunk.name ? chunk.name : null;
// chunk name, or for nameless chunks, just map the files directly.
name = name
? options.useEntryKeys && !path.endsWith('.map')
? name
: `${name}.${getFileType(path, options)}`
: path;
const reduceChunk = (files, chunk, options, auxiliaryFiles) => {
// auxiliary files contain things like images, fonts AND, most
// importantly, other files like .map sourcemap files
// we modify the auxiliaryFiles so that we can add any of these
// to the manifest that was not added by another method
// (sourcemaps files are not added via any other method)
Array.from(chunk.auxiliaryFiles || []).forEach((auxiliaryFile) => {
auxiliaryFiles[auxiliaryFile] = {
path: auxiliaryFile,
name: basename(auxiliaryFile),
isInitial: false,
isChunk: false,
isAsset: true,
isModuleAsset: true
};
});

return Array.from(chunk.files).reduce((prev, path) => {
let name = chunk.name ? chunk.name : null;
// chunk name, or for nameless chunks, just map the files directly.
name = name
? options.useEntryKeys && !path.endsWith('.map')
? name
: `${name}.${getFileType(path, options)}`
: path;

return prev.concat({
path,
chunk,
name,
isInitial: chunk.isOnlyInitial(),
isChunk: true,
isAsset: false,
isModuleAsset: false
});
},
files
);
return prev.concat({
path,
chunk,
name,
isInitial: chunk.isOnlyInitial(),
isChunk: true,
isAsset: false,
isModuleAsset: false
});
}, files);
};

const standardizeFilePaths = (file) => {
const result = Object.assign({}, file);
Expand Down
14 changes: 13 additions & 1 deletion lib/webpack-manifest-plugin/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ const emitHook = function emit(

emitCountMap.set(manifestFileName, emitCount);

const auxiliaryFiles = {};
let files = Array.from(compilation.chunks).reduce(
(prev, chunk) => reduceChunk(prev, chunk, options),
(prev, chunk) => reduceChunk(prev, chunk, options, auxiliaryFiles),
[]
);

Expand All @@ -66,6 +67,17 @@ const emitHook = function emit(
typeof emitCountMap.get(join(compiler.options.output.path, name)) === 'undefined'
);

// auxiliary files are "extra" files that are probably already included
// in other ways. Loop over files and remove any from auxiliaryFiles
files.forEach((file) => {
delete auxiliaryFiles[file.path];
});
// if there are any auxiliaryFiles left, add them to the files
// this handles, specifically, sourcemaps
Object.keys(auxiliaryFiles).forEach((auxiliaryFile) => {
files = files.concat(auxiliaryFiles[auxiliaryFile]);
});

files = files.map((file) => {
const changes = {
// Append optional basepath onto all references. This allows output path to be reflected in the manifest.
Expand Down
20 changes: 20 additions & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ describe('Functional tests using webpack', function() {
});
});

it('Check manifest.json with node_module includes', (done) => {
const config = createWebpackConfig('web/build', 'dev');
config.addEntry('main', './js/import_node_modules_image');
config.setPublicPath('/build');

testSetup.runWebpack(config, (webpackAssert) => {
// should have a main.js file
// should have a manifest.json with public/main.js

webpackAssert.assertOutputJsonFileMatches('manifest.json', {
'build/main.js': '/build/main.js',
'build/runtime.js': '/build/runtime.js',
'build/images/symfony_logo.png': '/build/images/symfony_logo.91beba37.png',
'build/images/ok.png': '/build/images/ok.c3f4e113.png',
});

done();
});
});

it('Use "all" splitChunks & look at entrypoints.json', (done) => {
const config = createWebpackConfig('web/build', 'dev');
config.addEntry('main', ['./css/roboto_font.css', './js/no_require', 'vue']);
Expand Down

0 comments on commit d83e6db

Please sign in to comment.