From 043186864311e9d58f597b7f94b98b267814937e Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Tue, 16 Jul 2019 13:30:40 -0400 Subject: [PATCH] Use a .js extension when creating swDest --- .../options/webpack-inject-manifest-schema.js | 6 +++- .../node/inject-manifest.js | 33 +++++++++++++++++++ test/workbox-webpack-plugin/static/sw.ts | 10 ++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/workbox-webpack-plugin/static/sw.ts diff --git a/packages/workbox-build/src/entry-points/options/webpack-inject-manifest-schema.js b/packages/workbox-build/src/entry-points/options/webpack-inject-manifest-schema.js index 642438b15..df21e0d32 100644 --- a/packages/workbox-build/src/entry-points/options/webpack-inject-manifest-schema.js +++ b/packages/workbox-build/src/entry-points/options/webpack-inject-manifest-schema.js @@ -14,7 +14,11 @@ const defaults = require('./defaults'); const webpackCommon = require('./webpack-common'); // See https://github.com/hapijs/joi/blob/v16.0.0-rc2/API.md#anydefaultvalue-description -const swSrcBasename = (context) => upath.basename(context.swSrc); +const swSrcBasename = (context) => { + const {name} = upath.parse(context.swSrc); + // Always use the .js extension when generating a default filename. + return name + '.js'; +}; swSrcBasename.description = 'derived from the swSrc file name'; module.exports = baseSchema.keys(Object.assign({ diff --git a/test/workbox-webpack-plugin/node/inject-manifest.js b/test/workbox-webpack-plugin/node/inject-manifest.js index 9f1549aec..77b7e378b 100644 --- a/test/workbox-webpack-plugin/node/inject-manifest.js +++ b/test/workbox-webpack-plugin/node/inject-manifest.js @@ -1140,4 +1140,37 @@ describe(`[workbox-webpack-plugin] InjectManifest (End to End)`, function() { }); }); }); + + describe(`[workbox-webpack-plugin] TypeScript compilation`, function() { + it(`should rename a swSrc with a .ts extension to .js`, function(done) { + const outputDir = tempy.directory(); + const config = { + mode: 'production', + entry: path.join(SRC_DIR, WEBPACK_ENTRY_FILENAME), + output: { + filename: '[name].[hash:6].js', + path: outputDir, + }, + plugins: [ + new InjectManifest({ + swSrc: path.join(__dirname, '..', 'static', 'sw.ts'), + }), + ], + }; + + const compiler = webpack(config); + compiler.run(async (webpackError, stats) => { + try { + webpackBuildCheck(webpackError, stats); + + const files = await globby('*', {cwd: outputDir}); + expect(files).to.contain('sw.js'); + + done(); + } catch (error) { + done(error); + } + }); + }); + }); }); diff --git a/test/workbox-webpack-plugin/static/sw.ts b/test/workbox-webpack-plugin/static/sw.ts new file mode 100644 index 000000000..6395905ce --- /dev/null +++ b/test/workbox-webpack-plugin/static/sw.ts @@ -0,0 +1,10 @@ +/* + Copyright 2019 Google LLC + + Use of this source code is governed by an MIT-style + license that can be found in the LICENSE file or at + https://opensource.org/licenses/MIT. +*/ + +// At the moment, this is just used to test the extension renaming. +workbox.precaching.precacheAndRoute(self.__WB_MANIFEST, {});