Skip to content

Commit

Permalink
fix: inject experimental loader
Browse files Browse the repository at this point in the history
  • Loading branch information
dword-design committed Jul 2, 2023
1 parent d9b17aa commit ee6e3c8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@rollup/plugin-babel": "^6.0.3",
"@vitejs/plugin-vue": "^4.2.3",
"@vue/compiler-sfc": "^3.3.4",
"execa": "^7.1.1",
"vite-plugin-babel": "^1.1.3",
"vue-sfc-descriptor-to-string": "^2.0.0"
},
Expand All @@ -43,7 +44,6 @@
"@dword-design/tester-plugin-tmp-dir": "^2.1.26",
"axios": "^1.4.0",
"depcheck-package-name": "^3.0.1",
"execa": "^7.1.1",
"fs-extra": "^11.1.1",
"nuxt": "^3.6.1",
"nuxt-dev-ready": "^2.0.1",
Expand Down
14 changes: 12 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
#!/usr/bin/env node

import api from './index.js'
import { execa } from 'execa'
import { createRequire } from 'module'

const resolver = createRequire(import.meta.url)
try {
await api(process.argv[2], process.argv.slice(3))
await execa(
'node',
[
'--experimental-loader=babel-register-esm',
resolver.resolve('./inner-cli.js'),
...process.argv.slice(2),
],
{ stdio: 'inherit' },
)
} catch (error) {
console.error(error)
process.exit(1)
Expand Down
34 changes: 17 additions & 17 deletions src/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import outputFiles from 'output-files'
import portReady from 'port-ready'
import kill from 'tree-kill-promise'

const _require = createRequire(import.meta.url)
const resolver = createRequire(import.meta.url)

export default tester(
{
Expand All @@ -26,7 +26,7 @@ export default tester(
`,
)

const nuxt = execa(_require.resolve('./cli'), ['dev'])
const nuxt = execa(resolver.resolve('./cli.js'), ['dev'])
try {
await nuxtDevReady()

Expand All @@ -44,7 +44,7 @@ export default tester(
'modules/foo/index.js',
'export default () => console.log(1 |> (x => x * 2))',
)
await execa(_require.resolve('./cli'), ['build'])
await execa(resolver.resolve('./cli.js'), ['build'])
},
async build() {
await fs.outputFile(
Expand All @@ -55,9 +55,9 @@ export default tester(
</template>
`,
)
await execa(_require.resolve('./cli'), ['build'])
await execa(resolver.resolve('./cli.js'), ['build'])

const nuxt = execa(_require.resolve('./cli'), ['start'])
const nuxt = execa(resolver.resolve('./cli.js'), ['start'])
try {
await portReady(3000)
await this.page.goto('http://localhost:3000')
Expand All @@ -70,9 +70,9 @@ export default tester(
},
'build errors': async () => {
await fs.outputFile('modules/foo/index.js', 'foo bar')
await expect(execa(_require.resolve('./cli'), ['build'])).rejects.toThrow(
'Missing semicolon.',
)
await expect(
execa(resolver.resolve('./cli.js'), ['build']),
).rejects.toThrow('Missing semicolon.')
},
async component() {
await fs.outputFile(
Expand All @@ -88,7 +88,7 @@ export default tester(
`,
)

const nuxt = execa(_require.resolve('./cli'), ['dev'])
const nuxt = execa(resolver.resolve('./cli.js'), ['dev'])
try {
await nuxtDevReady()
await this.page.goto('http://localhost:3000')
Expand All @@ -113,7 +113,7 @@ export default tester(
`,
})

const nuxt = execa(_require.resolve('./cli'), ['dev'])
const nuxt = execa(resolver.resolve('./cli.js'), ['dev'])
try {
await nuxtDevReady()
await this.page.goto('http://localhost:3000')
Expand All @@ -134,7 +134,7 @@ export default tester(
`,
)

const nuxt = execa(_require.resolve('./cli'), ['dev'])
const nuxt = execa(resolver.resolve('./cli.js'), ['dev'])
try {
await nuxtDevReady()
await this.page.goto('http://localhost:3000')
Expand Down Expand Up @@ -177,7 +177,9 @@ export default tester(
</script>
`,
})
await expect(execa(_require.resolve('./cli'), ['build'])).rejects.toThrow(
await expect(
execa(resolver.resolve('./cli.js'), ['build']),
).rejects.toThrow(
'This experimental syntax requires enabling the parser plugin: "pipelineOperator".',
)
},
Expand All @@ -201,9 +203,7 @@ export default tester(
const oldNodeOptions = process.env.NODE_OPTIONS
process.env.NODE_OPTIONS = ''

const nuxt = execa(_require.resolve('./cli'), ['dev'], {
env: { NODE_OPTIONS: '--experimental-loader=babel-register-esm' },
})
const nuxt = execa(resolver.resolve('./cli.js'), ['dev'])
try {
await nuxtDevReady()
await this.page.goto('http://localhost:3000')
Expand All @@ -227,7 +227,7 @@ export default tester(
`,
)

const nuxt = execa(_require.resolve('./cli'), ['dev'])
const nuxt = execa(resolver.resolve('./cli.js'), ['dev'])
try {
await nuxtDevReady()
await this.page.goto('http://localhost:3000')
Expand Down Expand Up @@ -259,7 +259,7 @@ export default tester(
`,
})

const nuxt = execa(_require.resolve('./cli'), ['dev'])
const nuxt = execa(resolver.resolve('./cli.js'), ['dev'])
try {
await nuxtDevReady()
await this.page.goto('http://localhost:3000')
Expand Down
8 changes: 8 additions & 0 deletions src/inner-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import api from './index.js'

try {
await api(process.argv[2], process.argv.slice(3))
} catch (error) {
console.error(error)
process.exit(1)
}

0 comments on commit ee6e3c8

Please sign in to comment.