diff --git a/jsconfig.json b/jsconfig.json index 2e7c7fcdf..16f7d8882 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "target": "ESNext", + "module": "ESNext", "moduleResolution": "Node", "allowJs": true, "checkJs": true, diff --git a/packages/cli/lib/lib/webpack/webpack-base-config.js b/packages/cli/lib/lib/webpack/webpack-base-config.js index 556a02cb6..adca0179e 100644 --- a/packages/cli/lib/lib/webpack/webpack-base-config.js +++ b/packages/cli/lib/lib/webpack/webpack-base-config.js @@ -292,9 +292,12 @@ module.exports = function createBaseConfig(env) { new MiniCssExtractPlugin({ filename: isProd && !env.isServer ? '[name].[contenthash:5].css' : '[name].css', - chunkFilename: isProd - ? '[name].chunk.[contenthash:5].css' - : '[name].chunk.css', + chunkFilename: pathData => { + const chunkName = pathData.chunk.id.split('_').slice(0, -1).join('-'); + return isProd + ? `${chunkName}.chunk.[chunkhash:5].css` + : `${chunkName}.chunk.css`; + }, }), ProgressBarPlugin({ format: diff --git a/packages/cli/lib/lib/webpack/webpack-client-config.js b/packages/cli/lib/lib/webpack/webpack-client-config.js index 136c1f230..fd7c540af 100644 --- a/packages/cli/lib/lib/webpack/webpack-client-config.js +++ b/packages/cli/lib/lib/webpack/webpack-client-config.js @@ -88,7 +88,12 @@ async function clientConfig(env) { } return env.isProd ? '[name].[chunkhash:5].js' : '[name].js'; }, - chunkFilename: '[name].chunk.[chunkhash:5].js', + chunkFilename: pathData => { + const chunkName = pathData.chunk.id.split('_').slice(0, -1).join('-'); + return env.isProd + ? `${chunkName}.chunk.[chunkhash:5].js` + : `${chunkName}.chunk.js`; + }, }, resolveLoader: { @@ -173,6 +178,7 @@ function isProd(env) { cache: true, optimization: { + chunkIds: 'named', minimizer: [ new TerserPlugin({ extractComments: false, diff --git a/packages/cli/package.json b/packages/cli/package.json index c92b6941f..84adfdbf1 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -42,6 +42,7 @@ "p-retry": "^4.5.0", "polka": "^0.5.2", "preact": "^10.6.5", + "preact-iso": "^2.3.0", "preact-render-to-string": "^5.1.19", "preact-router": "^3.0.1", "puppeteer": "^9.1.1", diff --git a/packages/cli/tests/build.test.js b/packages/cli/tests/build.test.js index 08cb5e21f..54e842fb1 100644 --- a/packages/cli/tests/build.test.js +++ b/packages/cli/tests/build.test.js @@ -1,9 +1,9 @@ -const { join } = require('path'); +const { join, relative } = require('path'); const { access, mkdir, readdir, readFile, rename, unlink, writeFile } = require('fs').promises; const looksLike = require('html-looks-like'); const { create, build, buildFast } = require('./lib/cli'); -const { snapshot } = require('./lib/utils'); +const { expand, snapshot } = require('./lib/utils'); const { subject } = require('./lib/output'); const images = require('./images/build'); const minimatch = require('minimatch'); @@ -84,6 +84,26 @@ describe('preact build', () => { await expect(buildFast(dir)).resolves.not.toThrow(); }); + it.only('lazy loads routes with preact-iso `lazy`', async () => { + let dir = await subject('lazy-load-iso'); + await buildFast(dir, { prerender: false }); + + let output = await expand(join(dir, 'build')).then(arr => { + return arr.map(x => relative(dir, x)); + }); + + let expected = [ + /build\/a\.chunk\.\w{5}\.js$/, + /build\/a\.chunk\.\w{5}\.css$/, + /build\/b\.chunk\.\w{5}\.js$/, + /build\/b\.chunk\.\w{5}\.css$/, + ]; + + expected.forEach(pattern => { + expect(output.find(file => pattern.test(file))).not.toBeUndefined(); + }); + }); + it('should patch global location object', async () => { let dir = await subject('location-patch'); diff --git a/packages/cli/tests/subjects/lazy-load-iso/a.css b/packages/cli/tests/subjects/lazy-load-iso/a.css new file mode 100644 index 000000000..818d40628 --- /dev/null +++ b/packages/cli/tests/subjects/lazy-load-iso/a.css @@ -0,0 +1,3 @@ +h1 { + color: red; +} diff --git a/packages/cli/tests/subjects/lazy-load-iso/a.js b/packages/cli/tests/subjects/lazy-load-iso/a.js new file mode 100644 index 000000000..639fd9ce7 --- /dev/null +++ b/packages/cli/tests/subjects/lazy-load-iso/a.js @@ -0,0 +1,3 @@ +import './a.css'; + +export default () =>