-
-
Notifications
You must be signed in to change notification settings - Fork 27k
Plugin system #2756
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
Closed
Closed
Plugin system #2756
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
88f67ea
Preliminary typescript plugin
Timer 3fbed1b
[revert] add tsconfig
Timer d552cca
Support new oneOf change
Timer 1985795
Remove log
Timer 645a022
Update comment
Timer 8542ccf
Remove reusable logic
Timer 4ea787e
Add basis of eject webpack config
Timer 4e10e53
Set test string
Timer c7c0235
Add extensions AST mode
Timer 25daa37
Add pushExclusiveLoader AST mode
Timer b841040
Apply plugins
Timer dba1bf9
Run the eject file when ejecting
Timer 213eeff
Oops
Timer f307e8d
Eject file first
Timer 3203428
Remove configFileName after eject
Timer 5d14634
prettier (because why not?)
Timer 9395a83
Add plugin deps
Timer 8a29236
Clean version before comparing and fix sync
Timer ddf3d42
Strip off range commonly used
Timer 5853b89
Allow local path
Timer cf2b071
Use correct option
Timer 2adcc64
Make node types a peer dep
Timer 93d3537
remove lockfile
Timer c238223
Run eject function
Timer 17df673
oops
Timer 15d0da7
Add missing newline
Timer 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
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,236 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const babylon = require('babylon'); | ||
const traverse = require('babel-traverse').default; | ||
const template = require('babel-template'); | ||
const generator = require('babel-generator').default; | ||
const t = require('babel-types'); | ||
const { readFileSync } = require('fs'); | ||
const prettier = require('prettier'); | ||
|
||
function applyPlugins(config, plugins, { paths }) { | ||
const pluginPaths = plugins | ||
.map(p => { | ||
try { | ||
return require.resolve(`react-scripts-plugin-${p}`); | ||
} catch (e) { | ||
return null; | ||
} | ||
}) | ||
.filter(e => e != null); | ||
for (const pluginPath of pluginPaths) { | ||
const { apply } = require(pluginPath); | ||
config = apply(config, { paths }); | ||
} | ||
return config; | ||
} | ||
|
||
function _getArrayValues(arr) { | ||
const { elements } = arr; | ||
return elements.map(e => { | ||
if (e.type === 'StringLiteral') { | ||
return e.value; | ||
} | ||
return e; | ||
}); | ||
} | ||
|
||
// arr: [[afterExt, strExt1, strExt2, ...], ...] | ||
function pushExtensions({ config, ast }, arr) { | ||
if (ast != null) { | ||
traverse(ast, { | ||
enter(path) { | ||
const { type } = path; | ||
if (type !== 'ArrayExpression') { | ||
return; | ||
} | ||
const { key } = path.parent; | ||
if (key == null || key.name !== 'extensions') { | ||
return; | ||
} | ||
const { elements } = path.node; | ||
const extensions = _getArrayValues(path.node); | ||
for (const [after, ...exts] of arr) { | ||
// Find the extension we want to add after | ||
const index = extensions.findIndex(s => s === after); | ||
if (index === -1) { | ||
throw new Error( | ||
`Unable to find extension ${after} in configuration.` | ||
); | ||
} | ||
// Push the extensions into array in the order we specify | ||
elements.splice( | ||
index + 1, | ||
0, | ||
...exts.map(ext => t.stringLiteral(ext)) | ||
); | ||
// Simulate into our local copy of the array to keep proper indices | ||
extensions.splice(index + 1, 0, ...exts); | ||
} | ||
}, | ||
}); | ||
} else if (config != null) { | ||
const { resolve: { extensions } } = config; | ||
|
||
for (const [after, ...exts] of arr) { | ||
// Find the extension we want to add after | ||
const index = extensions.findIndex(s => s === after); | ||
if (index === -1) { | ||
throw new Error(`Unable to find extension ${after} in configuration.`); | ||
} | ||
// Push the extensions into array in the order we specify | ||
extensions.splice(index + 1, 0, ...exts); | ||
} | ||
} | ||
} | ||
|
||
function pushExclusiveLoader({ config, ast }, testStr, loader) { | ||
if (ast != null) { | ||
traverse(ast, { | ||
enter(path) { | ||
const { type } = path; | ||
if (type !== 'ArrayExpression') { | ||
return; | ||
} | ||
const { key } = path.parent; | ||
if (key == null || key.name !== 'oneOf') { | ||
return; | ||
} | ||
const entries = _getArrayValues(path.node); | ||
const afterIndex = entries.findIndex(entry => { | ||
const { properties } = entry; | ||
return ( | ||
properties.find(property => { | ||
if (property.value.type !== 'RegExpLiteral') { | ||
return false; | ||
} | ||
return property.value.pattern === testStr.slice(1, -1); | ||
}) != null | ||
); | ||
}); | ||
if (afterIndex === -1) { | ||
throw new Error('Unable to match pre-loader.'); | ||
} | ||
console.log('holy shit it works'); | ||
path.node.elements.splice(afterIndex + 1, 0, loader); | ||
}, | ||
}); | ||
} else if (config != null) { | ||
const { module: { rules: [, { oneOf: rules }] } } = config; | ||
const loaderIndex = rules.findIndex( | ||
rule => rule.test.toString() === testStr | ||
); | ||
if (loaderIndex === -1) { | ||
throw new Error('Unable to match pre-loader.'); | ||
} | ||
rules.splice(loaderIndex + 1, 0, loader); | ||
} | ||
} | ||
|
||
function ejectFile({ filename, code }) { | ||
if (filename != null) { | ||
code = readFileSync(filename, 'utf8'); | ||
} | ||
let ast = babylon.parse(code); | ||
|
||
let plugins = []; | ||
traverse(ast, { | ||
enter(path) { | ||
const { type } = path; | ||
if (type === 'VariableDeclaration') { | ||
const { node: { declarations: [{ id: { name }, init }] } } = path; | ||
if (name !== 'base') { | ||
return; | ||
} | ||
path.replaceWith(template('module.exports = RIGHT;')({ RIGHT: init })); | ||
} else if (type === 'AssignmentExpression') { | ||
const { node: { left, right } } = path; | ||
if (left.type !== 'MemberExpression') { | ||
return; | ||
} | ||
if (right.type !== 'CallExpression') { | ||
return; | ||
} | ||
const { callee: { name }, arguments: args } = right; | ||
if (name !== 'applyPlugins') { | ||
return; | ||
} | ||
plugins = _getArrayValues(args[1]); | ||
path.parentPath.remove(); | ||
} | ||
}, | ||
}); | ||
let deferredTransforms = []; | ||
plugins.forEach(p => { | ||
let path; | ||
try { | ||
path = require.resolve(`react-scripts-plugin-${p}`); | ||
} catch (e) { | ||
return; | ||
} | ||
const pluginCode = readFileSync(path, 'utf8'); | ||
const pluginAst = babylon.parse(pluginCode); | ||
traverse(pluginAst, { | ||
enter(path) { | ||
const { type } = path; | ||
if (type !== 'CallExpression') { | ||
return; | ||
} | ||
const { node: { callee: { name }, arguments: pluginArgs } } = path; | ||
switch (name) { | ||
case 'pushExtensions': { | ||
const [, _exts] = pluginArgs; | ||
const exts = _getArrayValues(_exts).map(entry => | ||
_getArrayValues(entry) | ||
); | ||
deferredTransforms.push( | ||
pushExtensions.bind(undefined, { ast }, exts) | ||
); | ||
break; | ||
} | ||
case 'pushExclusiveLoader': { | ||
const [, { value: testStr }, _loader] = pluginArgs; | ||
deferredTransforms.push( | ||
pushExclusiveLoader.bind(undefined, { ast }, testStr, _loader) | ||
); | ||
break; | ||
} | ||
default: { | ||
// Not a call we care about | ||
break; | ||
} | ||
} | ||
}, | ||
}); | ||
}); | ||
// Execute 'em! | ||
for (const transform of deferredTransforms) { | ||
transform(); | ||
} | ||
let { code: outCode } = generator( | ||
ast, | ||
{ sourceMaps: false, comments: true, retainLines: false }, | ||
code | ||
); | ||
outCode = prettier.format(outCode, { | ||
singleQuote: true, | ||
trailingComma: 'es5', | ||
}); | ||
return outCode; | ||
} | ||
|
||
module.exports = { | ||
applyPlugins, | ||
pushExtensions, | ||
pushExclusiveLoader, | ||
ejectFile, | ||
}; |
This file contains hidden or 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,30 @@ | ||
{ | ||
"name": "react-scripts-plugin-typescript", | ||
"version": "0.1.0", | ||
"description": "A plugin for react-scripts which enables TypeScript support.", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"react-scripts", | ||
"typescript", | ||
"cra", | ||
"create", | ||
"react", | ||
"app", | ||
"plugin" | ||
], | ||
"license": "BSD-3-Clause", | ||
"dependencies": { | ||
"awesome-typescript-loader": "^3.2.1", | ||
"tsconfig-react-app": "^1.0.0", | ||
"typescript": "^2.4.1" | ||
}, | ||
"devDependencies": { | ||
"react-dev-utils": "^3.0.2" | ||
}, | ||
"peerDependencies": { | ||
"react-dev-utils": "^3.0.2" | ||
} | ||
} |
This file contains hidden or 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,25 @@ | ||
'use strict'; | ||
|
||
const { | ||
pushExtensions, | ||
pushExclusiveLoader, | ||
} = require('react-dev-utils/plugins'); | ||
|
||
function apply(config, { paths }) { | ||
pushExtensions({ config }, [['.js', '.tsx', '.ts']]); | ||
pushExclusiveLoader({ config }, '/\\.(js|jsx)$/', { | ||
// Process TypeScript with `at-loader` | ||
test: /\.(ts|tsx)$/, | ||
include: paths.appSrc, | ||
loader: require.resolve('awesome-typescript-loader'), | ||
options: { | ||
silent: true, | ||
// @remove-on-eject-begin | ||
configFileName: require.resolve('tsconfig-react-app'), | ||
// @remove-on-eject-end | ||
}, | ||
}); | ||
return config; | ||
} | ||
|
||
module.exports = { apply }; |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Any expected errors that can happen here?
Uh oh!
There was an error while loading. Please reload this page.
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.
Plugin installation is optional, so
require.resolve
throws an error if the package is missing.