Skip to content

Commit

Permalink
feat: added the sourceFilename info (original source filename) to a…
Browse files Browse the repository at this point in the history
…ssets info (#542)
  • Loading branch information
evilebottnawi committed Oct 22, 2020
1 parent c892451 commit db2e3bf
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module.exports = {
> ℹ️ If you want `webpack-dev-server` to write files to the output directory during development, you can force it with the [`writeToDisk`](https://github.com/webpack/webpack-dev-middleware#writetodisk) option or the [`write-file-webpack-plugin`](https://github.com/gajus/write-file-webpack-plugin).
> ℹ️ You can get the original source filename from [Asset Objects](https://webpack.js.org/api/stats/#asset-objects).
## Options

The plugin's signature:
Expand Down
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"dependencies": {
"cacache": "^15.0.5",
"fast-glob": "^3.2.4",
"file-loader": "^6.1.1",
"find-cache-dir": "^3.3.1",
"glob-parent": "^5.1.1",
"globby": "^11.0.1",
Expand Down
16 changes: 11 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ class CopyPlugin {
pattern.to = path.normalize(
typeof pattern.to !== 'undefined' ? pattern.to : ''
);
pattern.compilerContext = compiler.context;
pattern.context = path.normalize(
typeof pattern.context !== 'undefined'
? !path.isAbsolute(pattern.context)
? path.join(compiler.options.context, pattern.context)
? path.join(pattern.compilerContext, pattern.context)
: pattern.context
: compiler.options.context
: pattern.compilerContext
);

logger.debug(`processing from "${pattern.from}" to "${pattern.to}"`);
Expand Down Expand Up @@ -303,7 +304,11 @@ class CopyPlugin {

logger.log(`determined that "${from}" should write to "${webpackTo}"`);

return { absoluteFrom, relativeFrom, webpackTo };
const sourceFilename = normalizePath(
path.relative(pattern.compilerContext, absoluteFrom)
);

return { absoluteFrom, sourceFilename, relativeFrom, webpackTo };
});

return Promise.all(
Expand Down Expand Up @@ -527,6 +532,7 @@ class CopyPlugin {
.filter(Boolean)
.forEach((asset) => {
const {
sourceFilename,
absoluteFrom,
targetPath,
webpackTo,
Expand All @@ -551,7 +557,7 @@ class CopyPlugin {
`force updating "${webpackTo}" to compilation assets from "${absoluteFrom}"`
);

const info = { copied: true };
const info = { copied: true, sourceFilename };

if (asset.immutable) {
info.immutable = true;
Expand All @@ -573,7 +579,7 @@ class CopyPlugin {
`writing "${webpackTo}" to compilation assets from "${absoluteFrom}"`
);

const info = { copied: true };
const info = { copied: true, sourceFilename };

if (asset.immutable) {
info.immutable = true;
Expand Down
116 changes: 116 additions & 0 deletions test/CopyPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';

import webpack from 'webpack';
import del from 'del';
import { createFsFromVolume, Volume } from 'memfs';

import CopyPlugin from '../src';

Expand Down Expand Up @@ -380,6 +381,70 @@ describe('CopyPlugin', () => {
.then(done)
.catch(done);
});

it('should work with multi compiler mode', async () => {
const compiler = webpack([
{
mode: 'development',
context: path.resolve(__dirname, './fixtures'),
entry: path.resolve(__dirname, './helpers/enter.js'),
output: {
path: path.resolve(__dirname, './outputs/multi-compiler/dist/a'),
},
stats: {
source: true,
},
plugins: [
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, './fixtures/directory'),
},
],
}),
],
},
{
mode: 'development',
entry: path.resolve(__dirname, './helpers/enter.js'),
output: {
path: path.resolve(__dirname, './outputs/multi-compiler/dist/b'),
},
stats: {
source: true,
},
plugins: [
new CopyPlugin({
patterns: [
{
context: path.resolve(__dirname, './fixtures'),
from: path.resolve(__dirname, './fixtures/directory'),
},
],
}),
],
},
]);

compiler.compilers.forEach((item) => {
const outputFileSystem = createFsFromVolume(new Volume());
// Todo remove when we drop webpack@4 support
outputFileSystem.join = path.join.bind(path);

// eslint-disable-next-line no-param-reassign
item.outputFileSystem = outputFileSystem;
});

const { stats } = await compile(compiler);

stats.stats.forEach((item, index) => {
expect(item.compilation.errors).toMatchSnapshot('errors');
expect(item.compilation.warnings).toMatchSnapshot('warnings');
expect(readAssets(compiler.compilers[index], item)).toMatchSnapshot(
'assets'
);
});
});
});

describe('watch mode', () => {
Expand Down Expand Up @@ -747,6 +812,57 @@ describe('CopyPlugin', () => {
});
});

describe('stats', () => {
it('should work have assets info', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, './helpers/enter-with-asset-modules.js'),
});

new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, './fixtures/directory'),
},
],
}).apply(compiler);

