diff --git a/package.json b/package.json index 4b4fce614..6b10bfaaf 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ "eslint": "^3.19.0", "eslint-plugin-react": "^7.0.1", "html-looks-like": "^1.0.2", + "html-webpack-exclude-assets-plugin": "0.0.5", "lodash": "^4.17.4", "tape": "^4.6.3", "uuid": "^3.0.1" diff --git a/src/lib/entry.js b/src/lib/entry.js index 92289b620..83ec53052 100644 --- a/src/lib/entry.js +++ b/src/lib/entry.js @@ -23,13 +23,5 @@ if (typeof app==='function') { if (module.hot) module.hot.accept('preact-cli-entrypoint', init); - if (typeof fetch==='function') { - init(); - } - else { - require.ensure(['preact-cli-polyfills'], () => { - require('preact-cli-polyfills'); - init(); - }, 'polyfills'); - } + init(); } diff --git a/src/lib/webpack-config.js b/src/lib/webpack-config.js index abb5fb80f..8af29d9c8 100644 --- a/src/lib/webpack-config.js +++ b/src/lib/webpack-config.js @@ -19,6 +19,7 @@ import ExtractTextPlugin from 'extract-text-webpack-plugin'; import autoprefixer from 'autoprefixer'; import HtmlWebpackPlugin from 'html-webpack-plugin'; import ScriptExtHtmlWebpackPlugin from 'script-ext-html-webpack-plugin'; +import HtmlWebpackExcludeAssetsPlugin from 'html-webpack-exclude-assets-plugin'; import ProgressBarPlugin from 'progress-bar-webpack-plugin'; import CopyWebpackPlugin from 'copy-webpack-plugin'; import ReplacePlugin from 'webpack-plugin-replace'; @@ -68,11 +69,14 @@ export default env => { return createConfig.vanilla([ setContext(src('.')), - entryPoint(resolve(__dirname, './entry')), + entryPoint({ + 'bundle': resolve(__dirname, './entry'), + 'polyfills': resolve(__dirname, './polyfills'), + }), setOutput({ path: resolve(cwd, env.dest || 'build'), publicPath: '/', - filename: 'bundle.js', + filename: '[name].js', chunkFilename: '[name].chunk.[chunkhash:5].js' }), @@ -85,7 +89,6 @@ export default env => { extensions: ['.js', '.jsx', '.ts', '.tsx', '.json', '.less', '.scss', '.sass', '.css'], alias: { 'preact-cli-entrypoint': src('index.js'), - 'preact-cli-polyfills': resolve(__dirname, 'polyfills.js'), style: src('style'), preact$: resolveDep(isProd ? 'preact/dist/preact.min.js' : 'preact', env.cwd), // preact-compat aliases for supporting React dependencies: @@ -441,6 +444,7 @@ const production = config => addPlugins([ minify: true, stripPrefix: config.cwd, staticFileGlobsIgnorePatterns: [ + /polyfills\.js$/, /\.map$/, /push-manifest\.json$/ ] @@ -465,14 +469,15 @@ const htmlPlugin = config => addPlugins([ compile: true, preload: config.preload===true, title: config.title || config.manifest.name || config.manifest.short_name || (config.pkg.name || '').replace(/^@[a-z]\//, '') || 'Preact App', + excludeAssets: [/(bundle|polyfills)(\..*)?\.js$/], config, ssr(params) { return config.prerender ? prerender(config, params) : ''; } }), - + new HtmlWebpackExcludeAssetsPlugin(), new ScriptExtHtmlWebpackPlugin({ // inline: 'bundle.js', - defaultAttribute: 'async' + defaultAttribute: 'defer' }) ]); diff --git a/src/resources/template.html b/src/resources/template.html index eaf25cf9d..bccd366bf 100644 --- a/src/resources/template.html +++ b/src/resources/template.html @@ -11,6 +11,7 @@ <% } %> <% for (var chunk of webpack.chunks) { %> + <% if (chunk.names.length === 1 && chunk.names[0] === 'polyfills') continue; %> <% for (var file of chunk.files) { %> <% if (htmlWebpackPlugin.options.preload && file.match(/\.(js|css)$/)) { %> @@ -24,5 +25,7 @@ <%= htmlWebpackPlugin.options.ssr({ url: '/' }) %> + + diff --git a/tests/build.snapshot.js b/tests/build.snapshot.js index 127516520..5a906faf4 100644 --- a/tests/build.snapshot.js +++ b/tests/build.snapshot.js @@ -17,7 +17,7 @@ const smallBuildCommons = { 'favicon.ico': { size: 15086 }, 'icon.png': { size: 51484 } }, - 'polyfills.chunk.*.js': { size: 4068 }, + 'polyfills.js': { size: 4068 }, 'favicon.ico': { size: 15086 }, 'sw.js': { size: 3378 }, 'manifest.json': { size: 298 }, @@ -36,7 +36,7 @@ const fullBuildCommons = { 'mstile-150x150.png': { size: 9050 } } }, - 'polyfills.chunk.*.js': { size: 4066 }, + 'polyfills.js': { size: 4066 }, 'push-manifest.json': { size: 303 }, 'favicon.ico': { size: 15086 }, 'manifest.json': { size: 426 }, diff --git a/tests/serve.snapshot.js b/tests/serve.snapshot.js index 0e635c0e5..2cd3d828f 100644 --- a/tests/serve.snapshot.js +++ b/tests/serve.snapshot.js @@ -14,7 +14,8 @@ export const homePageHTML = `

This is the Home component.

- + + {{ ... }} `; @@ -36,6 +37,7 @@ export const profilePageHtml = `

Clicked 10 times.

- + + {{ ... }} `;