forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use custom Babel loader to avoid using separate Babel copies for load…
…er and loader options (vercel#4417) This resolves the > .value is not a valid Plugin property error showing up for people in vercel#4227 cc @timneutkens
- Loading branch information
1 parent
0a14a37
commit 3165957
Showing
10 changed files
with
170 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import babelLoader from 'babel-loader' | ||
|
||
module.exports = babelLoader.custom(babel => { | ||
const presetItem = babel.createConfigItem(require('../babel/preset'), {type: 'preset'}) | ||
const hotLoaderItem = babel.createConfigItem(require('react-hot-loader/babel'), {type: 'plugin'}) | ||
const reactJsxSourceItem = babel.createConfigItem(require('@babel/plugin-transform-react-jsx-source'), {type: 'plugin'}) | ||
|
||
const configs = new Set() | ||
|
||
return { | ||
customOptions (opts) { | ||
const custom = { | ||
isServer: opts.isServer, | ||
dev: opts.dev | ||
} | ||
const loader = Object.assign({ | ||
cacheDirectory: true | ||
}, opts) | ||
delete loader.isServer | ||
delete loader.dev | ||
|
||
return { loader, custom } | ||
}, | ||
config (cfg, {customOptions: {isServer, dev}}) { | ||
const options = Object.assign({}, cfg.options) | ||
if (cfg.hasFilesystemConfig()) { | ||
for (const file of [cfg.babelrc, cfg.config]) { | ||
if (file && !configs.has(file)) { | ||
configs.add(file) | ||
console.log(`> Using external babel configuration`) | ||
console.log(`> Location: "${file}"`) | ||
} | ||
} | ||
} else { | ||
// Add our default preset if the no "babelrc" found. | ||
options.presets = [...options.presets, presetItem] | ||
} | ||
|
||
options.plugins = [ | ||
...options.plugins, | ||
dev && !isServer && hotLoaderItem, | ||
dev && reactJsxSourceItem | ||
].filter(Boolean) | ||
|
||
return options | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"presets": [ | ||
"next/babel", | ||
"@babel/preset-flow" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This page is written in flowtype to test Babel's functionality | ||
import * as React from 'react' | ||
|
||
type Props = {} | ||
|
||
export default class MyComponent extends React.Component<Props> { | ||
render () { | ||
return <div id='text'>Test Babel</div> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"presets": [ | ||
["next/babel", { | ||
"preset-env": { | ||
"modules": "commonjs" | ||
} | ||
}] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* global jasmine, describe, beforeAll, afterAll */ | ||
|
||
import { join } from 'path' | ||
import { | ||
renderViaHTTP, | ||
fetchViaHTTP, | ||
findPort, | ||
launchApp, | ||
killApp | ||
} from 'next-test-utils' | ||
|
||
// test suits | ||
import rendering from './rendering' | ||
|
||
const context = {} | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5 | ||
|
||
describe('Babel', () => { | ||
beforeAll(async () => { | ||
context.appPort = await findPort() | ||
context.server = await launchApp(join(__dirname, '../'), context.appPort, true) | ||
|
||
// pre-build all pages at the start | ||
await Promise.all([ | ||
renderViaHTTP(context.appPort, '/') | ||
]) | ||
}) | ||
afterAll(() => killApp(context.server)) | ||
|
||
rendering(context, 'Rendering via HTTP', (p, q) => renderViaHTTP(context.appPort, p, q), (p, q) => fetchViaHTTP(context.appPort, p, q)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* global describe, it, expect */ | ||
|
||
import cheerio from 'cheerio' | ||
|
||
export default function ({ app }, suiteName, render, fetch) { | ||
async function get$ (path, query) { | ||
const html = await render(path, query) | ||
return cheerio.load(html) | ||
} | ||
|
||
describe(suiteName, () => { | ||
it('Should compile a page with flowtype correctly', async () => { | ||
const $ = await get$('/') | ||
expect($('#text').text()).toBe('Test Babel') | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1171,13 +1171,14 @@ [email protected], babel-jest@^21.2.0: | |
babel-plugin-istanbul "^4.0.0" | ||
babel-preset-jest "^21.2.0" | ||
|
||
[email protected].2: | ||
version "8.0.0-beta.2" | ||
resolved "https://registry.yarnpkg.com/babel-loader/-/babel-loader-8.0.0-beta.2.tgz#4d5b67c964dc8c9cba866fd13d6b90df3acf8723" | ||
[email protected].3: | ||
version "8.0.0-beta.3" | ||
resolved "https://registry.yarnpkg.com/babel-loader/-/babel-loader-8.0.0-beta.3.tgz#49efeea6e8058d5af860a18a6de88b8c1450645b" | ||
dependencies: | ||
find-cache-dir "^1.0.0" | ||
loader-utils "^1.0.2" | ||
mkdirp "^0.5.1" | ||
util.promisify "^1.0.0" | ||
|
||
babel-messages@^6.23.0: | ||
version "6.23.0" | ||
|
@@ -2544,7 +2545,7 @@ error-stack-parser@^2.0.0: | |
dependencies: | ||
stackframe "^1.0.3" | ||
|
||
es-abstract@^1.7.0: | ||
es-abstract@^1.5.1, es-abstract@^1.7.0: | ||
version "1.11.0" | ||
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.11.0.tgz#cce87d518f0496893b1a30cd8461835535480681" | ||
dependencies: | ||
|
@@ -5341,6 +5342,13 @@ object.assign@^4.0.4: | |
has-symbols "^1.0.0" | ||
object-keys "^1.0.11" | ||
|
||
object.getownpropertydescriptors@^2.0.3: | ||
version "2.0.3" | ||
resolved "https://registry.yarnpkg.com/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.0.3.tgz#8758c846f5b407adab0f236e0986f14b051caa16" | ||
dependencies: | ||
define-properties "^1.1.2" | ||
es-abstract "^1.5.1" | ||
|
||
object.omit@^2.0.0: | ||
version "2.0.1" | ||
resolved "https://registry.yarnpkg.com/object.omit/-/object.omit-2.0.1.tgz#1a9c744829f39dbb858c76ca3579ae2a54ebd1fa" | ||
|
@@ -7447,6 +7455,13 @@ util-deprecate@^1.0.2, util-deprecate@~1.0.1: | |
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" | ||
|
||
util.promisify@^1.0.0: | ||
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/util.promisify/-/util.promisify-1.0.0.tgz#440f7165a459c9a16dc145eb8e72f35687097030" | ||
dependencies: | ||
define-properties "^1.1.2" | ||
object.getownpropertydescriptors "^2.0.3" | ||
|
||
[email protected], util@^0.10.3: | ||
version "0.10.3" | ||
resolved "https://registry.yarnpkg.com/util/-/util-0.10.3.tgz#7afb1afe50805246489e3db7fe0ed379336ac0f9" | ||
|