Skip to content

feat!: turn on modern mode by default, and provide a --no-module option #6416

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

Merged
merged 10 commits into from
Apr 14, 2021
Next Next commit
test: add test for skipping safari nomodule fix injection
  • Loading branch information
haoqunjiang committed Apr 8, 2021
commit 2726dca2992690c28dc2ab36eccde56f1eb8b0ea
38 changes: 29 additions & 9 deletions packages/@vue/cli-service/__tests__/modernMode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ test('modern mode', async () => {
expect(index).toMatch(/<script defer="defer" src="\/js\/chunk-vendors-legacy\.\w{8}\.js" nomodule>/)
expect(index).toMatch(/<script defer="defer" src="\/js\/app-legacy\.\w{8}\.js" nomodule>/)

// should inject Safari 10 nomodule fix
const { safariFix } = require('../lib/webpack/ModernModePlugin')
expect(index).toMatch(`<script>${safariFix}</script>`)

// Test crossorigin="use-credentials"
await project.write('vue.config.js', `module.exports = { crossorigin: 'use-credentials' }`)
const { stdout: stdout2 } = await project.run('vue-cli-service build --modern')
Expand Down Expand Up @@ -82,18 +78,42 @@ test('modern mode', async () => {
expect(await getH1Text()).toMatch('Welcome to Your Vue.js App')
})

test('no-unsafe-inline', async () => {
const project = await create('no-unsafe-inline', defaultPreset)
test('should not inject the nomodule-fix script if Safari 10 is not targeted', async () => {
// the default targets already excludes safari 10
const project = await create('skip-safari-fix', defaultPreset)

const { stdout } = await project.run('vue-cli-service build --modern --no-unsafe-inline')
const { stdout } = await project.run('vue-cli-service build --modern')
expect(stdout).toMatch('Build complete.')

// should contain no inline scripts in the output html
const index = await project.read('dist/index.html')
expect(index).not.toMatch(/[^>]\s*<\/script>/)
// should not contain the safari-nomodule-fix bundle, either
const files = await fs.readdir(path.join(project.dir, 'dist/js'))
expect(files.some(f => /^safari-nomodule-fix\.js$/.test(f))).toBe(false)
})

test('should inject nomodule-fix script when Safari 10 support is required', async () => {
const project = await create('safari-nomodule-fix', defaultPreset)

const pkg = JSON.parse(await project.read('package.json'))
pkg.browserslist.push('safari > 10')
await project.write('package.json', JSON.stringify(pkg, null, 2))

let { stdout } = await project.run('vue-cli-service build --modern')
let index = await project.read('dist/index.html')
// should inject Safari 10 nomodule fix as an inline script
const { safariFix } = require('../lib/webpack/ModernModePlugin')
expect(index).toMatch(`<script>${safariFix}</script>`)

// `--no-unsafe-inline` option
stdout = (await project.run('vue-cli-service build --modern --no-unsafe-inline')).stdout
expect(stdout).toMatch('Build complete.')
// should output a separate safari-nomodule-fix bundle
const files = await fs.readdir(path.join(project.dir, 'dist/js'))
expect(files.some(f => /^safari-nomodule-fix\.js$/.test(f))).toBe(true)

// should contain no inline scripts in the output html
const index = await project.read('dist/index.html')
index = await project.read('dist/index.html')
expect(index).not.toMatch(/[^>]\s*<\/script>/)
})

Expand Down