From a13c7ce2b69fc99df168c4c3826d8bccfd0b2d17 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Mon, 17 May 2021 09:35:23 +0530 Subject: [PATCH 1/7] feat: add `getFilenameFromUrl` to API --- src/index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/index.js b/src/index.js index e5edae42b..f52a867a8 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import { validate } from 'schema-utils'; import mime from 'mime-types'; import middleware from './middleware'; +import getFilenameFromUrl from './utils/getFilenameFromUrl'; import setupHooks from './utils/setupHooks'; import setupWriteToDisk from './utils/setupWriteToDisk'; import setupOutputFileSystem from './utils/setupOutputFileSystem'; @@ -79,15 +80,20 @@ export default function wdm(compiler, options = {}) { instance.waitUntilValid = (callback = noop) => { ready(context, callback); }; + instance.invalidate = (callback = noop) => { ready(context, callback); context.watching.invalidate(); }; + instance.close = (callback = noop) => { context.watching.close(callback); }; + instance.context = context; + instance.getFilenameFromUrl = getFilenameFromUrl; + return instance; } From 3c7c404ce369f6ec5c995cac6b0a84249fae9902 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 18 May 2021 08:15:50 +0530 Subject: [PATCH 2/7] docs: update --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index eb787f3fd..fea93273b 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,24 @@ instance.waitUntilValid(() => { }); ``` +### `getFilenameFromUrl(context, url)` + +Get filename from url. + +```js +const webpack = require('webpack'); +const compiler = webpack({ ... }); +const middleware = require('webpack-dev-middleware'); +const instance = middleware(compiler); + +app.use(instance); + +const processRequest = (req, res) => { + const filename = instance.getFilenameFromUrl(instance.contex, req.url); + console.log(`filename is ${filename}`) +} +``` + ## Known Issues ### Multiple Successive Builds From 5702b1b81350c95645e62ef3613db6608b14d4b0 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 18 May 2021 17:18:36 +0300 Subject: [PATCH 3/7] refactor: code --- README.md | 37 ++++++++++++++--------- src/index.js | 4 +-- src/utils/getFilenameFromUrl.js | 11 +++++-- test/api.test.js | 52 +++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index fea93273b..e6c849b64 100644 --- a/README.md +++ b/README.md @@ -198,9 +198,9 @@ middleware(compiler, { outputFileSystem: myOutputFileSystem }); `webpack-dev-middleware` also provides convenience methods that can be use to interact with the middleware at runtime: -### `close(callback)` +### `close(callback?)` -Instructs a webpack-dev-middleware instance to stop watching for file changes. +Instructs `webpack-dev-middleware` instance to stop watching for file changes. ### Parameters @@ -210,14 +210,14 @@ Type: `Function` A function executed once the middleware has stopped watching. -### `invalidate()` +### `invalidate(callback?)` Instructs a webpack-dev-middleware instance to recompile the bundle. e.g. after a change to the configuration. ```js const webpack = require('webpack'); -const compiler = webpack({ ... }); +const compiler = webpack(); const middleware = require('webpack-dev-middleware'); const instance = middleware(compiler); @@ -233,6 +233,14 @@ setTimeout(() => { }, 1000); ``` +### Parameters + +#### callback + +Type: `Function` + +A function executed once the middleware has invalidated. + ### `waitUntilValid(callback)` Executes a callback function when the compiler bundle is valid, typically after @@ -244,12 +252,12 @@ compilation. Type: `Function` -A function executed when the bundle becomes valid. If the bundle is -valid at the time of calling, the callback is executed immediately. +A function executed when the bundle becomes valid. +If the bundle is valid at the time of calling, the callback is executed immediately. ```js const webpack = require('webpack'); -const compiler = webpack({ ... }); +const compiler = webpack(); const middleware = require('webpack-dev-middleware'); const instance = middleware(compiler); @@ -260,22 +268,23 @@ instance.waitUntilValid(() => { }); ``` -### `getFilenameFromUrl(context, url)` +### `getFilenameFromUrl(url)` -Get filename from url. +Get filename from URL. ```js const webpack = require('webpack'); -const compiler = webpack({ ... }); +const compiler = webpack(); const middleware = require('webpack-dev-middleware'); const instance = middleware(compiler); app.use(instance); -const processRequest = (req, res) => { - const filename = instance.getFilenameFromUrl(instance.contex, req.url); - console.log(`filename is ${filename}`) -} +instance.waitUntilValid(() => { + const filename = instance.getFilenameFromUrl('/bundle.js'); + + console.log(`Filename is ${filename}`); +}); ``` ## Known Issues diff --git a/src/index.js b/src/index.js index f52a867a8..714df3350 100644 --- a/src/index.js +++ b/src/index.js @@ -77,6 +77,8 @@ export default function wdm(compiler, options = {}) { const instance = middleware(context); // API + instance.getFilenameFromUrl = (url) => getFilenameFromUrl(context, url); + instance.waitUntilValid = (callback = noop) => { ready(context, callback); }; @@ -93,7 +95,5 @@ export default function wdm(compiler, options = {}) { instance.context = context; - instance.getFilenameFromUrl = getFilenameFromUrl; - return instance; } diff --git a/src/utils/getFilenameFromUrl.js b/src/utils/getFilenameFromUrl.js index c9af30ee6..d4d1fba04 100644 --- a/src/utils/getFilenameFromUrl.js +++ b/src/utils/getFilenameFromUrl.js @@ -12,17 +12,18 @@ export default function getFilenameFromUrl(context, url) { const { options } = context; const paths = getPaths(context); - let filename; + let foundFilename; let urlObject; try { // The `url` property of the `request` is contains only `pathname`, `search` and `hash` urlObject = memoizedParse(url, false, true); } catch (_ignoreError) { - return filename; + return; } for (const { publicPath, outputPath } of paths) { + let filename; let publicPathObject; try { @@ -62,6 +63,8 @@ export default function getFilenameFromUrl(context, url) { } if (fsStats.isFile()) { + foundFilename = filename; + break; } else if ( fsStats.isDirectory() && @@ -83,11 +86,13 @@ export default function getFilenameFromUrl(context, url) { } if (fsStats.isFile()) { + foundFilename = filename; + break; } } } } - return filename; + return foundFilename; } diff --git a/test/api.test.js b/test/api.test.js index bf8fec3be..8da5f4853 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -1,3 +1,5 @@ +import path from 'path'; + import express from 'express'; import connect from 'connect'; import webpack, { Stats } from 'webpack'; @@ -372,6 +374,56 @@ describe.each([ }); }); + describe('getFilenameFromUrl method', () => { + beforeEach((done) => { + compiler = getCompiler(webpackConfig); + + instance = middleware(compiler); + + app = framework(); + app.use(instance); + + listen = app.listen((error) => { + if (error) { + return done(error); + } + + return done(); + }); + }); + + afterEach((done) => { + if (instance.context.watching.closed) { + if (listen) { + listen.close(done); + } else { + done(); + } + + return; + } + + instance.close(() => { + if (listen) { + listen.close(done); + } else { + done(); + } + }); + }); + + it('should work', (done) => { + instance.waitUntilValid(() => { + expect(instance.getFilenameFromUrl('/unknown.unknown')).toBeUndefined(); + expect(instance.getFilenameFromUrl('/bundle.js')).toBe( + path.join(webpackConfig.output.path, '/bundle.js') + ); + + done(); + }); + }); + }); + describe('close method', () => { beforeEach((done) => { compiler = getCompiler(webpackConfig); From 3ba2fbe5eb1e7e488761b6a80303251248e09e5c Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 18 May 2021 17:50:28 +0300 Subject: [PATCH 4/7] docs: improve --- README.md | 87 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index e6c849b64..e61d0275f 100644 --- a/README.md +++ b/README.md @@ -198,49 +198,73 @@ middleware(compiler, { outputFileSystem: myOutputFileSystem }); `webpack-dev-middleware` also provides convenience methods that can be use to interact with the middleware at runtime: -### `close(callback?)` +### `close(callback)` Instructs `webpack-dev-middleware` instance to stop watching for file changes. -### Parameters +#### Parameters -#### callback +##### `callback` Type: `Function` +Required: `No` A function executed once the middleware has stopped watching. -### `invalidate(callback?)` - -Instructs a webpack-dev-middleware instance to recompile the bundle. -e.g. after a change to the configuration. - ```js +const express = require('express'); const webpack = require('webpack'); -const compiler = webpack(); +const compiler = webpack({ + /* Webpack configuration */ +}); const middleware = require('webpack-dev-middleware'); const instance = middleware(compiler); +const app = new express(); + app.use(instance); setTimeout(() => { - // After a short delay the configuration is changed and a banner plugin is added - // to the config - new webpack.BannerPlugin('A new banner').apply(compiler); - - // Recompile the bundle with the banner plugin: - instance.invalidate(); + // Says `webpack` to stop watch changes + instance.close(); }, 1000); ``` -### Parameters +### `invalidate(callback)` -#### callback +Instructs `webpack-dev-middleware` instance to recompile the bundle, e.g. after a change to the configuration. + +#### Parameters + +##### callback Type: `Function` +Required: `No` A function executed once the middleware has invalidated. +```js +const express = require('express'); +const webpack = require('webpack'); +const compiler = webpack({ + /* Webpack configuration */ +}); +const middleware = require('webpack-dev-middleware'); +const instance = middleware(compiler); + +const app = new express(); + +app.use(instance); + +setTimeout(() => { + // After a short delay the configuration is changed and a banner plugin is added to the config + new webpack.BannerPlugin('A new banner').apply(compiler); + + // Recompile the bundle with the banner plugin: + instance.invalidate(); +}, 1000); +``` + ### `waitUntilValid(callback)` Executes a callback function when the compiler bundle is valid, typically after @@ -251,16 +275,22 @@ compilation. #### callback Type: `Function` +Required: `No` A function executed when the bundle becomes valid. If the bundle is valid at the time of calling, the callback is executed immediately. ```js +const express = require('express'); const webpack = require('webpack'); -const compiler = webpack(); +const compiler = webpack({ + /* Webpack configuration */ +}); const middleware = require('webpack-dev-middleware'); const instance = middleware(compiler); +const app = new express(); + app.use(instance); instance.waitUntilValid(() => { @@ -272,12 +302,26 @@ instance.waitUntilValid(() => { Get filename from URL. +### Parameters + +#### url + +Type: `String` +Required: `Yes` + +URL for the requested file. + ```js +const express = require('express'); const webpack = require('webpack'); -const compiler = webpack(); +const compiler = webpack({ + /* Webpack configuration */ +}); const middleware = require('webpack-dev-middleware'); const instance = middleware(compiler); +const app = new express(); + app.use(instance); instance.waitUntilValid(() => { @@ -316,13 +360,16 @@ process is finished with server-side rendering enabled._ Example Implementation: ```js +const express = require('express'); const webpack = require('webpack'); const compiler = webpack({ - // webpack options + /* Webpack configuration */ }); const isObject = require('is-object'); const middleware = require('webpack-dev-middleware'); +const app = new express(); + // This function makes server rendering of asset references consistent with different webpack chunk/entry configurations function normalizeAssets(assets) { if (isObject(assets)) { From bdd2ec50c6255de85bf2f2deebc1c1d5312310be Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 18 May 2021 17:51:20 +0300 Subject: [PATCH 5/7] docs: fix nested --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e61d0275f..7022ac2c3 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,7 @@ Instructs `webpack-dev-middleware` instance to recompile the bundle, e.g. after #### Parameters -##### callback +##### `callback` Type: `Function` Required: `No` @@ -270,9 +270,9 @@ setTimeout(() => { Executes a callback function when the compiler bundle is valid, typically after compilation. -### Parameters +#### Parameters -#### callback +##### `callback` Type: `Function` Required: `No` @@ -302,9 +302,9 @@ instance.waitUntilValid(() => { Get filename from URL. -### Parameters +#### Parameters -#### url +##### `url` Type: `String` Required: `Yes` From e64280c34a2c04398636162b0b3d5cc0a8da7a7a Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 18 May 2021 19:33:39 +0300 Subject: [PATCH 6/7] test: refactor --- test/api.test.js | 285 ++++++++++++++++++-- test/fixtures/webpack.public-path.config.js | 11 +- test/utils/getFilenameFromUrl.test.js | 154 ----------- 3 files changed, 267 insertions(+), 183 deletions(-) delete mode 100644 test/utils/getFilenameFromUrl.test.js diff --git a/test/api.test.js b/test/api.test.js index 8da5f4853..cae872962 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -9,6 +9,8 @@ import middleware from '../src'; import getCompiler from './helpers/getCompiler'; import getCompilerHooks from './helpers/getCompilerHooks'; import webpackConfig from './fixtures/webpack.config'; +import webpackPublicPathConfig from './fixtures/webpack.public-path.config'; +import webpackMultiConfig from './fixtures/webpack.array.config'; // Suppress unnecessary stats output global.console.log = jest.fn(); @@ -375,51 +377,278 @@ describe.each([ }); describe('getFilenameFromUrl method', () => { - beforeEach((done) => { - compiler = getCompiler(webpackConfig); + describe('should work', () => { + beforeEach((done) => { + compiler = getCompiler(webpackConfig); - instance = middleware(compiler); + instance = middleware(compiler); - app = framework(); - app.use(instance); + app = framework(); + app.use(instance); - listen = app.listen((error) => { - if (error) { - return done(error); + listen = app.listen((error) => { + if (error) { + return done(error); + } + + return done(); + }); + }); + + afterEach((done) => { + if (instance.context.watching.closed) { + if (listen) { + listen.close(done); + } else { + done(); + } + + return; } - return done(); + instance.close(() => { + if (listen) { + listen.close(done); + } else { + done(); + } + }); }); - }); - afterEach((done) => { - if (instance.context.watching.closed) { - if (listen) { - listen.close(done); - } else { + it('should work', (done) => { + instance.waitUntilValid(() => { + expect(instance.getFilenameFromUrl('/bundle.js')).toBe( + path.join(webpackConfig.output.path, '/bundle.js') + ); + expect(instance.getFilenameFromUrl('/')).toBe( + path.join(webpackConfig.output.path, '/index.html') + ); + expect(instance.getFilenameFromUrl('/index.html')).toBe( + path.join(webpackConfig.output.path, '/index.html') + ); + expect(instance.getFilenameFromUrl('/svg.svg')).toBe( + path.join(webpackConfig.output.path, '/svg.svg') + ); + expect( + instance.getFilenameFromUrl('/unknown.unknown') + ).toBeUndefined(); + expect( + instance.getFilenameFromUrl('/unknown/unknown.unknown') + ).toBeUndefined(); + done(); + }); + }); + }); + + describe('should work when the "index" option disabled', () => { + beforeEach((done) => { + compiler = getCompiler(webpackConfig); + + instance = middleware(compiler, { index: false }); + + app = framework(); + app.use(instance); + + listen = app.listen((error) => { + if (error) { + return done(error); + } + + return done(); + }); + }); + + afterEach((done) => { + if (instance.context.watching.closed) { + if (listen) { + listen.close(done); + } else { + done(); + } + + return; } - return; - } + instance.close(() => { + if (listen) { + listen.close(done); + } else { + done(); + } + }); + }); + + it('should work', (done) => { + instance.waitUntilValid(() => { + expect(instance.getFilenameFromUrl('/bundle.js')).toBe( + path.join(webpackConfig.output.path, '/bundle.js') + ); + // eslint-disable-next-line no-undefined + expect(instance.getFilenameFromUrl('/')).toBe(undefined); + expect(instance.getFilenameFromUrl('/index.html')).toBe( + path.join(webpackConfig.output.path, '/index.html') + ); + expect(instance.getFilenameFromUrl('/svg.svg')).toBe( + path.join(webpackConfig.output.path, '/svg.svg') + ); + expect( + instance.getFilenameFromUrl('/unknown.unknown') + ).toBeUndefined(); + expect( + instance.getFilenameFromUrl('/unknown/unknown.unknown') + ).toBeUndefined(); - instance.close(() => { - if (listen) { - listen.close(done); - } else { done(); + }); + }); + }); + + describe('should work with the "publicPath" option', () => { + beforeEach((done) => { + compiler = getCompiler(webpackPublicPathConfig); + + instance = middleware(compiler); + + app = framework(); + app.use(instance); + + listen = app.listen((error) => { + if (error) { + return done(error); + } + + return done(); + }); + }); + + afterEach((done) => { + if (instance.context.watching.closed) { + if (listen) { + listen.close(done); + } else { + done(); + } + + return; } + + instance.close(() => { + if (listen) { + listen.close(done); + } else { + done(); + } + }); + }); + + it('should work', (done) => { + instance.waitUntilValid(() => { + expect(instance.getFilenameFromUrl('/public/path/bundle.js')).toBe( + path.join(webpackPublicPathConfig.output.path, '/bundle.js') + ); + expect(instance.getFilenameFromUrl('/public/path/')).toBe( + path.join(webpackPublicPathConfig.output.path, '/index.html') + ); + expect(instance.getFilenameFromUrl('/public/path/index.html')).toBe( + path.join(webpackPublicPathConfig.output.path, '/index.html') + ); + expect(instance.getFilenameFromUrl('/public/path/svg.svg')).toBe( + path.join(webpackPublicPathConfig.output.path, '/svg.svg') + ); + + expect(instance.getFilenameFromUrl('/')).toBeUndefined(); + expect( + instance.getFilenameFromUrl('/unknown.unknown') + ).toBeUndefined(); + expect( + instance.getFilenameFromUrl('/unknown/unknown.unknown') + ).toBeUndefined(); + + done(); + }); }); }); - it('should work', (done) => { - instance.waitUntilValid(() => { - expect(instance.getFilenameFromUrl('/unknown.unknown')).toBeUndefined(); - expect(instance.getFilenameFromUrl('/bundle.js')).toBe( - path.join(webpackConfig.output.path, '/bundle.js') - ); + describe('should work in multi compiler mode', () => { + beforeEach((done) => { + compiler = getCompiler(webpackMultiConfig); - done(); + instance = middleware(compiler); + + app = framework(); + app.use(instance); + + listen = app.listen((error) => { + if (error) { + return done(error); + } + + return done(); + }); + }); + + afterEach((done) => { + if (instance.context.watching.closed) { + if (listen) { + listen.close(done); + } else { + done(); + } + + return; + } + + instance.close(() => { + if (listen) { + listen.close(done); + } else { + done(); + } + }); + }); + + it('should work', (done) => { + instance.waitUntilValid(() => { + expect(instance.getFilenameFromUrl('/static-one/bundle.js')).toBe( + path.join(webpackMultiConfig[0].output.path, '/bundle.js') + ); + expect(instance.getFilenameFromUrl('/static-one/')).toBe( + path.join(webpackMultiConfig[0].output.path, '/index.html') + ); + expect(instance.getFilenameFromUrl('/static-one/index.html')).toBe( + path.join(webpackMultiConfig[0].output.path, '/index.html') + ); + expect(instance.getFilenameFromUrl('/static-one/svg.svg')).toBe( + path.join(webpackMultiConfig[0].output.path, '/svg.svg') + ); + expect( + instance.getFilenameFromUrl('/static-one/unknown.unknown') + ).toBeUndefined(); + expect( + instance.getFilenameFromUrl('/static-one/unknown/unknown.unknown') + ).toBeUndefined(); + + expect(instance.getFilenameFromUrl('/static-two/bundle.js')).toBe( + path.join(webpackMultiConfig[1].output.path, '/bundle.js') + ); + expect( + instance.getFilenameFromUrl('/static-two/unknown.unknown') + ).toBeUndefined(); + expect( + instance.getFilenameFromUrl('/static-two/unknown/unknown.unknown') + ).toBeUndefined(); + + expect(instance.getFilenameFromUrl('/')).toBeUndefined(); + expect( + instance.getFilenameFromUrl('/static-one/unknown.unknown') + ).toBeUndefined(); + expect( + instance.getFilenameFromUrl('/static-one/unknown/unknown.unknown') + ).toBeUndefined(); + + done(); + }); }); }); }); diff --git a/test/fixtures/webpack.public-path.config.js b/test/fixtures/webpack.public-path.config.js index e99686fd1..12ba710c7 100644 --- a/test/fixtures/webpack.public-path.config.js +++ b/test/fixtures/webpack.public-path.config.js @@ -5,12 +5,21 @@ const path = require('path'); module.exports = { mode: 'development', context: path.resolve(__dirname), - entry: './simple.js', + entry: './foo.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, '../outputs/public-path'), publicPath: '/public/path/', }, + module: { + rules: [ + { + test: /\.(svg|html)$/, + loader: 'file-loader', + options: { name: '[name].[ext]' }, + }, + ], + }, infrastructureLogging: { level: 'none' }, diff --git a/test/utils/getFilenameFromUrl.test.js b/test/utils/getFilenameFromUrl.test.js deleted file mode 100644 index 7d8c7c4bf..000000000 --- a/test/utils/getFilenameFromUrl.test.js +++ /dev/null @@ -1,154 +0,0 @@ -import path from 'path'; - -import express from 'express'; - -import middleware from '../../src'; -import getFilenameFromUrl from '../../src/utils/getFilenameFromUrl'; -import getCompiler from '../helpers/getCompiler'; -import listenAndCompile from '../helpers/listenAndCompile'; -import webpackSimpleConfig from '../fixtures/webpack.simple.config'; -import webpackPublicPathConfig from '../fixtures/webpack.public-path.config'; -import webpackMultiConfig from '../fixtures/webpack.array.config'; - -// Suppress unnecessary stats output -global.console.log = jest.fn(); - -describe('getFilenameFromUrl', () => { - const configs = [ - { - title: 'simple config with path /', - config: webpackSimpleConfig, - middlewareConfig: {}, - url: '/', - expected: path.resolve(__dirname, '../outputs/simple/index.html'), - }, - { - title: 'simple config with path /index.html', - config: webpackSimpleConfig, - middlewareConfig: {}, - url: '/index.html', - expected: path.resolve(__dirname, '../outputs/simple/index.html'), - }, - { - title: 'simple config with path /path', - config: webpackSimpleConfig, - middlewareConfig: {}, - url: '/path', - expected: path.resolve(__dirname, '../outputs/simple/path'), - }, - { - title: 'simple config with path /path/file.html', - config: webpackSimpleConfig, - middlewareConfig: {}, - url: '/path/file.html', - expected: path.resolve(__dirname, '../outputs/simple/path/file.html'), - }, - { - title: 'simple config with index false and path /', - config: webpackSimpleConfig, - middlewareConfig: { - index: false, - }, - url: '/', - expected: path.resolve(__dirname, '../outputs/simple'), - }, - { - title: 'simple config with index file.html and path /', - config: webpackSimpleConfig, - middlewareConfig: { - index: 'file.html', - }, - url: '/', - expected: path.resolve(__dirname, '../outputs/simple/file.html'), - }, - - { - title: 'publicPath config with path /', - config: webpackPublicPathConfig, - middlewareConfig: {}, - url: '/', - expected: null, - }, - { - title: 'publicPath config with path /public/path/', - config: webpackPublicPathConfig, - middlewareConfig: {}, - url: '/public/path/', - expected: path.resolve(__dirname, '../outputs/public-path/index.html'), - }, - { - title: 'publicPath config with path /public/path/more/file.html', - config: webpackPublicPathConfig, - middlewareConfig: {}, - url: '/public/path/more/file.html', - expected: path.resolve( - __dirname, - '../outputs/public-path/more/file.html' - ), - }, - - { - title: 'multi config with path /', - config: webpackMultiConfig, - middlewareConfig: {}, - url: '/', - expected: null, - }, - { - title: 'multi config with path /static-one/', - config: webpackMultiConfig, - middlewareConfig: {}, - url: '/static-one/', - expected: path.resolve(__dirname, '../outputs/array/js1/index.html'), - }, - { - title: 'multi config with path /static-two/', - config: webpackMultiConfig, - middlewareConfig: {}, - url: '/static-two/', - expected: path.resolve(__dirname, '../outputs/array/js2/index.html'), - }, - ]; - - configs.forEach((config) => { - describe(config.title, () => { - let instance; - let listen; - let app; - let compiler; - - beforeEach((done) => { - compiler = getCompiler(config.config); - - instance = middleware(compiler, config.middlewareConfig); - - app = express(); - app.use(instance); - - listen = listenAndCompile(app, compiler, done); - }); - - afterEach((done) => { - if (instance) { - instance.close(); - } - - if (listen) { - listen.close(done); - } else { - done(); - } - }); - - it('should return correct filename from url', () => { - const filename = getFilenameFromUrl(instance.context, config.url); - const { expected } = config; - if (expected) { - expect(filename).toEqual(expected); - } else { - expect(filename).toBeUndefined(); - } - }); - }); - }); -}); From c8251d4528b02cf84ae6f7ead28f4ddba4796e0a Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 18 May 2021 19:38:34 +0300 Subject: [PATCH 7/7] chore: fix lint --- src/utils/getFilenameFromUrl.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/getFilenameFromUrl.js b/src/utils/getFilenameFromUrl.js index d4d1fba04..a802f9f48 100644 --- a/src/utils/getFilenameFromUrl.js +++ b/src/utils/getFilenameFromUrl.js @@ -94,5 +94,6 @@ export default function getFilenameFromUrl(context, url) { } } + // eslint-disable-next-line consistent-return return foundFilename; }