Skip to content

Commit

Permalink
fix: Webpack 5 automatic publicPath error (#23760)
Browse files Browse the repository at this point in the history
* fix: Webpack 5 automatic publicPath error

* update webpack-preprocessor test

* fix: disable webpack-preprocessor chunking

* stub LimitChunkCountPlugin

Co-authored-by: Zachary Williams <zachjw34@gmail.com>
  • Loading branch information
caseyjhol and ZachJW34 authored Sep 19, 2022
1 parent 466be9f commit 823ffd0
Show file tree
Hide file tree
Showing 8 changed files with 1,988 additions and 0 deletions.
6 changes: 6 additions & 0 deletions npm/webpack-preprocessor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F
// we need to set entry and output
entry,
output: {
// disable automatic publicPath
publicPath: '',
path: path.dirname(outputPath),
filename: path.basename(outputPath),
},
Expand All @@ -220,6 +222,10 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F

// override typescript to always generate proper source maps
overrideSourceMaps(true, options.typescript)

// To support dynamic imports, we have to disable any code splitting.
debug('Limiting number of chunks to 1')
opts.plugins = (opts.plugins || []).concat(new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }))
})
.value() as any

Expand Down
6 changes: 6 additions & 0 deletions npm/webpack-preprocessor/test/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const expect = chai.expect
chai.use(require('sinon-chai'))

const webpack = sinon.stub()
const LimitChunkCountPluginStub = sinon.stub()

webpack.optimize = {
LimitChunkCountPlugin: LimitChunkCountPluginStub,
}

mockery.enable({
warnOnUnregistered: false,
Expand Down Expand Up @@ -149,6 +154,7 @@ describe('webpack preprocessor', function () {

return this.run().then(() => {
expect(webpack.lastCall.args[0].output).to.eql({
publicPath: '',
path: 'output',
filename: 'output.ts.js',
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Since we are linking the `@cypress/webpack-preprocessor` package,
// `require('webpack')` will resolve the version found in our repo (v4).
// We need to override the webpack require to point to the local node_modules
const Module = require('module')

const originalModuleLoad = Module._load

Module._load = function (request, parent, isMain) {
if (request === 'webpack' || request.startsWith('webpack/')) {
const resolvePath = require.resolve(request, {
paths: [__dirname],
})

return originalModuleLoad(resolvePath, parent, isMain)
}

return originalModuleLoad(request, parent, isMain)
}

const { defineConfig } = require('cypress')
const wp = require('@cypress/webpack-preprocessor')

module.exports = defineConfig({
e2e: {
supportFile: false,
setupNodeEvents (on, config) {
on('file:preprocessor', wp({
webpackOptions: {
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', { targets: 'defaults' }],
],
plugins: ['@babel/plugin-transform-runtime'],
},
},
},
],
},
},
}))
},
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
it('should work', async () => {
const { default: lazyModule } = await import('./lazy.js')

expect(lazyModule.hello).eq('world')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default { hello: 'world' }
10 changes: 10 additions & 0 deletions system-tests/projects/webpack-preprocessor-webpack-5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"dependencies": {
"@babel/core": "7.19.0",
"@babel/plugin-transform-runtime": "7.18.10",
"@babel/preset-env": "7.19.0",
"@cypress/webpack-preprocessor": "file:../../../npm/webpack-preprocessor",
"babel-loader": "8.2.5",
"webpack": "^5.74.0"
}
}
1,897 changes: 1,897 additions & 0 deletions system-tests/projects/webpack-preprocessor-webpack-5/yarn.lock

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions system-tests/test/webpack_preprocessor_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import systemTests from '../lib/system-tests'

describe('@cypress/webpack-preprocessor', function () {
systemTests.setup()

systemTests.it('with Webpack 5', {
project: 'webpack-preprocessor-webpack-5',
testingType: 'e2e',
spec: '**/*.cy.js',
browser: 'chrome',
expectedExitCode: 0,
})
})

5 comments on commit 823ffd0

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 823ffd0 Sep 19, 2022

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.9.0/linux-x64/develop-823ffd0ca920c82170705d9abfa60592b8f3979a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 823ffd0 Sep 19, 2022

Choose a reason for hiding this comment

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

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.9.0/linux-arm64/develop-823ffd0ca920c82170705d9abfa60592b8f3979a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 823ffd0 Sep 19, 2022

Choose a reason for hiding this comment

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

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.9.0/darwin-arm64/develop-823ffd0ca920c82170705d9abfa60592b8f3979a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 823ffd0 Sep 19, 2022

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.9.0/darwin-x64/develop-823ffd0ca920c82170705d9abfa60592b8f3979a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 823ffd0 Sep 19, 2022

Choose a reason for hiding this comment

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

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.9.0/win32-x64/develop-823ffd0ca920c82170705d9abfa60592b8f3979a/cypress.tgz

Please sign in to comment.