From 341f641ab75ce3a8eaf5a9975bf08815132b1b93 Mon Sep 17 00:00:00 2001 From: Zach Toben Date: Tue, 29 May 2018 09:31:38 -0500 Subject: [PATCH] Update to webpack 4 (fixes #89) --- .gitignore | 4 + .travis.yml | 2 +- appveyor.yml | 5 +- index.js | 30 +++-- lib/output/createQueuedWriter.js | 2 +- lib/pathTemplate.js | 12 +- package.json | 23 ++-- test/index.test.js | 181 ------------------------------- test/multiCompiler.test.js | 17 +-- test/pathTemplate.test.js | 2 +- test/utils/expectOutput.js | 2 + 11 files changed, 57 insertions(+), 223 deletions(-) diff --git a/.gitignore b/.gitignore index 142ab74..96f083a 100644 --- a/.gitignore +++ b/.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 diff --git a/.travis.yml b/.travis.yml index b3118fb..13147a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ sudo: false language: node_js node_js: - - 4.4 + - "8" script: - npm run lint diff --git a/appveyor.yml b/appveyor.yml index 0ec7768..0a1d5bd 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,5 +1,8 @@ environment: - nodejs_version: 4.4 + nodejs_version: "8" + +platform: + - x64 install: - ps: Install-Product node $env:nodejs_version diff --git a/index.js b/index.js index 9eef499..d64f6d9 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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) { @@ -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) + } } } diff --git a/lib/output/createQueuedWriter.js b/lib/output/createQueuedWriter.js index b12400b..37b00a4 100644 --- a/lib/output/createQueuedWriter.js +++ b/lib/output/createQueuedWriter.js @@ -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)) } } diff --git a/lib/pathTemplate.js b/lib/pathTemplate.js index 5fa4321..82d81d6 100644 --- a/lib/pathTemplate.js +++ b/lib/pathTemplate.js @@ -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) { @@ -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': diff --git a/package.json b/package.json index 6525064..fa61956 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/index.test.js b/test/index.test.js index e8b3997..dd4fb46 100644 --- a/test/index.test.js +++ b/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) @@ -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'), @@ -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'), @@ -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) - }) }) diff --git a/test/multiCompiler.test.js b/test/multiCompiler.test.js index ac6cb48..03c39a2 100644 --- a/test/multiCompiler.test.js +++ b/test/multiCompiler.test.js @@ -1,4 +1,5 @@ /* eslint-env mocha */ +/* eslint-disable no-unused-expressions */ var path = require('path') var expect = require('chai').expect @@ -59,7 +60,7 @@ describe('Plugin', function () { }) it('updates output between compiler calls when options.update is true', function (done) { - var config_1 = { + var config1 = { entry: { one: path.join(__dirname, 'fixtures/one.js') }, @@ -69,7 +70,7 @@ describe('Plugin', function () { }, plugins: [new Plugin({path: 'tmp', update: true})] } - var config_2 = { + var config2 = { entry: { two: path.join(__dirname, 'fixtures/two.js') }, @@ -81,9 +82,9 @@ describe('Plugin', function () { } var expected = {one: {js: 'one-bundle.js'}, two: {js: 'two-bundle.js'}} - var args = {config: config_2, expected: expected} + var args = {config: config2, expected: expected} - webpack(config_1, function (err, stats) { + webpack(config1, function (err, stats) { expect(err).to.be.null expect(stats.hasErrors()).to.be.false expectOutput(args, done) @@ -91,7 +92,7 @@ describe('Plugin', function () { }) it('overwrites output between compiler calls when options.update is false', function (done) { - var config_1 = { + var config1 = { entry: { one: path.join(__dirname, 'fixtures/one.js') }, @@ -101,7 +102,7 @@ describe('Plugin', function () { }, plugins: [new Plugin({path: 'tmp', update: false})] } - var config_2 = { + var config2 = { entry: { two: path.join(__dirname, 'fixtures/two.js') }, @@ -113,9 +114,9 @@ describe('Plugin', function () { } var expected = {two: {js: 'two-bundle.js'}} - var args = {config: config_2, expected: expected} + var args = {config: config2, expected: expected} - webpack(config_1, function (err, stats) { + webpack(config1, function (err, stats) { expect(err).to.be.null expect(stats.hasErrors()).to.be.false expectOutput(args, done) diff --git a/test/pathTemplate.test.js b/test/pathTemplate.test.js index 44c910d..9adcf8c 100644 --- a/test/pathTemplate.test.js +++ b/test/pathTemplate.test.js @@ -66,7 +66,7 @@ describe('parseTemplate', function () { it('matches strings with [id] placeholder', function () { var tpl = parseTemplate('foo-bar.[id].js') expect(tpl.matches('foo-bar.666.js')).to.eq(true) - expect(tpl.matches('foo-bar.nope.js')).to.eq(false) + expect(tpl.matches('foo-bar.chunk.js')).to.eq(true) }) it('matches strings with [name] placeholder', function () { diff --git a/test/utils/expectOutput.js b/test/utils/expectOutput.js index fcad154..7d7dd58 100644 --- a/test/utils/expectOutput.js +++ b/test/utils/expectOutput.js @@ -1,3 +1,5 @@ +/* eslint-disable no-unused-expressions */ + var _ = require('lodash') var expect = require('chai').expect var mkdirp = require('mkdirp')