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

Fix Chunk.modules DeprecationWarning in Webpack 3.x #3

Merged
merged 1 commit into from Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
@@ -0,0 +1,8 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,2 +1,4 @@
/.idea

/node_modules
/npm-debug.log
/npm-debug.log
21 changes: 15 additions & 6 deletions Plugin.js
@@ -1,8 +1,11 @@
'use strict';
'use strict'

class ExtractFilePlugin {

apply(compiler) {

compiler.plugin('this-compilation', (compilation) => {

compilation.plugin('normal-module-loader', (loaderContext, module) => {
loaderContext[__dirname] = () => {
module.meta[__dirname] = true;
Expand All @@ -11,7 +14,7 @@ class ExtractFilePlugin {

compilation.plugin('additional-assets', callback => {
compilation.chunks.forEach(chunk => {
chunk.modules.forEach(module => processModule(chunk, module));
chunk.forEachModule(module => processModule(chunk, module));
});

callback();
Expand All @@ -21,22 +24,28 @@ class ExtractFilePlugin {
}

function processModule(chunk, ourModule) {

if (ourModule.meta && ourModule.meta[__dirname]) {

let moduleFound = false;

// let's find module, which was issued by ours (proxied module)
chunk.modules.some((module) => {
if (module.reasons.some(reason => reason.module === ourModule)) {
chunk.forEachModule((module) => {

if (!moduleFound && module.reasons.some(reason => reason.module === ourModule)) {
// add assets from that module
addAssets(chunk, module);
// break cycle
return true;
moduleFound = true;
}
});
}
}

function addAssets(chunk, module) {

// add any emitted assets via proxied module to this chunk
for (var file in module.assets) {
for (let file in module.assets) {
chunk.files.push(file);
}
}
Expand Down
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -9,6 +9,15 @@ not added to the chunk files
- this loader is used as a proxy to include this emitted file in the chunk
so it's available in generated manifest

# Install

```bash
# for webpack 3
npm install --save-dev extract-text-webpack-plugin
# for webpack 1, 2
npm install --save-dev extract-text-webpack-plugin@0.1.0
```

## Usage

```js
Expand Down
10 changes: 5 additions & 5 deletions package.json
@@ -1,17 +1,17 @@
{
"name": "extract-file-loader",
"version": "0.1.0",
"version": "0.2.0",
"author": "Marius Balčytis",
"description": "Loader and plugin for webpack to add proxied asset as a chunk file",
"dependencies": {
"loader-utils": "~0.2.5"
},
"devDependencies": {
"assets-webpack-plugin": "^3.2.0",
"file-loader": "^0.8.4",
"image-webpack-loader": "^2.0.0",
"assets-webpack-plugin": "^3.5.0",
"file-loader": "^1.0.0-beta",
"image-webpack-loader": "^3",
"mocha": "^3.0.2",
"webpack": "^1.13.2"
"webpack": "^3"
},
"engines": {
"node": ">=4"
Expand Down
21 changes: 12 additions & 9 deletions test/webpack.config.js
Expand Up @@ -20,13 +20,16 @@ module.exports = {
new AssetsPlugin({filename: 'manifest.json', path: outputDir})
],
module: {
preLoaders: [{
test: /\.(gif|png|jpe?g|svg)$/i,
loader: 'image-webpack'
}],
loaders: [{
test: /\.(gif|png|jpe?g|svg)$/i,
loader: 'file?name=[name].optimized.[ext]'
}]
rules: [
{
enforce: 'pre',
test: /\.(gif|png|jpe?g|svg)$/i,
loader: 'image-webpack-loader'
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
loader: 'file-loader?name=[name].optimized.[ext]'
},
],
}
};
};