Skip to content

Commit

Permalink
Adds a unique identifier to AMP sw as a comment (#33)
Browse files Browse the repository at this point in the history
* adding a unique identifier to the service worker body
* adding a unique identifier to the AMP_SW
* fixing version number problem
* fixing test as per comments
  • Loading branch information
prateekbh authored and kristoferbaxter committed Aug 5, 2019
1 parent a62de34 commit 3aded4e
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 59 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"name": "@ampproject/amp-sw",
"version": "0.1.2",
"version": "0.1.3",
"description": "A service worker library to turn your AMP pages into PWA with network resiliency.",
"main": "dist/amp-sw.js",
"scripts": {
"build": "webpack",
"build:prod": "npm run build -- --public-path='https://cdn.ampproject.org/sw/'",
"transpile": "tsc -p ./src/tsconfig.json && tsc -p ./test/builder/tsconfig.json",
"pretest": "npm run transpile && npm run build -- --public-path='/test/dist/' && mkdir -p test/dist && cp -R dist/* test/dist",
"pretest:unit": "npm run transpile",
"test": "npm-run-all test:*",
"test:conformance": "node -r esm ./node_modules/.bin/ava ./test/conformance-test/*.js --verbose",
"test:e2e": "node -r esm ./test/index.js",
"pretest:unit": "tsc -p src",
"test:unit": "node -r esm ./node_modules/.bin/ava ./test/modules/**/unit/*-test.js --verbose",
"test:perf": "node -r esm ./node_modules/.bin/ava ./test/performance-test/*.js",
"prepublishOnly": "npm run build:prod",
"test:perf": "node -r esm ./node_modules/.bin/ava ./test/performance-test/*.js --verbose",
"version": "npm run build:prod",
"release": "np --yolo"
},
"repository": {
Expand Down
44 changes: 44 additions & 0 deletions test/conformance-test/version-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import test from 'ava';
import * as fs from 'fs';
import { promisify } from 'util';
import { resolve, join } from 'path';
import webpack from 'webpack';
import webpackConfig from '../../webpack.config';

const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const copyFile = promisify(fs.copyFile);
const deleteFile = promisify(fs.unlink);

const newPackageFile = resolve(join('.', '.test-package.json'));
const version = `${Date.now()}.0.0`;

test.before(async () => {
await copyFile('./package.json', newPackageFile);
const packageData = await readFile(newPackageFile, {
encoding: 'utf-8',
});
const packageJSON = JSON.parse(packageData);
packageJSON.version = version;
await writeFile(newPackageFile, JSON.stringify(packageJSON));

const compiler = webpack(webpackConfig({ packageFile: newPackageFile }));
return new Promise((res, rej) => {
compiler.run((err, stats) => {
if (err || (stats && stats.hasErrors())) {
rej();
}
res(stats);
});
});
});

test.after(async () => {
await deleteFile(newPackageFile);
});

test('should have the package version for the bundle', async t => {
const entryBundlePath = resolve(__dirname, '../../dist/amp-sw.js');
const entryBundleContent = await readFile(entryBundlePath, 'utf-8');
t.assert(entryBundleContent.startsWith(`/*! AMP_SW_v${version} */`));
});
125 changes: 70 additions & 55 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const SizePlugin = require('size-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin');
const {argv} = require('yargs');
const { BannerPlugin } = require('webpack');

const babelOptions = {
presets: [
Expand All @@ -39,62 +40,76 @@ const babelOptions = {
const buildPath = `${__dirname}/dist`;
const publicPath = argv.publicPath || './dist/';

module.exports = {
entry: {
'amp-sw': './src/modules/core/index.ts',
},
output: {
path: buildPath,
filename: '[name].js',
chunkFilename: '[name].js',
publicPath,
},
target: "webworker",
mode: "production",
module: {
rules: [
{
test: /\.ts$/,
use: [
{
module.exports = (options = {}) => {
let packageFile = options.packageFile || './package.json';
const { version } = require(packageFile);

return {
entry: {
'amp-sw': './src/modules/core/index.ts',
},
output: {
path: buildPath,
filename: '[name].js',
chunkFilename: '[name].js',
publicPath,
},
target: "webworker",
mode: "production",
module: {
rules: [
{
test: /\.ts$/,
use: [
{
loader: 'babel-loader',
options: babelOptions
},
{
loader: 'ts-loader',
options: {
compilerOptions: {
resolveJsonModule: true
}
}
}
]
},
{
test: /\.m?js$/,
use: {
loader: 'babel-loader',
options: babelOptions
},
{
loader: 'ts-loader'
options: babelOptions,
}
]
},
{
test: /\.m?js$/,
use: {
loader: 'babel-loader',
options: babelOptions,
}
}
]
},
plugins: [
new SizePlugin(),
new CleanWebpackPlugin([buildPath]),
new ReplaceInFileWebpackPlugin([{
dir: 'dist',
files: ['amp-sw.js'],
rules: [{
search: 'importScripts(',
replace: `importScripts('${publicPath}' + `,
}],
}]),
],
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
optimization: {
minimizer: [new TerserPlugin({
terserOptions: {
ecma: 6,
module: true,
}
})],
},
]
},
plugins: [
new SizePlugin(),
new CleanWebpackPlugin([buildPath]),
new ReplaceInFileWebpackPlugin([{
dir: 'dist',
files: ['amp-sw.js'],
rules: [{
search: 'importScripts(',
replace: `importScripts('${publicPath}' + `,
}],
}]),
new BannerPlugin({
banner: `AMP_SW_v${version}`,
entryOnly: true,
})
],
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
optimization: {
minimizer: [new TerserPlugin({
terserOptions: {
ecma: 6,
module: true,
}
})],
},
};
}

0 comments on commit 3aded4e

Please sign in to comment.