Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added [chunkhash]/[hash]/[id]/[name] support for importScripts #70

Merged
merged 7 commits into from
May 2, 2017

Conversation

hulkish
Copy link
Contributor

@hulkish hulkish commented May 1, 2017

For documentation on how this works, please see my changes to README.md.

This satisfies #60

@hulkish hulkish changed the title Added [chunkhash] for importScripts Added [chunkhash] support for importScripts May 1, 2017
@hulkish hulkish mentioned this pull request May 2, 2017
@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@goldhand hmm.. any issue with this? Was hoping to move this forward soon.

@goldhand
Copy link
Owner

goldhand commented May 2, 2017

@hulkish nope! I was away this weekend. Glancing over this just now, looks great! Will look a little more in depth today before I merge

Copy link
Owner

@goldhand goldhand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, a couple changes, mostly to the readme. Also, just to clarify, will importScripts: ['myscript.[hash].js'] still be supported or will they need to use the format importScripts: {filename: myscript.[hash].js'}` to get the legacy hash support

README.md Outdated
- When importScripts array item is a `String`:
- Converts to object format `{ filename: '<publicPath>/my-script.js'}`
- When importScripts array item is an `Object`:
- Looks for
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you for got finish a thought here

README.md Outdated
@@ -1,4 +1,4 @@
SW Precache Webpack Plugin
# SW Precache Webpack Plugin
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove the extra lines under all the titles that were reformatted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

README.md Outdated
]
```

### `importSripts` usage example
Uses one option from `sw-precache` (`cacheId`) with one option from `SWPrecacheWebpackPlugin` (`filename`) in a configuration hash:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't explain the example

README.md Outdated
]
```

Here's a more elaborate example with `mergeStaticsConfig: true` and `staticFileGlobsIgnorePatterns`. `mergeStaticsConfig: true` allows you to add some additional static file globs to the emitted ServiceWorker file alongside Webpack's emitted assets. `staticFileGlobsIgnorePatterns` can be used to avoid including sourcemap file references in the generated ServiceWorker.
### `cacheId` usage example
Uses one option from `sw-precache` (`cacheId`) with one option from `SWPrecacheWebpackPlugin` (`filename`) in a configuration hash:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably can remove the cacheId example

const chunk = chunks.find(c => c.names.includes(criteria.chunkName));

if (!chunk) {
compilation.errors.push(new Error(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe push this to this.warnings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should report an error, because it can only happen if they specify a chunk name which does not exist.

Copy link
Contributor Author

@hulkish hulkish May 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only outstanding requested change, I can change it to warning if you insist - but I believe this should prompt the user to remove their definition of invalid chunkName.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sounds good, we can leave it

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

Looks good overall, a couple changes, mostly to the readme. Also, just to clarify, will importScripts: ['myscript.[hash].js'] still be supported or will they need to use the format importScripts: {filename:myscript.[hash].js'}` to get the legacy hash support

@goldhand I think it should be deprecated in a later release. However, yes - the current usage is legacy supported:

importScripts: ['myscript.[hash].js']

The exact same can also be accomplished using new syntax:

importScripts: [{ filename: 'myscript.[hash].js' }]

Additional Notes:

  • This will fully respect & support the filename template used for the webpack config's output.filename & output.chunkFilename. So, if your filename or chunkFilename templates use [hash], [chunkhash], [id], [name], etc - it will be processed accordingly.

  • This supports all types of named chunks. This means you can use an entry point chunk name, or a dynamically imported chunk name: (docs for magic comment syntax here)

module.exports = function() {
  return import(/* webpackChunkName: "service-worker-imported-script-2" */ './service-worker-imported-script-2');
};

Then, in your importScripts config you can directly mention this named chunk:

importScripts: [{ chunkName: 'service-worker-imported-script-2' }]

I added tests for this, also.

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@goldhand How's that?

README.md Outdated
@@ -1,4 +1,4 @@
SW Precache Webpack Plugin
# SW Precache Webpack Plugin
==========================
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgot this one

@goldhand
Copy link
Owner

goldhand commented May 2, 2017

@hulkish almost there! Just forgot the ======== at the top of the readme. Then it is good to go. Really great work 👍

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@goldhand The line is crushed! Also, updated title of this PR to more accurately represent what this offers.

@hulkish hulkish changed the title Added [chunkhash] support for importScripts Added [chunkhash]/[hash]/[id]/[name] support for importScripts May 2, 2017
@goldhand goldhand merged commit 7308aff into goldhand:master May 2, 2017
@diachedelic
Copy link

How is the [hash] derived when using filename? It does not seem to ever change.

    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../src/sw-import.js'),
        to: path.resolve(
          config.build.assetsRoot,
          config.build.assetsSubDirectory,
          'js/[name].[hash].[ext]',
        ),
      },
    ]),
    new SWPrecacheWebpackPlugin({
      cacheId: 'static',
      filename: 'service-worker.js',
      staticFileGlobs: ['dist/**/*.{js,html,css,woff}'],
      minify: false,
      stripPrefix: 'dist/',
      importScripts: [{ 
        filename: `${config.build.assetsSubDirectory}/js/sw-import.[hash].js`,
      }],
    }),

If I change either the filename or content of sw-import.js, instead of the hash changing, I always get 09acbae1608b7ed292fd

importScripts("/static/js/sw-import.09acbae1608b7ed292fd.js");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants