-
- Notifications
You must be signed in to change notification settings - Fork 6.3k
feat(babel-plugin): use project's babel config to transpile dependencies #1282
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
Changes from 3 commits
d8e0f5d
1dd3903
9fc591b
fbc4b6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -6,3 +6,4 @@ packages/test | |
dist | ||
temp | ||
.vuerc | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
jest.setTimeout(30000) | ||
| ||
const { defaultPreset } = require('@vue/cli/lib/options') | ||
const create = require('@vue/cli-test-utils/createTestProject') | ||
| ||
let project | ||
| ||
beforeAll(async () => { | ||
project = await create('cli-plugin-babel', defaultPreset) | ||
| ||
await project.write( | ||
'node_modules/external-dep/package.json', | ||
`{ "name": "external-dep", "version": "1.0.0", "main": "index.js" }` | ||
) | ||
| ||
await project.write( | ||
'node_modules/external-dep/index.js', | ||
`const test = () => "__TEST__";\nexport default test` | ||
) | ||
| ||
let $packageJson = await project.read('package.json') | ||
| ||
$packageJson = JSON.parse($packageJson) | ||
$packageJson.dependencies['external-dep'] = '1.0.0' | ||
$packageJson = JSON.stringify($packageJson) | ||
| ||
await project.write( | ||
'package.json', | ||
$packageJson | ||
) | ||
| ||
let $mainjs = await project.read('src/main.js') | ||
| ||
$mainjs = `import test from 'external-dep'\n${$mainjs}\nconsole.log(test())` | ||
| ||
await project.write( | ||
'src/main.js', | ||
$mainjs | ||
) | ||
}) | ||
| ||
test('dep from node_modules should not been transpiled', async () => { | ||
const { stdout } = await project.run('vue-cli-service build') | ||
| ||
let $vendorjs = stdout.match(/(js\/vendors~app\.[^.]+\.js)/)[1] | ||
| ||
$vendorjs = `dist/${$vendorjs}` | ||
$vendorjs = await project.read($vendorjs) | ||
| ||
expect($vendorjs).toMatch('() => "__TEST__"') | ||
}) | ||
| ||
test('dep from node_modules should been transpiled', async () => { | ||
await project.write( | ||
'vue.config.js', | ||
`module.exports = { transpileDependencies: ['external-dep'] }` | ||
) | ||
| ||
const { stdout } = await project.run('vue-cli-service build') | ||
| ||
let $vendorjs = stdout.match(/(js\/vendors~app\.[^.]+\.js)/)[1] | ||
| ||
$vendorjs = `dist/${$vendorjs}` | ||
$vendorjs = await project.read($vendorjs) | ||
| ||
expect($vendorjs).toMatch('return "__TEST__"') | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -4,7 +4,8 @@ | |
'spinner', | ||
'validate', | ||
'openBrowser', | ||
'pluginResolution' | ||
'pluginResolution', | ||
'babelHelpers' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see why this needs to be placed in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because | ||
].forEach(m => { | ||
Object.assign(exports, require(`./lib/${m}`)) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const babel = require('@babel/core') | ||
| ||
const lib = exports.babelHelpers = {} | ||
| ||
lib.loadUsersConfig = (rootDir) => { | ||
if (typeof rootDir !== 'string') { | ||
throw new TypeError('`rootDir` must be a string') | ||
} | ||
| ||
const options = { | ||
cwd: rootDir, | ||
filename: 'package.json' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could someone clarify what specifically this is meant to do? Babel's configuration can vary per-file, so by doing this you're essentially loading the subset of the configuration that happens to apply to this specific filename. I'd be happy to help make things work more easily with Babel, but at the moment this all seems to be going against a bunch of Babel's expectations. | ||
} | ||
| ||
const partial = babel.loadPartialConfig(options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| ||
delete partial.options.filename | ||
| ||
const normalize = item => [item.file.resolved, item.options] | ||
| ||
partial.options.plugins = partial.options.plugins.map(normalize) | ||
partial.options.presets = partial.options.presets.map(normalize) | ||
| ||
const a = partial.options | ||
const b = { configFile: partial.config } | ||
const config = Object.assign({}, a, b) | ||
| ||
return config | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does
babelLoader
here end up calling Babel exactly? I'd normally expect the loader to do all the work of reading the config, so I'm not totally following why this tries to read the config up front.