Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 #197

Closed
wants to merge 10 commits into from
19 changes: 6 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
language: node_js
node_js:
- '9'
- '8'
- '6'
- 'node'
- '10'
- '8.3.0'

env:
- WEBPACK_VERSION=2 EXTRACT_PLUGIN_VERSION=2
- WEBPACK_VERSION=3 EXTRACT_PLUGIN_VERSION=3.0.2
- WEBPACK_VERSION=4 EXTRACT_PLUGIN_VERSION=next
- WEBPACK_VERSION=next EXTRACT_PLUGIN_VERSION=next

matrix:
exclude:
- node_js: '6'
env: WEBPACK_VERSION=next EXTRACT_PLUGIN_VERSION=next
- WEBPACK_VERSION=4
- WEBPACK_VERSION=next

install:
- npm install
- npm install webpack@$WEBPACK_VERSION extract-text-webpack-plugin@$EXTRACT_PLUGIN_VERSION || true
- npm install webpack@$WEBPACK_VERSION || true

script:
- npm test
Expand Down
85 changes: 34 additions & 51 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var entries = require('object.entries');
var path = require('path');
var fse = require('fs-extra');
var _ = require('lodash');
Expand Down Expand Up @@ -59,15 +58,21 @@ ManifestPlugin.prototype.apply = function(compiler) {
var outputFile = path.resolve(outputFolder, this.opts.fileName);
var outputName = path.relative(outputFolder, outputFile);

var moduleAsset = function (module, file) {
if (module.userRequest) {
moduleAssets[file] = path.join(
path.dirname(file),
path.basename(module.userRequest)
);
}
};
// TODO: check with @evilebottnawi if this is still needed for webpack@5
var normalModuleLoader = function (loaderContext, module) {
const { emitFile } = loaderContext;

loaderContext.emitFile = (file, content, sourceMap) => {
if (module.userRequest && !moduleAssets[file]) {
moduleAssets[file] = path.join(
path.dirname(file),
path.basename(module.userRequest)
);
}

return emitFile.call(module, file, content, sourceMap);
};
};

var emit = function(compilation, compileCallback) {
const emitCount = emitCountMap.get(outputFile) - 1
Expand All @@ -81,6 +86,7 @@ ManifestPlugin.prototype.apply = function(compiler) {
all: false,
// Add asset Information
assets: true,
ids: true, // needed by Webpack 5
// Show cached assets (setting this to `false` only shows emitted files)
cachedAssets: true,
});
Expand All @@ -96,14 +102,11 @@ ManifestPlugin.prototype.apply = function(compiler) {
name = path;
}

// Webpack 4: .isOnlyInitial()
// Webpack 3: .isInitial()
// Webpack 1/2: .initial
return files.concat({
path: path,
chunk: chunk,
name: name,
isInitial: chunk.isOnlyInitial ? chunk.isOnlyInitial() : (chunk.isInitial ? chunk.isInitial() : chunk.initial),
isInitial: chunk.isOnlyInitial(),
isChunk: true,
isAsset: false,
isModuleAsset: false
Expand All @@ -126,7 +129,7 @@ ManifestPlugin.prototype.apply = function(compiler) {
});
}

var isEntryAsset = asset.chunks.length > 0;
var isEntryAsset = asset.chunks && asset.chunks.length > 0;
if (isEntryAsset) {
return files;
}
Expand Down Expand Up @@ -185,13 +188,7 @@ ManifestPlugin.prototype.apply = function(compiler) {

var manifest;
if (this.opts.generate) {
const entrypointsArray = Array.from(
compilation.entrypoints instanceof Map ?
// Webpack 4+
compilation.entrypoints.entries() :
// Webpack 3
entries(compilation.entrypoints)
);
const entrypointsArray = Array.from(compilation.entrypoints.entries());
const entrypoints = entrypointsArray.reduce(
(e, [name, entrypoint]) => Object.assign(e, { [name]: entrypoint.getFiles() }),
{}
Expand Down Expand Up @@ -222,11 +219,7 @@ ManifestPlugin.prototype.apply = function(compiler) {
}
}

if (compiler.hooks) {
ManifestPlugin.getCompilerHooks(compiler).afterEmit.call(manifest);
} else {
compilation.applyPluginsAsync('webpack-manifest-plugin-after-emit', manifest, compileCallback);
}
ManifestPlugin.getCompilerHooks(compiler).afterEmit.call(manifest);
}.bind(this);

function beforeRun (compiler, callback) {
Expand All @@ -238,34 +231,24 @@ ManifestPlugin.prototype.apply = function(compiler) {
}
}

if (compiler.hooks) {
const pluginOptions = {
name: 'ManifestPlugin',
stage: Infinity
};

// Preserve exposure of custom hook in Webpack 4 for back compatability.
// Going forward, plugins should call `ManifestPlugin.getCompilerHooks(compiler)` directy.
if (!Object.isFrozen(compiler.hooks)) {
compiler.hooks.webpackManifestPluginAfterEmit = ManifestPlugin.getCompilerHooks(compiler).afterEmit;
}
const pluginOptions = {
name: 'ManifestPlugin',
stage: Infinity
};

compiler.hooks.compilation.tap(pluginOptions, function (compilation) {
compilation.hooks.moduleAsset.tap(pluginOptions, moduleAsset);
});
compiler.hooks.emit.tap(pluginOptions, emit);
// Preserve exposure of custom hook in Webpack 4 for back compatability.
// Going forward, plugins should call `ManifestPlugin.getCompilerHooks(compiler)` directy.
if (!Object.isFrozen(compiler.hooks)) {
compiler.hooks.webpackManifestPluginAfterEmit = ManifestPlugin.getCompilerHooks(compiler).afterEmit;
}

compiler.hooks.run.tap(pluginOptions, beforeRun);
compiler.hooks.watchRun.tap(pluginOptions, beforeRun);
} else {
compiler.plugin('compilation', function (compilation) {
compilation.plugin('module-asset', moduleAsset);
});
compiler.plugin('emit', emit);
compiler.hooks.compilation.tap(pluginOptions, function (compilation) {
compilation.hooks.normalModuleLoader.tap(pluginOptions, normalModuleLoader);
});
compiler.hooks.emit.tap(pluginOptions, emit);

compiler.plugin('before-run', beforeRun);
compiler.plugin('watch-run', beforeRun);
}
compiler.hooks.run.tap(pluginOptions, beforeRun);
compiler.hooks.watchRun.tap(pluginOptions, beforeRun);
};

module.exports = ManifestPlugin;
36 changes: 19 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-manifest-plugin",
"version": "2.2.0",
"version": "3.0.0-rc.0",
"description": "webpack plugin for generating asset manifests",
"main": "index.js",
"scripts": {
Expand All @@ -13,20 +13,20 @@
"node": ">=6.11.5"
},
"peerDependencies": {
"webpack": "2 || 3 || 4"
"webpack": "4"
},
"devDependencies": {
"codecov": "^3.1.0",
"css-loader": "^1.0.0",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^2.0.0",
"jest": "^23.5.0",
"@svgr/webpack": "^4.3.3",
"codecov": "^3.6.1",
"css-loader": "^3.2.0",
"file-loader": "^4.2.0",
"jest": "^24.9.0",
"memory-fs": "^0.4.1",
"react": "^16.3.2",
"rimraf": "^2.6.1",
"style-loader": "^0.23.0",
"svgr": "^1.9.2",
"webpack": "^3.12.0"
"mini-css-extract-plugin": "^0.8.0",
"react": "^16.9.0",
"rimraf": "^3.0.0",
"style-loader": "^1.0.0",
"webpack": "^4.41.0"
},
"files": [
"index.js",
Expand All @@ -41,14 +41,16 @@
},
"homepage": "https://github.com/danethurber/webpack-manifest-plugin",
"dependencies": {
"fs-extra": "^7.0.0",
"lodash": ">=3.5 <5",
"object.entries": "^1.1.0",
"tapable": "^1.0.0"
"fs-extra": "^8.1.0",
"lodash": "^4",
"tapable": "^1.1.3"
},
"jest": {
"testEnvironment": "node",
"coverageDirectory": "./coverage/",
"collectCoverage": true
"collectCoverage": true,
"coveragePathIgnorePatterns": [
"<rootDir>/spec/helpers/"
]
}
}
54 changes: 27 additions & 27 deletions spec/plugin.integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ var ManifestPlugin = require('../index.js');

