Skip to content

Commit

Permalink
Update to webpack 4 (fixes #89)
Browse files Browse the repository at this point in the history
  • Loading branch information
ztoben authored and kossnocorp committed May 29, 2018
1 parent a1c91de commit 341f641
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 223 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -1,3 +1,7 @@
node_modules
dist
tmp

# Lock files shouldn't be committed for libraries https://stackoverflow.com/a/40206145
yarn.lock
package-lock.json
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,7 +1,7 @@
sudo: false
language: node_js
node_js:
- 4.4
- "8"

script:
- npm run lint
Expand Down
5 changes: 4 additions & 1 deletion appveyor.yml
@@ -1,5 +1,8 @@
environment:
nodejs_version: 4.4
nodejs_version: "8"

platform:
- x64

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
30 changes: 19 additions & 11 deletions index.js
Expand Up @@ -25,7 +25,7 @@ AssetsWebpackPlugin.prototype = {
apply: function (compiler) {
var self = this

compiler.plugin('after-emit', function (compilation, callback) {
var afterEmit = (compilation, callback) => {
var options = compiler.options
var stats = compilation.getStats().toJson({
hash: true,
Expand All @@ -37,17 +37,17 @@ AssetsWebpackPlugin.prototype = {
errorDetails: false,
timings: false
})
// publicPath with resolved [hash] placeholder
// publicPath with resolved [hash] placeholder

var assetPath = (stats.publicPath && self.options.fullPath) ? stats.publicPath : ''
// assetsByChunkName contains a hash with the bundle names and the produced files
// e.g. { one: 'one-bundle.js', two: 'two-bundle.js' }
// in some cases (when using a plugin or source maps) it might contain an array of produced files
// e.g. {
// main:
// [ 'index-bundle-42b6e1ec4fa8c5f0303e.js',
// 'index-bundle-42b6e1ec4fa8c5f0303e.js.map' ]
// }
// assetsByChunkName contains a hash with the bundle names and the produced files
// e.g. { one: 'one-bundle.js', two: 'two-bundle.js' }
// in some cases (when using a plugin or source maps) it might contain an array of produced files
// e.g. {
// main:
// [ 'index-bundle-42b6e1ec4fa8c5f0303e.js',
// 'index-bundle-42b6e1ec4fa8c5f0303e.js.map' ]
// }
var assetsByChunkName = stats.assetsByChunkName

var output = Object.keys(assetsByChunkName).reduce(function (chunkMap, chunkName) {
Expand Down Expand Up @@ -94,7 +94,15 @@ AssetsWebpackPlugin.prototype = {
}
callback()
})
})
}

if (compiler.hooks) {
var plugin = {name: 'AssetsWebpackPlugin'}

compiler.hooks.afterEmit.tapAsync(plugin, afterEmit)
} else {
compiler.plugin('after-emit', afterEmit)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/output/createQueuedWriter.js
Expand Up @@ -25,7 +25,7 @@ module.exports = function createQueuedWriter (processor) {
queue.push({data: data, callback: callback})

if (empty) {
// start processing
// start processing
processor(data, iterator(callback))
}
}
Expand Down
12 changes: 5 additions & 7 deletions lib/pathTemplate.js
Expand Up @@ -3,14 +3,14 @@ var escapeRegExp = require('escape-string-regexp')
var SIMPLE_PLACEHOLDER_RX = /^\[(id|name|file|query|filebase)\]/i
var HASH_PLACEHOLDER_RX = /^\[((?:chunk)?hash)(?::(\d+))?\]/i

var template_cache = Object.create(null)
var templateCache = Object.create(null)

module.exports = function createTemplate (str) {
if (!template_cache[str]) {
template_cache[str] = new PathTemplate(str)
if (!templateCache[str]) {
templateCache[str] = new PathTemplate(str)
}

return template_cache[str]
return templateCache[str]
}

function PathTemplate (template) {
Expand Down Expand Up @@ -138,14 +138,12 @@ function createTemplateMatcher (fields) {
}
if (field.placeholder) {
switch (field.placeholder) {
case 'id':
pattern += '\\d+'
break
case 'hash':
case 'chunkhash':
pattern += '[0-9a-fA-F]'
pattern += field.width ? '{1,' + field.width + '}' : '+'
break
case 'id':
case 'name':
case 'file':
case 'filebase':
Expand Down
23 changes: 11 additions & 12 deletions package.json
Expand Up @@ -25,22 +25,21 @@
},
"homepage": "https://github.com/sporto/assets-webpack-plugin",
"devDependencies": {
"chai": "^3.0.0",
"css-loader": "^0.9.1",
"extract-text-webpack-plugin": "^0.3.8",
"chai": "^4.1.2",
"css-loader": "^0.28.11",
"lodash": "^3.9.3",
"mocha": "^2.2.5",
"rimraf": "^2.2.8",
"snazzy": "^3.0.0",
"standard": "^6.0.8",
"style-loader": "^0.8.3",
"webpack": "^1.3.3-beta1"
"mocha": "^5.2.0",
"rimraf": "^2.6.2",
"snazzy": "^7.1.1",
"standard": "^11.0.1",
"style-loader": "^0.21.0",
"webpack": "^4.8.3"
},
"dependencies": {
"camelcase": "^1.2.1",
"camelcase": "^5.0.0",
"escape-string-regexp": "^1.0.3",
"lodash.assign": "^3.2.0",
"lodash.merge": "^3.3.2",
"lodash.assign": "^4.2.0",
"lodash.merge": "^4.6.1",
"mkdirp": "^0.5.1"
}
}
181 changes: 0 additions & 181 deletions test/index.test.js
@@ -1,11 +1,8 @@
/* eslint-env mocha */

var path = require('path')
var webpack = require('webpack')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var rmRf = require('rimraf')
var Plugin = require('../index.js')
var manifestStr = require('./fixtures/manifest.js')

var OUTPUT_DIR = path.join(__dirname, '../tmp')
var expectOutput = require('./utils/expectOutput')(OUTPUT_DIR)
Expand Down Expand Up @@ -165,51 +162,6 @@ describe('Plugin', function () {
expectOutput(args, done)
})

it('works with ExtractTextPlugin for stylesheets', function (done) {
var webpackConfig = {
entry: {
one: path.join(__dirname, 'fixtures/one.js'),
two: path.join(__dirname, 'fixtures/two.js'),
styles: path.join(__dirname, 'fixtures/styles.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]-bundle.js'
},
module: {
loaders: [
{test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader')}
]
},
plugins: [
new ExtractTextPlugin('[name]-bundle.css', {allChunks: true}),
new Plugin({
path: 'tmp'
})
]
}

var expected = {
one: {
js: 'one-bundle.js'
},
two: {
js: 'two-bundle.js'
},
styles: {
js: 'styles-bundle.js',
css: 'styles-bundle.css'
}
}

var args = {
config: webpackConfig,
expected: expected
}

expectOutput(args, done)
})

it('includes full publicPath', function (done) {
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
Expand Down Expand Up @@ -261,36 +213,6 @@ describe('Plugin', function () {
expectOutput(args, done)
})

it('works with CommonChunksPlugin', function (done) {
var webpackConfig = {
entry: {
one: path.join(__dirname, 'fixtures/common-chunks/one.js'),
two: path.join(__dirname, 'fixtures/common-chunks/two.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({name: 'common'}),
new Plugin({path: 'tmp'})
]
}

var expected = {
one: {js: 'one.js'},
two: {js: 'two.js'},
common: {js: 'common.js'}
}

var args = {
config: webpackConfig,
expected: expected
}

expectOutput(args, done)
})

it('allows injection of metadata', function (done) {
var webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
Expand Down Expand Up @@ -325,107 +247,4 @@ describe('Plugin', function () {

expectOutput(args, done)
})

it('works with default includeManifest', function (done) {
var webpackConfig = {
entry: {
one: path.join(__dirname, 'fixtures/common-chunks/one.js'),
two: path.join(__dirname, 'fixtures/common-chunks/two.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({names: ['common', 'manifest']}),
new Plugin({path: 'tmp', includeManifest: true})
]
}

var expected = {
one: {js: 'one.js'},
two: {js: 'two.js'},
common: {js: 'common.js'},
manifest: {
js: 'manifest.js',
text: manifestStr
}
}

var args = {
config: webpackConfig,
expected: expected
}

expectOutput(args, done)
})

it('works with custom includeManifest', function (done) {
var webpackConfig = {
entry: {
one: path.join(__dirname, 'fixtures/common-chunks/one.js'),
two: path.join(__dirname, 'fixtures/common-chunks/two.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({names: ['common', 'manifesto']}),
new Plugin({path: 'tmp', includeManifest: 'manifesto'})
]
}

var expected = {
one: {js: 'one.js'},
two: {js: 'two.js'},
common: {js: 'common.js'},
manifesto: {
js: 'manifesto.js',
text: manifestStr
}
}

var args = {
config: webpackConfig,
expected: expected
}

expectOutput(args, done)
})

it('supports source maps with includeManifest', function (done) {
var webpackConfig = {
devtool: 'sourcemap',
entry: {
one: path.join(__dirname, 'fixtures/common-chunks/one.js'),
two: path.join(__dirname, 'fixtures/common-chunks/two.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({names: ['common', 'manifesto']}),
new Plugin({path: 'tmp', includeManifest: 'manifesto'})
]
}

var expected = {
one: {js: 'one.js'},
two: {js: 'two.js'},
common: {js: 'common.js'},
manifesto: {
js: 'manifesto.js',
text: require('./fixtures/manifestWithSourceMap')
}
}

var args = {
config: webpackConfig,
expected: expected
}

expectOutput(args, done)
})
})

0 comments on commit 341f641

Please sign in to comment.