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

Ensure browserslist extends works properly #33890

Merged
merged 4 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/compiled/browserslist/index.js

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion packages/next/taskfile.js
Expand Up @@ -310,10 +310,26 @@ export async function ncc_chalk(task, opts) {
// eslint-disable-next-line camelcase
externals['browserslist'] = 'next/dist/compiled/browserslist'
export async function ncc_browserslist(task, opts) {
const browserslistModule = require.resolve('browserslist')
const nodeFile = join(dirname(browserslistModule), 'node.js')

const content = await fs.readFile(nodeFile, 'utf8')
// ensure ncc doesn't attempt to bundle dynamic requires
// so that they work at runtime correctly
await fs.writeFile(
nodeFile,
content.replace(
/require\(require\.resolve\(/g,
`__non_webpack_require__(__non_webpack_require__.resolve(`
Copy link
Member

Choose a reason for hiding this comment

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

Might be good to add some comments for why replacing this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in cbdb13b

)
)

await task
.source(opts.src || relative(__dirname, require.resolve('browserslist')))
.ncc({ packageName: 'browserslist', externals })
.target('compiled/browserslist')

await fs.writeFile(nodeFile, content)
}

// eslint-disable-next-line camelcase
Expand Down Expand Up @@ -1577,7 +1593,6 @@ export async function ncc(task, opts) {
'ncc_node_html_parser',
'ncc_watchpack',
'ncc_chalk',
'ncc_browserslist',
'ncc_napirs_triples',
'ncc_etag',
'ncc_p_limit',
Expand Down Expand Up @@ -1686,6 +1701,7 @@ export async function ncc(task, opts) {
await task.parallel(['ncc_babel_bundle_packages'], opts)
await task.serial(
[
'ncc_browserslist',
'ncc_next__react_dev_overlay',
'copy_regenerator_runtime',
'copy_babel_runtime',
Expand Down
38 changes: 38 additions & 0 deletions test/e2e/browserslist-extends/index.test.ts
@@ -0,0 +1,38 @@
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'

describe('browserslist-extends', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
import styles from './index.module.css'

export default function Page() {
return <p className={styles.hello}>hello world</p>
}
`,
Copy link
Member

Choose a reason for hiding this comment

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

Is there anything specifix to this code that browser list is interacting with? Or is it more of a hello world?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mainly just a hello world, it failed from just reading the browserslist config in package.json before this fix.

'pages/index.module.css': `
.hello {
color: pink;
}
`,
},
dependencies: {
'browserslist-config-google': '^3.0.1',
},
packageJson: {
browserslist: ['extends browserslist-config-google'],
},
})
})
afterAll(() => next.destroy())

it('should work', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('hello world')
})
})