Skip to content

Commit

Permalink
Fix quotation chars when eval-cheap-source-map is used (#2847)
Browse files Browse the repository at this point in the history
* Fix some quotation characters

* Don't use ?.

* Linying
  • Loading branch information
jeffposnick committed Jun 2, 2021
1 parent 8388786 commit f6fb94a
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 10 deletions.
4 changes: 3 additions & 1 deletion infra/testing/validator/service-worker-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ module.exports = async ({

vm.runInNewContext(swString, context);

validateMethodCalls({methodsToSpies, expectedMethodCalls, context});
if (expectedMethodCalls) {
validateMethodCalls({methodsToSpies, expectedMethodCalls, context});
}

// Optionally check the usage of addEventListener().
if (addEventListenerValidation) {
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
},
"scripts": {
"version": "gulp build && git add -A packages",
"gulp": "gulp",
"gulp": "gulp",
"build": "gulp build",
"lint": "gulp lint",
"test_server" : "gulp test_server",
"test_server": "gulp test_server",
"test_node": "gulp test_node",
"test_integration": "gulp test_integration"
},
Expand Down
5 changes: 0 additions & 5 deletions packages/workbox-expiration/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion packages/workbox-webpack-plugin/src/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,14 @@ class InjectManifest {
compilation, config);

let manifestString = stringify(sortedEntries);
if (this.config.compileSrc) {
if (this.config.compileSrc &&
// See https://github.com/GoogleChrome/workbox/issues/2729
// (TODO: Switch to ?. once our linter supports it.)
!(compilation.options &&
compilation.options.devtool === 'eval-cheap-source-map' &&
compilation.options.optimization &&
compilation.options.optimization.minimize)
) {
// See https://github.com/GoogleChrome/workbox/issues/2263
manifestString = manifestString.replace(/"/g, `'`);
}
Expand Down
94 changes: 94 additions & 0 deletions test/workbox-webpack-plugin/node/v4/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,100 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v4`, function() {
}
});
});

// See https://github.com/GoogleChrome/workbox/issues/2729
it(`should produce valid JavaScript when eval-cheap-source-map and minimization are used`, function(done) {
const outputDir = tempy.directory();

const config = {
mode: 'development',
entry: upath.join(SRC_DIR, WEBPACK_ENTRY_FILENAME),
output: {
filename: WEBPACK_ENTRY_FILENAME,
path: outputDir,
},
devtool: 'eval-cheap-source-map',
optimization: {
minimize: true,
},
plugins: [
new InjectManifest({
swSrc: upath.join(__dirname, '..', '..', 'static', 'module-import-sw.js'),
swDest: 'service-worker.js',
}),
],
};

const compiler = webpack(config);
compiler.run(async (webpackError, stats) => {
const swFile = upath.join(outputDir, 'service-worker.js');
try {
webpackBuildCheck(webpackError, stats);

const files = await globby('**', {cwd: outputDir});
expect(files).to.have.length(2);

await validateServiceWorkerRuntime({
swFile,
entryPoint: 'injectManifest',
// We can't verify expectedMethodCalls here, since we're using
// a compiled ES module import, not the workbox-sw interfaces.
// This test just confirms that the compilation produces valid JS.
});

done();
} catch (error) {
done(error);
}
});
});

// See https://github.com/GoogleChrome/workbox/issues/2729
it(`should produce valid JavaScript when eval-cheap-source-map is used without minimization`, function(done) {
const outputDir = tempy.directory();

const config = {
mode: 'development',
entry: upath.join(SRC_DIR, WEBPACK_ENTRY_FILENAME),
output: {
filename: WEBPACK_ENTRY_FILENAME,
path: outputDir,
},
devtool: 'eval-cheap-source-map',
optimization: {
minimize: false,
},
plugins: [
new InjectManifest({
swSrc: upath.join(__dirname, '..', '..', 'static', 'module-import-sw.js'),
swDest: 'service-worker.js',
}),
],
};

const compiler = webpack(config);
compiler.run(async (webpackError, stats) => {
const swFile = upath.join(outputDir, 'service-worker.js');
try {
webpackBuildCheck(webpackError, stats);

const files = await globby('**', {cwd: outputDir});
expect(files).to.have.length(2);

await validateServiceWorkerRuntime({
swFile,
entryPoint: 'injectManifest',
// We can't verify expectedMethodCalls here, since we're using
// a compiled ES module import, not the workbox-sw interfaces.
// This test just confirms that the compilation produces valid JS.
});

done();
} catch (error) {
done(error);
}
});
});
});

describe(`[workbox-webpack-plugin] Filtering via include/exclude`, function() {
Expand Down
94 changes: 94 additions & 0 deletions test/workbox-webpack-plugin/node/v5/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,100 @@ describe(`[workbox-webpack-plugin] InjectManifest with webpack v5`, function() {
}
});
});

// See https://github.com/GoogleChrome/workbox/issues/2729
it(`should produce valid JavaScript when eval-cheap-source-map and minimization are used`, function(done) {
const outputDir = tempy.directory();

const config = {
mode: 'development',
entry: upath.join(SRC_DIR, WEBPACK_ENTRY_FILENAME),
output: {
filename: WEBPACK_ENTRY_FILENAME,
path: outputDir,
},
devtool: 'eval-cheap-source-map',
optimization: {
minimize: true,
},
plugins: [
new InjectManifest({
swSrc: upath.join(__dirname, '..', '..', 'static', 'module-import-sw.js'),
swDest: 'service-worker.js',
}),
],
};

const compiler = webpack(config);
compiler.run(async (webpackError, stats) => {
const swFile = upath.join(outputDir, 'service-worker.js');
try {
webpackBuildCheck(webpackError, stats);

const files = await globby('**', {cwd: outputDir});
expect(files).to.have.length(4);

await validateServiceWorkerRuntime({
swFile,
entryPoint: 'injectManifest',
// We can't verify expectedMethodCalls here, since we're using
// a compiled ES module import, not the workbox-sw interfaces.
// This test just confirms that the compilation produces valid JS.
});

done();
} catch (error) {
done(error);
}
});
});

// See https://github.com/GoogleChrome/workbox/issues/2729
it(`should produce valid JavaScript when eval-cheap-source-map is used without minimization`, function(done) {
const outputDir = tempy.directory();

const config = {
mode: 'development',
entry: upath.join(SRC_DIR, WEBPACK_ENTRY_FILENAME),
output: {
filename: WEBPACK_ENTRY_FILENAME,
path: outputDir,
},
devtool: 'eval-cheap-source-map',
optimization: {
minimize: false,
},
plugins: [
new InjectManifest({
swSrc: upath.join(__dirname, '..', '..', 'static', 'module-import-sw.js'),
swDest: 'service-worker.js',
}),
],
};

const compiler = webpack(config);
compiler.run(async (webpackError, stats) => {
const swFile = upath.join(outputDir, 'service-worker.js');
try {
webpackBuildCheck(webpackError, stats);

const files = await globby('**', {cwd: outputDir});
expect(files).to.have.length(2);

await validateServiceWorkerRuntime({
swFile,
entryPoint: 'injectManifest',
// We can't verify expectedMethodCalls here, since we're using
// a compiled ES module import, not the workbox-sw interfaces.
// This test just confirms that the compilation produces valid JS.
});

done();
} catch (error) {
done(error);
}
});
});
});

describe(`[workbox-webpack-plugin] Filtering via include/exclude`, function() {
Expand Down
11 changes: 11 additions & 0 deletions test/workbox-webpack-plugin/static/module-import-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Copyright 2018 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.
*/

import {precacheAndRoute} from '../../../packages/workbox-precaching';

precacheAndRoute(self.__WB_MANIFEST);

0 comments on commit f6fb94a

Please sign in to comment.