jasmine.DEFAULT_TIMEOUT_INTERVAL = 5 * 60 * 1000;


function applyDefaultWebpackConfig(webpackOpts) {
var defaults = isWebpackVersionGte(4) ? {
var defaults = {
optimization: {
chunkIds: 'natural',
}
} : {};
};
return _.merge(defaults, webpackOpts);
}

Expand Down Expand Up @@ -247,33 +246,34 @@ describe('ManifestPlugin using real fs', function() {
expect(manifest).toBeDefined();

if (isFirstRun) {
expect(manifest).toEqual(isWebpackVersionGte(5) ? {
'main.js': 'main.js',
'0.js': '0.js',
'2.js': '2.js'
} : isWebpackVersionGte(4) ? {
'main.js': 'main.js',
'1.js': '1.js',
'2.js': '2.js'
} : {
'main.js': 'main.js',
'0.js': '0.js',
'1.js': '1.js'
});
if (isWebpackVersionGte(5)) {
expect(manifest).toEqual({
'main.js': 'main.js',
'0.js': '0.js',
'2.js': '2.js'
});
} else {
expect(manifest).toEqual({
'main.js': 'main.js',
'1.js': '1.js',
'2.js': '2.js'
});
}

isFirstRun = false;
fse.outputFileSync(path.join(__dirname, 'output/watch-import-chunk/index.js'), 'import(\'./chunk1\')');
} else {
expect(manifest).toEqual(isWebpackVersionGte(5) ? {
'main.js': 'main.js',
'2.js': '2.js',
} : isWebpackVersionGte(4) ? {
'main.js': 'main.js',
'1.js': '1.js',
} : {
'main.js': 'main.js',
'3.js': '3.js',
});
if (isWebpackVersionGte(5)) {
expect(manifest).toEqual({
'main.js': 'main.js',
'2.js': '2.js',
});
} else {
expect(manifest).toEqual({
'main.js': 'main.js',
'1.js': '1.js',
});
}

done();
}
Expand Down Expand Up @@ -500,7 +500,7 @@ describe('scoped hoisting', function() {
rules: [
{
test: /\.svg$/,
use: ['svgr/webpack', 'file-loader']
use: ['@svgr/webpack', 'file-loader']
},
],
},
Expand Down