const { stats } = await compile(compiler);

expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
expect(readAssets(compiler, stats)).toMatchSnapshot('assets');

const assetsInfo = [];

for (const [name, info] of stats.compilation.assetsInfo.entries()) {
assetsInfo.push({
name,
info: {
// Workaround for `file-loader`
// eslint-disable-next-line no-undefined
immutable: info.immutable === false ? undefined : info.immutable,
copied: info.copied,
sourceFilename: info.sourceFilename,
},
});
}

expect(
assetsInfo.sort((a, b) => {
if (a.name < b.name) {
return -1;
}

if (a.name > b.name) {
return 1;
}

return 0;
})
).toMatchSnapshot('assets info');
});
});

describe('logging', () => {
it('should logging when "from" is a file', (done) => {
const expectedAssetKeys = ['file.txt'];
Expand Down
96 changes: 96 additions & 0 deletions test/__snapshots__/CopyPlugin.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CopyPlugin basic should work with multi compiler mode: assets 1`] = `
Object {
".dottedfile": "dottedfile contents
",
"directoryfile.txt": "new",
"nested/deep-nested/deepnested.txt": "",
"nested/nestedfile.txt": "",
}
`;

exports[`CopyPlugin basic should work with multi compiler mode: assets 2`] = `
Object {
".dottedfile": "dottedfile contents
",
"directoryfile.txt": "new",
"nested/deep-nested/deepnested.txt": "",
"nested/nestedfile.txt": "",
}
`;

exports[`CopyPlugin basic should work with multi compiler mode: errors 1`] = `Array []`;

exports[`CopyPlugin basic should work with multi compiler mode: errors 2`] = `Array []`;

exports[`CopyPlugin basic should work with multi compiler mode: warnings 1`] = `Array []`;

exports[`CopyPlugin basic should work with multi compiler mode: warnings 2`] = `Array []`;

exports[`CopyPlugin cache should work with the "filesystem" cache: assets 1`] = `
Object {
".dottedfile": "dottedfile contents
Expand Down Expand Up @@ -132,3 +160,71 @@ Object {
],
}
`;
exports[`CopyPlugin stats should work have assets info: assets 1`] = `
Object {
".dottedfile": "dottedfile contents
",
"asset-modules/deepnested.txt": "",
"directoryfile.txt": "new",
"nested/deep-nested/deepnested.txt": "",
"nested/nestedfile.txt": "",
}
`;
exports[`CopyPlugin stats should work have assets info: assets info 1`] = `
Array [
Object {
"info": Object {
"copied": true,
"immutable": undefined,
"sourceFilename": "directory/.dottedfile",
},
"name": ".dottedfile",
},
Object {
"info": Object {
"copied": undefined,
"immutable": undefined,
"sourceFilename": undefined,
},
"name": "asset-modules/deepnested.txt",
},
Object {
"info": Object {
"copied": true,
"immutable": undefined,
"sourceFilename": "directory/directoryfile.txt",
},
"name": "directoryfile.txt",
},
Object {
"info": Object {
"copied": undefined,
"immutable": undefined,
"sourceFilename": undefined,
},
"name": "main.js",
},
Object {
"info": Object {
"copied": true,
"immutable": undefined,
"sourceFilename": "directory/nested/deep-nested/deepnested.txt",
},
"name": "nested/deep-nested/deepnested.txt",
},
Object {
"info": Object {
"copied": true,
"immutable": undefined,
"sourceFilename": "directory/nested/nestedfile.txt",
},
"name": "nested/nestedfile.txt",
},
]
`;
exports[`CopyPlugin stats should work have assets info: errors 1`] = `Array []`;
exports[`CopyPlugin stats should work have assets info: warnings 1`] = `Array []`;
3 changes: 3 additions & 0 deletions test/helpers/enter-with-asset-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import txtURL from '../fixtures/directory/nested/deep-nested/deepnested.txt';

export default txtURL;
23 changes: 19 additions & 4 deletions test/helpers/getCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ export default (config = {}) => {
output: {
path: path.resolve(__dirname, '../build'),
},
module: {
rules: [
webpack.version[0] === '5'
? {
test: /\.txt/,
type: 'asset/resource',
generator: {
filename: 'asset-modules/[name][ext]',
},
}
: {
test: /\.txt/,
loader: 'file-loader',
options: {
name: 'asset-modules/[name].[ext]',
},
},
],
},
...config,
};

if (webpack.version[0] === 5) {
fullConfig.stats.source = true;
}

const compiler = webpack(fullConfig);

if (!config.outputFileSystem) {
Expand Down

0 comments on commit db2e3bf

Please sign in to comment.