-
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.
👽 update webpack config and react page demo.
- Loading branch information
1 parent
b72042e
commit ecf7b47
Showing
19 changed files
with
379 additions
and
27 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
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,68 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
// 根目录 | ||
const appDirectory = fs.realpathSync(process.cwd()); | ||
|
||
// 依据根目录,找到相对文件或相对目录 | ||
const resolveApp = relativePath => path.resolve(appDirectory, relativePath); | ||
|
||
const moduleFileExtensions = [ | ||
'web.mjs', | ||
'mjs', | ||
'web.js', | ||
'js', | ||
'web.ts', | ||
'ts', | ||
'web.tsx', | ||
'tsx', | ||
'json', | ||
'web.jsx', | ||
'jsx', | ||
]; | ||
|
||
const resolveModule = (resolveFn, filePath) => { | ||
const extension = moduleFileExtensions.find(extension => { | ||
return fs.existsSync(resolveFn(`${filePath}.${extension}`)) | ||
} | ||
); | ||
|
||
if (extension) { | ||
return resolveFn(`${filePath}.${extension}`); | ||
} | ||
|
||
return resolveFn(`${filePath}.js`); | ||
}; | ||
|
||
module.exports = { | ||
// 解析 env 环境变量 | ||
dotenv: resolveApp('.env'), | ||
// 项目根目录 | ||
appPath: resolveApp('.'), | ||
// 项目打包的目录 | ||
appBuild: resolveApp('build'), | ||
// public 资源目录 | ||
appPublic: resolveApp('public'), | ||
// public 目录下的 index.html 文件 | ||
appHtml: resolveApp('public/index.html'), | ||
// 解析入口文件,入口文件可能是 index.js, index.jsx, index.ts, index.tsx | ||
appIndexJs: resolveModule(resolveApp, 'src/index'), | ||
// package.json 的路径 | ||
appPackageJson: resolveApp('package.json'), | ||
// src 目录 | ||
appSrc: resolveApp('src'), | ||
// tsconfig 的路径 | ||
appTsConfig: resolveApp('tsconfig.json'), | ||
// jsconfig 的路径 | ||
appJsConfig: resolveApp('jsconfig.json'), | ||
// yarn.lock 文件的路径 | ||
yarnLockFile: resolveApp('yarn.lock'), | ||
// setupTests 文件的路径 | ||
testsSetup: resolveModule(resolveApp, 'src/setupTests'), | ||
// setupProxy 文件的路径 | ||
proxySetup: resolveApp('src/setupProxy.js'), | ||
// node_modules 的目录路径 | ||
appNodeModules: resolveApp('node_modules'), | ||
// service-worker 文件的路径 | ||
swSrc: resolveModule(resolveApp, 'src/service-worker'), | ||
}; |
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,146 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const chalk = require('chalk'); | ||
const findUp = require('find-up'); | ||
const path = require('path'); | ||
|
||
class ModuleNotFoundPlugin { | ||
constructor(appPath, yarnLockFile) { | ||
this.appPath = appPath; | ||
this.yarnLockFile = yarnLockFile; | ||
|
||
this.useYarnCommand = this.useYarnCommand.bind(this); | ||
this.getRelativePath = this.getRelativePath.bind(this); | ||
this.prettierError = this.prettierError.bind(this); | ||
} | ||
|
||
useYarnCommand() { | ||
try { | ||
return findUp.sync('yarn.lock', { cwd: this.appPath }) != null; | ||
} catch (_) { | ||
return false; | ||
} | ||
} | ||
|
||
getRelativePath(_file) { | ||
let file = path.relative(this.appPath, _file); | ||
if (file.startsWith('..')) { | ||
file = _file; | ||
} else if (!file.startsWith('.')) { | ||
file = '.' + path.sep + file; | ||
} | ||
return file; | ||
} | ||
|
||
prettierError(err) { | ||
let { details: _details = '', origin } = err; | ||
|
||
if (origin == null) { | ||
const caseSensitivity = | ||
err.message && | ||
/\[CaseSensitivePathsPlugin\] `(.*?)` .* `(.*?)`/.exec(err.message); | ||
if (caseSensitivity) { | ||
const [, incorrectPath, actualName] = caseSensitivity; | ||
const actualFile = this.getRelativePath( | ||
path.join(path.dirname(incorrectPath), actualName) | ||
); | ||
const incorrectName = path.basename(incorrectPath); | ||
err.message = `Cannot find file: '${incorrectName}' does not match the corresponding name on disk: '${actualFile}'.`; | ||
} | ||
return err; | ||
} | ||
|
||
const file = this.getRelativePath(origin.resource); | ||
let details = _details.split('\n'); | ||
|
||
const request = /resolve '(.*?)' in '(.*?)'/.exec(details); | ||
if (request) { | ||
const isModule = details[1] && details[1].includes('module'); | ||
const isFile = details[1] && details[1].includes('file'); | ||
|
||
let [, target, context] = request; | ||
context = this.getRelativePath(context); | ||
if (isModule) { | ||
const isYarn = this.useYarnCommand(); | ||
details = [ | ||
`Cannot find module: '${target}'. Make sure this package is installed.`, | ||
'', | ||
'You can install this package by running: ' + | ||
(isYarn | ||
? chalk.bold(`yarn add ${target}`) | ||
: chalk.bold(`npm install ${target}`)) + | ||
'.', | ||
]; | ||
} else if (isFile) { | ||
details = [`Cannot find file '${target}' in '${context}'.`]; | ||
} else { | ||
details = [err.message]; | ||
} | ||
} else { | ||
details = [err.message]; | ||
} | ||
err.message = [file, ...details].join('\n').replace('Error: ', ''); | ||
|
||
const isModuleScopePluginError = | ||
err.error && err.error.__module_scope_plugin; | ||
if (isModuleScopePluginError) { | ||
err.message = err.message.replace('Module not found: ', ''); | ||
} | ||
return err; | ||
} | ||
|
||
apply(compiler) { | ||
const { prettierError } = this; | ||
compiler.hooks.make.intercept({ | ||
register(tap) { | ||
if ( | ||
!(tap.name === 'MultiEntryPlugin' || tap.name === 'SingleEntryPlugin') | ||
) { | ||
return tap; | ||
} | ||
return Object.assign({}, tap, { | ||
fn: (compilation, callback) => { | ||
tap.fn(compilation, (err, ...args) => { | ||
if (err && err.name === 'ModuleNotFoundError') { | ||
err = prettierError(err); | ||
} | ||
callback(err, ...args); | ||
}); | ||
}, | ||
}); | ||
}, | ||
}); | ||
compiler.hooks.normalModuleFactory.tap('ModuleNotFoundPlugin', nmf => { | ||
nmf.hooks.afterResolve.intercept({ | ||
register(tap) { | ||
if (tap.name !== 'CaseSensitivePathsPlugin') { | ||
return tap; | ||
} | ||
return Object.assign({}, tap, { | ||
fn: (compilation, callback) => { | ||
tap.fn(compilation, (err, ...args) => { | ||
if ( | ||
err && | ||
err.message && | ||
err.message.includes('CaseSensitivePathsPlugin') | ||
) { | ||
err = prettierError(err); | ||
} | ||
callback(err, ...args); | ||
}); | ||
}, | ||
}); | ||
}, | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ModuleNotFoundPlugin; |
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,33 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// This webpack plugin ensures `npm install <library>` forces a project rebuild. | ||
// We’re not sure why this isn't webpack's default behavior. | ||
// See https://github.com/facebook/create-react-app/issues/186. | ||
|
||
'use strict'; | ||
|
||
class WatchMissingNodeModulesPlugin { | ||
constructor(nodeModulesPath) { | ||
this.nodeModulesPath = nodeModulesPath; | ||
} | ||
|
||
apply(compiler) { | ||
compiler.hooks.emit.tap('WatchMissingNodeModulesPlugin', compilation => { | ||
var missingDeps = Array.from(compilation.missingDependencies); | ||
var nodeModulesPath = this.nodeModulesPath; | ||
|
||
// If any missing files are expected to appear in node_modules... | ||
if (missingDeps.some(file => file.includes(nodeModulesPath))) { | ||
// ...tell webpack to watch node_modules recursively until they appear. | ||
compilation.contextDependencies.add(nodeModulesPath); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = WatchMissingNodeModulesPlugin; |
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
Oops, something went wrong.