Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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__"')
})

13 changes: 12 additions & 1 deletion packages/@vue/cli-plugin-babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// TODO remove after upgrading Babel to 7.0.0-beta.47
require('./patchBabel')

const { babelHelpers } = require('@vue/cli-shared-utils')

module.exports = (api, {
parallel,
transpileDependencies
Expand Down Expand Up @@ -44,8 +46,17 @@ module.exports = (api, {
.loader('thread-loader')
}

jsRule
const babelLoader = jsRule
.use('babel-loader')
.loader('babel-loader')

// use project's babel config for dependencies
if (transpileDependencies.length !== 0) {
// `babelrc` = false
const config = babelHelpers.loadUsersConfig(api.resolve('.'))

babelLoader
.options(config)

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.

}
})
}
1 change: 1 addition & 0 deletions packages/@vue/cli-plugin-babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"@babel/core": "7.0.0-beta.46",
"@vue/babel-preset-app": "^3.0.0-beta.10",
"@vue/cli-shared-utils": "^3.0.0-beta.10",
"babel-loader": "^8.0.0-0"
},
"publishConfig": {
Expand Down
3 changes: 2 additions & 1 deletion packages/@vue/cli-shared-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
'spinner',
'validate',
'openBrowser',
'pluginResolution'
'pluginResolution',
'babelHelpers'
Copy link
Member

Choose a reason for hiding this comment

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

I don't see why this needs to be placed in cli-shared-utils. It's used only by cli-plugin-babel and should be placed in that package instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because cli-plugin-typescript also use babel-loader, may be in the future this will need there or some where else

].forEach(m => {
Object.assign(exports, require(`./lib/${m}`))
})
29 changes: 29 additions & 0 deletions packages/@vue/cli-shared-utils/lib/babelHelpers.js
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'

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

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

why use loadPartialConfig then manually normalize instead of just using loadOptions?

Copy link
Contributor Author

@Hokid Hokid May 15, 2018

Choose a reason for hiding this comment

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

loadPartialConfig loads only users config(from: package.json, .babelrc, ...etc). both loadPartialConfig and loadOptions return not valid config for passing it to the babel-loader, this why there is need manually normilize config.


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
}
1 change: 1 addition & 0 deletions packages/@vue/cli-shared-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"homepage": "https://github.com/vuejs/vue-cli/packages/@vue/cli-shared-utils#readme",
"dependencies": {
"@babel/core": "^7.0.0-beta.46",
"chalk": "^2.3.0",
"cmd-shim": "^2.0.2",
"execa": "^0.9.0",
Expand Down
Loading